I'm trying to use the HTML <details>
tag to create a simple expandable section using semantic html in combination with React.
The <details><summary></summary></details>
behaviour works great out of the box and for the 1-2% of my users that use IE users that don't get the show-hide nature of the content, it really isn't the end of the world for the content to always be shown for the time being.
My issue comes when using React hooks to hold onto whether the <details>
panel is open or closed. The basic layout of the React component is as follows:
const DetailsComponent = ({startOpen}) => {
const [open, toggleOpen] = useState(startOpen);
return (
<details onToggle={() => toggleOpen(!open)} open={open}>
<summary>Summary</summary>
<p>Hidden content hidden content hidden content</p>
</details>
);
};
The reason I need to use the onToggle
event is to update the open
state variable to trigger some other javascript in my real world example. I use the startOpen
prop to decide whether on page render whether the details pane is open or closed.
The expected behaviour happens when I use the component as, <DetailsComponent startOpen={ false } />
.
However, when is want to start with the pane open on load (<DetailsComponent startOpen={ true } />
), I can visibly see the pane opening and closing very very quickly over and over again forever.