-
Notifications
You must be signed in to change notification settings - Fork 0
feat(layer): add 'd' shortcut to toggle color mode #30
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,5 +17,8 @@ export default defineAppConfig({ | |
| url: '', | ||
| branch: 'main', | ||
| }, | ||
| shortcuts: { | ||
| toggleColorMode: 'd', | ||
| }, | ||
| }, | ||
| }) | ||
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,73 @@ | ||
| import { onKeyStroke } from '@vueuse/core' | ||
| import { computed } from 'vue' | ||
|
|
||
| /** | ||
| * Returns `true` when the keystroke originated from an editable element | ||
| * (`<input>`, `<textarea>`, or any element with `[contenteditable]`), | ||
| * in which case the shortcut should be ignored so the user can type freely. | ||
| * | ||
| * Traverses up the DOM tree from the event target so that non-`HTMLElement` | ||
| * descendants (e.g. `SVGElement`, `MathMLElement`) inside an editable | ||
| * container are still treated as editable. | ||
| */ | ||
| function isEditableTarget(target: EventTarget | null): boolean { | ||
| let el: Node | null = target instanceof Node ? target : null | ||
| while (el && !(el instanceof HTMLElement)) { | ||
| el = el.parentNode | ||
| } | ||
|
|
||
| if (!el) | ||
| return false | ||
|
|
||
| const tag = el.tagName | ||
| if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') | ||
| return true | ||
|
|
||
| if (el.isContentEditable) | ||
| return true | ||
|
|
||
| return false | ||
| } | ||
|
amondnet marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Registers a keyboard shortcut (default `d`) that toggles between | ||
| * light and dark color modes. Mirrors docus' `useDocusShortcuts` | ||
| * composable (upstream commit `61c36d03`) but is reimplemented on top of | ||
| * `@vueuse/core`'s `onKeyStroke` instead of `@nuxt/ui`'s `defineShortcuts`. | ||
| * | ||
| * The handler bails out when: | ||
| * - the color mode is forced (e.g. `colorMode.forced === true`), | ||
| * - the keystroke originated from an editable element, | ||
| * - any modifier key (meta / ctrl / alt / shift) is pressed, | ||
| * - the configured shortcut is empty. | ||
| */ | ||
| export function useDocsShortcuts(): void { | ||
| const appConfig = useAppConfig() | ||
| const colorMode = useColorMode() | ||
|
|
||
| const toggleColorModeShortcut = computed<string>( | ||
| () => appConfig.docs?.shortcuts?.toggleColorMode ?? 'd', | ||
| ) | ||
|
|
||
| onKeyStroke( | ||
| (event: KeyboardEvent) => { | ||
| const key = toggleColorModeShortcut.value | ||
| if (!key) | ||
| return false | ||
| return event.key?.toLowerCase() === key.toLowerCase() | ||
| }, | ||
| (event: KeyboardEvent) => { | ||
| if (colorMode.forced) | ||
| return | ||
|
|
||
| if (event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) | ||
| return | ||
|
|
||
| if (isEditableTarget(event.target)) | ||
| return | ||
|
|
||
| const resolved = colorMode.value === 'dark' ? 'light' : 'dark' | ||
| colorMode.preference = resolved | ||
| }, | ||
| ) | ||
| } | ||
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 @@ | ||
| /** | ||
| * Installs the documentation layer's keyboard shortcuts on app boot. | ||
| * | ||
| * Client-only because the shortcut targets `window` keystrokes, which | ||
| * have no meaning on the server. | ||
| */ | ||
| export default defineNuxtPlugin(() => { | ||
| useDocsShortcuts() | ||
| }) |
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
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.