Last active
March 19, 2023 20:28
-
-
Save mstewartgallus/a822c4113c9e0e931b33309664affd51 to your computer and use it in GitHub Desktop.
Fscked up ebil shenigans. It doesn't actually seem possible to use details for an accordion not even by abusing aria-owns. Unfortunately the spec says that summary is only sometimes exposed as a button role. details has only slightly less weirdness.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useId, useState, useCallback } from "react"; | |
const Panel = ({children, heading}) => { | |
const detailsId = useId(); | |
const headingTextId = useId(); | |
const headingId = useId(); | |
const contentId = useId(); | |
const [open, setOpen] = useState(false); | |
const onToggle = useCallback(e => { | |
e.preventDefault(); | |
setOpen(o => !o); | |
}, []); | |
return <article aria-labelledby={headingId}> | |
<span role="presentation" aria-owns={headingId} /> | |
<span role="presentation" aria-owns={contentId} /> | |
<details | |
id={detailsId} | |
onToggle={onToggle} | |
open={open ? "open" : null} | |
> | |
<summary | |
style={{display: 'block'}} | |
aria-labelledby={headingTextId} | |
aria-controls={contentId} | |
aria-expanded={String(open)} | |
> | |
<span role="presentation" aria-owns={headingTextId} /> | |
<h2 id={headingId} aria-labelledby={headingTextId}> | |
<span role="presentation" aria-owns={detailsId} /> | |
<span role="presentation" id={headingTextId}> | |
{heading} | |
</span> | |
</h2> | |
</summary> | |
<div role="presentation" id={contentId}> | |
{children} | |
</div> | |
</details> | |
</article>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment