mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-31 06:52:50 +08:00
39 lines
894 B
JavaScript
39 lines
894 B
JavaScript
import { useEffect, useState } from '@wordpress/element';
|
|
|
|
const checkIfCurrentTab = ( id ) => {
|
|
return id && window.location.hash === `#${ id }`;
|
|
};
|
|
|
|
const determineInitialState = ( id, initiallyOpen ) => {
|
|
if ( initiallyOpen !== null ) {
|
|
return initiallyOpen;
|
|
}
|
|
return checkIfCurrentTab( id );
|
|
};
|
|
|
|
export function useAccordionState( { id = '', initiallyOpen = null } ) {
|
|
const [ isOpen, setIsOpen ] = useState(
|
|
determineInitialState( id, initiallyOpen )
|
|
);
|
|
|
|
useEffect( () => {
|
|
const handleHashChange = () => {
|
|
if ( checkIfCurrentTab( id ) ) {
|
|
setIsOpen( true );
|
|
}
|
|
};
|
|
|
|
window.addEventListener( 'hashchange', handleHashChange );
|
|
return () => {
|
|
window.removeEventListener( 'hashchange', handleHashChange );
|
|
};
|
|
}, [ id ] );
|
|
|
|
const toggleOpen = ( ev ) => {
|
|
setIsOpen( ! isOpen );
|
|
ev?.preventDefault();
|
|
return false;
|
|
};
|
|
|
|
return { isOpen, toggleOpen };
|
|
}
|