forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrols.js
More file actions
149 lines (141 loc) · 3.92 KB
/
controls.js
File metadata and controls
149 lines (141 loc) · 3.92 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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
InspectorControls,
useSettings,
__experimentalSpacingSizesControl as SpacingSizesControl,
isValueSpacingPreset,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import {
__experimentalUseCustomUnits as useCustomUnits,
__experimentalUnitControl as UnitControl,
__experimentalParseQuantityAndUnitFromRawValue as parseQuantityAndUnitFromRawValue,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { View } from '@wordpress/primitives';
/**
* Internal dependencies
*/
import { unlock } from '../lock-unlock';
import { DEFAULT_HEIGHT, MIN_SPACER_SIZE } from './constants';
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
const { useSpacingSizes } = unlock( blockEditorPrivateApis );
function DimensionInput( { label, onChange, isResizing, value = '' } ) {
const inputId = useInstanceId( UnitControl, 'block-spacer-height-input' );
const spacingSizes = useSpacingSizes();
const [ spacingUnits ] = useSettings( 'spacing.units' );
// In most contexts the spacer size cannot meaningfully be set to a
// percentage, since this is relative to the parent container. This
// unit is disabled from the UI.
const availableUnits = spacingUnits
? spacingUnits.filter( ( unit ) => unit !== '%' )
: [ 'px', 'em', 'rem', 'vw', 'vh' ];
const units = useCustomUnits( {
availableUnits,
defaultValues: { px: 100, em: 10, rem: 10, vw: 10, vh: 25 },
} );
// Force the unit to update to `px` when the Spacer is being resized.
const [ parsedQuantity, parsedUnit ] =
parseQuantityAndUnitFromRawValue( value );
const computedValue = isValueSpacingPreset( value )
? value
: [ parsedQuantity, isResizing ? 'px' : parsedUnit ].join( '' );
return (
<>
{ spacingSizes?.length < 2 ? (
<UnitControl
id={ inputId }
isResetValueOnUnitChange
min={ MIN_SPACER_SIZE }
onChange={ onChange }
value={ computedValue }
units={ units }
label={ label }
__next40pxDefaultSize
/>
) : (
<View className="tools-panel-item-spacing">
<SpacingSizesControl
values={ { all: computedValue } }
onChange={ ( { all } ) => {
onChange( all );
} }
label={ label }
sides={ [ 'all' ] }
units={ units }
allowReset={ false }
splitOnAxis={ false }
showSideInLabel={ false }
/>
</View>
) }
</>
);
}
export default function SpacerControls( {
setAttributes,
orientation,
height,
width,
isResizing,
} ) {
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
return (
<InspectorControls>
<ToolsPanel
label={ __( 'Settings' ) }
resetAll={ () => {
setAttributes( {
width: undefined,
height: DEFAULT_HEIGHT,
} );
} }
dropdownMenuProps={ dropdownMenuProps }
>
{ orientation === 'horizontal' && (
<ToolsPanelItem
label={ __( 'Width' ) }
isShownByDefault
hasValue={ () => width !== undefined }
onDeselect={ () =>
setAttributes( { width: undefined } )
}
>
<DimensionInput
label={ __( 'Width' ) }
value={ width }
onChange={ ( nextWidth ) =>
setAttributes( { width: nextWidth } )
}
isResizing={ isResizing }
/>
</ToolsPanelItem>
) }
{ orientation !== 'horizontal' && (
<ToolsPanelItem
label={ __( 'Height' ) }
isShownByDefault
hasValue={ () => height !== DEFAULT_HEIGHT }
onDeselect={ () =>
setAttributes( { height: DEFAULT_HEIGHT } )
}
>
<DimensionInput
label={ __( 'Height' ) }
value={ height }
onChange={ ( nextHeight ) =>
setAttributes( { height: nextHeight } )
}
isResizing={ isResizing }
/>
</ToolsPanelItem>
) }
</ToolsPanel>
</InspectorControls>
);
}