File size: 3,595 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import { Tooltip } from '@wordpress/components';
import clsx from 'clsx';
import {
Children,
cloneElement,
createRef,
forwardRef,
useEffect,
useLayoutEffect,
useRef,
} from 'react';
import { LocalizedLink } from 'calypso/my-sites/patterns/components/localized-link';
import './style.scss';
// We do this to silence a noisy React warning about `useLayoutEffect` not playing well with SSR.
// See https://reactjs.org/link/uselayouteffect-ssr
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
type PatternLibraryToggleOptionProps = Omit< JSX.IntrinsicElements[ 'a' ], 'onClick' > & {
onClick?( value: string ): void;
tooltipText: string;
value: string;
};
export const PatternLibraryToggleOption = forwardRef<
HTMLAnchorElement,
PatternLibraryToggleOptionProps
>( ( { className, children, href, onClick, tooltipText, value, ...props }, ref ) => {
return (
<Tooltip text={ tooltipText } { ...{ style: { maxWidth: '200px', top: '3px' } } }>
<LocalizedLink
{ ...props }
className={ clsx( 'pattern-library__toggle-option', className ) }
href={ href }
onClick={ () => {
onClick?.( value );
} }
ref={ ref }
>
{ children }
</LocalizedLink>
</Tooltip>
);
} );
type PatternLibraryToggleProps = {
className?: string;
children: React.ReactElement< PatternLibraryToggleOptionProps >[];
onChange: PatternLibraryToggleOptionProps[ 'onClick' ];
selected: string;
};
export function PatternLibraryToggle( {
className,
children,
onChange,
selected,
}: PatternLibraryToggleProps ) {
const wrapperRef = useRef< HTMLDivElement >( null );
const prevSelectionRef = useRef< string >( selected );
const options = Children.map( children, ( child ) => child.props.value );
const optionRefs = Children.map( children, () => createRef< HTMLAnchorElement >() );
// Animate the backdrop element to move from the previously selected option to the new one using
// the FLIP principle
useIsomorphicLayoutEffect( () => {
if ( selected === prevSelectionRef.current ) {
return;
}
const activeOptionIndex = options.indexOf( selected );
const activeOptionRef = optionRefs[ activeOptionIndex ];
const prevOptionIndex = options.indexOf( prevSelectionRef.current );
const prevOptionRef = optionRefs[ prevOptionIndex ];
prevSelectionRef.current = selected;
if ( activeOptionRef?.current && prevOptionRef?.current ) {
const backdrop = wrapperRef.current?.querySelector< HTMLDivElement >(
'.pattern-library__toggle-backdrop'
);
const activeCoords = activeOptionRef.current.getBoundingClientRect();
const lastSelectionCoords = prevOptionRef.current.getBoundingClientRect();
const offset = lastSelectionCoords.left - activeCoords.left;
backdrop?.animate( [ { transform: `translateX(${ offset }px)` }, { transform: 'none' } ], {
easing: 'ease',
duration: 200,
} );
}
}, [ selected ] );
return (
<div className={ clsx( 'pattern-library__toggle', className ) } ref={ wrapperRef }>
{ Children.map( children, ( child, index ) =>
cloneElement< PatternLibraryToggleOptionProps >( child, {
children: (
<>
{ child.props.value === selected && (
<div className="pattern-library__toggle-backdrop" />
) }
{ child.props.children }
</>
),
className: clsx( child.props.className, {
'is-active': child.props.value === selected,
} ),
onClick( value ) {
if ( value !== selected ) {
onChange?.( value );
}
},
ref: optionRefs[ index ],
} )
) }
</div>
);
}
|