-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.tsx
209 lines (196 loc) · 5.11 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
/**
* External dependencies
*/
import { type LinearGradientNode } from 'gradient-parser';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import AnglePickerControl from '../angle-picker-control';
import CustomGradientBar from './gradient-bar';
import { Flex } from '../flex';
import SelectControl from '../select-control';
import { VStack } from '../v-stack';
import {
getGradientAstWithDefault,
getLinearGradientRepresentation,
getGradientAstWithControlPoints,
getStopCssColor,
} from './utils';
import { serializeGradient } from './serializer';
import {
DEFAULT_LINEAR_GRADIENT_ANGLE,
HORIZONTAL_GRADIENT_ORIENTATION,
GRADIENT_OPTIONS,
} from './constants';
import {
AccessoryWrapper,
SelectWrapper,
} from './styles/custom-gradient-picker-styles';
import type {
CustomGradientPickerProps,
GradientAnglePickerProps,
GradientTypePickerProps,
} from './types';
const GradientAnglePicker = ( {
gradientAST,
hasGradient,
onChange,
}: GradientAnglePickerProps ) => {
const angle =
gradientAST?.orientation?.value ?? DEFAULT_LINEAR_GRADIENT_ANGLE;
const onAngleChange = ( newAngle: number ) => {
onChange(
serializeGradient( {
...gradientAST,
orientation: {
type: 'angular',
value: `${ newAngle }`,
},
} )
);
};
return (
<AnglePickerControl
onChange={ onAngleChange }
value={ hasGradient ? angle : '' }
/>
);
};
const GradientTypePicker = ( {
gradientAST,
hasGradient,
onChange,
}: GradientTypePickerProps ) => {
const { type } = gradientAST;
const onSetLinearGradient = () => {
onChange(
serializeGradient( {
...gradientAST,
orientation: gradientAST.orientation
? undefined
: HORIZONTAL_GRADIENT_ORIENTATION,
type: 'linear-gradient',
} satisfies LinearGradientNode )
);
};
const onSetRadialGradient = () => {
const { orientation, ...restGradientAST } = gradientAST;
onChange(
serializeGradient( {
...restGradientAST,
type: 'radial-gradient',
} )
);
};
const handleOnChange = ( next: string ) => {
if ( next === 'linear-gradient' ) {
onSetLinearGradient();
}
if ( next === 'radial-gradient' ) {
onSetRadialGradient();
}
};
return (
<SelectControl
__nextHasNoMarginBottom
className="components-custom-gradient-picker__type-picker"
label={ __( 'Type' ) }
labelPosition="top"
onChange={ handleOnChange }
options={ GRADIENT_OPTIONS }
size="__unstable-large"
value={ hasGradient ? type : undefined }
/>
);
};
/**
* CustomGradientPicker is a React component that renders a UI for specifying
* linear or radial gradients. Radial gradients are displayed in the picker as
* a slice of the gradient from the center to the outside.
*
* ```jsx
* import { CustomGradientPicker } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* const MyCustomGradientPicker = () => {
* const [ gradient, setGradient ] = useState();
*
* return (
* <CustomGradientPicker
* value={ gradient }
* onChange={ setGradient }
* />
* );
* };
* ```
*/
export function CustomGradientPicker( {
value,
onChange,
__experimentalIsRenderedInSidebar = false,
}: CustomGradientPickerProps ) {
const { gradientAST, hasGradient } = getGradientAstWithDefault( value );
// On radial gradients the bar should display a linear gradient.
// On radial gradients the bar represents a slice of the gradient from the center until the outside.
// On liner gradients the bar represents the color stops from left to right independently of the angle.
const background = getLinearGradientRepresentation( gradientAST );
// Control points color option may be hex from presets, custom colors will be rgb.
// The position should always be a percentage.
const controlPoints = gradientAST.colorStops.map( ( colorStop ) => {
return {
color: getStopCssColor( colorStop ),
// Although it's already been checked by `hasUnsupportedLength` in `getGradientAstWithDefault`,
// TypeScript doesn't know that `colorStop.length` is not undefined here.
// @ts-expect-error
position: parseInt( colorStop.length.value ),
};
} );
return (
<VStack spacing={ 4 } className="components-custom-gradient-picker">
<CustomGradientBar
__experimentalIsRenderedInSidebar={
__experimentalIsRenderedInSidebar
}
background={ background }
hasGradient={ hasGradient }
value={ controlPoints }
onChange={ ( newControlPoints ) => {
onChange(
serializeGradient(
getGradientAstWithControlPoints(
gradientAST,
newControlPoints
)
)
);
} }
/>
<Flex
gap={ 3 }
className="components-custom-gradient-picker__ui-line"
>
<SelectWrapper>
<GradientTypePicker
gradientAST={ gradientAST }
hasGradient={ hasGradient }
onChange={ onChange }
/>
</SelectWrapper>
<AccessoryWrapper>
{ gradientAST.type === 'linear-gradient' && (
<GradientAnglePicker
gradientAST={ gradientAST }
hasGradient={ hasGradient }
onChange={ onChange }
/>
) }
</AccessoryWrapper>
</Flex>
</VStack>
);
}
export default CustomGradientPicker;