Shopify Polaris
JS behaviour
Color Picker
A saturation rectangle, hue slider and alpha bar in one compact panel with an editable hex field — picking product variant colors the Shopify-native way.
Real scenarios
The component as it is used in a product — not a specimen grid.
Drag the rectangle, own the hex
Drag inside the saturation field (or paste a hex — with or without alpha) and every control follows: the hue slider, the checkerboarded alpha bar, the preview swatch and the readouts.
Preset swatches & the compact panel
The swatches really load into the picker above; the compact panel drops the alpha bar for tight variant editors.
#00A97F
no alpha
Important props
| Prop | Type | Default | What it does |
|---|---|---|---|
| color | hsv | '{h:120,s:60,v:80}' | Polaris models colour as HSV; the rectangle drags s and v, the slider h. |
| alpha | slider | '0–1' | The alpha bar slides over a transparency checkerboard; dropping it densifies the panel. |
| hex | input | '#008060' | The editable hex field; typing or pasting moves every control at once. |
Snippet
<div class="plcp" x-data="{ h: 160, s: 100, v: 50, a: 1 }">
<div class="plcp-area" x-on:pointerdown="pick($event)">
<span class="plcp-thumb" x-bind:style="`left:${s}%;top:${100 - v}%`"></span>
</div>
<input type="range" min="0" max="360" x-model.number="h">
<input class="plcp-hex" x-model="hex">
</div>
<style>
.plcp { display: grid; gap: 12px; padding: 12px; border-radius: 12px;
background: #FFF; box-shadow: 0 1px 4px #00000012; }
.plcp-area { aspect-ratio: 16/9; border-radius: 8px; touch-action: none;
background: linear-gradient(to top, #000, transparent),
linear-gradient(to right, #FFF, hsl(var(--hue) 100% 50%)); }
.plcp-thumb { position: absolute; inline-size: 18px; aspect-ratio: 1;
border-radius: 50%; border: 2px solid #FFF; translate: -50% -50%; }
</style>
// resources/js/Pages/Products/Variant.tsx — the picker feeds a
// variant form field; Inertia submits the whole form on save.
import { Head } from '@inertiajs/react';
import ColorPicker from '@/components/polaris/ColorPicker';
export default function ProductVariant() {
return (
<>
<Head title="Edit variant" />
{/* bind the hex into <input type="hidden" name="color"> */}
<ColorPicker />
</>
);
}
import { useState } from 'react';
import '@nabuxai/ui-core/css';
export default function ColorPicker() {
const [h, setH] = useState(162);
const [sv, setSv] = useState({ s: 92, v: 58 });
const hue = `hsl(${Math.round(h)} 100% 50%)`;
const pick = (e: React.PointerEvent<HTMLDivElement>) => {
const r = e.currentTarget.getBoundingClientRect();
const clamp = (n: number) => Math.min(100, Math.max(0, n));
setSv({
s: clamp(((e.clientX - r.left) / r.width) * 100),
v: clamp(100 - ((e.clientY - r.top) / r.height) * 100),
});
};
return (
<div className="plcp">
<div
className="plcp-area" style={{ backgroundColor: hue }} role="slider" aria-label="Saturation and brightness"
onPointerDown={(e) => { e.currentTarget.setPointerCapture(e.pointerId); pick(e); }}
onPointerMove={(e) => e.currentTarget.hasPointerCapture(e.pointerId) && pick(e)}
>
<span className="plcp-thumb" style={{ left: `${sv.s}%`, top: `${100 - sv.v}%`, background: hue }} />
</div>
<input type="range" min={0} max={360} value={h} onChange={(e) => setH(Number(e.target.value))} aria-label="Hue" />
<style>{`
.plcp { display: grid; gap: 12px; padding: 12px; border-radius: 12px;
background: #FFF; box-shadow: 0 1px 4px #00000012; }
.plcp-area { position: relative; aspect-ratio: 16/9; border-radius: 8px; touch-action: none;
background-image: linear-gradient(to top, #000, transparent),
linear-gradient(to right, #FFF, transparent); }
.plcp-thumb { position: absolute; inline-size: 18px; aspect-ratio: 1; border-radius: 50%;
border: 2px solid #FFF; translate: -50% -50%; }
`}</style>
</div>
);
}
<!-- ColorPicker.vue -->
<script setup lang="ts">
import { ref, computed } from 'vue';
import '@nabuxai/ui-core/css';
const h = ref(162);
const sv = ref({ s: 92, v: 58 });
const hue = computed(() => `hsl(${Math.round(h.value)} 100% 50%)`);
function pick(e: PointerEvent) {
const r = (e.currentTarget as HTMLElement).getBoundingClientRect();
const clamp = (n: number) => Math.min(100, Math.max(0, n));
sv.value = {
s: clamp(((e.clientX - r.left) / r.width) * 100),
v: clamp(100 - ((e.clientY - r.top) / r.height) * 100),
};
}
</script>
<template>
<div class="plcp">
<div
class="plcp-area" :style="{ backgroundColor: hue }" role="slider" aria-label="Saturation and brightness"
@pointerdown="(e: PointerEvent) => { (e.currentTarget as HTMLElement).setPointerCapture(e.pointerId); pick(e); }"
@pointermove="(e: PointerEvent) => (e.currentTarget as HTMLElement).hasPointerCapture(e.pointerId) && pick(e)"
>
<span class="plcp-thumb" :style="{ left: sv.s + '%', top: 100 - sv.v + '%', background: hue }" />
</div>
<input v-model.number="h" type="range" min="0" max="360" aria-label="Hue" />
</div>
</template>
<style scoped>
.plcp { display: grid; gap: 12px; padding: 12px; border-radius: 12px;
background: #fff; box-shadow: 0 1px 4px #00000012; }
.plcp-area { position: relative; aspect-ratio: 16/9; border-radius: 8px; touch-action: none;
background-image: linear-gradient(to top, #000, transparent),
linear-gradient(to right, #fff, transparent); }
.plcp-thumb { position: absolute; inline-size: 18px; aspect-ratio: 1; border-radius: 50%;
border: 2px solid #fff; translate: -50% -50%; }
</style>
<!-- ColorPicker.svelte -->
<script lang="ts">
import '@nabuxai/ui-core/css';
let h = $state(162);
let sv = $state({ s: 92, v: 58 });
const hue = $derived(`hsl(${Math.round(h)} 100% 50%)`);
const pick = (e: PointerEvent) => {
const r = (e.currentTarget as HTMLElement).getBoundingClientRect();
const clamp = (n: number) => Math.min(100, Math.max(0, n));
sv = {
s: clamp(((e.clientX - r.left) / r.width) * 100),
v: clamp(100 - ((e.clientY - r.top) / r.height) * 100),
};
};
</script>
<div class="plcp">
<div
class="plcp-area" style:background-color={hue} role="slider" aria-label="Saturation and brightness"
onpointerdown={(e: PointerEvent) => { (e.currentTarget as HTMLElement).setPointerCapture(e.pointerId); pick(e); }}
onpointermove={(e: PointerEvent) => (e.currentTarget as HTMLElement).hasPointerCapture(e.pointerId) && pick(e)}
>
<span class="plcp-thumb" style:left="{sv.s}%" style:top="{100 - sv.v}%" style:background={hue}></span>
</div>
<input type="range" min="0" max="360" bind:value={h} aria-label="Hue" />
</div>
<style>
.plcp { display: grid; gap: 12px; padding: 12px; border-radius: 12px;
background: #fff; box-shadow: 0 1px 4px #00000012; }
.plcp-area { position: relative; aspect-ratio: 16/9; border-radius: 8px; touch-action: none;
background-image: linear-gradient(to top, #000, transparent),
linear-gradient(to right, #fff, transparent); }
.plcp-thumb { position: absolute; inline-size: 18px; aspect-ratio: 1; border-radius: 50%;
border: 2px solid #fff; translate: -50% -50%; }
</style>