Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"pyodide": "^0.29.3",
"random-words": "^2.0.1",
"react": "^19.2.4",
"react-colorful": "^5.6.1",
"react-day-picker": "^9.13.2",
"react-dom": "^19.2.4",
"react-error-boundary": "^6.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type ChangeEvent, useEffect, useState } from "react";
import { ContentBlock } from "@/components/shared/ContextPanel/Blocks/ContentBlock";
import { KeyValueList } from "@/components/shared/ContextPanel/Blocks/KeyValueList";
import { CopyText } from "@/components/shared/CopyText/CopyText";
import { ColorPicker } from "@/components/ui/color";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { BlockStack, InlineStack } from "@/components/ui/layout";
Expand Down Expand Up @@ -199,8 +200,47 @@ const ColorEditor = ({
flexNode: FlexNodeData;
readOnly: boolean;
}) => {
const {
componentSpec,
currentSubgraphSpec,
currentSubgraphPath,
setComponentSpec,
} = useComponentSpec();

const { properties } = flexNode;

const [backgroundColor, setBackgroundColor] = useState(properties.color);

const handleBackgroundColorChange = (newColor: string) => {
setBackgroundColor(newColor);
saveColors(newColor);
};

const saveColors = (newBackgroundColor: string) => {
const updatedSubgraphSpec = updateFlexNodeInComponentSpec(
currentSubgraphSpec,
{
...flexNode,
properties: {
...properties,
color: newBackgroundColor,
},
},
);

const newRootSpec = updateSubgraphSpec(
componentSpec,
currentSubgraphPath,
updatedSubgraphSpec,
);

setComponentSpec(newRootSpec);
};
Comment on lines +219 to +238
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a repetative logic - we can extract it into a generic "updateFlexNode" or smth an re-use inside of this file


useEffect(() => {
setBackgroundColor(properties.color);
}, [properties]);

if (readOnly) {
return (
<KeyValueList
Expand All @@ -221,9 +261,10 @@ const ColorEditor = ({
<BlockStack gap="1">
<InlineStack gap="4" blockAlign="center">
<Paragraph size="xs">Background</Paragraph>
<div
className="aspect-square h-4 rounded-full border border-muted-foreground"
style={{ backgroundColor: properties.color }}
<ColorPicker
title="Background Color"
color={backgroundColor}
setColor={handleBackgroundColorChange}
/>
<CopyText className="text-xs font-mono">{properties.color}</CopyText>
</InlineStack>
Expand Down
137 changes: 137 additions & 0 deletions src/components/ui/color.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { useState } from "react";
import { HexColorPicker } from "react-colorful";

import { useDebouncedState } from "@/hooks/useDebouncedState";
import { cn } from "@/lib/utils";

import { Button } from "./button";
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "./collapsible";
import { Icon } from "./icon";
import { Input } from "./input";
import { BlockStack, InlineStack } from "./layout";
import { Popover, PopoverContent, PopoverTrigger } from "./popover";
import { Heading } from "./typography";

const PRESET_COLORS = [
"#FFF9C4",
"#C8E6C9",
"#BBDEFB",
"#D1C4E9",
"#FFE0B2",
"#EF9A9A",
"#FFCCBC",
"#D7CCC8",
"#F5F5F5",
"#CFD8DC",
"#B0BEC5",
"transparent",
];

interface ColorPickerProps {
title?: string;
color: string;
debounceMs?: number;
setColor: (color: string) => void;
onClose?: () => void;
}

export const ColorPicker = ({
color,
title,
debounceMs = 300,
setColor,
onClose,
}: ColorPickerProps) => {
const [open, setOpen] = useState(false);
const [localColor, setLocalColor] = useState(color);

const { clearDebounce, updatePreviousState } = useDebouncedState(
localColor,
setColor,
() => false,
{ debounceMs },
);

const handleOpenChange = (isOpen: boolean) => {
setOpen(isOpen);
if (!isOpen) {
onClose?.();
}
};

const handlePresetClick = (preset: string) => {
clearDebounce();
setLocalColor(preset);
setColor(preset);
updatePreviousState(preset);
};

return (
<Popover open={open} onOpenChange={handleOpenChange}>
<PopoverTrigger>
<div
className="aspect-square h-4 rounded-full border border-muted-foreground cursor-pointer"
style={{ backgroundColor: color }}
/>
</PopoverTrigger>
<PopoverContent className="w-fit">
<BlockStack gap="4" align="center">
<Heading level={3}>{title ?? "Pick a color"}</Heading>
<InlineStack gap="2" className="grid grid-cols-6 px-2">
{PRESET_COLORS.map((preset) => (
<div
key={preset}
className={cn(
"aspect-square w-6 rounded-sm border border-muted-foreground cursor-pointer relative overflow-hidden",
{
"ring-2 ring-offset-2 ring-black": preset === color,
},
)}
style={{ backgroundColor: preset }}
onClick={() => handlePresetClick(preset)}
>
{preset === "transparent" && (
<div className="absolute inset-0 flex items-center justify-center">
<div className="h-px w-full bg-red-500 rotate-45 origin-center" />
</div>
)}
</div>
))}
</InlineStack>
<div className="relative w-full">
<Input
value={localColor}
onChange={(e) => setLocalColor(e.target.value)}
/>
<Collapsible>
<InlineStack blockAlign="center" gap="1">
<CollapsibleTrigger asChild>
<Button
size="icon"
variant="ghost"
className="absolute right-2 top-0 hover:bg-transparent hover:text-black!"
style={{
color:
localColor === "transparent" ? "lightgray" : localColor,
filter: "brightness(0.8)",
}}
>
<Icon name="Palette" />
<span className="sr-only">Toggle</span>
</Button>
</CollapsibleTrigger>
</InlineStack>
<CollapsibleContent className="mt-2">
<HexColorPicker color={localColor} onChange={setLocalColor} />
</CollapsibleContent>
</Collapsible>
</div>
</BlockStack>
</PopoverContent>
</Popover>
);
};
Loading