forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
181 lines (164 loc) · 4.21 KB
/
index.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
/**
* External dependencies
*/
import { noop } from 'lodash';
/**
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import Button from '../button';
import { FlexItem, FlexBlock } from '../flex';
import AllInputControl from './all-input-control';
import InputControls from './input-controls';
import VerticalHorizontalInputControls from './vertical-horizontal-input-controls';
import BoxControlIcon from './icon';
import { Text } from '../text';
import LinkedButton from './linked-button';
import Visualizer from './visualizer';
import {
Root,
Header,
HeaderControlWrapper,
} from './styles/box-control-styles';
import {
DEFAULT_VALUES,
DEFAULT_VISUALIZER_VALUES,
getInitialSide,
isValuesMixed,
isValuesDefined,
} from './utils';
import { useControlledState } from '../utils/hooks';
const defaultInputProps = {
min: 0,
};
function useUniqueId( idProp ) {
const instanceId = useInstanceId( BoxControl, 'inspector-box-control' );
return idProp || instanceId;
}
export default function BoxControl( {
id: idProp,
inputProps = defaultInputProps,
onChange = noop,
onChangeShowVisualizer = noop,
label = __( 'Box Control' ),
values: valuesProp,
units,
sides,
isGroupedDirections = false,
allowReset = true,
resetValues = DEFAULT_VALUES,
} ) {
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(
getInitialSide( isLinked, isGroupedDirections )
);
const id = useUniqueId( idProp );
const headingId = `${ id }-heading`;
const toggleLinked = () => {
setIsLinked( ! isLinked );
setSide( getInitialSide( ! isLinked, isGroupedDirections ) );
};
const handleOnFocus = ( event, { side: nextSide } ) => {
setSide( nextSide );
};
const handleOnChange = ( nextValues ) => {
onChange( nextValues );
setValues( nextValues );
setIsDirty( true );
};
const handleOnHoverOn = ( next = {} ) => {
onChangeShowVisualizer( { ...DEFAULT_VISUALIZER_VALUES, ...next } );
};
const handleOnHoverOff = ( next = {} ) => {
onChangeShowVisualizer( { ...DEFAULT_VISUALIZER_VALUES, ...next } );
};
const handleOnReset = () => {
onChange( resetValues );
setValues( resetValues );
setIsDirty( false );
};
const inputControlProps = {
...inputProps,
onChange: handleOnChange,
onFocus: handleOnFocus,
onHoverOn: handleOnHoverOn,
onHoverOff: handleOnHoverOff,
isLinked,
units,
sides,
values: inputValues,
};
return (
<Root id={ id } role="region" aria-labelledby={ headingId }>
<Header className="component-box-control__header">
<FlexItem>
<Text
id={ headingId }
className="component-box-control__label"
>
{ label }
</Text>
</FlexItem>
{ allowReset && (
<FlexItem>
<Button
className="component-box-control__reset-button"
isSecondary
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 && isGroupedDirections && (
<FlexBlock>
<VerticalHorizontalInputControls
{ ...inputControlProps }
/>
</FlexBlock>
) }
{ ! hasOneSide && (
<FlexItem>
<LinkedButton
onClick={ toggleLinked }
isLinked={ isLinked }
/>
</FlexItem>
) }
</HeaderControlWrapper>
{ ! isLinked && ! isGroupedDirections && (
<InputControls { ...inputControlProps } />
) }
</Root>
);
}
BoxControl.__Visualizer = Visualizer;