-
-
Notifications
You must be signed in to change notification settings - Fork 153
feat: command bar #470
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
Open
Gianthard-cyh
wants to merge
21
commits into
npmx-dev:main
Choose a base branch
from
Gianthard-cyh:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+444
−0
Open
feat: command bar #470
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
79475b7
WIP: basic ui logic command bar
Gianthard-cyh 727cc82
WIP(command-bar): trigger handlers
Gianthard-cyh 1fbab7e
WIP(command-bar): trrigger feedback
Gianthard-cyh 6d44e65
WIP(command-bar): command prompt
Gianthard-cyh 7ada4b1
Update app/components/CommandBar.vue
Gianthard-cyh cc11944
WIP(command-bar): basic api
Gianthard-cyh 4d8e6aa
WIP(command-bar): scoped command example
Gianthard-cyh 95639ea
feat(command-bar): context api (input, select, etc.)
Gianthard-cyh 270c386
chore: revert [...package].vue
Gianthard-cyh d92cd1b
Merge branch 'main' into main
Gianthard-cyh e2b331e
[autofix.ci] apply automated fixes
autofix-ci[bot] af1a50a
fix(test): remove unused variable
Gianthard-cyh f87acf9
Merge branch 'main' of github.com:Gianthard-cyh/npmx.dev
Gianthard-cyh 6020a50
chore: remove unused variable
Gianthard-cyh 516baeb
chore: apply suggestions from copilot
Gianthard-cyh 8f087d4
[autofix.ci] apply automated fixes
autofix-ci[bot] efce157
fix: remove useI18n() to prevent error
Gianthard-cyh acc860e
Merge branch 'main' of github.com:Gianthard-cyh/npmx.dev
Gianthard-cyh 24a56be
Merge branch 'main' into Gianthard-cyh/main
danielroe 0c69097
fix(test): ignore a11y due to the need of full app context.
Gianthard-cyh 527cdd4
test: add a11y test to CommandBar.vue
Gianthard-cyh 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,241 @@ | ||
| <template> | ||
| <Teleport to="body"> | ||
| <Transition name="fade"> | ||
| <div | ||
| class="fixed inset-0 z-[1000] flex items-start justify-center bg-black/50 backdrop-blur-sm" | ||
| v-show="show" | ||
| > | ||
| <div | ||
| class="cmdbar-container flex items-center justify-center border border-border shadow-lg rounded-xl bg-bg p2 flex-col gap-2 mt-5rem" | ||
| role="dialog" | ||
| aria-modal="true" | ||
| aria-labelledby="command-input-label" | ||
| > | ||
| <label for="command-input" id="command-input-label" class="sr-only">{{ | ||
| t('command.label') | ||
| }}</label> | ||
|
|
||
| <search class="relative w-xl h-12 flex items-center"> | ||
| <span class="absolute inset-is-4 text-fg-subtle font-mono pointer-events-none"> | ||
| > | ||
| </span> | ||
| <input | ||
| type="text" | ||
| v-model="inputVal" | ||
| id="command-input" | ||
| ref="inputRef" | ||
| class="w-full h-full px-4 pl-8 text-fg outline-none bg-bg-subtle border border-border rounded-md" | ||
| :placeholder="placeholderText" | ||
| @keydown="handleKeydown" | ||
| /> | ||
| </search> | ||
|
|
||
| <div class="w-xl max-h-lg overflow-auto" v-if="view.type != 'INPUT'"> | ||
| <div | ||
| v-for="item in filteredCmdList" | ||
| :key="item.id" | ||
| class="px-4 py-2 not-first:mt-2 hover:bg-bg-elevated select-none cursor-pointer rounded-md transition" | ||
| :class="{ | ||
| 'bg-bg-subtle': item.id === selected, | ||
| 'trigger-anim': item.id === triggeringId, | ||
| }" | ||
| @click="onTrigger(item.id)" | ||
| > | ||
| <div class="text-fg">{{ item.name }}</div> | ||
| <div class="text-fg-subtle text-sm">{{ item.description }}</div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </Transition> | ||
| </Teleport> | ||
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| const { t } = useI18n() | ||
|
|
||
| type ViewState = | ||
| | { type: 'ROOT' } | ||
| | { type: 'INPUT'; prompt: string; resolve: (val: string) => void } | ||
| | { type: 'SELECT'; prompt: string; items: any[]; resolve: (val: any) => void } | ||
| const view = ref<ViewState>({ type: 'ROOT' }) | ||
|
|
||
| const cmdCtx: CommandContext = { | ||
| async input(options) { | ||
| return new Promise(resolve => { | ||
| view.value = { type: 'INPUT', prompt: options.prompt, resolve } | ||
| }) | ||
| }, | ||
| async select(options) { | ||
| return new Promise(resolve => { | ||
| view.value = { type: 'SELECT', prompt: options.prompt, items: options.items, resolve } | ||
| }) | ||
| }, | ||
| } | ||
|
|
||
| const { commands } = useCommandRegistry() | ||
|
|
||
| const selected = shallowRef(commands.value[0]?.id || '') | ||
| const inputVal = shallowRef('') | ||
| const show = shallowRef(false) | ||
| const triggeringId = shallowRef('') | ||
| const inputRef = useTemplateRef('inputRef') | ||
|
|
||
| const { focused: inputFocused } = useFocus(inputRef) | ||
|
|
||
| const placeholderText = computed(() => { | ||
| if (view.value.type === 'INPUT' || view.value.type === 'SELECT') { | ||
| return view.value.prompt | ||
| } | ||
| return t('command.placeholder') | ||
| }) | ||
|
|
||
| const filteredCmdList = computed(() => { | ||
| if (view.value.type === 'INPUT') { | ||
| return [] | ||
| } | ||
|
|
||
| const list = view.value.type === 'SELECT' ? view.value.items : commands.value | ||
|
|
||
| if (!inputVal.value) { | ||
| return list | ||
| } | ||
| const filter = inputVal.value.trim().toLowerCase() | ||
| return list.filter( | ||
| (item: any) => | ||
| item.name.toLowerCase().includes(filter) || | ||
| item.description?.toLowerCase().includes(filter) || | ||
| item.id.includes(filter), | ||
| ) | ||
| }) | ||
|
|
||
| watch( | ||
| () => filteredCmdList.value, | ||
| newVal => { | ||
| if (newVal.length) { | ||
| selected.value = newVal[0]?.id || '' | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
| function focusInput() { | ||
| inputFocused.value = true | ||
| } | ||
|
|
||
| function open() { | ||
| inputVal.value = '' | ||
| selected.value = commands.value[0]?.id || '' | ||
| show.value = true | ||
| view.value = { type: 'ROOT' } | ||
| nextTick(focusInput) | ||
| } | ||
|
|
||
| function close() { | ||
| inputVal.value = '' | ||
| selected.value = commands.value[0]?.id || '' | ||
| show.value = false | ||
| } | ||
|
|
||
| function toggle() { | ||
| if (show.value) { | ||
| close() | ||
| } else { | ||
| open() | ||
| } | ||
| } | ||
|
|
||
| function onTrigger(id: string) { | ||
| triggeringId.value = id | ||
|
|
||
| if (view.value.type === 'ROOT') { | ||
| const selectedItem = filteredCmdList.value.find((item: any) => item.id === id) | ||
| selectedItem?.handler?.(cmdCtx) | ||
| setTimeout(() => { | ||
| triggeringId.value = '' | ||
| if (view.value.type === 'ROOT') { | ||
| close() | ||
| } | ||
| }, 100) | ||
| } else if (view.value.type === 'INPUT') { | ||
| view.value.resolve(inputVal.value) | ||
| close() | ||
| } else if (view.value.type === 'SELECT') { | ||
| const selectedItem = filteredCmdList.value.find((item: any) => item.id === id) | ||
| view.value.resolve(selectedItem) | ||
| close() | ||
| } | ||
| } | ||
|
|
||
| const handleKeydown = useThrottleFn((e: KeyboardEvent) => { | ||
| if (view.value.type === 'INPUT' && e.key === 'Enter') { | ||
| e.preventDefault() | ||
| onTrigger('') // Trigger for input doesn't need ID | ||
| return | ||
| } | ||
|
|
||
| if ((e.key === 'ArrowDown' || e.key === 'ArrowUp') && !filteredCmdList.value.length) { | ||
| e.preventDefault() | ||
| return | ||
| } | ||
|
|
||
| const currentIndex = filteredCmdList.value.findIndex((item: any) => item.id === selected.value) | ||
|
|
||
| if (e.key === 'ArrowDown') { | ||
| e.preventDefault() | ||
| const nextIndex = (currentIndex + 1) % filteredCmdList.value.length | ||
| selected.value = filteredCmdList.value[nextIndex]?.id || '' | ||
| } else if (e.key === 'ArrowUp') { | ||
| e.preventDefault() | ||
| const prevIndex = | ||
| (currentIndex - 1 + filteredCmdList.value.length) % filteredCmdList.value.length | ||
Gianthard-cyh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| selected.value = filteredCmdList.value[prevIndex]?.id || '' | ||
| } else if (e.key === 'Enter') { | ||
| e.preventDefault() | ||
| onTrigger(selected.value) | ||
| } else if (e.key === 'Escape') { | ||
| e.preventDefault() | ||
| close() | ||
| } | ||
| }, 50) | ||
|
|
||
| defineExpose({ | ||
| open, | ||
| close, | ||
| toggle, | ||
| }) | ||
| </script> | ||
|
|
||
| <style scoped> | ||
| .fade-enter-active { | ||
| transition: all 0.05s ease-out; | ||
| } | ||
|
|
||
| .fade-leave-active { | ||
| transition: all 0.1s ease-in; | ||
| } | ||
|
|
||
| .fade-enter-from, | ||
| .fade-leave-to { | ||
| opacity: 0; | ||
| transform: translateY(-10px); | ||
| } | ||
|
|
||
| @keyframes trigger-pulse { | ||
| 0% { | ||
| transform: scale(1); | ||
| } | ||
|
|
||
| 50% { | ||
| transform: scale(0.96); | ||
| background-color: var(--bg-elevated); | ||
| } | ||
|
|
||
| 100% { | ||
| transform: scale(1); | ||
| } | ||
| } | ||
|
|
||
| .trigger-anim { | ||
| animation: trigger-pulse 0.1s ease-in-out; | ||
| } | ||
| </style> | ||
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,81 @@ | ||
| import { computed } from 'vue' | ||
|
|
||
| export interface Command { | ||
| id: string | ||
| name: string | ||
| description: string | undefined | ||
| handler?: (ctx: CommandContext) => Promise<void> | ||
| } | ||
|
|
||
| export interface CommandContext { | ||
| input: (options: CommandInputOptions) => Promise<string | undefined> | ||
| select: <T>(options: CommandSelectOptions<T>) => Promise<T | undefined> | ||
| } | ||
|
|
||
| export interface CommandInputOptions { | ||
| prompt: string | ||
| } | ||
|
|
||
| export interface CommandSelectOptions<T> { | ||
| prompt: string | ||
| items: T[] | ||
| } | ||
|
|
||
| /** | ||
| * Composable for global command registry. | ||
| * @public | ||
| */ | ||
| export const useCommandRegistry = () => { | ||
| const commands = useState<Map<string, Command>>('commands', () => new Map()) | ||
|
|
||
| const register = (command: Command) => { | ||
| const serverCommand = { | ||
| ...command, | ||
| handler: undefined, | ||
| } | ||
| if (import.meta.server) { | ||
| commands.value.set(command.id, serverCommand) | ||
| } else { | ||
| commands.value.set(command.id, command) | ||
| } | ||
| return () => { | ||
| commands.value.delete(command.id) | ||
Gianthard-cyh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| return { | ||
| register, | ||
| commands: computed(() => Array.from(commands.value.values())), | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Registers a global command. | ||
| * @public | ||
| */ | ||
| export const registerGlobalCommand = (command: Command) => { | ||
| const { register } = useCommandRegistry() | ||
| return register(command) | ||
| } | ||
|
|
||
| /** | ||
| * Registers a command bound to the current component's lifecycle. | ||
| * | ||
| * The command is automatically unregistered when the component unmounts. | ||
| * Use this to register commands that rely on local component state (context) | ||
| * via closure capture. | ||
| * | ||
| * @public | ||
| */ | ||
| export const registerScopedCommand = (command: Command) => { | ||
| const { register } = useCommandRegistry() | ||
| let unregister: () => void | ||
|
|
||
| onMounted(() => { | ||
| unregister = register(command) | ||
| }) | ||
|
|
||
| onUnmounted(() => { | ||
| unregister() | ||
| }) | ||
Gianthard-cyh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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.