Color Picker
A palette of swatches with a custom colour tile, for role and accent colours.
Use it where somebody gives a thing a colour: a role, a category, an embed’s accent. A short palette covers the common choice in one click, and the larger tile opens the browser’s own colour dialog for anything else. That tile wears the colour whenever the value is not one of the swatches.
The palette is data, not theme: these are values an application stores, so they are passed as a prop and are not --newt-* tokens.
newt-color-picker"use client"
import * as React from "react"
import { ColorPicker } from "@/registry/bases/newt/ui/color-picker"
import { RoleTag } from "@/registry/bases/newt/ui/role-tag"
export default function ColorPickerDemo() {
const [colour, setColour] = React.useState("#1abc9c")
return (
<div className="flex flex-col items-start gap-3">
<ColorPicker
label="Role colour"
value={colour}
onValueChange={setColour}
/>
<div className="flex items-center gap-2 text-xs text-newt-text-muted">
<RoleTag color={colour}>Moderator</RoleTag>
<span className="font-mono">{colour}</span>
</div>
</div>
)
}<script setup lang="ts">
import { ref } from "vue"
import { ColorPicker } from "~~/registry/bases/newt/ui/color-picker"
import { RoleTag } from "~~/registry/bases/newt/ui/role-tag"
const colour = ref("#1abc9c")
</script>
<template>
<div class="flex flex-col items-start gap-3">
<ColorPicker v-model="colour" label="Role colour" />
<div class="flex items-center gap-2 text-xs text-newt-text-muted">
<RoleTag :color="colour">Moderator</RoleTag>
<span class="font-mono">{{ colour }}</span>
</div>
</div>
</template>Installation
npx newtui@latest add color-pickernpx shadcn@latest add @newtui/color-pickernpx shadcn-vue@latest add @newtui/color-pickerpnpm dlx newtui@latest add color-pickerpnpm dlx shadcn@latest add @newtui/color-pickerpnpm dlx shadcn-vue@latest add @newtui/color-pickeryarn dlx newtui@latest add color-pickeryarn dlx shadcn@latest add @newtui/color-pickeryarn dlx shadcn-vue@latest add @newtui/color-pickerbunx newtui@latest add color-pickerbunx shadcn@latest add @newtui/color-pickerbunx shadcn-vue@latest add @newtui/color-pickerdeno run -A npm:newtui@latest add color-pickerdeno run -A npm:shadcn@latest add @newtui/color-pickerdeno run -A npm:shadcn-vue@latest add @newtui/color-pickervlx newtui@latest add color-pickervlx shadcn@latest add @newtui/color-pickervlx shadcn-vue@latest add @newtui/color-pickervp dlx newtui@latest add color-pickervp dlx shadcn@latest add @newtui/color-pickervp dlx shadcn-vue@latest add @newtui/color-pickernubx newtui@latest add color-pickernubx shadcn@latest add @newtui/color-pickernubx shadcn-vue@latest add @newtui/color-pickernlx newtui@latest add color-pickernlx shadcn@latest add @newtui/color-pickernlx shadcn-vue@latest add @newtui/color-pickerupx newtui@latest add color-pickerupx shadcn@latest add @newtui/color-pickerupx shadcn-vue@latest add @newtui/color-picker- newtui
- shadcn
- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
The shadcn CLI needs the @newtui registry in your components.json. Set it up.
Copy the source into your project
"use client"
import * as React from "react"
import { cn } from "@/lib/utils"
/** One entry of the palette. */
export interface ColorSwatch {
/** Any CSS colour; a hex string if the custom tile should round-trip it. */
value: string
/** What a screen reader calls it. */
label: string
}
/*
* A starting palette, not a token set: these are values a user picks and an
* application stores, so they are data rather than part of the theme.
*/
export const defaultSwatches: readonly ColorSwatch[] = [
{ value: "#5865f2", label: "Indigo" },
{ value: "#3ba55d", label: "Green" },
{ value: "#1abc9c", label: "Teal" },
{ value: "#3498db", label: "Blue" },
{ value: "#9b59b6", label: "Purple" },
{ value: "#eb459e", label: "Pink" },
{ value: "#f0b232", label: "Amber" },
{ value: "#e67e22", label: "Orange" },
{ value: "#ed4245", label: "Red" },
{ value: "#95a5a6", label: "Grey" },
]
export interface ColorPickerProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"onChange" | "defaultValue"
> {
/** Controlled colour. Leave undefined to let the component own it. */
value?: string
defaultValue?: string
onValueChange?: (value: string) => void
swatches?: readonly ColorSwatch[]
/** Show the tile that opens the browser's own colour dialog. */
allowCustom?: boolean
/** Accessible name for the group. */
label?: string
/** Accessible name for the custom tile. */
customLabel?: string
/** Shared `name` for the radios. One is generated when it is not given. */
name?: string
disabled?: boolean
}
const HEX = /^#[0-9a-f]{6}$/i
/*
* A short palette and one way out of it. The swatches are a real radio set,
* so the arrow keys move between them and a form posts the chosen value; the
* larger tile is the browser's own colour dialog for anything the palette
* does not hold, and it wears the colour whenever the value is not a swatch.
*/
const ColorPicker = React.forwardRef<HTMLDivElement, ColorPickerProps>(
(
{
className,
value,
defaultValue,
onValueChange,
swatches = defaultSwatches,
allowCustom = true,
label = "Colour",
customLabel = "Custom colour",
name,
disabled = false,
...props
},
ref
) => {
const generated = React.useId()
const [internal, setInternal] = React.useState(
defaultValue ?? swatches[0]?.value ?? ""
)
const isControlled = value !== undefined
const current = (isControlled ? value : internal).toLowerCase()
const isCustom = !swatches.some(
(swatch) => swatch.value.toLowerCase() === current
)
const commit = (next: string) => {
if (!isControlled) setInternal(next)
onValueChange?.(next)
}
return (
<div
ref={ref}
role="group"
aria-label={label}
className={cn(
"inline-flex items-stretch gap-2.5",
disabled && "pointer-events-none opacity-50",
className
)}
{...props}
>
{allowCustom ? (
<label
data-active={isCustom || undefined}
className="relative flex min-h-[54px] w-[66px] cursor-pointer items-start justify-end rounded-[var(--newt-radius-sm)] border border-newt-border bg-newt-bg-input p-1 text-newt-on-accent focus-within:outline focus-within:outline-2 focus-within:outline-offset-2 focus-within:outline-newt-text-link data-[active]:border-newt-text-primary data-[active]:ring-1 data-[active]:ring-newt-text-primary"
style={isCustom ? { backgroundColor: current } : undefined}
>
<input
type="color"
aria-label={customLabel}
disabled={disabled}
value={HEX.test(current) ? current : "#000000"}
onChange={(event) => commit(event.target.value)}
className="absolute inset-0 h-full w-full cursor-pointer opacity-0"
/>
<svg
viewBox="0 0 16 16"
aria-hidden="true"
className="h-3.5 w-3.5 drop-shadow-[0_0_1px_rgb(0_0_0/0.7)]"
>
<path
fill="currentColor"
d="M13.4 2.6a2 2 0 0 0-2.8 0L9 4.2l-.3-.3a1 1 0 0 0-1.4 1.4l.3.3-4.3 4.3a1 1 0 0 0-.3.7V13h2.4a1 1 0 0 0 .7-.3l4.3-4.3.3.3a1 1 0 0 0 1.4-1.4l-.3-.3 1.6-1.6a2 2 0 0 0 0-2.8ZM5 11.6H4.4V11L8.6 6.8l.6.6L5 11.6Z"
/>
</svg>
</label>
) : null}
<div className="grid grid-cols-[repeat(5,1.5rem)] content-center gap-1.5">
{swatches.map((swatch) => (
<label
key={swatch.value}
className="group relative block cursor-pointer"
>
<input
type="radio"
name={name ?? generated}
value={swatch.value}
disabled={disabled}
checked={swatch.value.toLowerCase() === current}
onChange={() => commit(swatch.value)}
className="peer sr-only"
/>
<span
aria-hidden="true"
className="flex h-6 w-6 items-center justify-center rounded-[var(--newt-radius-sm)] text-newt-on-accent transition-transform duration-fast ease-newt group-hover:scale-110 peer-focus-visible:outline peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-newt-text-link"
style={{ backgroundColor: swatch.value }}
>
<svg
viewBox="0 0 16 16"
className="h-4 w-4 scale-0 drop-shadow-[0_0_1px_rgb(0_0_0/0.7)] transition-transform duration-fast ease-newt group-has-[:checked]:scale-100"
>
<path
fill="currentColor"
d="M6.2 11.6 3.1 8.5a1 1 0 0 1 1.4-1.4l1.7 1.7 4.3-4.3a1 1 0 1 1 1.4 1.4l-5 5a1 1 0 0 1-1.4 0Z"
/>
</svg>
</span>
<span className="sr-only">{swatch.label}</span>
</label>
))}
</div>
</div>
)
}
)
ColorPicker.displayName = "ColorPicker"
export { ColorPicker }<script setup lang="ts">
import { computed, useId, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
import { defaultSwatches, type ColorSwatch } from "."
/*
* A short palette and one way out of it. The swatches are a real radio set,
* so the arrow keys move between them and a form posts the chosen value; the
* larger tile is the browser's own colour dialog for anything the palette
* does not hold, and it wears the colour whenever the value is not a swatch.
*/
const props = withDefaults(
defineProps<{
class?: HTMLAttributes["class"]
swatches?: readonly ColorSwatch[]
/** Show the tile that opens the browser's own colour dialog. */
allowCustom?: boolean
/** Accessible name for the group. */
label?: string
/** Accessible name for the custom tile. */
customLabel?: string
/** Shared `name` for the radios. One is generated when it is not given. */
name?: string
disabled?: boolean
}>(),
{
swatches: () => defaultSwatches,
allowCustom: true,
label: "Colour",
customLabel: "Custom colour",
disabled: false,
}
)
const model = defineModel<string>()
const HEX = /^#[0-9a-f]{6}$/i
const generated = useId()
const current = computed(() =>
(model.value ?? props.swatches[0]?.value ?? "").toLowerCase()
)
const isCustom = computed(
() =>
!props.swatches.some(
(swatch) => swatch.value.toLowerCase() === current.value
)
)
function onCustom(event: Event) {
if (event.target instanceof HTMLInputElement) model.value = event.target.value
}
</script>
<template>
<div
role="group"
:aria-label="props.label"
:class="
cn(
'inline-flex items-stretch gap-2.5',
props.disabled && 'pointer-events-none opacity-50',
props.class
)
"
>
<label
v-if="props.allowCustom"
:data-active="isCustom ? true : undefined"
class="relative flex min-h-[54px] w-[66px] cursor-pointer items-start justify-end rounded-[var(--newt-radius-sm)] border border-newt-border bg-newt-bg-input p-1 text-newt-on-accent focus-within:outline focus-within:outline-2 focus-within:outline-offset-2 focus-within:outline-newt-text-link data-[active]:border-newt-text-primary data-[active]:ring-1 data-[active]:ring-newt-text-primary"
:style="isCustom ? { backgroundColor: current } : undefined"
>
<input
type="color"
:aria-label="props.customLabel"
:disabled="props.disabled"
:value="HEX.test(current) ? current : '#000000'"
class="absolute inset-0 h-full w-full cursor-pointer opacity-0"
@input="onCustom"
/>
<svg
viewBox="0 0 16 16"
aria-hidden="true"
class="h-3.5 w-3.5 drop-shadow-[0_0_1px_rgb(0_0_0/0.7)]"
>
<path
fill="currentColor"
d="M13.4 2.6a2 2 0 0 0-2.8 0L9 4.2l-.3-.3a1 1 0 0 0-1.4 1.4l.3.3-4.3 4.3a1 1 0 0 0-.3.7V13h2.4a1 1 0 0 0 .7-.3l4.3-4.3.3.3a1 1 0 0 0 1.4-1.4l-.3-.3 1.6-1.6a2 2 0 0 0 0-2.8ZM5 11.6H4.4V11L8.6 6.8l.6.6L5 11.6Z"
/>
</svg>
</label>
<div class="grid grid-cols-[repeat(5,1.5rem)] content-center gap-1.5">
<label
v-for="swatch in props.swatches"
:key="swatch.value"
class="group relative block cursor-pointer"
>
<input
type="radio"
class="peer sr-only"
:name="props.name ?? generated"
:value="swatch.value"
:disabled="props.disabled"
:checked="swatch.value.toLowerCase() === current"
@change="model = swatch.value"
/>
<span
aria-hidden="true"
class="flex h-6 w-6 items-center justify-center rounded-[var(--newt-radius-sm)] text-newt-on-accent transition-transform duration-fast ease-newt group-hover:scale-110 peer-focus-visible:outline peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-newt-text-link"
:style="{ backgroundColor: swatch.value }"
>
<svg
viewBox="0 0 16 16"
class="h-4 w-4 scale-0 drop-shadow-[0_0_1px_rgb(0_0_0/0.7)] transition-transform duration-fast ease-newt group-has-[:checked]:scale-100"
>
<path
fill="currentColor"
d="M6.2 11.6 3.1 8.5a1 1 0 0 1 1.4-1.4l1.7 1.7 4.3-4.3a1 1 0 1 1 1.4 1.4l-5 5a1 1 0 0 1-1.4 0Z"
/>
</svg>
</span>
<span class="sr-only">{{ swatch.label }}</span>
</label>
</div>
</div>
</template>export { default as ColorPicker } from "./ColorPicker.vue"
/** One entry of the palette. */
export interface ColorSwatch {
/** Any CSS colour; a hex string if the custom tile should round-trip it. */
value: string
/** What a screen reader calls it. */
label: string
}
/*
* A starting palette, not a token set: these are values a user picks and an
* application stores, so they are data rather than part of the theme.
*/
export const defaultSwatches: readonly ColorSwatch[] = [
{ value: "#5865f2", label: "Indigo" },
{ value: "#3ba55d", label: "Green" },
{ value: "#1abc9c", label: "Teal" },
{ value: "#3498db", label: "Blue" },
{ value: "#9b59b6", label: "Purple" },
{ value: "#eb459e", label: "Pink" },
{ value: "#f0b232", label: "Amber" },
{ value: "#e67e22", label: "Orange" },
{ value: "#ed4245", label: "Red" },
{ value: "#95a5a6", label: "Grey" },
]Update the import paths
The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Color Picker to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Color Picker to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
Usage
import { ColorPicker } from "@/components/ui/color-picker"
export function Example() {
return <ColorPicker />
}<script setup lang="ts">
import { ColorPicker } from "@/components/ui/color-picker"
</script>
<template>
<ColorPicker />
</template>API reference
ColorPicker
value / v-model?string
The chosen colour. In React, leave it undefined and pass `defaultValue` to let the component own it.
stringonValueChange?(value: string) => void
React only. Called with the new colour; Vue emits `update:modelValue`.
(value: string) => voidswatches?{ value: string; label: string }[]
The palette. `label` is what a screen reader calls the swatch.
{ value: string; label: string }[]defaultSwatchesallowCustom?boolean
Shows the tile that opens the browser's colour dialog.
booleantruelabel?string
Accessible name of the group.
string"Colour"customLabel?string
Accessible name of the custom tile.
string"Custom colour"name?string
Shared `name` of the swatch radios. One is generated when it is not given.
stringdisabled?boolean
Dims the picker and stops it taking input.
booleanfalsedefaultSwatches is exported beside the component, so a palette can extend it instead of restating it.
Accessibility
The swatches are a native radio set: Tab reaches the chosen one and the arrow keys move between them. A colour is never its own name, so every swatch needs a label. The chosen swatch is marked with a check as well as by its position, and the check carries a dark edge so it stays visible on a pale swatch. Show the chosen value somewhere as text or on the thing being coloured, as the example does.
label, customLabel and each swatch’s label name the picker in your language. Translating labels lists every English default.
Tokens
Color Picker reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-color-picker.
--newt-radius-sm--newt-on-accent--newt-duration-fast--newt-ease--newt-text-link--newt-border--newt-bg-input--newt-text-primary