-
Notifications
You must be signed in to change notification settings - Fork 4k
/
padding.js
230 lines (208 loc) · 5.49 KB
/
padding.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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
Platform,
useState,
useRef,
useEffect,
useMemo,
} from '@wordpress/element';
import { getBlockSupport } from '@wordpress/blocks';
import {
__experimentalUseCustomUnits as useCustomUnits,
__experimentalBoxControl as BoxControl,
} from '@wordpress/components';
import isShallowEqual from '@wordpress/is-shallow-equal';
/**
* Internal dependencies
*/
import useSetting from '../components/use-setting';
import {
AXIAL_SIDES,
SPACING_SUPPORT_KEY,
useCustomSides,
useIsDimensionsSupportValid,
} from './dimensions';
import { cleanEmptyObject } from './utils';
import BlockPopover from '../components/block-popover';
import SpacingSizesControl from '../components/spacing-sizes-control';
import {
getSpacingPresetCssVar,
isValueSpacingPreset,
} from '../components/spacing-sizes-control/utils';
/**
* Determines if there is padding support.
*
* @param {string|Object} blockType Block name or Block Type object.
*
* @return {boolean} Whether there is support.
*/
export function hasPaddingSupport( blockType ) {
const support = getBlockSupport( blockType, SPACING_SUPPORT_KEY );
return !! ( true === support || support?.padding );
}
/**
* Checks if there is a current value in the padding block support attributes.
*
* @param {Object} props Block props.
* @return {boolean} Whether or not the block has a padding value set.
*/
export function hasPaddingValue( props ) {
return props.attributes.style?.spacing?.padding !== undefined;
}
/**
* Resets the padding block support attributes. This can be used when disabling
* the padding support controls for a block via a `ToolsPanel`.
*
* @param {Object} props Block props.
* @param {Object} props.attributes Block's attributes.
* @param {Object} props.setAttributes Function to set block's attributes.
*/
export function resetPadding( { attributes = {}, setAttributes } ) {
const { style } = attributes;
setAttributes( {
style: cleanEmptyObject( {
...style,
spacing: {
...style?.spacing,
padding: undefined,
},
} ),
} );
}
/**
* Custom hook that checks if padding settings have been disabled.
*
* @param {string} name The name of the block.
*
* @return {boolean} Whether padding setting is disabled.
*/
export function useIsPaddingDisabled( { name: blockName } = {} ) {
const isDisabled = ! useSetting( 'spacing.padding' );
const isInvalid = ! useIsDimensionsSupportValid( blockName, 'padding' );
return ! hasPaddingSupport( blockName ) || isDisabled || isInvalid;
}
/**
* Inspector control panel containing the padding related configuration
*
* @param {Object} props
*
* @return {WPElement} Padding edit element.
*/
export function PaddingEdit( props ) {
const {
name: blockName,
attributes: { style },
setAttributes,
} = props;
const spacingSizes = useSetting( 'spacing.spacingSizes' );
const units = useCustomUnits( {
availableUnits: useSetting( 'spacing.units' ) || [
'%',
'px',
'em',
'rem',
'vw',
],
} );
const sides = useCustomSides( blockName, 'padding' );
const splitOnAxis =
sides && sides.some( ( side ) => AXIAL_SIDES.includes( side ) );
if ( useIsPaddingDisabled( props ) ) {
return null;
}
const onChange = ( next ) => {
const newStyle = {
...style,
spacing: {
...style?.spacing,
padding: next,
},
};
setAttributes( {
style: cleanEmptyObject( newStyle ),
} );
};
return Platform.select( {
web: (
<>
{ ( ! spacingSizes || spacingSizes?.length === 0 ) && (
<BoxControl
values={ style?.spacing?.padding }
onChange={ onChange }
label={ __( 'Padding' ) }
sides={ sides }
units={ units }
allowReset={ false }
splitOnAxis={ splitOnAxis }
/>
) }
{ spacingSizes?.length > 0 && (
<SpacingSizesControl
values={ style?.spacing?.padding }
onChange={ onChange }
label={ __( 'Padding' ) }
sides={ sides }
units={ units }
allowReset={ false }
splitOnAxis={ splitOnAxis }
/>
) }
</>
),
native: null,
} );
}
export function PaddingVisualizer( { clientId, attributes } ) {
const padding = attributes?.style?.spacing?.padding;
const style = useMemo( () => {
return {
borderTopWidth: isValueSpacingPreset( padding?.top )
? getSpacingPresetCssVar( padding?.top )
: padding?.top,
borderRightWidth: isValueSpacingPreset( padding?.right )
? getSpacingPresetCssVar( padding?.right )
: padding?.right,
borderBottomWidth: isValueSpacingPreset( padding?.bottom )
? getSpacingPresetCssVar( padding?.bottom )
: padding?.bottom,
borderLeftWidth: isValueSpacingPreset( padding?.left )
? getSpacingPresetCssVar( padding?.left )
: padding?.left,
};
}, [ padding ] );
const [ isActive, setIsActive ] = useState( false );
const valueRef = useRef( padding );
const timeoutRef = useRef();
const clearTimer = () => {
if ( timeoutRef.current ) {
window.clearTimeout( timeoutRef.current );
}
};
useEffect( () => {
if ( ! isShallowEqual( padding, valueRef.current ) ) {
setIsActive( true );
valueRef.current = padding;
clearTimer();
timeoutRef.current = setTimeout( () => {
setIsActive( false );
}, 400 );
}
return () => clearTimer();
}, [ padding ] );
if ( ! isActive ) {
return null;
}
return (
<BlockPopover
clientId={ clientId }
__unstableCoverTarget
__unstableRefreshSize={ padding }
shift={ false }
>
<div className="block-editor__padding-visualizer" style={ style } />
</BlockPopover>
);
}