-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
border-panel.js
237 lines (217 loc) · 6.14 KB
/
border-panel.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
/**
* WordPress dependencies
*/
import {
__experimentalBorderBoxControl as BorderBoxControl,
__experimentalHasSplitBorders as hasSplitBorders,
__experimentalIsDefinedBorder as isDefinedBorder,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { useCallback, useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import BorderRadiusControl from '../border-radius-control';
import { useColorsPerOrigin } from './hooks';
import { getValueFromVariable } from './utils';
export function useHasBorderPanel( settings ) {
const controls = [
useHasBorderColorControl( settings ),
useHasBorderRadiusControl( settings ),
useHasBorderStyleControl( settings ),
useHasBorderWidthControl( settings ),
];
return controls.some( Boolean );
}
function useHasBorderColorControl( settings ) {
return settings?.border?.color;
}
function useHasBorderRadiusControl( settings ) {
return settings?.border?.radius;
}
function useHasBorderStyleControl( settings ) {
return settings?.border?.style;
}
function useHasBorderWidthControl( settings ) {
return settings?.border?.width;
}
function BorderToolsPanel( {
resetAllFilter,
onChange,
value,
panelId,
children,
} ) {
const resetAll = () => {
const updatedValue = resetAllFilter( value );
onChange( updatedValue );
};
return (
<ToolsPanel
label={ __( 'Border' ) }
resetAll={ resetAll }
panelId={ panelId }
>
{ children }
</ToolsPanel>
);
}
const DEFAULT_CONTROLS = {
radius: true,
color: true,
width: true,
};
export default function BorderPanel( {
as: Wrapper = BorderToolsPanel,
value,
onChange,
inheritedValue = value,
settings,
panelId,
defaultControls = DEFAULT_CONTROLS,
} ) {
const colors = useColorsPerOrigin( settings );
const decodeValue = ( rawValue ) =>
getValueFromVariable( { settings }, '', rawValue );
const encodeColorValue = ( colorValue ) => {
const allColors = colors.flatMap(
( { colors: originColors } ) => originColors
);
const colorObject = allColors.find(
( { color } ) => color === colorValue
);
return colorObject
? 'var:preset|color|' + colorObject.slug
: colorValue;
};
const decodeColorValue = useCallback(
( colorValue ) => {
const allColors = colors.flatMap(
( { colors: originColors } ) => originColors
);
const colorObject = allColors.find(
( { slug } ) => colorValue === 'var:preset|color|' + slug
);
return colorObject ? colorObject.color : colorValue;
},
[ colors ]
);
const border = useMemo( () => {
if ( hasSplitBorders( inheritedValue?.border ) ) {
const borderValue = { ...inheritedValue?.border };
[ 'top', 'right', 'bottom', 'left' ].forEach( ( side ) => {
borderValue[ side ] = {
...borderValue[ side ],
color: decodeColorValue( borderValue[ side ]?.color ),
};
} );
return borderValue;
}
return {
...inheritedValue?.border,
color: inheritedValue?.border?.color
? decodeColorValue( inheritedValue?.border?.color )
: undefined,
};
}, [ inheritedValue?.border, decodeColorValue ] );
const setBorder = ( newBorder ) =>
onChange( { ...value, border: newBorder } );
const showBorderColor = useHasBorderColorControl( settings );
const showBorderStyle = useHasBorderStyleControl( settings );
const showBorderWidth = useHasBorderWidthControl( settings );
// Border radius.
const showBorderRadius = useHasBorderRadiusControl( settings );
const borderRadiusValues = decodeValue( border?.radius );
const setBorderRadius = ( newBorderRadius ) =>
setBorder( { ...border, radius: newBorderRadius } );
const hasBorderRadius = () => {
const borderValues = value?.border?.radius;
if ( typeof borderValues === 'object' ) {
return Object.entries( borderValues ).some( Boolean );
}
return !! borderValues;
};
const resetBorder = () => {
if ( hasBorderRadius() ) {
return setBorder( { radius: value?.border?.radius } );
}
setBorder( undefined );
};
const onBorderChange = ( newBorder ) => {
// Ensure we have a visible border style when a border width or
// color is being selected.
const updatedBorder = { ...newBorder };
if ( hasSplitBorders( updatedBorder ) ) {
[ 'top', 'right', 'bottom', 'left' ].forEach( ( side ) => {
if ( updatedBorder[ side ] ) {
updatedBorder[ side ] = {
...updatedBorder[ side ],
color: encodeColorValue( updatedBorder[ side ]?.color ),
};
}
} );
} else if ( updatedBorder ) {
updatedBorder.color = encodeColorValue( updatedBorder.color );
}
// As radius is maintained separately to color, style, and width
// maintain its value. Undefined values here will be cleaned when
// global styles are saved.
setBorder( { radius: border?.radius, ...updatedBorder } );
};
const resetAllFilter = useCallback( ( previousValue ) => {
return {
...previousValue,
border: undefined,
};
}, [] );
const showBorderByDefault =
defaultControls?.color || defaultControls?.width;
return (
<Wrapper
resetAllFilter={ resetAllFilter }
value={ value }
onChange={ onChange }
panelId={ panelId }
>
{ ( showBorderWidth || showBorderColor ) && (
<ToolsPanelItem
hasValue={ () => isDefinedBorder( value?.border ) }
label={ __( 'Border' ) }
onDeselect={ () => resetBorder() }
isShownByDefault={ showBorderByDefault }
panelId={ panelId }
>
<BorderBoxControl
colors={ colors }
enableAlpha={ true }
enableStyle={ showBorderStyle }
onChange={ onBorderChange }
popoverOffset={ 40 }
popoverPlacement="left-start"
value={ border }
__experimentalIsRenderedInSidebar={ true }
size={ '__unstable-large' }
/>
</ToolsPanelItem>
) }
{ showBorderRadius && (
<ToolsPanelItem
hasValue={ hasBorderRadius }
label={ __( 'Radius' ) }
onDeselect={ () => setBorderRadius( undefined ) }
isShownByDefault={ defaultControls.radius }
panelId={ panelId }
>
<BorderRadiusControl
values={ borderRadiusValues }
onChange={ ( newValue ) => {
setBorderRadius( newValue || undefined );
} }
/>
</ToolsPanelItem>
) }
</Wrapper>
);
}