diff --git a/src/sections/Learn/Workshop-grid/index.js b/src/sections/Learn/Workshop-grid/index.js index 2b33222214f5f..8f5139282a166 100644 --- a/src/sections/Learn/Workshop-grid/index.js +++ b/src/sections/Learn/Workshop-grid/index.js @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useState, useRef } from "react"; import { graphql, useStaticQuery, Link } from "gatsby"; import { MDXRenderer } from "gatsby-plugin-mdx"; import { Container, Row, Col } from "../../../reusecore/Layout"; @@ -55,26 +55,44 @@ const WorkshopsPage = () => { }` ); + const [scrollPosition, setScrollPosition] = useState(0); + const scrollRefMap = useRef({}); + + const toggleActive = (id) => { - if (open){ - if (ID === id){ - setOpen(false); - setContent(false); - setID(""); - } else { - setOpen(false); - setContent(false); - setID(id); - setContent(true); - setOpen(true); + const targetElement = scrollRefMap.current[id]; + + if (open && ID === id) { + setOpen(false); + setContent(false); + setID(""); + + if (targetElement) { + // Wait for DOM to collapse, then scroll + requestAnimationFrame(() => { + requestAnimationFrame(() => { + const rect = targetElement.getBoundingClientRect(); + const y = rect.top + window.scrollY; // adjust for header + window.scrollTo({ + top: y, + behavior: "smooth", + }); + }); + }); } } else { + if (targetElement) { + const rect = targetElement.getBoundingClientRect(); + const y = rect.top + window.scrollY - 20; + setScrollPosition(y); + } setID(id); setContent(true); setOpen(true); } }; + return ( @@ -87,7 +105,9 @@ const WorkshopsPage = () => { }}> {data.allMdx.nodes.map(({ id, frontmatter, fields, body }) => ( -
+
{ + if (el) scrollRefMap.current[id] = el; + }}>
@@ -104,10 +124,23 @@ const WorkshopsPage = () => {
- {content && ID === id ? - : - } + {content && ID === id ? ( + + ) : ( + + )}
+