mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
🔀 Merge branch 'temp/settings-ui'
# Conflicts: # modules/ppcp-settings/resources/js/data/onboarding/hooks.js # modules/ppcp-settings/resources/js/data/onboarding/reducer.js
This commit is contained in:
commit
5c50a3d970
53 changed files with 2094 additions and 1671 deletions
|
@ -0,0 +1,39 @@
|
|||
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 };
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue