You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I refactored the code functionally to try and solve the issue, and the events are firing correctly now, however it's behaving very strangely. (PS. I tried to create a boolean variable to stop the event handlers from running if another is running to try and solve the issue. No bueno.)
importReact,{useEffect}from"react"import{useStaticQuery,graphql}from"gatsby"import*asbootstrapfrom'bootstrap'constNavigation=({data})=>{constnav=useStaticQuery(graphql` query NavQuery { allMarkdownRemark( sort: {fields: [frontmatter___tags, frontmatter___chapter], order: DESC} ) { edges { node { id frontmatter { path chapter title date tags chapter chapter_path chapter_type } } } } } `)// Using Bootstrap 5's nav event handlers to attempt to solve the strange 'undefined'// issue by stopping collapse events from running cocurrentlyletisHandlerRunning=0;useEffect(()=>{document.getElementById('nav-collapse-parent').querySelectorAll('.collapse').forEach(navItem=>{navItem.addEventListener('show.bs.collapse',function(){isHandlerRunning=1;navItem.removeEventListener('mouseenter',hoverCollapseOver);})navItem.addEventListener('hidden.bs.collapse',function(){isHandlerRunning=0;navItem.addEventListener('mouseenter',hoverCollapseOver);})})})//onMouseEnter & onMouseLeave events for navigation expansionconsthoverCollapseOver=(event)=>{if(isHandlerRunning!=1){event.target.children[0].setAttribute('aria-expanded','true');event.target.children[0].classList.remove('collapsed');newbootstrap.Collapse(event.target.children[1],{toggle: true,parent: '#nav-collapse-parent'});console.log(isHandlerRunning);}else{return;}}consthoverCollapseOut=(event)=>{if(isHandlerRunning!=0){//this is the original element the event handler was assigned tolete=event.toElement||event.relatedTarget;if(e.parentNode==this||e==this){return;}event.target.children[0].setAttribute('aria-expanded','false');event.target.children[0].classList.add('collapsed');console.log(event.target)newbootstrap.Collapse(event.target.children[1],{toggle: true,parent: '#nav-collapse-parent'});}else{return;}}// Looping and iteration traversing to reorganize nav items into a nested formconstnavData={};nav.allMarkdownRemark.edges.map(function(post,index){if(post.node.frontmatter.chapter){if(!navData[post.node.frontmatter.chapter]){navData[post.node.frontmatter.chapter]={};navData[post.node.frontmatter.chapter]['path']=post.node.frontmatter.chapter_path;}if(!navData[post.node.frontmatter.chapter]['children']){navData[post.node.frontmatter.chapter]['children']={};}navData[post.node.frontmatter.chapter]['children'][post.node.frontmatter.title]=post.node.frontmatter.path;}else{navData[post.node.frontmatter.title]={};navData[post.node.frontmatter.title]['path']=post.node.frontmatter.path;}});// the renderreturn(<asideclass="bd-sidebar"><navclass="collapse bd-links"id="bd-docs-nav"aria-label="Docs navigation"><ulclass="list-unstyled mb-0 py-3 pt-md-1"id="nav-collapse-parent">{Object.keys(navData).map(function(key){if(navData[key]['children']!==undefined){return(<liclass="mb-1"aria-expanded="false"onMouseEnter={(e)=>hoverCollapseOver(e)}onMouseLeave={(e)=>hoverCollapseOut(e)}data-bs-parent="#nav-collapse-parent"data-bs-target={"#"+key.replaceAll(' ','-').toLowerCase()}><ahref={navData[key]['path']}class="btn d-inline-flex align-items-center rounded collapsed text-start"id={key.replaceAll(' ','-').toLowerCase()+'-source'}>{key}</a><divclass="collapse"id={key.replaceAll(' ','-').toLowerCase()}style={{margin: 1+'em'}}><ulclass="list-unstyled fw-normal pb-1 small">{Object.keys(navData[key]['children']).map(function(innerKey){return(<li><ahref={navData[key][innerKey]}class="d-inline-flex align-items-center rounded">{innerKey}</a></li>)})}</ul></div></li>)}else{return(<liclass="mb-1"><ahref={navData[key]['path']}class="d-inline-flex align-items-center rounded text-start"id={key.replaceAll(' ','-').toLowerCase()+'-source'}>{key}</a></li>)}})}</ul></nav></aside>)}exportdefaultNavigation
UPDATE 2
I updated the code and was able to make the errors go away. Close but still no cigar - the problem is narrowed down to this: in the mouseLeave function, the bootstrap.Collapse toggle doesn't fire consistently.
Here's a video (you have to wait until the end before I'm able to recreate the problem of multiple collapse elements opening at once).
Here's the code:
importReactfrom"react"import{useStaticQuery,graphql}from"gatsby"import*asbootstrapfrom'bootstrap'constNavigation=({data})=>{constnav=useStaticQuery(graphql` query NavQuery { allMarkdownRemark( sort: {fields: [frontmatter___tags, frontmatter___chapter], order: DESC} ) { edges { node { id frontmatter { path chapter title date tags chapter chapter_path chapter_type } } } } } `)// finds siblings and returns them in an arrayconstgetSiblings=(elem)=>{// Setup siblings array and get the first siblingconstsiblings=[];letsibling=elem.parentNode.firstChild;// Loop through each sibling and push to the arraywhile(sibling){if(sibling.nodeType===1&&sibling!==elem){siblings.push(sibling);}sibling=sibling.nextSibling}returnsiblings;};//onMouseEnter & onMouseLeave events for navigation expansionconsthoverCollapse=(event,triggerType)=>{constmouseLeave=(elem)=>{if(!elem.children[1].classList.contains("show")&&!elem.children[1].classList.contains("collapsing"))return;// break if doesn't have visible childrenelem.children[0].setAttribute('aria-expanded','false');elem.children[0].classList.add('collapsed');newbootstrap.Collapse(elem.children[1],{toggle: true,show: false,hide: true})console.log(elem);console.log("^ should be collapsed")}constmouseEnter=(elem)=>{if(elem.children[1].classList.contains("show")||elem.children[1].classList.contains("collapsing"))return;// break if doesn't have hidden childrenelem.children[0].setAttribute('aria-expanded','true');elem.children[0].classList.remove('collapsed');newbootstrap.Collapse(elem.children[1],{toggle: true,show: true,hide: false//parent: '#nav-collapse-parent'});}constcollapseSiblings=(elem)=>{getSiblings(elem).forEach(sibling=>{if(!sibling.classList.contains("collapse-trigger"))return;// break if isn't a collapse elementmouseLeave(sibling)})}if(!event.target.classList.contains("collapse-trigger"))return;// break if isn't a collapse elementif(triggerType=="onMouseLeave"){mouseLeave(event.target)collapseSiblings(event.target)}elseif(triggerType="onMouseEnter"){mouseEnter(event.target)collapseSiblings(event.target)}}// Looping and iteration traversing to reorganize nav items into a nested formconstnavData={};nav.allMarkdownRemark.edges.map((post,index)=>{if(post.node.frontmatter.chapter){if(!navData[post.node.frontmatter.chapter]){navData[post.node.frontmatter.chapter]={};navData[post.node.frontmatter.chapter]['path']=post.node.frontmatter.chapter_path;}if(!navData[post.node.frontmatter.chapter]['children']){navData[post.node.frontmatter.chapter]['children']={};}navData[post.node.frontmatter.chapter]['children'][post.node.frontmatter.title]=post.node.frontmatter.path;}else{navData[post.node.frontmatter.title]={};navData[post.node.frontmatter.title]['path']=post.node.frontmatter.path;}});// the renderreturn(<asideclass="bd-sidebar"><navclass="collapse bd-links"id="bd-docs-nav"aria-label="Docs navigation"><ulclass="list-unstyled mb-0 py-3 pt-md-1"id="nav-collapse-parent">{Object.keys(navData).map((key)=>{if(navData[key]['children']!==undefined){return(<liclass="collapse-trigger"style={{marginBottom: 5+'px',paddingBottom: 5+'px'}}onMouseEnter={(e)=>hoverCollapse(e,'onMouseEnter')}onMouseLeave={(e)=>hoverCollapse(e,'onMouseLeave')}data-bs-parent="#nav-collapse-parent"data-bs-target={"#"+key.replaceAll(' ','-').toLowerCase()}><aaria-expanded="false"href={navData[key]['path']}class="btn d-inline-flex align-items-center rounded collapsed text-start"id={key.replaceAll(' ','-').toLowerCase()+'-source'}>{key}</a><divclass="collapse collapse-target"id={key.replaceAll(' ','-').toLowerCase()}style={{margin: 5+'px'}}><ulclass="list-unstyled fw-normal pb-1 small"style={{marginBottom: 5+'px'}}>{Object.keys(navData[key]['children']).map((innerKey)=>{return(<li><ahref={navData[key][innerKey]}class="d-inline-flex align-items-center rounded">{innerKey}</a></li>)})}</ul></div></li>)}else{return(<liclass="mb-1"><ahref={navData[key]['path']}class="d-inline-flex align-items-center rounded text-start"id={key.replaceAll(' ','-').toLowerCase()+'-source'}>{key}</a></li>)}})}</ul></nav></aside>)}exportdefaultNavigation
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Seems I can reference this method everywhere but within the
<StaticQuery />
component.How can I reference the methods I try to mention in
onMouseEnter
andonMouseLeave
?The error I'm getting is
TypeError: Cannot read property 'collapseOpen' of undefined
I'm using Gatsby with Bootstrap 5.
UPDATE
I refactored the code functionally to try and solve the issue, and the events are firing correctly now, however it's behaving very strangely. (PS. I tried to create a boolean variable to stop the event handlers from running if another is running to try and solve the issue. No bueno.)
Here's a quick video showing the strange behavior.
Here's the updated code:
UPDATE 2
I updated the code and was able to make the errors go away. Close but still no cigar - the problem is narrowed down to this: in the
mouseLeave
function, thebootstrap.Collapse
toggle doesn't fire consistently.Here's a video (you have to wait until the end before I'm able to recreate the problem of multiple collapse elements opening at once).
Here's the code:
Beta Was this translation helpful? Give feedback.
All reactions