File size: 2,977 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
import { ToolbarGroup, ToolbarButton as BaseToolbarButton } from '@wordpress/components';
import clsx from 'clsx';
import { ReactNode, useState, useRef, useEffect, useMemo } from 'react';
import type { Button } from '@wordpress/components';
import './style.scss';
const ToolbarButton = BaseToolbarButton as React.ComponentType<
| ( React.ComponentProps< typeof BaseToolbarButton > & { href?: string } )
| React.ComponentProps< typeof Button >
>;
export default function SwipeGroup( {
children,
className = '',
onClick = () => null,
initialActiveIndex = -1,
initialActiveIndexes,
isMultiSelection,
hrefList = [],
}: {
children: ReactNode[];
className?: string;
onClick?: ( index: number ) => void;
initialActiveIndex?: number;
initialActiveIndexes?: number[];
isMultiSelection?: boolean;
hrefList?: string[];
} ) {
const classes = clsx( 'responsive-toolbar-group__swipe', className );
const defaultActiveIndexes = useMemo( () => {
if ( isMultiSelection ) {
return initialActiveIndexes || [];
}
return initialActiveIndex !== -1 ? [ initialActiveIndex ] : [];
}, [ isMultiSelection, initialActiveIndex, initialActiveIndexes ] );
const [ activeIndexes, setActiveIndexes ] = useState< Set< number > >(
new Set( defaultActiveIndexes )
);
// Set active on prop change from above
useEffect( () => {
setActiveIndexes( new Set( defaultActiveIndexes ) );
}, [ defaultActiveIndexes ] );
const ref = useRef< HTMLAnchorElement >( null );
// Scroll to category on load
useEffect( () => {
if ( ref.current ) {
ref.current.scrollIntoView( { block: 'end', inline: 'center' } );
}
}, [] );
// Scroll to the beginning when activeIndexes changes to 0. This indicates a state reset.
useEffect( () => {
if ( ref.current ) {
ref.current.scrollIntoView( { block: 'end', inline: 'center' } );
}
}, [ activeIndexes ] );
return (
<div className={ classes }>
<ToolbarGroup className="responsive-toolbar-group__swipe-list">
{ children.map( ( child, index ) => (
<ToolbarButton
key={ `button-item-${ index }` }
id={ `button-item-${ index }` }
isActive={ activeIndexes.has( index ) }
href={ hrefList[ index ] }
ref={ activeIndexes.has( index ) ? ref : null }
onClick={ ( event: React.MouseEvent ) => {
setActiveIndexes( ( currentActiveIndexes: Set< number > ) => {
if ( ! isMultiSelection ) {
return new Set( [ index ] );
}
if ( ! currentActiveIndexes.has( index ) ) {
currentActiveIndexes.add( index );
} else if ( currentActiveIndexes.size > 1 ) {
currentActiveIndexes.delete( index );
}
return currentActiveIndexes;
} );
onClick( index );
if ( typeof hrefList[ index ] === 'string' ) {
event.preventDefault();
}
} }
className="responsive-toolbar-group__swipe-item"
>
{ child }
</ToolbarButton>
) ) }
</ToolbarGroup>
</div>
);
}
|