From 963912a4123c71b0194a1aecb0c6162e0b9722e2 Mon Sep 17 00:00:00 2001 From: jakubabrzy Date: Mon, 18 Sep 2023 16:09:14 +0200 Subject: [PATCH] add functionality to share faq item --- src/components/Accordion/Item.tsx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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 (