Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto scroll for tabs and accordion on opening #2288

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/blocks/frontend/accordion/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
/**
* Internal dependencies
*/
import { domReady } from '../../helpers/frontend-helper-functions.js';
import { domReady, scrollIntoViewIfNeeded } from '../../helpers/frontend-helper-functions.js';

domReady( () => {
const accordions = document.querySelectorAll( '.wp-block-themeisle-blocks-accordion' );

/**
* Handle the opening of the accordion items.
*
* @param {HTMLDetailsElement} accordion The accordion root element.
* @returns
*/
const handleItemOpening = accordion => {
if ( ! accordion.classList.contains( 'exclusive' ) ) {
return;
Expand All @@ -25,6 +31,9 @@ domReady( () => {
openSibling.forEach( sibling => {
sibling.removeAttribute( 'open' );
});

const title = item.querySelector( ':scope > .wp-block-themeisle-blocks-accordion-item__title' );
scrollIntoViewIfNeeded( title );
});
});
};
Expand Down
6 changes: 5 additions & 1 deletion src/blocks/frontend/tabs/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import { domReady } from '../../helpers/frontend-helper-functions.js';
import { domReady, scrollIntoViewIfNeeded } from '../../helpers/frontend-helper-functions.js';

domReady( () => {
const tabs = document.querySelectorAll( '.wp-block-themeisle-blocks-tabs' );
Expand Down Expand Up @@ -57,6 +57,10 @@ domReady( () => {
h.classList.toggle( 'hidden', idx !== index );
h.setAttribute( 'aria-selected', idx === index );
});

if ( idx === index && headerMobile ) {
scrollIntoViewIfNeeded( headerMobile );
}
};

headerItem.addEventListener( 'click', () => items.forEach( toggleTabs ) );
Expand Down
24 changes: 24 additions & 0 deletions src/blocks/helpers/frontend-helper-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,27 @@ export const rgb2hsl = ( color, asArray = true ) => {

return `hsl( ${h * 360}, ${s * 100}, ${l * 100} )`;
};

/**
* Scrolls the element into view if it's not fully visible.
*
* @param {HTMLElement} element The element to scroll into view.
* @param {*} options The options for the scrollIntoView method.
*/
export const scrollIntoViewIfNeeded = ( element, options = {}) => {
if ( ! element ) {
return;
}

const rect = element.getBoundingClientRect();
const isFullyVisible = (
0 <= rect.top &&
0 <= rect.left &&
rect.bottom <= ( window.innerHeight || document.documentElement.clientHeight ) &&
rect.right <= ( window.innerWidth || document.documentElement.clientWidth )
);

if ( ! isFullyVisible ) {
element.scrollIntoView( options );
}
};
Loading