Skip to content
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

[Accessibility] Use id instead of children #254

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions cmdk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type Context = {
type State = {
search: string
value: string
selectedItemId?: string
filtered: { count: number; items: Map<string, number>; groups: Set<string> }
}
type Store = {
Expand Down Expand Up @@ -180,6 +181,8 @@ const Command = React.forwardRef<HTMLDivElement, CommandProps>((props, forwarded
search: '',
/** Currently selected item value. */
value: props.value ?? props.defaultValue ?? '',
/** Currently selected item id. */
selectedItemId: undefined,
filtered: {
/** The count of all visible items. */
count: 0,
Expand Down Expand Up @@ -247,6 +250,18 @@ const Command = React.forwardRef<HTMLDivElement, CommandProps>((props, forwarded
sort()
schedule(1, selectFirstItem)
} else if (key === 'value') {
// Force focus input or root so accessibility works
if (document.activeElement.hasAttribute('cmdk-input') || document.activeElement.hasAttribute('cmdk-root')) {
const input = document.getElementById(inputId)
if (input) input.focus()
else document.getElementById(listId)?.focus()
}

schedule(7, () => {
state.current.selectedItemId = getSelectedItem()?.id
store.emit()
})

// opts is a boolean referring to whether it should NOT be scrolled into view
if (!opts) {
// Scroll the selected item into view
Expand Down Expand Up @@ -779,16 +794,9 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, forwardedRe
const isControlled = props.value != null
const store = useStore()
const search = useCmdk((state) => state.search)
const value = useCmdk((state) => state.value)
const selectedItemId = useCmdk((state) => state.selectedItemId)
const context = useCommand()

const selectedItemId = React.useMemo(() => {
const item = context.listInnerRef.current?.querySelector(
`${ITEM_SELECTOR}[${VALUE_ATTR}="${encodeURIComponent(value)}"]`,
)
return item?.getAttribute('id')
}, [])

React.useEffect(() => {
if (props.value != null) {
store.setState('search', props.value)
Expand Down Expand Up @@ -831,6 +839,7 @@ const List = React.forwardRef<HTMLDivElement, ListProps>((props, forwardedRef) =
const { children, label = 'Suggestions', ...etc } = props
const ref = React.useRef<HTMLDivElement>(null)
const height = React.useRef<HTMLDivElement>(null)
const selectedItemId = useCmdk((state) => state.selectedItemId)
const context = useCommand()

React.useEffect(() => {
Expand Down Expand Up @@ -858,6 +867,8 @@ const List = React.forwardRef<HTMLDivElement, ListProps>((props, forwardedRef) =
{...etc}
cmdk-list=""
role="listbox"
tabIndex={-1}
aria-activedescendant={selectedItemId}
aria-label={label}
id={context.listId}
>
Expand Down
Loading