mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
✨ Accordion can be toggled via URL hash
This commit is contained in:
parent
1b96b112c2
commit
10a3767e2f
2 changed files with 27 additions and 4 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import { useEffect } from '@wordpress/element';
|
||||||
import { Icon } from '@wordpress/components';
|
import { Icon } from '@wordpress/components';
|
||||||
import { chevronDown, chevronUp } from '@wordpress/icons';
|
import { chevronDown, chevronUp } from '@wordpress/icons';
|
||||||
|
|
||||||
|
@ -5,11 +6,33 @@ import { useState } from 'react';
|
||||||
|
|
||||||
const Accordion = ( {
|
const Accordion = ( {
|
||||||
title,
|
title,
|
||||||
initiallyOpen = false,
|
initiallyOpen = null,
|
||||||
className = '',
|
className = '',
|
||||||
|
id = '',
|
||||||
children,
|
children,
|
||||||
} ) => {
|
} ) => {
|
||||||
const [ isOpen, setIsOpen ] = useState( initiallyOpen );
|
const determineInitialState = () => {
|
||||||
|
if ( id && initiallyOpen === null ) {
|
||||||
|
return window.location.hash === `#${ id }`;
|
||||||
|
}
|
||||||
|
return !! initiallyOpen;
|
||||||
|
};
|
||||||
|
|
||||||
|
const [ isOpen, setIsOpen ] = useState( determineInitialState );
|
||||||
|
|
||||||
|
useEffect( () => {
|
||||||
|
const handleHashChange = () => {
|
||||||
|
if ( id && window.location.hash === `#${ id }` ) {
|
||||||
|
setIsOpen( true );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener( 'hashchange', handleHashChange );
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener( 'hashchange', handleHashChange );
|
||||||
|
};
|
||||||
|
}, [ id ] );
|
||||||
|
|
||||||
const toggleOpen = ( ev ) => {
|
const toggleOpen = ( ev ) => {
|
||||||
setIsOpen( ! isOpen );
|
setIsOpen( ! isOpen );
|
||||||
|
@ -26,7 +49,7 @@ const Accordion = ( {
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={ wrapperClasses.join( ' ' ) }>
|
<div className={ wrapperClasses.join( ' ' ) } id={ id }>
|
||||||
<button
|
<button
|
||||||
onClick={ toggleOpen }
|
onClick={ toggleOpen }
|
||||||
className="ppcp-r-accordion--title"
|
className="ppcp-r-accordion--title"
|
||||||
|
|
|
@ -57,7 +57,7 @@ const StepWelcome = ( { setStep, currentStep, setCompleted } ) => {
|
||||||
'woocommerce-paypal-payments'
|
'woocommerce-paypal-payments'
|
||||||
) }
|
) }
|
||||||
className="onboarding-advanced-options"
|
className="onboarding-advanced-options"
|
||||||
initiallyOpen={ false }
|
id="advanced-options"
|
||||||
>
|
>
|
||||||
<AdvancedOptionsForm setCompleted={ setCompleted } />
|
<AdvancedOptionsForm setCompleted={ setCompleted } />
|
||||||
</AccordionSection>
|
</AccordionSection>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue