-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
128 lines (118 loc) · 3.53 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
/**
* WordPress dependencies
*/
import { Children, cloneElement, useState, useMemo } from '@wordpress/element';
import {
Button,
privateApis as componentsPrivateApis,
__experimentalUseSlotFills as useSlotFills,
} from '@wordpress/components';
import { ESCAPE } from '@wordpress/keycodes';
import { __ } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import { closeSmall } from '@wordpress/icons';
import { useFocusOnMount, useFocusReturn } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { unlock } from '../../private-apis';
import { store as editSiteStore } from '../../store';
/**
* Returns a translated string for the title of the editor canvas container.
*
* @param {string} view Editor canvas container view.
*
* @return {string} Translated string corresponding to value of view. Default is ''.
*/
function getEditorCanvasContainerTitle( view ) {
switch ( view ) {
case 'style-book':
return __( 'Style Book' );
case 'global-styles-revisions':
return __( 'Global styles revisions' );
default:
return '';
}
}
// Creates a private slot fill.
const { createPrivateSlotFill } = unlock( componentsPrivateApis );
const SLOT_FILL_NAME = 'EditSiteEditorCanvasContainerSlot';
const {
privateKey,
Slot: EditorCanvasContainerSlot,
Fill: EditorCanvasContainerFill,
} = createPrivateSlotFill( SLOT_FILL_NAME );
function EditorCanvasContainer( { children, closeButtonLabel, onClose } ) {
const editorCanvasContainerView = useSelect(
( select ) =>
unlock( select( editSiteStore ) ).getEditorCanvasContainerView(),
[]
);
const [ isClosed, setIsClosed ] = useState( false );
const { setEditorCanvasContainerView } = unlock(
useDispatch( editSiteStore )
);
const focusOnMountRef = useFocusOnMount( 'firstElement' );
const sectionFocusReturnRef = useFocusReturn();
const title = useMemo(
() => getEditorCanvasContainerTitle( editorCanvasContainerView ),
[ editorCanvasContainerView ]
);
function onCloseContainer() {
if ( typeof onClose === 'function' ) {
onClose();
}
setEditorCanvasContainerView( undefined );
setIsClosed( true );
}
function closeOnEscape( event ) {
if ( event.keyCode === ESCAPE && ! event.defaultPrevented ) {
event.preventDefault();
onCloseContainer();
}
}
const childrenWithProps = Array.isArray( children )
? Children.map( children, ( child, index ) =>
index === 0
? cloneElement( child, {
ref: sectionFocusReturnRef,
} )
: child
)
: cloneElement( children, {
ref: sectionFocusReturnRef,
} );
if ( isClosed ) {
return null;
}
const shouldShowCloseButton = onClose || closeButtonLabel;
return (
<EditorCanvasContainerFill>
{ /* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */ }
<section
className="edit-site-editor-canvas-container"
ref={ shouldShowCloseButton ? focusOnMountRef : null }
onKeyDown={ closeOnEscape }
aria-label={ title }
>
{ shouldShowCloseButton && (
<Button
className="edit-site-editor-canvas-container__close-button"
icon={ closeSmall }
label={ closeButtonLabel || __( 'Close' ) }
onClick={ onCloseContainer }
showTooltip={ false }
/>
) }
{ childrenWithProps }
</section>
</EditorCanvasContainerFill>
);
}
function useHasEditorCanvasContainer() {
const fills = useSlotFills( privateKey );
return !! fills?.length;
}
EditorCanvasContainer.Slot = EditorCanvasContainerSlot;
export default EditorCanvasContainer;
export { useHasEditorCanvasContainer, getEditorCanvasContainerTitle };