Skip to content

Color SwatchForm-associated

Baseline 2024 Widely available

Every feature it needs is Baseline widely available.

Works in Chrome 125+, Chrome Android 125+, Edge 125+, Firefox 126+, Firefox for Android 126+, Safari 17.4+, Safari on iOS 17.4+.

Why these versions:

  • :state() needs Chrome 125+, Chrome Android 125+, Edge 125+, Firefox 126+, Firefox for Android 126+, Safari 17.4+ and Safari on iOS 17.4+.
  • checkVisibility() needs Safari 17.4+ and Safari on iOS 17.4+. Inherited from sv-radio.

Partially supported:

  • Keyboard events (repeat): Chrome and Chrome Android ship it partially before 137. Inherited from sv-radio.
    Chrome before 137 misses a repeat only while several keys are held.

Optional features:

  • contrast-color(): Chrome 147+, Chrome Android 147+, Edge 147+, Firefox 146+, Firefox for Android 146+, Safari 26+, Safari on iOS 26+.
    The check mark that contrasts with the color. The check mark stays white.

Full browser support report

Depends on <sv-radio />

sv-color-swatch is a radio that shows a color, for picking one color out of a set of presets.

Usage

<div role="radiogroup" aria-label="Color">
<sv-color-swatch name="color" value="#2563eb" aria-label="Ocean" checked></sv-color-swatch>
<sv-color-swatch name="color" value="oklch(70% 0.1 80)" aria-label="Sand"></sv-color-swatch>
<sv-color-swatch name="color" value="#e11d48" aria-label="Rose"></sv-color-swatch>
</div>

value is the color, in any CSS color format. Swatches with the same name work like an sv-radio group, with the same keys and form behavior.

Since a swatch without a label shows no text, give each one an aria-label, and give the role="radiogroup" container a name too.

In a color group

Inside an sv-color-group, the swatches follow the group color:

  • Checking a swatch sets the group color, and the swatch's input and change bubble out of the group.
  • The swatch whose value equals the group color is checked.
  • Checking a swatch from code leaves the group color as it is.
  • The swatches submit nothing, since the group submits the color.

The group gives its color as a lowercase hex color, with 6 digits, or 8 when it is not opaque. To keep the swatches checked, give each one a value in that form, like #2563eb, because a swatch in another format sets the group color but unchecks right after the click.

<sv-color-group name="accent" value="#2563eb">
<sv-color-wheel aria-label="Color"></sv-color-wheel>
<div role="radiogroup" aria-label="Presets">
<sv-color-swatch name="preset" value="#2563eb" aria-label="Ocean"></sv-color-swatch>
<sv-color-swatch name="preset" value="#e11d48" aria-label="Rose"></sv-color-swatch>
</div>
</sv-color-group>

Structure

divcontainer
spanradio
slotcheck - Your check mark
svgcheck
slotdefault - Your label

API References

Methods

check()

Checks the radio and unchecks the rest of its group, skipping the animation when isInstant is true.

uncheck()

Unchecks the radio, skipping the animation when isInstant is true.

Properties

type

The type of the form element, always "radio". Setting it has no effect.

checked

Whether the radio is checked. Checking it unchecks the other radios in its group.

defaultChecked

The checked attribute, which sets the state that a form reset restores.

value

The value submitted with the form while the radio is checked, "on" without a value attribute.

name

Name of the radio group. Radios of the same tag with the same name and form are one group.

required

Whether one radio in the group must be checked. One required radio makes the whole group required.

disabled

Whether the radio's own disabled attribute is present.
A disabled ancestor fieldset also disables it.

Attributes

"disabled"

Prevents user interaction.

"checked"

Whether the radio is checked. Checking it unchecks the other radios in its group.

"name"

Name of the radio group. Radios of the same tag with the same name and form are one group.

"required"

Whether one radio in the group must be checked. One required radio makes the whole group required.

"value"

The value submitted with the form while the radio is checked, "on" without a value attribute.

Events

clicked

Fired before the user checks the radio with a click or a key. Canceling it keeps the group as it is.

checkedchange

Fired after checked changes, whether by the user or from code, including when another radio in the group takes over.

Slots

check

The mark shown while the swatch is checked. Since your own mark shows in every state, hide it outside :state(--checked).

Default

The radio label, which also names the radio for assistive technology.

CSS Properties

--sv-color-swatch-sz

The size of the swatch.

--sv-color-swatch-bdr-rad

The corner radius of the swatch.

--sv-color-swatch-bdr-clr

The color of the thin ring around the swatch.

--sv-color-swatch-check-clr

The color of the check mark, by default black or white, whichever contrasts more with the swatch color, and white with a shadow in browsers without contrast-color().

--sv-color-swatch-dur

The duration of the check animation.

--sv-color-swatch-ease

The easing function of the check animation.

--sv-radio-gap

The space between the radio ring and the label.

CSS Parts

::part(check)

The default check mark.

::part(container)

Wrapper of the ring and the label.

::part(radio)

The ring that marks the radio.

CSS States

:state(--checked)

The radio is checked.