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
Well I gave my other question a go and almost got it working. Eventually got stuck on part of the API. I've included an explanation video here.
TLDR: I can't get new bootstrap.collapse() to toggle the collapse of my element when another item is currently collapsing. I want to be able to interrupt a collapse with another one.
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
-
Well I gave my other question a go and almost got it working. Eventually got stuck on part of the API. I've included an explanation video here.
TLDR: I can't get
new bootstrap.collapse()
to toggle the collapse of my element when another item is currently collapsing. I want to be able to interrupt a collapse with another one.What do you guys think?
Here's my code (I'm using gatsby):
Beta Was this translation helpful? Give feedback.
All reactions