Skip to content

Commit d5315fe

Browse files
add comments for color-stop and hint behaviours
1 parent e9a0980 commit d5315fe

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

apps/builder/app/builder/features/style-panel/sections/backgrounds/gradient-control.tsx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,30 @@ const defaultAngle: UnitValue = {
2323
export const GradientControl = (props: GradientControlProps) => {
2424
const [stops, setStops] = useState<Array<GradientStop>>(props.gradient.stops);
2525
const [selectedStop, setSelectedStop] = useState<number | undefined>();
26-
const positions = stops.map((stop) => stop.position?.value);
26+
const positions = stops
27+
.map((stop) => stop.position?.value)
28+
.filter((item) => item !== undefined);
2729
const hints = props.gradient.stops
2830
.map((stop) => stop.hint?.value)
29-
.filter(Boolean);
31+
.filter((item) => item !== undefined);
3032
const background = reconstructLinearGradient({
3133
stops,
3234
sideOrCorner: props.gradient.sideOrCorner,
3335
angle: defaultAngle,
3436
});
3537

38+
// Every color stop should have a asociated position for us in-order to display the slider thumb.
39+
// But when users manually enter linear-gradient from the advanced-panle. They might add something like this
40+
// linear-gradient(to right, red, blue), or linear-gradient(150deg, red, blue 50%, yellow 50px)
41+
// Browsers handles all these cases by following the rules of the css spec.
42+
// https://www.w3.org/TR/css-images-4/#color-stop-fixup
43+
// In order to handle such examples from the advanced tab too. We need to implement the color-stop-fix-up spec during parsing.
44+
// But for now, we are just checking if every stop has a position or not. Since the main use-case if to add gradients from ui.
45+
// We will never run into this case of a color-stop missing a position associated with it.
46+
const isEveryStopHasAPosition = stops.every(
47+
(stop) => stop.position !== undefined && stop.color !== undefined
48+
);
49+
3650
const handleValueChange = useCallback(
3751
(newPositions: number[]) => {
3852
const newStops: GradientStop[] = stops.map((stop, index) => ({
@@ -62,6 +76,10 @@ export const GradientControl = (props: GradientControlProps) => {
6276
[stops, selectedStop]
6377
);
6478

79+
if (isEveryStopHasAPosition === false) {
80+
return;
81+
}
82+
6583
return (
6684
<Flex
6785
align="end"
@@ -93,6 +111,16 @@ export const GradientControl = (props: GradientControlProps) => {
93111
/>
94112
))}
95113

114+
{/*
115+
Hints are displayed as a chevron icon below the slider thumb.
116+
Usually hints are used to display the behaviour of the color-stop that is preciding.
117+
But, if we just move them along the UI. We will be basically altering the gradient itself.
118+
Because the position of the hint is the position of the color-stop. And moving it along, might associate the hint
119+
with a different color-stop. So, we are not allowing the user to move the hint along the slider.
120+
121+
None of the tools are even displaying the hints at the moment. We are just displaying them so users can know
122+
they are hints associated too.
123+
*/}
96124
{hints.map((hint) => {
97125
return (
98126
<Flex

0 commit comments

Comments
 (0)