Skip to content

Commit aace88a

Browse files
committed
Fix updating color using color input
1 parent efbfa7b commit aace88a

2 files changed

Lines changed: 13 additions & 18 deletions

File tree

src/editor.svelte

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<script lang="ts">
2+
import type { Snippet } from "svelte";
23
import { titleCase } from "title-case";
34
import { noCase } from "change-case";
45
import type { HTMLAttributes } from "svelte/elements";
56
import type { SvelteSet } from "svelte/reactivity";
67
import { Plus, X } from "@lucide/svelte";
8+
import type { ChangeDetail } from "hdr-color-input";
79
import {
810
treeState,
911
resolveTokenValue,
@@ -25,7 +27,6 @@
2527
import GradientEditor from "./gradient-editor.svelte";
2628
import AliasToken from "./alias-token.svelte";
2729
import type { TreeNode } from "./store";
28-
import type { Snippet } from "svelte";
2930
3031
let {
3132
id,
@@ -523,19 +524,15 @@
523524
{/if}
524525

525526
{#if rawValue?.type === "color"}
527+
<!-- Store in separate derived to cache and avoid infinite cycle -->
528+
{@const colorValue = serializeColor(rawValue.value)}
526529
<div class="form-group">
527530
<!-- svelte-ignore a11y_label_has_associated_control -->
528531
<label class="a-label">Color</label>
529532
<color-input
530-
value={serializeColor(rawValue.value)}
531-
onopen={(event: InputEvent) => {
532-
// track both open and close because of bug in css-color-component
533-
const input = event.target as HTMLInputElement;
534-
updateMeta({ value: parseColor(input.value) });
535-
}}
536-
onclose={(event: InputEvent) => {
537-
const input = event.target as HTMLInputElement;
538-
updateMeta({ value: parseColor(input.value) });
533+
value={colorValue}
534+
onchange={(event: CustomEvent<ChangeDetail>) => {
535+
updateMeta({ value: parseColor(event.detail.value) });
539536
}}
540537
></color-input>
541538
</div>

src/gradient-editor.svelte

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import { Plus, X } from "@lucide/svelte";
3+
import type { ChangeDetail } from "hdr-color-input";
34
import { parseColor, serializeColor } from "./color";
45
import type { GradientValue, RawGradientValue } from "./schema";
56
import AliasToken from "./alias-token.svelte";
@@ -71,17 +72,14 @@
7172
<div class="gradient-stops-list">
7273
{#each value as stop, index (index)}
7374
{@const rawStop = rawValue[index]}
75+
<!-- Store in separate derived to cache and avoid infinite cycle -->
76+
{@const colorValue = serializeColor(stop.color)}
7477
<div class="gradient-stop-row">
7578
<div class="color-with-alias">
7679
<color-input
77-
value={serializeColor(stop.color)}
78-
onopen={(event: InputEvent) => {
79-
const input = event.target as HTMLInputElement;
80-
updateItem(index, { color: parseColor(input.value) });
81-
}}
82-
onclose={(event: InputEvent) => {
83-
const input = event.target as HTMLInputElement;
84-
updateItem(index, { color: parseColor(input.value) });
80+
value={colorValue}
81+
onchange={(event: CustomEvent<ChangeDetail>) => {
82+
updateItem(index, { color: parseColor(event.detail.value) });
8583
}}
8684
></color-input>
8785
<AliasToken

0 commit comments

Comments
 (0)