-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
105 lines (93 loc) · 2.58 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect, useRef } from '@wordpress/element';
import {
Button,
Icon,
__unstableMotion as motion,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { wordpress } from '@wordpress/icons';
import { store as coreDataStore } from '@wordpress/core-data';
import { useReducedMotion } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../../store';
function NavigationToggle( { icon } ) {
const { isNavigationOpen, isRequestingSiteIcon, siteIconUrl } = useSelect(
( select ) => {
const { getEntityRecord, isResolving } = select( coreDataStore );
const siteData =
getEntityRecord( 'root', '__unstableBase', undefined ) || {};
return {
isNavigationOpen: select( editSiteStore ).isNavigationOpened(),
isRequestingSiteIcon: isResolving( 'core', 'getEntityRecord', [
'root',
'__unstableBase',
undefined,
] ),
siteIconUrl: siteData.site_icon_url,
};
},
[]
);
const { setIsNavigationPanelOpened } = useDispatch( editSiteStore );
const disableMotion = useReducedMotion();
const navigationToggleRef = useRef();
useEffect( () => {
// TODO: Remove this effect when alternative solution is merged.
// See: https://github.com/WordPress/gutenberg/pull/37314
if ( ! isNavigationOpen ) {
navigationToggleRef.current.focus();
}
}, [ isNavigationOpen ] );
const toggleNavigationPanel = () =>
setIsNavigationPanelOpened( ! isNavigationOpen );
let buttonIcon = <Icon size="36px" icon={ wordpress } />;
const effect = {
expand: {
scale: 1.7,
borderRadius: 0,
transition: { type: 'tween', duration: '0.2' },
},
};
if ( siteIconUrl ) {
buttonIcon = (
<motion.img
variants={ ! disableMotion && effect }
alt={ __( 'Site Icon' ) }
className="edit-site-navigation-toggle__site-icon"
src={ siteIconUrl }
/>
);
} else if ( isRequestingSiteIcon ) {
buttonIcon = null;
} else if ( icon ) {
buttonIcon = <Icon size="36px" icon={ icon } />;
}
return (
<motion.div
className={
'edit-site-navigation-toggle' +
( isNavigationOpen ? ' is-open' : '' )
}
whileHover="expand"
>
<Button
className="edit-site-navigation-toggle__button has-icon"
label={ __( 'Toggle navigation' ) }
ref={ navigationToggleRef }
// isPressed will add unwanted styles.
aria-pressed={ isNavigationOpen }
onClick={ toggleNavigationPanel }
showTooltip
>
{ buttonIcon }
</Button>
</motion.div>
);
}
export default NavigationToggle;