diff --git a/src/components/Accordion/Item.tsx b/src/components/Accordion/Item.tsx index fd8e5cfc..eb93913f 100644 --- a/src/components/Accordion/Item.tsx +++ b/src/components/Accordion/Item.tsx @@ -1,4 +1,5 @@ -import React, { useRef } from "react"; +import { useLocation } from "@docusaurus/router"; +import React, { useEffect, useRef } from "react"; type AccordionItemProps = { children: React.ReactNode; @@ -9,6 +10,7 @@ type AccordionItemProps = { const AccordionItem = ({ summary, open, children }: AccordionItemProps) => { const detailsRef = useRef(null); + const location = useLocation(); const handleItemClick = () => { document.querySelectorAll("details.accordion-item").forEach((item) => { @@ -16,8 +18,22 @@ const AccordionItem = ({ summary, open, children }: AccordionItemProps) => { item.removeAttribute("open"); } }); + + window.location.replace(`${location.pathname}#${encodeURI(summary)}`); }; + useEffect(() => { + if (decodeURI(location.hash) === `#${summary}`) { + document.querySelectorAll("details.accordion-item").forEach((item) => { + if (item !== detailsRef.current) { + item.removeAttribute("open"); + } else { + item.setAttribute("open", "true"); + } + }); + } + }, []); + return (