Skip to content

Commit

Permalink
Decouple ToC expanding logic from sidebar container
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwoodnz committed Jan 12, 2024
1 parent f389896 commit b9309f7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
14 changes: 0 additions & 14 deletions mu-plugins/blocks/sidebar-container/src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,6 @@ function onScroll() {
function init() {
container = document.querySelector( '.wp-block-wporg-sidebar-container' );
mainEl = document.getElementById( 'wp--skip-link--target' );
const toggleButton = container?.querySelector( '.wporg-table-of-contents__toggle' );
const list = container?.querySelector( '.wporg-table-of-contents__list' );

if ( toggleButton && list ) {
toggleButton.addEventListener( 'click', function () {
if ( toggleButton.getAttribute( 'aria-expanded' ) === 'true' ) {
toggleButton.setAttribute( 'aria-expanded', false );
list.removeAttribute( 'style' );
} else {
toggleButton.setAttribute( 'aria-expanded', true );
list.setAttribute( 'style', 'display:block;' );
}
} );
}

if ( mainEl && container ) {
onScroll(); // Run once to avoid footer collisions on load (ex, when linked to #reply-title).
Expand Down
3 changes: 2 additions & 1 deletion mu-plugins/blocks/table-of-contents/src/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
}
},
"editorScript": "file:./index.js",
"style": "file:./style.css"
"style": "file:./style.css",
"viewScript": "file:./view.js"
}
24 changes: 24 additions & 0 deletions mu-plugins/blocks/table-of-contents/src/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function init() {
const container = document.querySelector( '.wp-block-wporg-table-of-contents' );

if ( ! container ) {
return;
}

const toggleButton = container.querySelector( '.wporg-table-of-contents__toggle' );
const list = container.querySelector( '.wporg-table-of-contents__list' );

if ( toggleButton && list ) {
toggleButton.addEventListener( 'click', function () {
if ( toggleButton.getAttribute( 'aria-expanded' ) === 'true' ) {
toggleButton.setAttribute( 'aria-expanded', false );
list.removeAttribute( 'style' );
} else {
toggleButton.setAttribute( 'aria-expanded', true );
list.setAttribute( 'style', 'display:block;' );
}
} );
}
}

window.addEventListener( 'load', init );

0 comments on commit b9309f7

Please sign in to comment.