-
Notifications
You must be signed in to change notification settings - Fork 1
Extract color input UI into reusable ColorInput component #440
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,104 @@ | ||
| <script lang="ts"> | ||
| import { resolveColor, previewVersion } from '../../lib/previewResolver.svelte'; | ||
|
|
||
| let { | ||
| token, | ||
| value, | ||
| placeholder, | ||
| isOverridden, | ||
| onSet, | ||
| onReset, | ||
| }: { | ||
| token: string; | ||
| value: string; | ||
| placeholder?: string; | ||
| isOverridden: boolean; | ||
| onSet: (v: string) => void; | ||
| onReset: () => void; | ||
| } = $props(); | ||
|
|
||
| let editing = $state(false); | ||
| let cancelBlur = $state(false); | ||
|
|
||
| // Bare "--token" is a UI shorthand; normalize to "var(--token)" before resolving or storing. | ||
| function normalize(v: string): string { | ||
| const t = v.trim(); | ||
| return t.startsWith("--") && !t.startsWith("var(") ? `var(${t})` : t; | ||
| } | ||
|
|
||
| function paint(expr: string): string { | ||
| void previewVersion.value; | ||
| const norm = normalize(expr); | ||
| return resolveColor(norm) || norm || "transparent"; | ||
| } | ||
|
|
||
| let swatchColor = $derived(paint(value || `var(${token})`)); | ||
|
|
||
| // Detect if the current value is a CSS variable reference (can't use native picker) | ||
| let isVar = $derived(value.trim().startsWith("var(") || value.trim().startsWith("--")); | ||
|
|
||
| // Convert a resolved rgb(...) or a raw hex string to a #RRGGBB seed for the native picker. | ||
| function toHex(color: string): string { | ||
| // Pass through hex colors directly (3- or 6-digit) | ||
| const hex6 = color.match(/^#([0-9a-fA-F]{6})$/); | ||
| if (hex6) return color.toLowerCase(); | ||
| const hex3 = color.match(/^#([0-9a-fA-F]{3})$/); | ||
| if (hex3) return "#" + hex3[1].split("").map(c => c + c).join(""); | ||
| // Parse computed rgb()/rgba() strings from resolveColor | ||
| const m = color.match(/rgb\w*\((\d+)[,\s]+(\d+)[,\s]+(\d+)/); | ||
| if (m) return "#" + [m[1], m[2], m[3]].map(n => parseInt(n).toString(16).padStart(2, "0")).join(""); | ||
| return "#6366f1"; | ||
| } | ||
| </script> | ||
|
|
||
| <div class="flex items-center gap-2"> | ||
| <!-- Swatch / native picker trigger --> | ||
| <div class="relative shrink-0 w-7 h-7 rounded border border-white/10 overflow-hidden cursor-pointer"> | ||
| <div class="absolute inset-0" style={`background: ${swatchColor}`}></div> | ||
| {#if !isVar} | ||
| <input | ||
| type="color" | ||
| value={toHex(swatchColor)} | ||
| oninput={(e) => onSet((e.target as HTMLInputElement).value)} | ||
| class="absolute inset-0 w-full h-full opacity-0 cursor-pointer" | ||
| tabindex="-1" | ||
| /> | ||
| {/if} | ||
| </div> | ||
|
|
||
| <!-- Text display / editable input --> | ||
| {#if editing} | ||
| <input | ||
| value={value} | ||
| autofocus | ||
| onblur={(e) => { | ||
| if (cancelBlur) { cancelBlur = false; editing = false; return; } | ||
| const v = normalize((e.target as HTMLInputElement).value); | ||
| if (!v) onReset(); | ||
| else onSet(v); | ||
| editing = false; | ||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
| }} | ||
| onkeydown={(e) => { | ||
| if (e.key === "Enter") (e.currentTarget as HTMLInputElement).blur(); | ||
| if (e.key === "Escape") { cancelBlur = true; editing = false; } | ||
| }} | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| placeholder={placeholder ?? "default"} | ||
| class="flex-1 bg-white/8 border border-indigo-500/50 rounded px-1.5 py-0.5 text-[10px] font-mono text-slate-200 focus:outline-none" | ||
| /> | ||
| {:else} | ||
| <button | ||
| onclick={() => { editing = true; }} | ||
| class="flex-1 text-left text-[9px] font-mono text-slate-500 hover:text-slate-200 truncate cursor-pointer transition-colors" | ||
| > | ||
| {#if isOverridden} | ||
| <span class="text-indigo-300">{value}</span> | ||
| {:else} | ||
| {placeholder ?? "default"} | ||
| {/if} | ||
| </button> | ||
| {/if} | ||
|
|
||
| {#if isOverridden} | ||
| <button onclick={onReset} class="text-[8px] text-slate-500 hover:text-rose-400 cursor-pointer shrink-0">reset</button> | ||
| {/if} | ||
| </div> | ||
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
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
Oops, something went wrong.
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.