-
Notifications
You must be signed in to change notification settings - Fork 17
Menu select hotkeys #3915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Menu select hotkeys #3915
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
75c4396
chore(util): add helpers for detecting & using keyboard hotkeys
Kiarokh 5135781
refactor(util): improve `devices.ts` helpers
Kiarokh 06e305e
chore(hotkey): add new `@private` component
Kiarokh bc627ef
feat(menu): add `hotkey` prop for items & improve keyboard navigation
Kiarokh b115951
fix(markdown): improve visual style of `kbd` elements
Kiarokh d8f1c47
feat(select): add `hotkey` prop for items & improve keyboard navigation
Kiarokh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| :host { | ||
| display: grid; | ||
| grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); | ||
| gap: 2rem; | ||
| } | ||
|
|
||
| limel-example-value { | ||
| grid-column: 1/-1; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { Component, h, Host } from '@stencil/core'; | ||
|
|
||
| /** | ||
| * Basic example | ||
| * | ||
| * The value is passed as a string, indicating which hotkey to display. | ||
| * | ||
| * The component will automatically detect the operating system, and | ||
| * render the hotkey accordingly, using standard glyphs to save space. | ||
| * | ||
| * For example, the "meta" key will be rendered as <kbd>⌘</kbd> on macOS, | ||
| * and as <kbd>⊞ Win</kbd> on Windows/Linux. Or the "alt" key will be rendered | ||
| * as <kbd>⌥</kbd> on macOS, and as <kbd>Alt</kbd> on Windows. | ||
| * | ||
| * :::note | ||
| * `meta` always means the actual Meta key. | ||
| * | ||
| * This component will render `meta` using platform conventions: | ||
| * - macOS/iOS/iPadOS: <kbd>⌘</kbd> | ||
| * - Windows/Linux: <kbd>⊞ Win</kbd> | ||
| * | ||
| * If you want a hotkey that differs between operating systems (for example | ||
| * ⌘+C on macOS and Ctrl+C on Windows/Linux), detect the OS in your application | ||
| * and pass the appropriate hotkey string. | ||
| * | ||
| * - `ctrl` means "Control specifically" on all platforms. | ||
| * - `cmd` or `command` always render as <kbd>⌘</kbd> (even on Windows/Linux). | ||
| * ::: | ||
| * | ||
| * :::important | ||
| * This component is **display-only**. It does not listen for or handle | ||
| * any keyboard events. Keyboard event handling is the responsibility | ||
| * of the parent component (e.g. `limel-menu` or `limel-select`). | ||
| * ::: | ||
| */ | ||
| @Component({ | ||
| tag: 'limel-example-hotkey-basic', | ||
| shadow: true, | ||
| styleUrl: 'hotkey-basic.scss', | ||
| }) | ||
| export class HotkeyBasicExample { | ||
| public render() { | ||
| return ( | ||
| <Host> | ||
| <limel-hotkey value="s" /> | ||
| <limel-hotkey value="alt+s" /> | ||
| <limel-hotkey value="meta+c" /> | ||
| <limel-hotkey value="meta+alt+s" /> | ||
| <limel-hotkey value="meta+enter" /> | ||
| <limel-hotkey value="cmd+enter" /> | ||
| <limel-hotkey value="ctrl+shift+c" /> | ||
| <limel-hotkey value="f1" /> | ||
| <limel-hotkey value="tab" /> | ||
| <limel-hotkey value="+" /> | ||
| <limel-hotkey value="-" /> | ||
| </Host> | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { Component, h, Host } from '@stencil/core'; | ||
|
|
||
| /** | ||
| * The `disabled` prop | ||
| * | ||
| * When set to `true`, the hotkey is rendered in a visually disabled state. | ||
| * This is useful when the action associated with the hotkey is temporarily | ||
| * unavailable (e.g. a disabled menu item). | ||
| */ | ||
| @Component({ | ||
| tag: 'limel-example-hotkey-disabled', | ||
| shadow: true, | ||
| styleUrl: 'hotkey-basic.scss', | ||
| }) | ||
| export class HotkeyDisabledExample { | ||
| public render() { | ||
| return ( | ||
| <Host> | ||
| <limel-hotkey value="a" disabled={true} /> | ||
| <limel-hotkey value="b" /> | ||
| </Host> | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| @forward '../markdown/partial-styles/_kbd.scss'; | ||
| :host(limel-hotkey) { | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| gap: 0.25rem; | ||
| } | ||
|
|
||
| :host(limel-hotkey[disabled]:not([disabled='false'])) { | ||
| opacity: 0.5; | ||
| } | ||
|
|
||
| kbd { | ||
| margin: 0; | ||
| font-size: 0.75rem; | ||
| box-shadow: | ||
| var(--button-shadow-pressed), | ||
| 0 0.625rem 0.375px -0.5rem rgb(var(--color-black), 0.02), | ||
| 0 0.025rem 0.5rem 0 rgb(var(--contrast-100)) inset; | ||
| } | ||
|
|
||
| span { | ||
| display: inline-block; | ||
| &::first-letter { | ||
| text-transform: uppercase; | ||
| } | ||
| } | ||
|
|
||
| kbd.is-glyph { | ||
| span { | ||
| transform: scale(1.2); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { Component, Host, Prop, h } from '@stencil/core'; | ||
| import { tokenizeHotkeyString } from '../../util/hotkeys'; | ||
| import { isAppleDevice } from '../../util/device'; | ||
|
|
||
| /** | ||
| * This is a display-only component used to visualize keyboard shortcuts. | ||
| * It renders hotkey strings as styled `<kbd>` elements with | ||
| * platform-aware glyphs (e.g. `⌘` on macOS, `⊞ Win` on Windows). | ||
| * | ||
| * It does **not** listen for or handle any keyboard events. | ||
| * Keyboard event handling is the responsibility of the parent component | ||
| * (e.g. `limel-menu` or `limel-select`). | ||
| * | ||
| * @exampleComponent limel-example-hotkey-basic | ||
| * @exampleComponent limel-example-hotkey-disabled | ||
| * @private | ||
| */ | ||
| @Component({ | ||
| tag: 'limel-hotkey', | ||
| shadow: true, | ||
| styleUrl: 'hotkey.scss', | ||
| }) | ||
| export class Hotkey { | ||
| /** | ||
| * The hotkey string to visualize, e.g. `"meta+c"` or `"shift+enter"`. | ||
| */ | ||
| @Prop({ reflect: true }) | ||
| public value: string; | ||
|
|
||
| /** | ||
| * When `true`, the hotkey is rendered in a visually disabled state. | ||
| */ | ||
| @Prop({ reflect: true }) | ||
| public disabled = false; | ||
|
|
||
| public render() { | ||
| const isApple = isAppleDevice(); | ||
| const parts = tokenizeHotkeyString(this.value); | ||
| const ariaLabel = (this.value ?? '').trim(); | ||
|
|
||
| return ( | ||
| <Host aria-label={ariaLabel || undefined}> | ||
| {parts.map((part, index) => { | ||
| const { display, isGlyph } = this.formatDisplayToken( | ||
| part, | ||
| isApple | ||
| ); | ||
|
|
||
| return ( | ||
| <kbd | ||
| key={`${part}-${index}`} | ||
| class={isGlyph ? 'is-glyph' : undefined} | ||
| > | ||
| <span>{display}</span> | ||
| </kbd> | ||
| ); | ||
| })} | ||
| </Host> | ||
| ); | ||
| } | ||
|
|
||
| private formatDisplayToken( | ||
| token: string, | ||
| isApple: boolean | ||
| ): { | ||
| display: string; | ||
| isGlyph: boolean; | ||
| } { | ||
| const trimmed = (token ?? '').trim(); | ||
| if (!trimmed) { | ||
| return { display: '', isGlyph: false }; | ||
| } | ||
|
|
||
| if (trimmed === '+') { | ||
| return { display: '+', isGlyph: false }; | ||
| } | ||
|
|
||
| const lower = trimmed.toLowerCase(); | ||
|
|
||
| switch (lower) { | ||
| case 'meta': { | ||
| return isApple | ||
| ? { display: '⌘', isGlyph: true } | ||
| : { display: '⊞ Win', isGlyph: false }; | ||
| } | ||
|
|
||
| case 'cmd': | ||
| case 'command': { | ||
| return { display: '⌘', isGlyph: true }; | ||
| } | ||
|
|
||
| case 'alt': | ||
| case 'option': { | ||
| return isApple | ||
| ? { display: '⌥', isGlyph: true } | ||
| : { display: 'Alt', isGlyph: false }; | ||
| } | ||
|
|
||
| case 'shift': { | ||
| return { display: '⇧', isGlyph: true }; | ||
| } | ||
|
|
||
| case 'enter': | ||
| case 'return': { | ||
| return { display: '↩', isGlyph: true }; | ||
| } | ||
|
|
||
| case 'tab': { | ||
| return { display: '⇥', isGlyph: true }; | ||
| } | ||
|
|
||
| case 'delete': | ||
| case 'del': | ||
| case 'backspace': { | ||
| if (isApple) { | ||
| return { display: '⌫', isGlyph: true }; | ||
| } | ||
| return lower === 'backspace' | ||
| ? { display: 'Backspace', isGlyph: false } | ||
| : { display: 'Del', isGlyph: false }; | ||
| } | ||
|
|
||
| case 'ctrl': | ||
| case 'control': { | ||
| return { display: 'Ctrl', isGlyph: false }; | ||
| } | ||
| } | ||
|
|
||
| return { display: trimmed, isGlyph: false }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.