1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-04 23:58:01 +00:00

Add Details Tags Are A Controllable Component as a react til

This commit is contained in:
jbranchaud
2020-05-06 21:03:34 -05:00
parent e7d7efef68
commit c903cca82e
2 changed files with 33 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
# Details Tags Are A Controllable Component
HTML has a lovely, but little-used tag for summarizing a set of collapsed
details. These details can be expanded and recollapsed by clicking the summary.
This is the `<details>` tag and it can be controlled with the `open` prop and
`onToggle` handler.
```jsx
import React, { useState } from "react";
export default function Details({ summary, children, onToggle }) {
const [open, setOpen] = useState(false);
return (
<details
open={open}
onToggle={() => {
setOpen(prev => !prev);
onToggle();
}}
>
<summary>{summary}</summary>
{children}
</details>
);
}
```
[live
example](https://codesandbox.io/s/loving-merkle-hxlut?file=/src/App.js:0-545),
[source](https://github.com/facebook/react/issues/15486)