-
Notifications
You must be signed in to change notification settings - Fork 1.3k
experimental: add gradient controller to update color stops using ui #4159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
JayaKrishnaNamburu
wants to merge
29
commits into
main
Choose a base branch
from
add-gradient-control
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
12d8002
experimental: add gradient control for updating the color stops using ui
JayaKrishnaNamburu e9a0980
update stories to display the result
JayaKrishnaNamburu d5315fe
add comments for color-stop and hint behaviours
JayaKrishnaNamburu f62fdca
add interpolated color-stops when clicked in between the color stops
JayaKrishnaNamburu 59d61cd
pass props when a thumb is selected on the radix slider
JayaKrishnaNamburu 99bf5cd
Merge branch 'main' into add-gradient-control
JayaKrishnaNamburu aaebe2f
allow users to change the color of a color-stop
JayaKrishnaNamburu 4076799
refactor and add comments for position calculation
JayaKrishnaNamburu c0e21cd
Merge branch 'main' into add-gradient-control
JayaKrishnaNamburu bd5958b
Merge branch 'main' into add-gradient-control
JayaKrishnaNamburu b3c6d7d
use slider instead of root from radix
JayaKrishnaNamburu 6979a34
Merge branch 'main' into add-gradient-control
JayaKrishnaNamburu ab025c0
Merge branch 'main' into add-gradient-control
JayaKrishnaNamburu 9a9d8cc
remove popover interaction for color stop change
JayaKrishnaNamburu 598c902
Merge branch 'main' into add-gradient-control
JayaKrishnaNamburu 4328303
update the slider import from @radix-ui/slider
JayaKrishnaNamburu 4857c99
Merge branch 'main' into add-gradient-control
JayaKrishnaNamburu 8caa73d
fix ts typechecks
JayaKrishnaNamburu 42664ff
Merge branch 'main' into add-gradient-control
JayaKrishnaNamburu 3dce8ff
Merge branch 'main' into add-gradient-control
JayaKrishnaNamburu f7f35c3
update @radix-ui/react-slider
JayaKrishnaNamburu 2730ef7
add chevronbigiconup svg for color hints
JayaKrishnaNamburu 5d07118
Merge branch 'main' into add-gradient-control
JayaKrishnaNamburu d1dd551
update icons, added filled chevron
kof 8c6ffd9
use the right border color
kof b00e931
design change for handles
kof ca9043a
design changes
kof 136f21c
remove experimental cursor
kof c90954f
Merge remote-tracking branch 'origin/main' into add-gradient-control
JayaKrishnaNamburu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...uilder/app/builder/features/style-panel/sections/backgrounds/gradient-control.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { | ||
parseLinearGradient, | ||
reconstructLinearGradient, | ||
type ParsedGradient, | ||
} from "@webstudio-is/css-data"; | ||
import { GradientControl } from "./gradient-control"; | ||
import { Flex, Text } from "@webstudio-is/design-system"; | ||
import { useState } from "react"; | ||
|
||
export default { | ||
title: "Library/GradientControl", | ||
}; | ||
|
||
export const GradientWithoutAngle = () => { | ||
const gradientString = "linear-gradient(#e66465 0%, #9198e5 100%)"; | ||
const [gradient, setGradient] = useState<string>(gradientString); | ||
|
||
return ( | ||
<Flex direction="column" gap="4"> | ||
<GradientControl | ||
gradient={parseLinearGradient(gradientString) as ParsedGradient} | ||
onChange={(value) => { | ||
setGradient(reconstructLinearGradient(value)); | ||
}} | ||
/> | ||
<Text>{gradient}</Text> | ||
</Flex> | ||
); | ||
}; | ||
|
||
export const GradientWithAngleAndHints = () => { | ||
const gradientString = | ||
"linear-gradient(145deg, #ff00fa 0%, #00f497 34% 34%, #ffa800 56% 56%, #00eaff 100%)"; | ||
const [gradient, setGradient] = useState<string>(gradientString); | ||
|
||
return ( | ||
<Flex direction="column" gap="4"> | ||
<GradientControl | ||
gradient={parseLinearGradient(gradientString) as ParsedGradient} | ||
onChange={(value) => { | ||
setGradient(reconstructLinearGradient(value)); | ||
}} | ||
/> | ||
<Text>{gradient}</Text> | ||
</Flex> | ||
); | ||
}; | ||
|
||
export const GradientWithSideOrCorner = () => { | ||
const gradientString = "linear-gradient(to left top, blue 0%, red 100%)"; | ||
|
||
const [gradient, setGradient] = useState<string>(gradientString); | ||
|
||
return ( | ||
<Flex direction="column" gap="4"> | ||
<GradientControl | ||
gradient={parseLinearGradient(gradientString) as ParsedGradient} | ||
onChange={(value) => { | ||
setGradient(reconstructLinearGradient(value)); | ||
}} | ||
/> | ||
<Text>{gradient}</Text> | ||
</Flex> | ||
); | ||
}; |
171 changes: 171 additions & 0 deletions
171
apps/builder/app/builder/features/style-panel/sections/backgrounds/gradient-control.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import { toValue, UnitValue } from "@webstudio-is/css-engine"; | ||
import { Root, Range, Thumb, Track } from "@radix-ui/react-slider"; | ||
JayaKrishnaNamburu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
import { useState, useCallback } from "react"; | ||
import { | ||
reconstructLinearGradient, | ||
type GradientStop, | ||
type ParsedGradient, | ||
} from "@webstudio-is/css-data"; | ||
import { styled, theme, Flex } from "@webstudio-is/design-system"; | ||
import { ChevronBigUpIcon } from "@webstudio-is/icons"; | ||
|
||
type GradientControlProps = { | ||
gradient: ParsedGradient; | ||
onChange: (value: ParsedGradient) => void; | ||
}; | ||
|
||
const defaultAngle: UnitValue = { | ||
type: "unit", | ||
value: 90, | ||
unit: "deg", | ||
}; | ||
|
||
export const GradientControl = (props: GradientControlProps) => { | ||
const [stops, setStops] = useState<Array<GradientStop>>(props.gradient.stops); | ||
const [selectedStop, setSelectedStop] = useState<number | undefined>(); | ||
const positions = stops | ||
.map((stop) => stop.position?.value) | ||
.filter((item) => item !== undefined); | ||
const hints = props.gradient.stops | ||
.map((stop) => stop.hint?.value) | ||
.filter((item) => item !== undefined); | ||
const background = reconstructLinearGradient({ | ||
stops, | ||
sideOrCorner: props.gradient.sideOrCorner, | ||
angle: defaultAngle, | ||
}); | ||
|
||
// Every color stop should have a asociated position for us in-order to display the slider thumb. | ||
// But when users manually enter linear-gradient from the advanced-panle. They might add something like this | ||
// linear-gradient(to right, red, blue), or linear-gradient(150deg, red, blue 50%, yellow 50px) | ||
// Browsers handles all these cases by following the rules of the css spec. | ||
// https://www.w3.org/TR/css-images-4/#color-stop-fixup | ||
// In order to handle such examples from the advanced tab too. We need to implement the color-stop-fix-up spec during parsing. | ||
// 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. | ||
// We will never run into this case of a color-stop missing a position associated with it. | ||
const isEveryStopHasAPosition = stops.every( | ||
(stop) => stop.position !== undefined && stop.color !== undefined | ||
); | ||
|
||
const handleValueChange = useCallback( | ||
(newPositions: number[]) => { | ||
const newStops: GradientStop[] = stops.map((stop, index) => ({ | ||
...stop, | ||
position: { type: "unit", value: newPositions[index], unit: "%" }, | ||
})); | ||
|
||
setStops(newStops); | ||
props.onChange({ | ||
angle: props.gradient.angle, | ||
stops: newStops, | ||
sideOrCorner: props.gradient.sideOrCorner, | ||
}); | ||
}, | ||
[stops, props] | ||
); | ||
|
||
const handleKeyDown = useCallback( | ||
(event: React.KeyboardEvent) => { | ||
if (event.key === "Backspace" && selectedStop !== undefined) { | ||
const newStops = stops; | ||
newStops.splice(selectedStop, 1); | ||
setStops(newStops); | ||
setSelectedStop(undefined); | ||
} | ||
}, | ||
[stops, selectedStop] | ||
); | ||
|
||
if (isEveryStopHasAPosition === false) { | ||
return; | ||
} | ||
|
||
return ( | ||
<Flex | ||
align="end" | ||
css={{ | ||
width: theme.spacing[28], | ||
height: theme.spacing[14], | ||
}} | ||
> | ||
<SliderRoot | ||
css={{ background }} | ||
max={100} | ||
step={1} | ||
value={positions} | ||
onValueChange={handleValueChange} | ||
onKeyDown={handleKeyDown} | ||
> | ||
<Track> | ||
<SliderRange css={{ cursor: "copy" }} /> | ||
</Track> | ||
{stops.map((stop, index) => ( | ||
<SliderThumb | ||
key={index} | ||
onClick={() => { | ||
setSelectedStop(index); | ||
}} | ||
style={{ | ||
background: toValue(stop.color), | ||
}} | ||
/> | ||
))} | ||
|
||
{/* | ||
Hints are displayed as a chevron icon below the slider thumb. | ||
Usually hints are used to display the behaviour of the color-stop that is preciding. | ||
But, if we just move them along the UI. We will be basically altering the gradient itself. | ||
Because the position of the hint is the position of the color-stop. And moving it along, might associate the hint | ||
with a different color-stop. So, we are not allowing the user to move the hint along the slider. | ||
|
||
None of the tools are even displaying the hints at the moment. We are just displaying them so users can know | ||
they are hints associated too. | ||
*/} | ||
{hints.map((hint) => { | ||
return ( | ||
<Flex | ||
key={hint} | ||
align="center" | ||
justify="center" | ||
css={{ | ||
position: "absolute", | ||
left: `${hint}%`, | ||
top: theme.spacing[9], | ||
}} | ||
> | ||
<ChevronBigUpIcon color={theme.colors.borderMain} /> | ||
</Flex> | ||
); | ||
})} | ||
</SliderRoot> | ||
</Flex> | ||
); | ||
}; | ||
|
||
const SliderRoot = styled(Root, { | ||
position: "relative", | ||
width: "100%", | ||
height: theme.spacing[9], | ||
border: `1px solid ${theme.colors.borderInfo}`, | ||
borderRadius: theme.borderRadius[3], | ||
touchAction: "none", | ||
userSelect: "none", | ||
}); | ||
|
||
const SliderRange = styled(Range, { | ||
position: "absolute", | ||
background: "transparent", | ||
borderRadius: theme.borderRadius[3], | ||
}); | ||
|
||
const SliderThumb = styled(Thumb, { | ||
position: "absolute", | ||
width: theme.spacing[9], | ||
height: theme.spacing[9], | ||
border: `1px solid ${theme.colors.borderInfo}`, | ||
borderRadius: theme.borderRadius[3], | ||
top: `-${theme.spacing[11]}`, | ||
translate: "-9px", | ||
}); | ||
|
||
export default GradientControl; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./transition"; | ||
export * from "./shadow-properties-extractor"; | ||
export * from "./linear-gradient"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.