cmdk icon indicating copy to clipboard operation
cmdk copied to clipboard

Listen when CommandItem is Highlighted

Open orlandodiaz opened this issue 1 year ago • 2 comments

I want to conditionally render a different JSX based on whether the CommandItem is highlighted (not clicked). i.e mouse over or selected via using the arrow keys where we see the bg change

I don't see this being possible at the moment

Only thing I was able to find was doing something like this

[cmdk-item][data-selected] { outline: 1px solid black; }

but this only applies CSS. I want to conditionally apply different JSX and do more than just styling

orlandodiaz avatar Oct 22 '24 04:10 orlandodiaz

You can track the aria-selected or data-selected boolean values using MutationObserver.

I think the example in here could help. Which is the Model HoverCard showed in the Playground.

Then you may also need to check the [PR] (https://github.com/shadcn-ui/ui/pull/5382), which fixes the bugs of the implementation.

Lexachoc avatar Oct 26 '24 23:10 Lexachoc

I need the same thing, there is no official way?

gbertoncelli avatar Mar 19 '25 09:03 gbertoncelli