Replies: 3 comments 7 replies
-
Same here. My use case is that I want to add custom keyboard navigation to a page with an accordion, where hitting up/down arrow would open the previous/next |
Beta Was this translation helpful? Give feedback.
-
What I ended up doing was using a combination of refs, the underlying Headless UI I couldn't figure out how to close them (I don't think that's possible directly), but you can check if they're open and if they are, trigger a if (currentRef.getAttribute('data-headlessui-state') === 'open') currentRef?.click(); In my case, I wanted to expand the next one and collapse the previous one via keyboard shortcuts, so I tracked the current ref index in state and called I first set up a ref with an array: const questionRefs = useRef<HTMLButtonElement[]>([]);
const addRefs = (el: HTMLButtonElement) => {
if (el && !questionRefs.current.includes(el)) {
questionRefs.current.push(el);
}
}; ...and then later do this to get the refs set up to each Accordion entry: {
item.map((a: any, i: number) => (
<Accordion key={`question_${i + 1}`} defaultOpen={i === 0}>
<AccordionHeader className='text-left' ref={addRefs}>
...
</AccordionHeader>
...
</Accordion>
))
} I have no idea how |
Beta Was this translation helpful? Give feedback.
-
Do you mean a control prop that updates state of the accordion? |
Beta Was this translation helpful? Give feedback.
-
Hi team,
It could be great to have an expanded field that can control the expanded state.
Eg. set it to true would expand the accordion and to false would collapse it.
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions