File size: 1,910 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 |
import { SelectDropdown } from '@automattic/components';
import { Button } from '@wordpress/components';
import { useViewportMatch } from '@wordpress/compose';
import clsx from 'clsx';
import { Children, isValidElement } from 'react';
import { navigate } from 'calypso/lib/navigate';
import type { ReactNode, ReactElement } from 'react';
import './style.scss';
export function SidebarItem( {
enabled = true,
href,
children,
}: {
enabled?: boolean;
href: string;
children: ReactNode;
} ) {
const isActive = window.location.pathname.startsWith( href );
if ( ! enabled && ! isActive ) {
return null;
}
return (
<li>
<Button
href={ href }
className={ clsx( 'panel-sidebar-tab', {
'panel-sidebar-tab--active': isActive,
} ) }
>
{ children }
</Button>
</li>
);
}
export function Sidebar( { children }: { children: ReactNode } ) {
const isDesktop = useViewportMatch( 'small', '>=' );
const activeElement = Children.toArray( children ).find(
( child ) => isValidElement( child ) && window.location.pathname.startsWith( child.props.href )
) as ReactElement;
if ( isDesktop ) {
return <ul className="panel-sidebar">{ children }</ul>;
}
return (
<SelectDropdown
className="panel-sidebar panel-sidebar-dropdown"
selectedText={ activeElement?.props?.children }
>
{ Children.toArray( children )
.filter( ( child ) => child && isValidElement( child ) )
.map( ( child, index ) => {
const { href, ...childProps } = ( child as ReactElement ).props;
return (
<SelectDropdown.Item
{ ...childProps }
key={ index }
selected={ window.location.pathname.startsWith( href ) }
onClick={ () => navigate( href ) }
/>
);
} ) }
</SelectDropdown>
);
}
export function PanelWithSidebar( { children }: { children: ReactNode } ) {
return <div className="panel-with-sidebar">{ children }</div>;
}
|