-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
201 lines (190 loc) · 5.03 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* WordPress dependencies
*/
import { useDispatch } from '@wordpress/data';
import { cloneBlock } from '@wordpress/blocks';
import { Composite, VisuallyHidden } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { useInstanceId } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import BlockPreview from '../block-preview';
import SetupToolbar from './setup-toolbar';
import usePatternsSetup from './use-patterns-setup';
import { VIEWMODES } from './constants';
const SetupContent = ( {
viewMode,
activeSlide,
patterns,
onBlockPatternSelect,
showTitles,
} ) => {
const containerClass = 'block-editor-block-pattern-setup__container';
if ( viewMode === VIEWMODES.carousel ) {
const slideClass = new Map( [
[ activeSlide, 'active-slide' ],
[ activeSlide - 1, 'previous-slide' ],
[ activeSlide + 1, 'next-slide' ],
] );
return (
<div className="block-editor-block-pattern-setup__carousel">
<div className={ containerClass }>
<div className="carousel-container">
{ patterns.map( ( pattern, index ) => (
<BlockPatternSlide
active={ index === activeSlide }
className={ slideClass.get( index ) || '' }
key={ pattern.name }
pattern={ pattern }
/>
) ) }
</div>
</div>
</div>
);
}
return (
<div className="block-editor-block-pattern-setup__grid">
<Composite
role="listbox"
className={ containerClass }
aria-label={ __( 'Patterns list' ) }
>
{ patterns.map( ( pattern ) => (
<BlockPattern
key={ pattern.name }
pattern={ pattern }
onSelect={ onBlockPatternSelect }
showTitles={ showTitles }
/>
) ) }
</Composite>
</div>
);
};
function BlockPattern( { pattern, onSelect, showTitles } ) {
const baseClassName = 'block-editor-block-pattern-setup-list';
const { blocks, description, viewportWidth = 700 } = pattern;
const descriptionId = useInstanceId(
BlockPattern,
`${ baseClassName }__item-description`
);
return (
<div className={ `${ baseClassName }__list-item` }>
<Composite.Item
render={
<div
aria-describedby={
description ? descriptionId : undefined
}
aria-label={ pattern.title }
className={ `${ baseClassName }__item` }
/>
}
id={ `${ baseClassName }__pattern__${ pattern.name }` }
role="option"
onClick={ () => onSelect( blocks ) }
>
<BlockPreview
blocks={ blocks }
viewportWidth={ viewportWidth }
/>
{ showTitles && (
<div className={ `${ baseClassName }__item-title` }>
{ pattern.title }
</div>
) }
{ !! description && (
<VisuallyHidden id={ descriptionId }>
{ description }
</VisuallyHidden>
) }
</Composite.Item>
</div>
);
}
function BlockPatternSlide( { active, className, pattern, minHeight } ) {
const { blocks, title, description } = pattern;
const descriptionId = useInstanceId(
BlockPatternSlide,
'block-editor-block-pattern-setup-list__item-description'
);
return (
<div
aria-hidden={ ! active }
role="img"
className={ `pattern-slide ${ className }` }
aria-label={ title }
aria-describedby={ description ? descriptionId : undefined }
>
<BlockPreview blocks={ blocks } minHeight={ minHeight } />
{ !! description && (
<VisuallyHidden id={ descriptionId }>
{ description }
</VisuallyHidden>
) }
</div>
);
}
const BlockPatternSetup = ( {
clientId,
blockName,
filterPatternsFn,
onBlockPatternSelect,
initialViewMode = VIEWMODES.carousel,
showTitles = false,
} ) => {
const [ viewMode, setViewMode ] = useState( initialViewMode );
const [ activeSlide, setActiveSlide ] = useState( 0 );
const { replaceBlock } = useDispatch( blockEditorStore );
const patterns = usePatternsSetup( clientId, blockName, filterPatternsFn );
if ( ! patterns?.length ) {
return null;
}
const onBlockPatternSelectDefault = ( blocks ) => {
const clonedBlocks = blocks.map( ( block ) => cloneBlock( block ) );
replaceBlock( clientId, clonedBlocks );
};
const onPatternSelectCallback =
onBlockPatternSelect || onBlockPatternSelectDefault;
return (
<>
<div
className={ `block-editor-block-pattern-setup view-mode-${ viewMode }` }
>
<SetupContent
viewMode={ viewMode }
activeSlide={ activeSlide }
patterns={ patterns }
onBlockPatternSelect={ onPatternSelectCallback }
showTitles={ showTitles }
/>
<SetupToolbar
viewMode={ viewMode }
setViewMode={ setViewMode }
activeSlide={ activeSlide }
totalSlides={ patterns.length }
handleNext={ () => {
setActiveSlide( ( active ) =>
Math.min( active + 1, patterns.length - 1 )
);
} }
handlePrevious={ () => {
setActiveSlide( ( active ) =>
Math.max( active - 1, 0 )
);
} }
onBlockPatternSelect={ () => {
onPatternSelectCallback(
patterns[ activeSlide ].blocks
);
} }
/>
</div>
</>
);
};
export default BlockPatternSetup;