Skip to content

AI-native design editor. Open-source Figma alternative.

Extract ColorInput component, fix alpha slider checkerboard

Danila Poyarkov2695cffe6a57
diff --git a/components.d.ts b/components.d.ts
index 06e1ede06..97cf61985 100644
--- a/components.d.ts
+++ b/components.d.ts
@@ -13,6 +13,7 @@ declare module 'vue' {
export interface GlobalComponents {
AppearanceSection: typeof import('./src/components/properties/AppearanceSection.vue')['default']
CanvasContextMenu: typeof import('./src/components/CanvasContextMenu.vue')['default']
+ ColorInput: typeof import('./src/components/ColorInput.vue')['default']
ColorPicker: typeof import('./src/components/ColorPicker.vue')['default']
EditorCanvas: typeof import('./src/components/EditorCanvas.vue')['default']
EffectsSection: typeof import('./src/components/properties/EffectsSection.vue')['default']
diff --git a/src/components/ColorInput.vue b/src/components/ColorInput.vue
new file mode 100644
index 000000000..bb499b1ef
--- /dev/null
+++ b/src/components/ColorInput.vue
@@ -0,0 +1,40 @@
+<script setup lang="ts">
+import ColorPicker from './ColorPicker.vue'
+import { colorToHexRaw, parseColor } from '@/engine/color'
+
+import type { Color } from '@/types'
+
+const props = withDefaults(
+ defineProps<{
+ color: Color
+ editable?: boolean
+ }>(),
+ { editable: false }
+)
+
+const emit = defineEmits<{
+ update: [color: Color]
+}>()
+
+function onHexChange(e: Event) {
+ const hex = (e.target as HTMLInputElement).value
+ const color = parseColor(hex.startsWith('#') ? hex : `#${hex}`)
+ emit('update', { ...color, a: props.color.a })
+}
+</script>
+
+<template>
+ <div class="flex items-center gap-1.5">
+ <ColorPicker :color="color" @update="emit('update', $event)" />
+ <input
+ v-if="editable"
+ class="min-w-0 flex-1 border-none bg-transparent font-mono text-xs text-surface outline-none"
+ :value="colorToHexRaw(color)"
+ maxlength="6"
+ @change="onHexChange"
+ />
+ <span v-else class="min-w-0 flex-1 truncate font-mono text-xs text-muted">
+ {{ colorToHexRaw(color) }}
+ </span>
+ </div>
+</template>
diff --git a/src/components/ColorPicker.vue b/src/components/ColorPicker.vue
index dd2fcb9f4..41a9760a3 100644
--- a/src/components/ColorPicker.vue
+++ b/src/components/ColorPicker.vue
@@ -178,7 +178,11 @@ const swatchColor = computed(() => {
</div>
<!-- Alpha slider -->
- <div class="mt-2">
+ <div class="alpha-wrap mt-2">
+ <div
+ class="alpha-gradient"
+ :style="{ background: `linear-gradient(to right, transparent, ${hueColor})` }"
+ />
<input
type="range"
class="alpha-slider"
@@ -238,8 +242,34 @@ const swatchColor = computed(() => {
);
}
+.alpha-wrap {
+ position: relative;
+ height: 12px;
+ border-radius: 6px;
+ background-image:
+ linear-gradient(45deg, #444 25%, transparent 25%),
+ linear-gradient(-45deg, #444 25%, transparent 25%),
+ linear-gradient(45deg, transparent 75%, #444 75%),
+ linear-gradient(-45deg, transparent 75%, #444 75%);
+ background-size: 8px 8px;
+ background-position:
+ 0 0,
+ 0 4px,
+ 4px -4px,
+ -4px 0;
+ background-color: #333;
+}
+
+.alpha-gradient {
+ position: absolute;
+ inset: 0;
+ border-radius: 6px;
+}
+
.alpha-slider {
- background: linear-gradient(to right, transparent, v-bind(hueColor));
+ position: absolute;
+ inset: 0;
+ background: transparent;
}
.hue-slider::-webkit-slider-thumb,
diff --git a/src/components/VariablesDialog.vue b/src/components/VariablesDialog.vue
index 919a48fc1..53556c9c7 100644
--- a/src/components/VariablesDialog.vue
+++ b/src/components/VariablesDialog.vue
@@ -18,6 +18,7 @@ import {
} from 'reka-ui'
import { useVueTable, getCoreRowModel, FlexRender, type ColumnDef } from '@tanstack/vue-table'
+import ColorInput from './ColorInput.vue'
import { colorToHexRaw, parseColor } from '@/engine/color'
import { useEditorStore } from '@/stores/editor'
import type { Variable, Color } from '@/engine/scene-graph'
@@ -82,23 +83,6 @@ function formatModeValue(variable: Variable, modeId: string): string {
return String(value)
}
-function getModeSwatchColor(variable: Variable, modeId: string): string | null {
- if (variable.type !== 'COLOR') return null
- const value = variable.valuesByMode[modeId]
- if (!value) return null
-
- if (typeof value === 'object' && 'aliasId' in value) {
- const resolved = store.graph.resolveColorVariable(variable.id)
- if (!resolved) return null
- return `rgb(${Math.round(resolved.r * 255)}, ${Math.round(resolved.g * 255)}, ${Math.round(resolved.b * 255)})`
- }
- if (typeof value === 'object' && 'r' in value) {
- const c = value as Color
- return `rgb(${Math.round(c.r * 255)}, ${Math.round(c.g * 255)}, ${Math.round(c.b * 255)})`
- }
- return null
-}
-
function shortName(variable: Variable): string {
const parts = variable.name.split('/')
return parts[parts.length - 1] ?? variable.name
@@ -111,6 +95,11 @@ function commitNameEdit(variable: Variable, newName: string) {
}
}
+function updateColorValue(variable: Variable, modeId: string, color: Color) {
+ variable.valuesByMode[modeId] = color
+ store.requestRender()
+}
+
function commitValueEdit(variable: Variable, modeId: string, newValue: string) {
if (variable.type === 'COLOR') {
const color = parseColor(newValue.startsWith('#') ? newValue : `#${newValue}`)
@@ -217,40 +206,33 @@ const columns = computed<ColumnDef<Variable>[]>(() => {
maxSize: 500,
cell: ({ row }) => {
const v = row.original
- const swatch = getModeSwatchColor(v, mode.modeId)
- const children = []
-
- if (swatch) {
- children.push(
- h('div', {
- class: 'size-5 shrink-0 rounded border border-border',
- style: { background: swatch }
- })
- )
+ const value = v.valuesByMode[mode.modeId]
+
+ if (v.type === 'COLOR' && value && typeof value === 'object' && 'r' in value) {
+ return h(ColorInput, {
+ color: value as Color,
+ onUpdate: (c: Color) => updateColorValue(v, mode.modeId, c)
+ })
}
- children.push(
- h(
- EditableRoot,
- {
- defaultValue: formatModeValue(v, mode.modeId),
- class: 'min-w-0 flex-1',
- onSubmit: (val: string) => commitValueEdit(v, mode.modeId, val)
- },
- () =>
- h(EditableArea, { class: 'flex' }, () => [
- h(EditablePreview, {
- class: 'min-w-0 flex-1 cursor-text truncate font-mono text-xs text-muted'
- }),
- h(EditableInput, {
- class:
- 'min-w-0 flex-1 rounded border border-border bg-surface/10 px-1 py-0.5 font-mono text-xs text-surface outline-none'
- })
- ])
- )
+ return h(
+ EditableRoot,
+ {
+ defaultValue: formatModeValue(v, mode.modeId),
+ class: 'min-w-0 flex-1',
+ onSubmit: (val: string) => commitValueEdit(v, mode.modeId, val)
+ },
+ () =>
+ h(EditableArea, { class: 'flex' }, () => [
+ h(EditablePreview, {
+ class: 'min-w-0 flex-1 cursor-text truncate font-mono text-xs text-muted'
+ }),
+ h(EditableInput, {
+ class:
+ 'min-w-0 flex-1 rounded border border-border bg-surface/10 px-1 py-0.5 font-mono text-xs text-surface outline-none'
+ })
+ ])
)
-
- return h('div', { class: 'flex items-center gap-2' }, children)
}
}))
diff --git a/src/components/properties/EffectsSection.vue b/src/components/properties/EffectsSection.vue
index 6da3f7bb8..23f0d4901 100644
--- a/src/components/properties/EffectsSection.vue
+++ b/src/components/properties/EffectsSection.vue
@@ -1,9 +1,8 @@
<script setup lang="ts">
import { ref } from 'vue'
-import ColorPicker from '@/components/ColorPicker.vue'
+import ColorInput from '@/components/ColorInput.vue'
import ScrubInput from '@/components/ScrubInput.vue'
import { useNodeProps } from '@/composables/use-node-props'
-import { colorToHexRaw, parseColor } from '@/engine/color'
import type { Color, Effect } from '@/engine/scene-graph'
@@ -50,15 +49,6 @@ function updateColor(index: number, color: Color) {
updateEffect(index, { color })
}
-function updateHex(index: number, hex: string) {
- const n = node.value
- if (!n) return
- const color = parseColor(hex.startsWith('#') ? hex : `#${hex}`)
- if (!color) return
- const existing = n.effects[index]
- updateColor(index, { ...color, a: existing.color.a })
-}
-
function updateColorOpacity(index: number, opacity: number) {
const n = node.value
if (!n) return
@@ -197,12 +187,7 @@ function toggleExpand(index: number) {
</div>
<div class="flex items-center gap-1.5">
- <ColorPicker :color="effect.color" @update="updateColor(i, $event)" />
- <input
- class="min-w-0 flex-1 rounded border border-border bg-input px-1.5 py-0.5 font-mono text-xs text-surface outline-none"
- :value="colorToHexRaw(effect.color)"
- @change="updateHex(i, ($event.target as HTMLInputElement).value)"
- />
+ <ColorInput :color="effect.color" editable @update="updateColor(i, $event)" />
<ScrubInput
class="w-14"
suffix="%"
diff --git a/src/components/properties/PageSection.vue b/src/components/properties/PageSection.vue
index 4c9ac27ab..f9d434ff1 100644
--- a/src/components/properties/PageSection.vue
+++ b/src/components/properties/PageSection.vue
@@ -1,7 +1,6 @@
<script setup lang="ts">
-import ColorPicker from '@/components/ColorPicker.vue'
+import ColorInput from '@/components/ColorInput.vue'
import { useEditorStore } from '@/stores/editor'
-import { colorToHexRaw, parseColor } from '@/engine/color'
import type { Color } from '@/types'
@@ -11,24 +10,11 @@ function updateColor(color: Color) {
store.state.pageColor = color
store.requestRender()
}
-
-function updateHex(hex: string) {
- const color = parseColor(hex.startsWith('#') ? hex : `#${hex}`)
- if (!color) return
- updateColor(color)
-}
</script>
<template>
<div class="border-b border-border px-3 py-2">
<label class="mb-1.5 block text-[11px] text-muted">Page</label>
- <div class="flex items-center gap-1.5">
- <ColorPicker :color="store.state.pageColor" @update="updateColor" />
- <input
- class="min-w-0 flex-1 border-none bg-transparent font-mono text-xs text-surface outline-none"
- :value="colorToHexRaw(store.state.pageColor)"
- @change="updateHex(($event.target as HTMLInputElement).value)"
- />
- </div>
+ <ColorInput :color="store.state.pageColor" editable @update="updateColor" />
</div>
</template>
diff --git a/src/components/properties/StrokeSection.vue b/src/components/properties/StrokeSection.vue
index b01fa7656..526fa4a2e 100644
--- a/src/components/properties/StrokeSection.vue
+++ b/src/components/properties/StrokeSection.vue
@@ -1,8 +1,7 @@
<script setup lang="ts">
-import ColorPicker from '@/components/ColorPicker.vue'
+import ColorInput from '@/components/ColorInput.vue'
import ScrubInput from '@/components/ScrubInput.vue'
import { useNodeProps } from '@/composables/use-node-props'
-import { colorToHexRaw, parseColor } from '@/engine/color'
import type { Color, Stroke } from '@/engine/scene-graph'
@@ -14,12 +13,6 @@ function updateColor(index: number, color: Color) {
store.updateNodeWithUndo(node.value.id, { strokes }, 'Change stroke')
}
-function updateHex(index: number, hex: string) {
- const color = parseColor(hex.startsWith('#') ? hex : `#${hex}`)
- if (!color) return
- updateColor(index, color)
-}
-
function updateWeight(index: number, weight: number) {
const strokes = [...node.value.strokes]
strokes[index] = { ...strokes[index], weight }
@@ -78,12 +71,7 @@ function remove(index: number) {
:key="i"
class="group flex items-center gap-1.5 py-0.5"
>
- <ColorPicker :color="stroke.color" @update="updateColor(i, $event)" />
- <input
- class="min-w-0 flex-1 border-none bg-transparent font-mono text-xs text-surface outline-none"
- :value="colorToHexRaw(stroke.color)"
- @change="updateHex(i, ($event.target as HTMLInputElement).value)"
- />
+ <ColorInput :color="stroke.color" editable @update="updateColor(i, $event)" />
<ScrubInput
class="w-12"
suffix="%"