-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📚 docs: open details section when linking items on it (#42402)
- Loading branch information
1 parent
5056e67
commit 1d8e5b2
Showing
3 changed files
with
97 additions
and
6 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useLocation } from "@docusaurus/router"; | ||
import classNames from "classnames"; | ||
import React from "react"; | ||
import styles from "./Details.module.css"; | ||
|
||
export const Details = ({ className, children, ...rest }) => { | ||
const location = useLocation(); | ||
const [open, setOpen] = React.useState(false); | ||
const ref = React.useRef(null); | ||
|
||
const items = React.Children.toArray(children); | ||
const summary = items.find((item) => React.isValidElement(item) && item.type === "summary"); | ||
const content = items.filter((item) => React.isValidElement(item) && item.type !== "summary"); | ||
|
||
|
||
React.useEffect(() => { | ||
const detailsHeaderId = ref.current?.previousElementSibling?.id | ||
const contentIds = content | ||
.map((element) => { | ||
if (element.props.id) { | ||
return `#${element.props.id}`; | ||
} | ||
}) | ||
.filter(Boolean); | ||
|
||
if (contentIds.includes(location.hash) || location.hash === `#${detailsHeaderId}`) { | ||
setOpen(true); | ||
} else { | ||
setOpen(false); | ||
} | ||
}, [location.hash, content, summary, ref.current]); | ||
|
||
return ( | ||
<details | ||
open={open} | ||
ref={ref} | ||
className={classNames( | ||
className, | ||
"alert", | ||
"alert--info", | ||
styles.details | ||
)} | ||
{...rest} | ||
> | ||
{summary} | ||
<div className={classNames(styles.detailsContent)}>{content}</div> | ||
</details> | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
.details { | ||
--docusaurus-details-decoration-color: var(--ifm-color-info-dark); | ||
--docusaurus-details-transition: transform var(--ifm-transition-fast) ease; | ||
border: 1px solid var(--ifm-color-info-dark); | ||
margin: 0 0 var(--ifm-spacing-vertical); | ||
--docusaurus-details-summary-arrow-size: 0.38rem; | ||
} | ||
|
||
.details > summary { | ||
cursor: pointer; | ||
list-style: none; | ||
padding-left: 1rem; | ||
position: relative; | ||
display: flex; | ||
} | ||
|
||
.details>summary > p { | ||
margin-bottom: 0; | ||
} | ||
|
||
.details > summary::before { | ||
border-color: #0000 #0000 #0000 var(--docusaurus-details-decoration-color); | ||
border-style: solid; | ||
border-width: var(--docusaurus-details-summary-arrow-size); | ||
content: ""; | ||
left: 0; | ||
position: absolute; | ||
top: 0.45rem; | ||
transform: rotate(0); | ||
transform-origin: calc(var(--docusaurus-details-summary-arrow-size) / 2) 50%; | ||
transition: var(--docusaurus-details-transition); | ||
} | ||
|
||
.details[open] > summary::before { | ||
transform: rotate(90deg); | ||
} | ||
.detailsContent { | ||
border-top: 1px solid var(--docusaurus-details-decoration-color); | ||
margin-top: 1rem; | ||
padding-top: 1rem; | ||
} |
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