Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
Show currentcolor value in picker (#1339)
Ivan Starkov8c27c5c86ba9
diff --git a/apps/builder/app/builder/features/style-panel/controls/color/color-control.tsx b/apps/builder/app/builder/features/style-panel/controls/color/color-control.tsxindex d9d2e60fc3..2a6b04a447 100644--- a/apps/builder/app/builder/features/style-panel/controls/color/color-control.tsx+++ b/apps/builder/app/builder/features/style-panel/controls/color/color-control.tsx@@ -9,6 +9,32 @@ import { colord } from "colord";import { getStyleSource } from "../../shared/style-info";import { styleConfigByName } from "../../shared/configs";import { useState } from "react";+import type { RgbValue, StyleValue } from "@webstudio-is/css-data";++const parseColor = (color?: StyleValue): RgbValue => {+ const colordValue = colord(toValue(color));++ if (colordValue.isValid()) {+ const rgb = colordValue.toRgb();+ return {+ type: "rgb",+ r: rgb.r,+ g: rgb.g,+ b: rgb.b,+ alpha: rgb.a ?? 1,+ };+ }++ // @todo what to show as default?+ // Default to black+ return {+ type: "rgb",+ r: 0,+ g: 0,+ b: 0,+ alpha: 1,+ };+};export const ColorControl = ({property,@@ -32,33 +58,15 @@ export const ColorControl = ({if (value.type !== "rgb" && value.type !== "keyword") {// Support previously set colors- const colordValue = colord(toValue(value));-- if (colordValue.isValid()) {- const rgb = colordValue.toRgb();- value = {- type: "rgb",- r: rgb.r,- g: rgb.g,- b: rgb.b,- alpha: rgb.a ?? 1,- };- } else {- // @todo what to show as default?- // Default to black- value = {- type: "rgb",- r: 0,- g: 0,- b: 0,- alpha: 1,- };- }+ value = parseColor(value);}+ const currentColor = parseColor(currentStyle["color"]?.currentColor);+return (<Flex align="center" gap="1"><ColorPicker+ currentColor={currentColor}property={property}value={value}styleSource={getStyleSource(styleInfo)}diff --git a/apps/builder/app/builder/features/style-panel/shared/color-picker.tsx b/apps/builder/app/builder/features/style-panel/shared/color-picker.tsxindex deac410ea2..46f380c67e 100644--- a/apps/builder/app/builder/features/style-panel/shared/color-picker.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/color-picker.tsx@@ -56,6 +56,26 @@ const colorResultToRgbValue = (rgb: RgbaColor | RGBColor): RgbValue => {};};+const styleValueResolve = (+ value: RgbValue | KeywordValue,+ currentColor: RgbValue+): RgbValue | KeywordValue => {+ if (+ value.type === "keyword" &&+ value.value.toLowerCase() === "currentcolor"+ ) {+ return {+ type: "rgb",+ r: currentColor.r,+ g: currentColor.g,+ b: currentColor.b,+ alpha: currentColor.alpha,+ };+ }++ return value;+};+const styleValueToRgbaColor = (value: CssColorPickerValueInput): RgbaColor => {const color = colord(value.type === "intermediate" ? value.value : toValue(value)@@ -143,6 +163,7 @@ type ColorPickerProps = {onAbort: () => void;intermediateValue: CssColorPickerValueInput | undefined;value: RgbValue | KeywordValue;+ currentColor: RgbValue;styleSource: StyleSource;keywords?: Array<KeywordValue>;property: StyleProperty;@@ -150,6 +171,7 @@ type ColorPickerProps = {export const ColorPicker = ({value,+ currentColor,intermediateValue,onChange,onChangeComplete,@@ -162,15 +184,18 @@ export const ColorPicker = ({const [displayColorPicker, setDisplayColorPicker] = useState(false);const canvasFix = useFixDragOverCanvas();- const currentValue = intermediateValue ?? value;+ const currentValue =+ intermediateValue ?? styleValueResolve(value, currentColor);const rgbValue = styleValueToRgbaColor(currentValue);// Change prefix color in sync with color picker, don't change during input changed- const prefixColor =+ const prefixColor = styleValueResolve(currentValue.type === "keyword" || currentValue.type === "rgb"? currentValue- : value;+ : value,+ currentColor+ );const prefixColorRgba = styleValueToRgbaColor(prefixColor);diff --git a/apps/builder/app/builder/features/style-panel/shared/style-info.ts b/apps/builder/app/builder/features/style-panel/shared/style-info.tsindex 1b3c835281..02f1476418 100644--- a/apps/builder/app/builder/features/style-panel/shared/style-info.ts+++ b/apps/builder/app/builder/features/style-panel/shared/style-info.ts@@ -51,7 +51,19 @@ export type StyleValueInfo = {};export type StyleInfo = {- [property in StyleProperty]?: StyleValueInfo;+ [property in Exclude<StyleProperty, "color">]?: StyleValueInfo;+} & {+ /**+ * In order to maintain code efficiency and reduce clutter, we should only expose the currentColor value for the color property.+ * We can consider it an edge case when it is necessary to display the actual currentColor value in the Color Picker.+ */+ color?: StyleValueInfo & {+ /**+ * Only color property can have currentColor+ * Now we take it from the computed style, @todo: calculate when we are going to remove computed.+ **/+ currentColor?: StyleValue | undefined;+ };};export type StyleSource = "local" | "remote" | "preset" | "default";@@ -330,13 +342,24 @@ export const useStyleInfo = () => {const value =local ?? cascaded?.value ?? inherited?.value ?? preset ?? computed;if (value) {- styleInfoData[property] = {- value,- local,- cascaded,- inherited,- preset,- };+ if (property === "color") {+ styleInfoData[property] = {+ value,+ local,+ cascaded,+ inherited,+ preset,+ currentColor: computed,+ };+ } else {+ styleInfoData[property] = {+ value,+ local,+ cascaded,+ inherited,+ preset,+ };+ }}}return styleInfoData;diff --git a/packages/css-data/src/__generated__/keyword-values.ts b/packages/css-data/src/__generated__/keyword-values.tsindex 368f2d8ab1..5c82ed59e8 100644--- a/packages/css-data/src/__generated__/keyword-values.ts+++ b/packages/css-data/src/__generated__/keyword-values.ts@@ -151,7 +151,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -454,7 +454,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -650,7 +650,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -843,7 +843,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -1036,7 +1036,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -1229,7 +1229,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -1427,7 +1427,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -1620,7 +1620,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -1813,7 +1813,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -2006,7 +2006,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -2199,7 +2199,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -2392,7 +2392,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -2589,7 +2589,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -2812,7 +2812,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -3005,7 +3005,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -3190,7 +3190,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -3809,7 +3809,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -4063,7 +4063,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -4287,7 +4287,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -4488,7 +4488,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",@@ -4690,7 +4690,7 @@ export const keywordValues = {"whitesmoke","yellow","yellowgreen",- "currentcolor",+ "currentColor","ActiveBorder","ActiveCaption","AppWorkspace",