-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: headless ComboBox primitives #1877
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
Draft
sagalbot
wants to merge
18
commits into
review/primitives-base
Choose a base branch
from
@beta/dev
base: review/primitives-base
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.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0350397
docs: add branching strategy and commit conventions to plans
sagalbot b6dad1a
fix(useClickAway): store handler reference so removeEventListener works
sagalbot cef4732
feat(types): define ComboBoxProps and ComboBoxContext interfaces
sagalbot ce1611c
feat(useComboBox): implement core state management composable
sagalbot ce05321
feat(useComboBox): add filtering and taggable support
sagalbot eaaac83
feat(useComboBox): add typeAheadPointer keyboard navigation
sagalbot 7e26291
feat(useComboBox): add reduce prop support with reverse lookup
sagalbot 369fdc9
feat(useComboBox): add tagging, pushTags, and loading support
sagalbot abdfdda
feat(ComboBox): wire up useComboBox composable with provide/inject
sagalbot 0a9d437
feat(ComboBoxInput): full keyboard nav, ARIA, and IME support
sagalbot 8d5cb02
feat(ComboBoxMenu): ARIA listbox with auto-scroll
sagalbot f5d09ae
feat(ComboBoxOption)!: add ARIA, highlight, and disabled states
sagalbot 16c1a61
feat(ComboBoxButton, ComboBoxClear): toggle and clear primitives
sagalbot 93d2a0d
feat: export headless primitives and useComboBox from package
sagalbot 2ab50fd
test: add integration tests for full ComboBox composition
sagalbot 04bcc1a
fix: address PR review feedback for headless primitives
sagalbot 0d30d55
fix(ComboBoxMenu): use id-based lookup for autoscroll
sagalbot a79f488
fix: resolve ESLint errors in new v4 primitives and tests
sagalbot 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
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 |
|---|---|---|
| @@ -1,72 +1,55 @@ | ||
| <script setup lang="ts"> | ||
| import { ListBoxKey } from '@/keys' | ||
| import type { ComputedRef } from 'vue' | ||
| import { | ||
| provide, | ||
| computed, | ||
| reactive, | ||
| watch, | ||
| onMounted, | ||
| ref, | ||
| onUnmounted, | ||
| } from 'vue' | ||
| import { provide, ref, onMounted, onUnmounted } from 'vue' | ||
| import { useComboBox } from '@/hooks/useComboBox' | ||
| import { useClickAway } from '@/hooks/useClickAway' | ||
| import type { | ||
| InjectedListBoxProps, | ||
| ListBoxProps, | ||
| ResolvedListBoxProps, | ||
| VueSelectValue, | ||
| } from '@/types' | ||
|
|
||
| const emit = defineEmits(['update:modelValue', 'update:open', 'open', 'close']) | ||
|
|
||
| const props = withDefaults(defineProps<ListBoxProps>(), { | ||
| open: undefined, | ||
| import { ComboBoxKey } from '@/keys' | ||
| import type { ComboBoxProps } from '@/types' | ||
|
|
||
| const props = withDefaults(defineProps<ComboBoxProps>(), { | ||
| options: () => [], | ||
| multiple: false, | ||
| filterable: true, | ||
| taggable: false, | ||
| pushTags: false, | ||
| clearable: true, | ||
| closeOnSelect: true, | ||
| clearSearchOnSelect: true, | ||
| disabled: false, | ||
| label: 'label', | ||
| loading: false, | ||
| noDrop: false, | ||
| deselectFromDropdown: false, | ||
| autoscroll: true, | ||
| placeholder: '', | ||
| }) | ||
|
|
||
| const emit = defineEmits<{ | ||
| 'update:modelValue': [value: unknown] | ||
| 'update:open': [value: boolean] | ||
| open: [] | ||
| close: [] | ||
| search: [search: string, toggleLoading: (value?: boolean) => void] | ||
| 'option:created': [option: unknown] | ||
| 'option:selecting': [option: unknown] | ||
| 'option:selected': [option: unknown] | ||
| 'option:deselecting': [option: unknown] | ||
| 'option:deselected': [option: unknown] | ||
| }>() | ||
|
|
||
| const ctx = useComboBox(props, emit) | ||
| provide(ComboBoxKey, ctx) | ||
|
sagalbot marked this conversation as resolved.
sagalbot marked this conversation as resolved.
|
||
|
|
||
| // Click-away to close | ||
| const el = ref<HTMLElement>() | ||
| const state = reactive<{ | ||
| open: boolean | ||
| }>({ | ||
| open: props.open === undefined ? false : props.open, | ||
| const { addClickAwayListener, removeClickAwayListener } = useClickAway(() => { | ||
| ctx.setOpen(false) | ||
| }) | ||
|
|
||
| watch( | ||
| () => state.open, | ||
| (open) => emit('update:open', open), | ||
| ) | ||
|
|
||
| const inputText = ref('') | ||
|
|
||
| const isOpen = computed<boolean>(() => { | ||
| if (props.open !== undefined) { | ||
| return props.open | ||
| } | ||
| return state.open | ||
| }) | ||
|
|
||
| const { addClickAwayListener, removeClickAwayListener } = useClickAway( | ||
| () => (state.open = false), | ||
| ) | ||
|
|
||
| onMounted(() => addClickAwayListener(el.value)) | ||
| onUnmounted(() => removeClickAwayListener(el.value)) | ||
|
|
||
| provide<InjectedListBoxProps>( | ||
| ListBoxKey, | ||
| computed<ResolvedListBoxProps>(() => ({ | ||
| open: isOpen.value, | ||
| modelValue: props.modelValue, | ||
| inputText: inputText.value, | ||
| toggleOpen: () => (state.open = !state.open), | ||
| setModelValue: (modelValue) => emit('update:modelValue', modelValue), | ||
| setInputText: (value: string) => (inputText.value = value), | ||
| })), | ||
| ) | ||
| </script> | ||
|
|
||
| <template> | ||
| <div tabindex="0" role="combobox" ref="el"> | ||
| <slot></slot> | ||
| <div ref="el"> | ||
| <slot /> | ||
| </div> | ||
| </template> | ||
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 |
|---|---|---|
| @@ -1,28 +1,21 @@ | ||
| <script setup lang="ts"> | ||
| import { inject } from 'vue' | ||
| import { ListBoxKey } from '@/keys' | ||
| import { ComboBoxKey } from '@/keys' | ||
|
|
||
| withDefaults( | ||
| defineProps<{ | ||
| as?: string | ||
| }>(), | ||
| { | ||
| as: 'button', | ||
| }, | ||
| ) | ||
|
|
||
| const listBoxProps = inject(ListBoxKey) | ||
| const ctx = inject(ComboBoxKey) | ||
| if (!ctx) throw new Error('ComboBoxButton must be used inside a ComboBox component') | ||
| </script> | ||
|
|
||
| <template> | ||
| <Component | ||
| :is="as" | ||
| tabindex="0" | ||
| <button | ||
| type="button" | ||
| aria-haspopup="true" | ||
| :aria-expanded="listBoxProps.open" | ||
| @click="listBoxProps.toggleOpen" | ||
| aria-haspopup="listbox" | ||
| :aria-expanded="String(ctx.open.value)" | ||
| :aria-controls="`vs-${ctx.uid.value}-listbox`" | ||
| :disabled="ctx.disabled.value || undefined" | ||
| @click="ctx.toggleOpen()" | ||
| @mousedown.prevent | ||
| > | ||
| <slot></slot> | ||
| </Component> | ||
| <slot /> | ||
| </button> | ||
| </template> |
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,19 @@ | ||
| <script setup lang="ts"> | ||
| import { inject } from 'vue' | ||
| import { ComboBoxKey } from '@/keys' | ||
|
|
||
| const ctx = inject(ComboBoxKey) | ||
| if (!ctx) throw new Error('ComboBoxClear must be used inside a ComboBox component') | ||
| </script> | ||
|
|
||
| <template> | ||
| <button | ||
| v-show="!ctx.isValueEmpty.value && ctx.clearable.value" | ||
| type="button" | ||
| aria-label="Clear selection" | ||
| @click="ctx.clearSelection()" | ||
| @mousedown.prevent | ||
| > | ||
| <slot /> | ||
| </button> | ||
| </template> |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The v3 component emits
search:blurandsearch:focusevents that are documented in the API. These events are not emitted in the new primitives. If you want to maintain backward compatibility with v3 event listeners in the styled wrapper, consider adding these events to the emit types and emitting them from onFocus and onBlur handlers.