Skip to content

Add OKLAB and OKLCH to color-picker #2446

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions examples/next-ts/hooks/use-controls.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable jsx-a11y/no-onchange */
import { ControlRecord, deepGet, deepSet, getControlDefaults } from "@zag-js/shared"
import { useState } from "react"

Expand All @@ -18,7 +17,7 @@ export function useControls<T extends ControlRecord>(config: T) {
ui: () => (
<div className="controls-container">
{Object.keys(config).map((key) => {
const { type, label = key, options, placeholder, min, max } = (config[key] ?? {}) as any
const { type, label = key, options, placeholder, min, max, forceValue } = (config[key] ?? {}) as any
const value = deepGet(state, key)
switch (type) {
case "boolean":
Expand Down Expand Up @@ -67,7 +66,7 @@ export function useControls<T extends ControlRecord>(config: T) {
setState(key, e.target.value)
}}
>
<option>-----</option>
{!forceValue && <option>-----</option>}
{options.map((option) => (
<option key={option} value={option}>
{option}
Expand Down
101 changes: 86 additions & 15 deletions examples/next-ts/pages/color-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { normalizeProps, useMachine } from "@zag-js/react"
import { colorPickerControls } from "@zag-js/shared"
import serialize from "form-serialize"
import { useId } from "react"
import { Show } from "../components/show"
import { StateVisualizer } from "../components/state-visualizer"
import { Toolbar } from "../components/toolbar"
import { useControls } from "../hooks/use-controls"
Expand All @@ -30,13 +29,10 @@ export default function Page() {
const service = useMachine(colorPicker.machine, {
id: useId(),
name: "color",
format: "hsla",
defaultValue: colorPicker.parse("hsl(0, 100%, 50%)"),
...controls.context,
})

const api = colorPicker.connect(service, normalizeProps)

return (
<>
<main className="color-picker">
Expand Down Expand Up @@ -67,44 +63,119 @@ export default function Page() {
<div {...api.getAreaBackgroundProps()} />
<div {...api.getAreaThumbProps()} />
</div>

<div {...api.getChannelSliderProps({ channel: "hue" })}>
<div {...api.getChannelSliderTrackProps({ channel: "hue" })} />
<div {...api.getChannelSliderThumbProps({ channel: "hue" })} />
</div>
{api.format.startsWith("rgb") && (
<>
<div {...api.getChannelSliderProps({ channel: "red" })}>
<div {...api.getChannelSliderTrackProps({ channel: "red" })} />
<div {...api.getChannelSliderThumbProps({ channel: "red" })} />
</div>
<div {...api.getChannelSliderProps({ channel: "green" })}>
<div {...api.getChannelSliderTrackProps({ channel: "green" })} />
<div {...api.getChannelSliderThumbProps({ channel: "green" })} />
</div>
<div {...api.getChannelSliderProps({ channel: "blue" })}>
<div {...api.getChannelSliderTrackProps({ channel: "blue" })} />
<div {...api.getChannelSliderThumbProps({ channel: "blue" })} />
</div>
</>
)}
{api.format.startsWith("okl") && (
<div {...api.getChannelSliderProps({ channel: "lightness" })}>
<div {...api.getChannelSliderTrackProps({ channel: "lightness" })} />
<div {...api.getChannelSliderThumbProps({ channel: "lightness" })} />
</div>
)}
{api.format === "oklab" && (
<>
<div {...api.getChannelSliderProps({ channel: "a" })}>
<div {...api.getChannelSliderTrackProps({ channel: "a" })} />
<div {...api.getChannelSliderThumbProps({ channel: "a" })} />
</div>
<div {...api.getChannelSliderProps({ channel: "b" })}>
<div {...api.getChannelSliderTrackProps({ channel: "b" })} />
<div {...api.getChannelSliderThumbProps({ channel: "b" })} />
</div>
</>
)}
{api.format === "oklch" && (
<div {...api.getChannelSliderProps({ channel: "chroma" })}>
<div {...api.getChannelSliderTrackProps({ channel: "chroma" })} />
<div {...api.getChannelSliderThumbProps({ channel: "chroma" })} />
</div>
)}
{api.format !== "rgba" && api.format !== "oklab" && (
<div {...api.getChannelSliderProps({ channel: "hue" })}>
<div {...api.getChannelSliderTrackProps({ channel: "hue" })} />
<div {...api.getChannelSliderThumbProps({ channel: "hue" })} />
</div>
)}
{api.format.includes("s") && (
<div {...api.getChannelSliderProps({ channel: "saturation" })}>
<div {...api.getChannelSliderTrackProps({ channel: "saturation" })} />
<div {...api.getChannelSliderThumbProps({ channel: "saturation" })} />
</div>
)}
{api.format.startsWith("hsl") && (
<div {...api.getChannelSliderProps({ channel: "lightness" })}>
<div {...api.getChannelSliderTrackProps({ channel: "lightness" })} />
<div {...api.getChannelSliderThumbProps({ channel: "lightness" })} />
</div>
)}
{api.format.startsWith("hsb") && (
<div {...api.getChannelSliderProps({ channel: "brightness" })}>
<div {...api.getChannelSliderTrackProps({ channel: "brightness" })} />
<div {...api.getChannelSliderThumbProps({ channel: "brightness" })} />
</div>
)}

<div {...api.getChannelSliderProps({ channel: "alpha" })}>
<div {...api.getTransparencyGridProps({ size: "12px" })} />
<div {...api.getChannelSliderTrackProps({ channel: "alpha" })} />
<div {...api.getChannelSliderThumbProps({ channel: "alpha" })} />
</div>

<Show when={api.format.startsWith("hsl")}>
{api.format.startsWith("hsl") && (
<div style={{ display: "flex", width: "100%" }}>
<span>H</span> <input {...api.getChannelInputProps({ channel: "hue" })} />
<span>S</span> <input {...api.getChannelInputProps({ channel: "saturation" })} />
<span>L</span> <input {...api.getChannelInputProps({ channel: "lightness" })} />
<span>A</span> <input {...api.getChannelInputProps({ channel: "alpha" })} />
</div>
</Show>
)}

<Show when={api.format.startsWith("rgb")}>
{api.format.startsWith("rgb") && (
<div style={{ display: "flex", width: "100%" }}>
<span>R</span> <input {...api.getChannelInputProps({ channel: "red" })} />
<span>G</span> <input {...api.getChannelInputProps({ channel: "green" })} />
<span>B</span> <input {...api.getChannelInputProps({ channel: "blue" })} />
<span>A</span> <input {...api.getChannelInputProps({ channel: "alpha" })} />
</div>
</Show>
)}

<Show when={api.format.startsWith("hsb")}>
{api.format.startsWith("hsb") && (
<div style={{ display: "flex", width: "100%" }}>
<span>H</span> <input {...api.getChannelInputProps({ channel: "hue" })} />
<span>S</span> <input {...api.getChannelInputProps({ channel: "saturation" })} />
<span>B</span> <input {...api.getChannelInputProps({ channel: "brightness" })} />
<span>A</span> <input {...api.getChannelInputProps({ channel: "alpha" })} />
</div>
</Show>
)}
{api.format === "oklab" && (
<div style={{ display: "flex", width: "100%" }}>
<span>L</span> <input {...api.getChannelInputProps({ channel: "lightness" })} />
<span>A</span> <input {...api.getChannelInputProps({ channel: "a" })} />
<span>B</span> <input {...api.getChannelInputProps({ channel: "b" })} />
<span>Alpha</span> <input {...api.getChannelInputProps({ channel: "alpha" })} />
</div>
)}
{api.format === "oklch" && (
<div style={{ display: "flex", width: "100%" }}>
<span>L</span> <input {...api.getChannelInputProps({ channel: "lightness" })} />
<span>C</span> <input {...api.getChannelInputProps({ channel: "chroma" })} />
<span>H</span> <input {...api.getChannelInputProps({ channel: "hue" })} />
<span>Alpha</span> <input {...api.getChannelInputProps({ channel: "alpha" })} />
</div>
)}

<div style={{ display: "flex", gap: "10px", alignItems: "center" }}>
<div style={{ position: "relative" }}>
Expand Down
63 changes: 18 additions & 45 deletions examples/nuxt-ts/components/Controls.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,38 @@ defineProps<{ control: any }>()
<div v-for="(value, key) in control.config" :key="key">
<template v-if="value.type === 'boolean'">
<div class="checkbox">
<input
:data-testid="key"
:id="value.label || key"
type="checkbox"
:checked="control.getState(key)"
@input="
(e) => {
control.setState(key, (e.target as HTMLInputElement).checked)
}
"
/>
<input :data-testid="key" :id="value.label || key" type="checkbox" :checked="control.getState(key)" @input="
(e) => {
control.setState(key, (e.target as HTMLInputElement).checked)
}
" />
<label :for="value.label || key">{{ value.label || key }}</label>
</div>
</template>
<template v-if="value.type === 'string'">
<div class="text">
<label style="margin-right: 10px">{{ value.label || key }}</label>
<input
:data-testid="key"
type="text"
:placeholder="value.placeholder"
:value="control.getState(key)"
<input :data-testid="key" type="text" :placeholder="value.placeholder" :value="control.getState(key)"
@keydown.enter="
(event) => {
control.setState(key, (event.target as HTMLInputElement).value)
}
"
/>
" />
</div>
</template>
<template v-if="value.type === 'select'">
<div class="text">
<label :for="value.label || key" style="margin-right: 10px">
{{ value.label || key }}
</label>
<select
:data-testid="key"
:id="value.label || key"
:value="control.getState(key)"
@change="
(e) => {
control.setState(key, (e.target as HTMLSelectElement).value)
}
"
>
<option>-----</option>
<option
v-for="option in value.options"
:key="option"
:value="option"
:selected="control.getState(key) === option"
>
<select :data-testid="key" :id="value.label || key" :value="control.getState(key)" @change="
(e) => {
control.setState(key, (e.target as HTMLSelectElement).value)
}
">
<option v-if="!value.forceValue">-----</option>
<option v-for="option in value.options" :key="option" :value="option"
:selected="control.getState(key) === option">
{{ option }}
</option>
</select>
Expand All @@ -69,20 +49,13 @@ defineProps<{ control: any }>()
<label :for="value.label || key" style="margin-right: 10px">
{{ value.label || key }}
</label>
<input
:data-testid="key"
:id="value.label || key"
type="number"
:min="value.min"
:max="value.max"
:value="control.getState(key)"
@keydown.enter="
<input :data-testid="key" :id="value.label || key" type="number" :min="value.min" :max="value.max"
:value="control.getState(key)" @keydown.enter="
(e) => {
const val = parseFloat((e.target as HTMLInputElement).value)
control.setState(key, isNaN(val) ? 0 : val)
}
"
/>
" />
</div>
</template>
</div>
Expand Down
Loading