Liking cljdoc? Tell your friends :D

x-radio

A form-associated native Web Component that renders a radio button. Radios with the same name attribute form a mutually exclusive group: selecting one automatically deselects all others. Keyboard navigation follows the roving-tabindex pattern — Arrow keys move focus and selection within the group.

Tag name

<x-radio name="color" value="red">Red</x-radio>

Observed attributes

AttributeTypeDefaultDescription
checkedboolean presenceabsentSelected state
disabledboolean presenceabsentDisables interaction
readonlyboolean presenceabsentPrevents selection; does not affect form value
requiredboolean presenceabsentAt least one in the group must be checked
namestringGroups radios together (same name = same group)
valuestring"on"Value submitted with the form when checked
aria-labelstringAccessible label (use when no visible label is present)
aria-describedbystringID of description element
aria-labelledbystringID of label element

JS properties

All properties reflect to their corresponding attributes.

PropertyTypeReflects
checkedbooleanchecked
disabledbooleandisabled
readOnlybooleanreadonly
requiredbooleanrequired
namestringname
valuestringvalue

Events

x-radio-change-request

Fired before the state change. Cancelable — call event.preventDefault() to abort.

el.addEventListener('x-radio-change-request', (e) => {
  console.log(e.detail.value);           // string
  console.log(e.detail.previousChecked); // boolean
  console.log(e.detail.nextChecked);     // boolean
  // e.preventDefault() to cancel
});

x-radio-change

Fired after the state change. Not cancelable.

el.addEventListener('x-radio-change', (e) => {
  console.log(e.detail.value);   // string
  console.log(e.detail.checked); // boolean (always true)
});

Shadow DOM structure

<style>…</style>
<button part="control" role="radio" tabindex="0|−1">
  <span part="dot"></span>
</button>

CSS custom properties

PropertyDefault (light)Description
--x-radio-size16pxWidth and height of the radio circle
--x-radio-border-width2pxBorder thickness
--x-radio-border-color#6b7280Border color (unchecked)
--x-radio-bg#ffffffBackground color
--x-radio-checked-color#2563ebBorder and dot color when checked
--x-radio-dot-size6pxInner dot diameter
--x-radio-focus-ring#60a5faFocus ring color
--x-radio-disabled-opacity0.45Opacity when disabled
--x-radio-transitionbackground 120ms ease, border-color 120ms easeTransition for state changes

Dark mode overrides are applied automatically via @media (prefers-color-scheme: dark). Animations are suppressed when @media (prefers-reduced-motion: reduce) is active.

CSS parts

PartElementDescription
control<button>The circular radio control
dot<span>The inner filled dot shown when checked

Accessibility

  • role="radio" is set explicitly on [part=control].
  • aria-checked reflects the checked state ("true" / "false").
  • aria-disabled, aria-required, aria-readonly are kept in sync.
  • Roving tabindex: the checked radio in a group receives tabindex="0"; all others receive tabindex="-1". If no radio is checked, the first in DOM order receives tabindex="0".
  • Arrow keys move focus and selection within the group (wrapping).
  • Space / Enter select the focused radio.
  • The component is form-associated (formAssociated = true). It participates in form submission and formReset.

Usage examples

Basic group

<x-radio name="size" value="small" aria-label="Small"></x-radio>
<x-radio name="size" value="medium" aria-label="Medium" checked></x-radio>
<x-radio name="size" value="large" aria-label="Large"></x-radio>

Disabled

<x-radio name="plan" value="free" disabled aria-label="Free"></x-radio>
<x-radio name="plan" value="pro" aria-label="Pro"></x-radio>

Readonly

<x-radio name="status" value="active" readonly checked aria-label="Active"></x-radio>

Required

<x-radio name="agree" value="yes" required aria-label="I agree"></x-radio>

Custom styling

<style>
  x-radio {
    --x-radio-checked-color: #7c3aed;
    --x-radio-dot-size: 8px;
    --x-radio-size: 20px;
  }
</style>
<x-radio name="color" value="purple" checked aria-label="Purple"></x-radio>

JS interaction

import '@vanelsas/baredom/x-radio';

const radios = document.querySelectorAll('x-radio[name="color"]');

radios.forEach(r => {
  r.addEventListener('x-radio-change', (e) => {
    console.log('selected:', e.detail.value);
  });
});

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close