mirror of
https://github.com/antebudimir/feishin.git
synced 2026-01-02 02:43:33 +00:00
support tab navigation on ActionIcons in command palette
This commit is contained in:
parent
d28054cc7f
commit
33af5e625b
3 changed files with 127 additions and 45 deletions
|
|
@ -0,0 +1,37 @@
|
|||
import { Command } from 'cmdk';
|
||||
import { ComponentPropsWithoutRef, ReactNode, useEffect, useRef, useState } from 'react';
|
||||
|
||||
interface CommandItemSelectableProps
|
||||
extends Omit<ComponentPropsWithoutRef<typeof Command.Item>, 'children'> {
|
||||
children: (args: { isHighlighted: boolean }) => ReactNode;
|
||||
}
|
||||
|
||||
export function CommandItemSelectable({ children, ...itemProps }: CommandItemSelectableProps) {
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
const [isHighlighted, setIsHighlighted] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const el = ref.current;
|
||||
if (!el) return;
|
||||
|
||||
setIsHighlighted(el.getAttribute('aria-selected') === 'true');
|
||||
|
||||
const observer = new MutationObserver(() => {
|
||||
const selected = el.getAttribute('aria-selected') === 'true';
|
||||
setIsHighlighted(selected);
|
||||
});
|
||||
|
||||
observer.observe(el, {
|
||||
attributeFilter: ['aria-selected'],
|
||||
attributes: true,
|
||||
});
|
||||
|
||||
return () => observer.disconnect();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Command.Item {...itemProps} ref={ref}>
|
||||
{children({ isHighlighted })}
|
||||
</Command.Item>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue