-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.tsx
213 lines (196 loc) · 5.31 KB
/
index.tsx
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
/**
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { BaseControl } from '../base-control';
import Button from '../button';
import { FlexItem, FlexBlock } from '../flex';
import AllInputControl from './all-input-control';
import InputControls from './input-controls';
import AxialInputControls from './axial-input-controls';
import BoxControlIcon from './icon';
import LinkedButton from './linked-button';
import {
Root,
Header,
HeaderControlWrapper,
} from './styles/box-control-styles';
import { parseQuantityAndUnitFromRawValue } from '../unit-control/utils';
import {
DEFAULT_VALUES,
getInitialSide,
isValuesMixed,
isValuesDefined,
} from './utils';
import { useControlledState } from '../utils/hooks';
import type {
BoxControlIconProps,
BoxControlProps,
BoxControlValue,
} from './types';
const defaultInputProps = {
min: 0,
};
const noop = () => {};
function useUniqueId( idProp?: string ) {
const instanceId = useInstanceId( BoxControl, 'inspector-box-control' );
return idProp || instanceId;
}
/**
* BoxControl components let users set values for Top, Right, Bottom, and Left.
* This can be used as an input control for values like `padding` or `margin`.
*
* ```jsx
* import { __experimentalBoxControl as BoxControl } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* const Example = () => {
* const [ values, setValues ] = useState( {
* top: '50px',
* left: '10%',
* right: '10%',
* bottom: '50px',
* } );
*
* return (
* <BoxControl
* values={ values }
* onChange={ ( nextValues ) => setValues( nextValues ) }
* />
* );
* };
* ```
*/
function BoxControl( {
id: idProp,
inputProps = defaultInputProps,
onChange = noop,
label = __( 'Box Control' ),
values: valuesProp,
units,
sides,
splitOnAxis = false,
allowReset = true,
resetValues = DEFAULT_VALUES,
onMouseOver,
onMouseOut,
}: BoxControlProps ) {
const [ values, setValues ] = useControlledState( valuesProp, {
fallback: DEFAULT_VALUES,
} );
const inputValues = values || DEFAULT_VALUES;
const hasInitialValue = isValuesDefined( valuesProp );
const hasOneSide = sides?.length === 1;
const [ isDirty, setIsDirty ] = useState( hasInitialValue );
const [ isLinked, setIsLinked ] = useState(
! hasInitialValue || ! isValuesMixed( inputValues ) || hasOneSide
);
const [ side, setSide ] = useState< BoxControlIconProps[ 'side' ] >(
getInitialSide( isLinked, splitOnAxis )
);
// Tracking selected units via internal state allows filtering of CSS unit
// only values from being saved while maintaining preexisting unit selection
// behaviour. Filtering CSS only values prevents invalid style values.
const [ selectedUnits, setSelectedUnits ] = useState< BoxControlValue >( {
top: parseQuantityAndUnitFromRawValue( valuesProp?.top )[ 1 ],
right: parseQuantityAndUnitFromRawValue( valuesProp?.right )[ 1 ],
bottom: parseQuantityAndUnitFromRawValue( valuesProp?.bottom )[ 1 ],
left: parseQuantityAndUnitFromRawValue( valuesProp?.left )[ 1 ],
} );
const id = useUniqueId( idProp );
const headingId = `${ id }-heading`;
const toggleLinked = () => {
setIsLinked( ! isLinked );
setSide( getInitialSide( ! isLinked, splitOnAxis ) );
};
const handleOnFocus = (
_event: React.FocusEvent< HTMLInputElement >,
{ side: nextSide }: { side: typeof side }
) => {
setSide( nextSide );
};
const handleOnChange = ( nextValues: BoxControlValue ) => {
onChange( nextValues );
setValues( nextValues );
setIsDirty( true );
};
const handleOnReset = () => {
onChange( resetValues );
setValues( resetValues );
setSelectedUnits( resetValues );
setIsDirty( false );
};
const inputControlProps = {
...inputProps,
onChange: handleOnChange,
onFocus: handleOnFocus,
isLinked,
units,
selectedUnits,
setSelectedUnits,
sides,
values: inputValues,
onMouseOver,
onMouseOut,
};
return (
<Root id={ id } role="group" aria-labelledby={ headingId }>
<Header className="component-box-control__header">
<FlexItem>
<BaseControl.VisualLabel id={ headingId }>
{ label }
</BaseControl.VisualLabel>
</FlexItem>
{ allowReset && (
<FlexItem>
<Button
className="component-box-control__reset-button"
variant="secondary"
isSmall
onClick={ handleOnReset }
disabled={ ! isDirty }
>
{ __( 'Reset' ) }
</Button>
</FlexItem>
) }
</Header>
<HeaderControlWrapper className="component-box-control__header-control-wrapper">
<FlexItem>
<BoxControlIcon side={ side } sides={ sides } />
</FlexItem>
{ isLinked && (
<FlexBlock>
<AllInputControl
aria-label={ label }
{ ...inputControlProps }
/>
</FlexBlock>
) }
{ ! isLinked && splitOnAxis && (
<FlexBlock>
<AxialInputControls { ...inputControlProps } />
</FlexBlock>
) }
{ ! hasOneSide && (
<FlexItem>
<LinkedButton
onClick={ toggleLinked }
isLinked={ isLinked }
/>
</FlexItem>
) }
</HeaderControlWrapper>
{ ! isLinked && ! splitOnAxis && (
<InputControls { ...inputControlProps } />
) }
</Root>
);
}
export { applyValueToSides } from './utils';
export default BoxControl;