1- import React , { useState } from "react" ;
1+ import React , { useState , useRef } from "react" ;
22import { graphql , useStaticQuery , Link } from "gatsby" ;
33import { MDXRenderer } from "gatsby-plugin-mdx" ;
44import { Container , Row , Col } from "../../../reusecore/Layout" ;
@@ -55,26 +55,44 @@ const WorkshopsPage = () => {
5555}`
5656 ) ;
5757
58+ const [ scrollPosition , setScrollPosition ] = useState ( 0 ) ;
59+ const scrollRefMap = useRef ( { } ) ;
60+
61+
5862 const toggleActive = ( id ) => {
59- if ( open ) {
60- if ( ID === id ) {
61- setOpen ( false ) ;
62- setContent ( false ) ;
63- setID ( "" ) ;
64- } else {
65- setOpen ( false ) ;
66- setContent ( false ) ;
67- setID ( id ) ;
68- setContent ( true ) ;
69- setOpen ( true ) ;
63+ const targetElement = scrollRefMap . current [ id ] ;
64+
65+ if ( open && ID === id ) {
66+ setOpen ( false ) ;
67+ setContent ( false ) ;
68+ setID ( "" ) ;
69+
70+ if ( targetElement ) {
71+ // Wait for DOM to collapse, then scroll
72+ requestAnimationFrame ( ( ) => {
73+ requestAnimationFrame ( ( ) => {
74+ const rect = targetElement . getBoundingClientRect ( ) ;
75+ const y = rect . top + window . scrollY ; // adjust for header
76+ window . scrollTo ( {
77+ top : y ,
78+ behavior : "smooth" ,
79+ } ) ;
80+ } ) ;
81+ } ) ;
7082 }
7183 } else {
84+ if ( targetElement ) {
85+ const rect = targetElement . getBoundingClientRect ( ) ;
86+ const y = rect . top + window . scrollY - 20 ;
87+ setScrollPosition ( y ) ;
88+ }
7289 setID ( id ) ;
7390 setContent ( true ) ;
7491 setOpen ( true ) ;
7592 }
7693 } ;
7794
95+
7896 return (
7997 < WorkshopPageWrapper >
8098 < PageHeader title = "Cloud Native Workshops" />
@@ -87,7 +105,9 @@ const WorkshopsPage = () => {
87105 } } >
88106 { data . allMdx . nodes . map ( ( { id, frontmatter, fields, body } ) => (
89107 < Col { ...content && ID === id ? { $xs : 12 , $sm : 12 , $lg : 12 } : { $xs : 12 , $sm : 6 , $lg : 4 } } key = { id } className = "workshop-grid-col" >
90- < div className = "workshop-grid-card" >
108+ < div className = "workshop-grid-card" ref = { ( el ) => {
109+ if ( el ) scrollRefMap . current [ id ] = el ;
110+ } } >
91111 < WorkshopCard frontmatter = { frontmatter } content = { content } ID = { ID } id = { id } />
92112 < div className = { content && ID === id ? "active" : "text-contents" } >
93113 < div className = "content" >
@@ -108,6 +128,7 @@ const WorkshopsPage = () => {
108128 < button onClick = { ( ) => toggleActive ( id ) } className = "readmeBtn" > Read Less < BsArrowUp className = "icon" size = { 30 } /> </ button > :
109129 < button onClick = { ( ) => toggleActive ( id ) } className = "readmeBtn readmreBtn" > Read More < BsArrowDown className = "icon" size = { 30 } /> </ button > }
110130 </ div >
131+
111132 < div className = "externalLink" >
112133 < Link to = { fields . slug } className = "siteLink" > < FaRegWindowMaximize style = { { height : "25px" , width : "auto" } } /> </ Link >
113134 </ div >
0 commit comments