Skip to content

Commit

Permalink
[duoyun-ui] Support arrow navigation
Browse files Browse the repository at this point in the history
Closed #120
  • Loading branch information
mantou132 committed Jan 27, 2024
1 parent 34d4585 commit 9bf80ae
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 122 deletions.
2 changes: 1 addition & 1 deletion packages/duoyun-ui/docs/en/02-elements/form.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
name="dy-form"
props='{"style": "width: 100%;", "@change": "(evt) => {Object.keys(evt.detail).forEach(key => evt.target.querySelector(`[name=${key}]`).value = evt.detail[key])}"}'
html='<dy-form-item name="name" label="Name" multiple></dy-form-item>
<dy-form-item name="password" label="password" type="password"></dy-form-item>
<dy-form-item name="password" label="Password" type="password"></dy-form-item>
<dy-form-item name="email" label="Email" autofocus></dy-form-item>
<dy-form-item name="phone" label="Phone"></dy-form-item>
<dy-form-item name="date" label="Date" type="date-time"></dy-form-item>
Expand Down
2 changes: 1 addition & 1 deletion packages/duoyun-ui/docs/zh/02-elements/form.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
name="dy-form"
props='{"style": "width: 100%;", "@change": "(evt) => {Object.keys(evt.detail).forEach(key => evt.target.querySelector(`[name=${key}]`).value = evt.detail[key])}"}'
html='<dy-form-item name="name" label="Name" multiple></dy-form-item>
<dy-form-item name="password" label="password" type="password"></dy-form-item>
<dy-form-item name="password" label="Password" type="password"></dy-form-item>
<dy-form-item name="email" label="Email" autofocus></dy-form-item>
<dy-form-item name="phone" label="Phone"></dy-form-item>
<dy-form-item name="date" label="Date" type="date-time"></dy-form-item>
Expand Down
2 changes: 1 addition & 1 deletion packages/duoyun-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "duoyun-ui",
"version": "1.1.17",
"version": "1.1.18",
"description": "A lightweight desktop UI component library, implemented using Gem",
"keywords": [
"frontend",
Expand Down
22 changes: 9 additions & 13 deletions packages/duoyun-ui/src/elements/contextmenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ export class DuoyunContextmenuElement extends GemElement {
],
});
} else {
update({
menuStack: menuStack.slice(0, menuStackIndex + 1),
});
update({ menuStack: menuStack.slice(0, menuStackIndex + 1) });
}
};

Expand All @@ -242,16 +240,11 @@ export class DuoyunContextmenuElement extends GemElement {

#onKeydown = (evt: KeyboardEvent, menuStackIndex: number) => {
evt.stopPropagation();
const focusPrevMenu = () => {
update({
menuStack: contextmenuStore.menuStack.slice(0, menuStackIndex),
});
this.#menuElements[menuStackIndex - 1]?.focus();
};
hotkeys({
esc: menuStackIndex === 0 ? ContextMenu.close : focusPrevMenu,
left: focusPrevMenu,
right: () => this.#menuElements[menuStackIndex + 1]?.focus(),
esc:
menuStackIndex === 0
? ContextMenu.close
: () => update({ menuStack: contextmenuStore.menuStack.slice(0, menuStackIndex) }),
})(evt);
};

Expand Down Expand Up @@ -289,7 +282,10 @@ export class DuoyunContextmenuElement extends GemElement {
};

mounted = () => {
this.#menuElements.shift()?.focus();
this.effect(
() => this.#menuElements.at(-1)?.focus(),
() => [contextmenuStore.menuStack.length],
);
const restoreInert = setBodyInert(this);
ContextMenu.instance = this;
this.addEventListener('contextmenu', this.#preventDefault);
Expand Down
6 changes: 6 additions & 0 deletions packages/duoyun-ui/src/elements/date-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ export class DuoyunDatePanelElement extends GemElement<State> {
${Array.from({ length: 12 }).map(
(_, index) => html`
<span
tabindex="0"
role="button"
@keydown=${commonHandle}
class=${classMap({
item: true,
highlight: isCurrentYear && index === this.state.month,
Expand All @@ -284,6 +287,9 @@ export class DuoyunDatePanelElement extends GemElement<State> {
${Array.from({ length: 12 }, (_, index) => this.state.year - 7 + index).map(
(year) => html`
<span
tabindex="0"
role="button"
@keydown=${commonHandle}
class=${classMap({
item: true,
highlight: currentYear === year,
Expand Down
34 changes: 17 additions & 17 deletions packages/duoyun-ui/src/elements/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const formStyle = createCSSSheet(css`
:where(dy-form:not([hidden])) {
display: block;
}
dy-form:not([inline]) dy-form-item {
margin-block-end: 1.8em;
}
:where(dy-form[inline]:not([hidden])) {
display: flex;
flex-wrap: wrap;
Expand Down Expand Up @@ -67,19 +70,18 @@ const formStyle = createCSSSheet(css`
top: 100%;
left: 0;
}
dy-form-item {
flex-grow: 1;
margin-block-end: 1.8em;
}
dy-form-item[type='checkbox'] {
flex-grow: 0;
}
dy-form-item:where([data-invalid], :state(invalid)) * {
border-color: ${theme.negativeColor};
}
dy-form-item-inline-group {
display: flex;
gap: 1em;
@media not ${mediaQuery.PHONE} {
dy-form-item-inline-group {
display: flex;
gap: 1em;
}
dy-form:not([inline]) dy-form-item-inline-group > dy-form-item {
width: 0;
flex-grow: 1;
}
}
@media ${mediaQuery.PHONE} {
dy-form-item-inline-group {
Expand Down Expand Up @@ -161,6 +163,10 @@ const formItemStyle = createCSSSheet(css`
display: flex;
flex-direction: column;
}
:host([type='checkbox']) {
flex-direction: row;
align-items: center;
}
:host([required]) .label::after {
content: '*';
}
Expand All @@ -178,16 +184,10 @@ const formItemStyle = createCSSSheet(css`
}
.input {
width: 100%;
flex-grow: 1;
flex-shrink: 1;
}
.input + .input,
.footer {
margin-top: 10px;
}
:host([type='checkbox']) {
flex-direction: row;
align-items: center;
margin-top: 0.7em;
}
`);

Expand Down
Loading

0 comments on commit 9bf80ae

Please sign in to comment.