-
Notifications
You must be signed in to change notification settings - Fork 293
/
WidgetAreaRenderer.js
254 lines (228 loc) · 6.87 KB
/
WidgetAreaRenderer.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* WidgetAreaRenderer component.
*
* Site Kit by Google, Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useEffect, useRef, useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import Data from 'googlesitekit-data';
import { getWidgetLayout, combineWidgets, HIDDEN_CLASS } from '../util';
import { getStickyHeaderHeight } from '../../../util/scroll';
import { CORE_WIDGETS, WIDGET_AREA_STYLES } from '../datastore/constants';
import { CORE_UI, ACTIVE_CONTEXT_ID } from '../../datastore/ui/constants';
import { Cell, Grid, Row } from '../../../material-components';
import {
useBreakpoint,
BREAKPOINT_XLARGE,
BREAKPOINT_DESKTOP,
BREAKPOINT_TABLET,
BREAKPOINT_SMALL,
} from '../../../hooks/useBreakpoint';
import InViewProvider from '../../../components/InViewProvider';
import WidgetRenderer from './WidgetRenderer';
import WidgetCellWrapper from './WidgetCellWrapper';
import useViewOnly from '../../../hooks/useViewOnly';
import { CORE_USER } from '../../datastore/user/constants';
import useLatestIntersection from '../../../hooks/useLatestIntersection';
const { useSelect } = Data;
/**
* Gets root margin value for the intersection hook.
*
* @since 1.69.0
*
* @param {string} breakpoint The current breakpoint.
* @return {string} The root margin.
*/
function getRootMargin( breakpoint ) {
const gridGaps = {
[ BREAKPOINT_XLARGE ]: 48,
[ BREAKPOINT_DESKTOP ]: 48,
[ BREAKPOINT_TABLET ]: 32,
[ BREAKPOINT_SMALL ]: 32,
};
const gap = gridGaps[ breakpoint ];
const top = Math.abs( getStickyHeaderHeight( breakpoint ) + gap );
return `-${ top }px -${ gap }px -${ gap }px -${ gap }px`;
}
export default function WidgetAreaRenderer( { slug, contextID } ) {
const viewOnlyDashboard = useViewOnly();
const viewableModules = useSelect( ( select ) => {
if ( ! viewOnlyDashboard ) {
return null;
}
return select( CORE_USER ).getViewableModules();
} );
const breakpoint = useBreakpoint();
const widgetAreaRef = useRef();
const intersectionEntry = useLatestIntersection( widgetAreaRef, {
rootMargin: getRootMargin( breakpoint ),
threshold: 0, // Trigger "in-view" as soon as one pixel is visible.
} );
const widgetArea = useSelect( ( select ) =>
select( CORE_WIDGETS ).getWidgetArea( slug )
);
const widgets = useSelect( ( select ) =>
select( CORE_WIDGETS ).getWidgets( slug, {
modules: viewableModules ? viewableModules : undefined,
} )
);
const widgetStates = useSelect( ( select ) =>
select( CORE_WIDGETS ).getWidgetStates()
);
const isActive = useSelect( ( select ) =>
select( CORE_WIDGETS ).isWidgetAreaActive( slug, {
modules: viewableModules ? viewableModules : undefined,
} )
);
const activeContextID = useSelect( ( select ) =>
select( CORE_UI ).getValue( ACTIVE_CONTEXT_ID )
);
const [ inViewState, setInViewState ] = useState( {
key: `WidgetAreaRenderer-${ slug }`,
value: activeContextID
? activeContextID === contextID
: !! intersectionEntry?.intersectionRatio,
} );
useEffect( () => {
setInViewState( {
key: `WidgetAreaRenderer-${ slug }`,
value: activeContextID
? activeContextID === contextID
: !! intersectionEntry?.intersectionRatio,
} );
}, [ intersectionEntry, slug, activeContextID, contextID ] );
if ( viewableModules === undefined ) {
return null;
}
// Compute the layout.
const { columnWidths, rowIndexes } = getWidgetLayout(
widgets,
widgetStates
);
// Combine widgets with similar CTAs and prepare final props to pass to
// `WidgetRenderer` below. Only one consecutive instance of a similar CTA
// will be maintained (via an "override component"), and all other similar
// ones will receive a CSS class to hide them.
// A combined CTA will span the combined width of all widgets that it was
// combined from.
const { gridColumnWidths, overrideComponents } = combineWidgets(
widgets,
widgetStates,
{
columnWidths,
rowIndexes,
}
);
// Render all widgets.
const widgetsOutput = widgets.map( ( widget, i ) => (
<WidgetCellWrapper
key={ `${ widget.slug }-wrapper` }
gridColumnWidth={ gridColumnWidths[ i ] }
>
<WidgetRenderer
OverrideComponent={
overrideComponents[ i ]
? () => {
const { Component, metadata } =
overrideComponents[ i ];
return <Component { ...metadata } />;
}
: undefined
}
slug={ widget.slug }
/>
</WidgetCellWrapper>
) );
const { Icon, title, style, subtitle } = widgetArea;
// Here we render the bare output as it is guaranteed to render empty.
// This is important compared to returning `null` so that the area
// can maybe render later if conditions change for widgets to become active.
// Returning `null` here however would have the side-effect of making
// all widgets active again, which is why we must return the "null" output.
if ( ! isActive ) {
return (
<Grid
className={ classnames(
HIDDEN_CLASS,
'googlesitekit-widget-area',
{
[ `googlesitekit-widget-area--${ slug }` ]: !! slug,
[ `googlesitekit-widget-area--${ style }` ]: !! style,
}
) }
ref={ widgetAreaRef }
>
{ widgetsOutput }
</Grid>
);
}
return (
<InViewProvider value={ inViewState }>
<Grid
className={ classnames(
'googlesitekit-widget-area',
`googlesitekit-widget-area--${ slug }`,
`googlesitekit-widget-area--${ style }`
) }
ref={ widgetAreaRef }
>
<Row>
<Cell
className="googlesitekit-widget-area-header"
size={ 12 }
>
{ Icon && <Icon width={ 33 } height={ 33 } /> }
{ title && (
<h3 className="googlesitekit-widget-area-header__title googlesitekit-heading-3">
{ title }
</h3>
) }
{ subtitle && (
<h4 className="googlesitekit-widget-area-header__subtitle">
{ subtitle }
</h4>
) }
</Cell>
</Row>
<div className="googlesitekit-widget-area-widgets">
<Row>
{ style === WIDGET_AREA_STYLES.BOXES && widgetsOutput }
{ style === WIDGET_AREA_STYLES.COMPOSITE && (
<Cell size={ 12 }>
<Grid>
<Row>{ widgetsOutput }</Row>
</Grid>
</Cell>
) }
</Row>
</div>
</Grid>
</InViewProvider>
);
}
WidgetAreaRenderer.propTypes = {
slug: PropTypes.string.isRequired,
contextID: PropTypes.string,
};