Components › Components

On this page

Checkbox

A native checkbox with its label.

Accepted: off

Usage #

tscl
@import uiCheckbox from './checkbox.tscl'

<script>
  let terms = state(false)
</script>

<div class="demo">
  <uiCheckbox label="I accept the terms" [(checked)]="terms" />
  <p>
    Accepted: <span class="out">{{ terms ? 'on' : 'off' }}</span>
  </p>
</div>

<style scoped>
  :scope {
    display: grid;
    gap: 12px;
    justify-items: start;
    padding: 24px;
    border: 1px solid var(--ui-border);
    border-radius: var(--ui-radius);
    background: var(--ui-bg);
    color: var(--ui-fg);
  }
  p {
    margin: 0;
  }
</style>

Source #

Copy it into your components folder (next to the others) as checkbox.tscl. Its <style global> is the same as @clarusjs/ui's rules for this part, so using both looks the same.

tscl
{{-- Checkbox: a native checkbox with its label, bound two ways with [(checked)]. --}}
@import uiCheckbox from '@clarusjs/ui-primitives/checkbox.tscl'
@input label: string
@input name: string = ''
@input disabled: boolean = false
@model checked: boolean = false

<uiCheckbox [label]="label" [name]="name" [disabled]="disabled" [(checked)]="checked" />

<style global>
  /* The same rules as @clarusjs/ui's ui.css, for this part: change them here to change its look. */
  @layer theme, base, components, utilities;
  @layer components {
    /* Focus is always visible, on every part and what's inside it. */
    :where([data-part]):focus-visible,
    :where([data-part]) :focus-visible {
      outline: 2px solid var(--ui-ring);
      outline-offset: 2px;
    }

    /* Switch and checkbox: the native input, tinted; the label next to it. */
    :where([data-part='switch'], [data-part='checkbox']) {
      display: inline-flex;
      align-items: center;
      gap: 8px;
      font: var(--ui-font-size) var(--ui-font);
      color: var(--ui-fg);
      cursor: pointer;
    }
    :where([data-part='switch'], [data-part='checkbox']) input {
      accent-color: var(--ui-accent);
      width: 18px;
      height: 18px;
      margin: 0;
    }
    :where([data-part='switch']) input {
      appearance: none;
      width: 36px;
      height: 20px;
      border-radius: 999px;
      background: var(--ui-border);
      position: relative;
      transition: background 150ms;
    }
    :where([data-part='switch']) input::after {
      content: '';
      position: absolute;
      top: 2px;
      left: 2px;
      width: 16px;
      height: 16px;
      border-radius: 999px;
      background: var(--ui-bg);
      transition: transform 150ms;
    }
    :where([data-part='switch']) input:checked {
      background: var(--ui-accent);
    }
    :where([data-part='switch']) input:checked::after {
      transform: translateX(16px);
    }
    :where([data-part='switch'], [data-part='checkbox'])[data-disabled] {
      opacity: 0.55;
      cursor: not-allowed;
    }

    @media (prefers-reduced-motion: reduce) {
      :where([data-part]),
      :where([data-part]) * {
        transition: none !important;
      }
    }
  }
</style>

Styling #

Needs #