-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
83 lines (78 loc) · 1.8 KB
/
index.js
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
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import {
__experimentalItem as Item,
__experimentalHStack as HStack,
FlexBlock,
} from '@wordpress/components';
import { isRTL } from '@wordpress/i18n';
import { chevronRightSmall, chevronLeftSmall, Icon } from '@wordpress/icons';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useContext } from '@wordpress/element';
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { SidebarNavigationContext } from '../sidebar';
const { useHistory } = unlock( routerPrivateApis );
export default function SidebarNavigationItem( {
className,
icon,
withChevron = false,
suffix,
uid,
params,
onClick,
children,
...props
} ) {
const history = useHistory();
const { navigate } = useContext( SidebarNavigationContext );
// If there is no custom click handler, create one that navigates to `params`.
function handleClick( e ) {
if ( onClick ) {
onClick( e );
navigate( 'forward' );
} else if ( params ) {
e.preventDefault();
history.push( params );
navigate( 'forward', `[id="${ uid }"]` );
}
}
return (
<Item
className={ clsx(
'edit-site-sidebar-navigation-item',
{ 'with-suffix': ! withChevron && suffix },
className
) }
onClick={ handleClick }
id={ uid }
{ ...props }
>
<HStack justify="flex-start">
{ icon && (
<Icon
style={ { fill: 'currentcolor' } }
icon={ icon }
size={ 24 }
/>
) }
<FlexBlock>{ children }</FlexBlock>
{ withChevron && (
<Icon
icon={ isRTL() ? chevronLeftSmall : chevronRightSmall }
className="edit-site-sidebar-navigation-item__drilldown-indicator"
size={ 24 }
/>
) }
{ ! withChevron && suffix }
</HStack>
</Item>
);
}