-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathindex.js
More file actions
188 lines (164 loc) · 4.77 KB
/
index.js
File metadata and controls
188 lines (164 loc) · 4.77 KB
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
/**
* Internal dependencies
*/
import DesignLibraryListItem from './design-library-list-item'
/**
* External dependencies
*/
import { i18n, isPro } from 'stackable'
import classnames from 'classnames'
/**
* WordPress dependencies
*/
import { Spinner } from '@wordpress/components'
import { __ } from '@wordpress/i18n'
import {
useState, useEffect, useRef, memo, useMemo,
} from '@wordpress/element'
import { usePresetControls } from '~stackable/hooks'
import { useDesignLibraryContext } from '../modal-design-library/modal'
import ProControl from '../pro-control'
const DesignLibraryList = memo( props => {
const {
className = '',
designs,
isBusy,
selectedTab,
} = props
const containerRef = useRef( null )
const listClasses = classnames( [
'ugb-design-library-items',
className,
] )
useEffect( () => {
containerRef.current.scrollTop = 0
}, [ designs ] )
return <div
className="ugb-modal-design-library__designs"
ref={ containerRef }
>
{ selectedTab === 'saved' && ! isPro
? <ProControl type="design-library-saved-patterns" />
: <>
{ isBusy && <Spinner style={ { display: 'block', margin: '0 auto' } } /> }
{ ! isBusy && <div className={ listClasses }>
{ ( designs || [] ).map( ( design, i ) => {
return (
<DesignLibraryItem
design={ design }
key={ design.id || design.designId }
designIndex={ i }
/>
)
} ) }
{ ! ( designs || [] ).length &&
<p className="components-base-control__help" data-testid="nothing-found-note">{ __( 'No designs found', i18n ) }</p>
}
</div> }
</>
}
</div>
} )
DesignLibraryList.defaultProps = {
designs: [],
columns: 3,
onSelect: () => {},
isBusy: false,
}
export default DesignLibraryList
const DesignLibraryItem = memo( props => {
const { design, designIndex } = props
const wrapperRef = useRef( null )
const [ shouldRender, setShouldRender ] = useState( designIndex < 9 )
const [ selectedTab,
selectedDesignIds,
selectedDesignData,
onSelectDesign,
isMultiSelectBusy,
containerScheme,
backgroundScheme,
enableBackground,
] = useDesignLibraryContext()
const { selectedNum, selectedData } = useMemo( () => {
const selectedNum = selectedDesignIds.indexOf( design.id || design.designId ) + 1
const selectedData = selectedNum ? selectedDesignData[ selectedNum - 1 ] : null
return { selectedNum, selectedData }
}, [ selectedDesignIds, selectedDesignData ] )
const previewProps = useMemo( () => ( {
designId: design.id || design.designId,
template: design.template || design.content,
category: design.category,
plan: design.plan,
label: design.label,
containerScheme,
backgroundScheme,
enableBackground: selectedTab !== 'pages' ? enableBackground : true,
selectedTab,
selectedNum,
selectedData,
onClick: onSelectDesign,
} ), [
// Only track designId for memoization; other design properties will update when designId changes
design.id || design.designId,
containerScheme,
backgroundScheme,
enableBackground,
selectedTab,
// selectedNum and selectedData are always in sync; updating selectedNum also updates selectedData
selectedNum,
onSelectDesign,
] )
const { getPresetMarks } = usePresetControls( 'spacingSizes' )
// Intentionally no dependencies: presetMarks won't change while the design library is open
const presetMarks = useMemo( () => getPresetMarks() || null, [] )
useEffect( () => {
if ( selectedTab !== 'pages' ) {
return
}
let id
if ( typeof requestIdleCallback !== 'undefined' ) {
id = requestIdleCallback( () => ! shouldRender ? setShouldRender( true ) : {}, { timeout: ( designIndex + 1 ) * 500 } )
} else {
// fallback, always render immediately the first design
id = setTimeout( () => setShouldRender( true ), designIndex * 500 )
}
return () => {
if ( typeof cancelIdleCallback !== 'undefined' ) {
cancelIdleCallback( id )
} else {
clearTimeout( id )
}
}
}, [ selectedTab ] )
useEffect( () => {
if ( selectedTab === 'pages' ) {
return
}
const rootEl = document.querySelector( '.ugb-modal-design-library__designs' )
if ( ! wrapperRef.current || ! rootEl ) {
return
}
const observer = new IntersectionObserver( ( [ entry ] ) => {
// reduce flicker during rapid scrolls
requestAnimationFrame( () => {
requestAnimationFrame( () => setShouldRender( entry.isIntersecting || entry.intersectionRatio > 0 ) )
} )
}, {
root: rootEl,
rootMargin: '500px',
scrollMargin: '500px',
threshold: 0,
} )
observer.observe( wrapperRef.current )
return () => observer.disconnect()
}, [ selectedTab ] )
return (
<div ref={ wrapperRef }>
<DesignLibraryListItem
previewProps={ previewProps }
isMultiSelectBusy={ isMultiSelectBusy }
shouldRender={ shouldRender }
presetMarks={ presetMarks } />
</div>
)
} )