Skip to content

Commit

Permalink
feat: add aria-activedescendant in input (#108)
Browse files Browse the repository at this point in the history
feat: use aria-activedescendant in input

Co-authored-by: Paco <34928425+pacocoursey@users.noreply.github.com>
  • Loading branch information
joaom00 and pacocoursey authored Apr 10, 2023
1 parent 65168c2 commit 5a61d5f
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmdk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type Context = {
group: (id: string) => () => void
filter: () => boolean
label: string
commandRef: React.RefObject<HTMLDivElement | null>
// Ids
listId: string
labelId: string
Expand Down Expand Up @@ -273,6 +274,7 @@ const Command = React.forwardRef<HTMLDivElement, CommandProps>((props, forwarded
return propsRef.current.shouldFilter
},
label: label || props['aria-label'],
commandRef: ref,
listId,
inputId,
labelId,
Expand Down Expand Up @@ -609,6 +611,7 @@ const Item = React.forwardRef<HTMLDivElement, ItemProps>((props, forwardedRef) =
<div
ref={mergeRefs([ref, forwardedRef])}
{...etc}
id={id}
cmdk-item=""
role="option"
aria-disabled={disabled || undefined}
Expand Down Expand Up @@ -687,8 +690,14 @@ 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 context = useCommand()

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

React.useEffect(() => {
if (props.value != null) {
store.setState('search', props.value)
Expand All @@ -708,6 +717,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, forwardedRe
aria-expanded={true}
aria-controls={context.listId}
aria-labelledby={context.labelId}
aria-activedescendant={selectedItemId}
id={context.inputId}
type="text"
value={isControlled ? props.value : search}
Expand Down

1 comment on commit 5a61d5f

@vercel
Copy link

@vercel vercel bot commented on 5a61d5f Apr 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

cmdk-website – ./

cmdk.vercel.app
cmdk-website-paco.vercel.app
cmdk-website-git-main-paco.vercel.app
cmdk.paco.me

Please sign in to comment.