-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
181 lines (170 loc) · 4.69 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import {
Button,
__unstableMotion as motion,
__unstableAnimatePresence as AnimatePresence,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { useReducedMotion } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
import { forwardRef } from '@wordpress/element';
import { search, external } from '@wordpress/icons';
import { store as commandsStore } from '@wordpress/commands';
/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import SiteIcon from '../site-icon';
import { unlock } from '../../private-apis';
const HUB_ANIMATION_DURATION = 0.3;
const SiteHub = forwardRef( ( props, ref ) => {
const { canvasMode, dashboardLink, homeUrl } = useSelect( ( select ) => {
const { getCanvasMode, getSettings } = unlock(
select( editSiteStore )
);
const {
getUnstableBase, // Site index.
} = select( coreStore );
return {
canvasMode: getCanvasMode(),
dashboardLink:
getSettings().__experimentalDashboardLink || 'index.php',
homeUrl: getUnstableBase()?.home,
};
}, [] );
const { open: openCommandCenter } = useDispatch( commandsStore );
const disableMotion = useReducedMotion();
const { setCanvasMode } = unlock( useDispatch( editSiteStore ) );
const { clearSelectedBlock } = useDispatch( blockEditorStore );
const isBackToDashboardButton = canvasMode === 'view';
const siteIconButtonProps = isBackToDashboardButton
? {
href: dashboardLink,
label: __( 'Go back to the Dashboard' ),
}
: {
href: dashboardLink, // We need to keep the `href` here so the component doesn't remount as a `<button>` and break the animation.
role: 'button',
label: __( 'Open Navigation' ),
onClick: ( event ) => {
event.preventDefault();
if ( canvasMode === 'edit' ) {
clearSelectedBlock();
setCanvasMode( 'view' );
}
},
};
const siteTitle = useSelect(
( select ) =>
select( coreStore ).getEntityRecord( 'root', 'site' )?.title,
[]
);
return (
<motion.div
ref={ ref }
{ ...props }
className={ classnames( 'edit-site-site-hub', props.className ) }
initial={ false }
transition={ {
type: 'tween',
duration: disableMotion ? 0 : HUB_ANIMATION_DURATION,
ease: 'easeOut',
} }
>
<HStack
justify="space-between"
alignment="center"
className="edit-site-site-hub__container"
>
<HStack
justify="flex-start"
className="edit-site-site-hub__text-content"
spacing="0"
>
<motion.div
className="edit-site-site-hub__view-mode-toggle-container"
layout
transition={ {
type: 'tween',
duration: disableMotion
? 0
: HUB_ANIMATION_DURATION,
ease: 'easeOut',
} }
>
<Button
{ ...siteIconButtonProps }
className="edit-site-layout__view-mode-toggle"
>
<motion.div
initial={ false }
animate={ {
scale: canvasMode === 'view' ? 0.5 : 1,
} }
whileHover={ {
scale: canvasMode === 'view' ? 0.5 : 0.96,
} }
transition={ {
type: 'tween',
duration: disableMotion
? 0
: HUB_ANIMATION_DURATION,
ease: 'easeOut',
} }
>
<SiteIcon className="edit-site-layout__view-mode-toggle-icon" />
</motion.div>
</Button>
</motion.div>
<AnimatePresence>
<motion.div
layout={ canvasMode === 'edit' }
animate={ {
opacity: canvasMode === 'view' ? 1 : 0,
} }
exit={ {
opacity: 0,
} }
className="edit-site-site-hub__site-title"
transition={ {
type: 'tween',
duration: disableMotion ? 0 : 0.2,
ease: 'easeOut',
delay: canvasMode === 'view' ? 0.1 : 0,
} }
>
{ decodeEntities( siteTitle ) }
</motion.div>
</AnimatePresence>
<Button
href={ homeUrl }
target="_blank"
label={ __( 'View site' ) }
aria-label={ __( 'View site (opens in a new tab)' ) }
icon={ external }
className="edit-site-site-hub__site-view-link"
/>
</HStack>
{ canvasMode === 'view' && (
<Button
className="edit-site-site-hub_toggle-command-center"
icon={ search }
onClick={ () => openCommandCenter() }
label={ __( 'Open command center' ) }
/>
) }
</HStack>
</motion.div>
);
} );
export default SiteHub;