# Checkbox

SvCheckBox: a role-permissions block with a tri-state select-all parent (indeterminate when partial) plus a required, validated terms checkbox.

Uses `@svgrid/grid` (MIT). Category: Buttons & Toggles. Live demo: https://svgrid.com/demos/309-checkbox/

Install: `npm i @svgrid/grid` (Svelte 5).

## About this example

SvCheckBox in a Svelte 5 role-permissions block: a tri-state select-all parent that shows indeterminate when only some children are checked, child checkboxes per permission, and a required terms checkbox with validation that blocks submit until it is ticked. Controlled through checked, indeterminate and onChange.

SvCheckBox - a production permissions block with a tri-state "select all" parent (indeterminate when partial) plus a required terms checkbox with validation. Copy-paste ready.

## Imports, features and API used

- Imports: `@svgrid/grid`

## Frequently asked questions

### How does the select-all parent work?

Its checked is true when every child is on and its indeterminate is true when some are; onChange toggles every child at once. Both flags are $derived from the children's state.

### How is the terms checkbox validated?

It carries required and an error message; the form's submit checks the value and the checkbox shows the message until it is ticked.

### Does it work as a grid cell editor?

Yes. A column with editorType 'checkbox' mounts the same component in the cell.

## Related documentation

- [SvCheckBox](https://svgrid.com/docs/help/ui-components/sv-check-box.md)

## Related articles

- [A Tri-State Select-All Checkbox in SvGrid](https://svgrid.com/blog/tri-state-select-all/) - How SvGrid's rowSelectionFeature drives the header checkbox through unchecked, indeterminate, and checked states - and the pagination edge case that catches everyone.
- [SvGrid UI Components Tips and Tricks: Svelte 5 Buttons, Inputs, Tree, Forms](https://svgrid.com/blog/svgrid-ui-components-tips-and-tricks/) - Practical tips for the SvGrid UI component kit - SvButton states, a Cmd+K command palette, a virtualized searchable tree, schema-driven forms, a parsing date/time field, and the shared field contract - each with a code snippet.
- [An Actions Column (Edit, Delete) in SvGrid](https://svgrid.com/blog/actions-column-edit-delete/) - Build a per-row actions column with edit, delete, and duplicate buttons in SvGrid - handling data mutations, accessibility, and confirmation flows correctly.

## Source code (309-checkbox.svelte)

```svelte
<script lang="ts">
  /**
   * SvCheckBox - a production permissions block with a tri-state "select all"
   * parent (indeterminate when partial) plus a required terms checkbox with
   * validation. Copy-paste ready.
   */
  import { SvCheckBox } from '@svgrid/grid'

  const perms = ['Read', 'Write', 'Delete', 'Invite members']
  let checked = $state<boolean[]>([true, true, false, false])
  const allOn = $derived(checked.every(Boolean))
  const someOn = $derived(checked.some(Boolean) && !allOn)
  function toggleAll(v: boolean) { checked = checked.map(() => v) }

  let agree = $state(false)
  let submitted = $state(false)
  const agreeError = $derived(submitted && !agree ? 'You must accept the terms' : undefined)
</script>

<div class="wrap">
  <header>
    <h2>Checkbox</h2>
    <p>Tri-state (checked / unchecked / indeterminate) - a "select all" parent plus a validated terms box.</p>
  </header>

  <section class="block">
    <h3>Role permissions</h3>
    <div class="parent">
      <SvCheckBox checked={allOn} indeterminate={someOn} onChange={toggleAll}>All permissions</SvCheckBox>
    </div>
    <div class="children">
      {#each perms as p, i (p)}
        <SvCheckBox checked={checked[i]} onChange={(v) => (checked[i] = v)}>{p}</SvCheckBox>
      {/each}
    </div>
  </section>

  <section class="block">
    <h3>Terms</h3>
    <SvCheckBox checked={agree} required invalid={!!agreeError} error={agreeError} onChange={(v) => (agree = v)}>
      I agree to the terms of service
    </SvCheckBox>
    <div class="submit"><button class="btn" onclick={() => (submitted = true)}>Submit</button></div>
  </section>
</div>

<style>
  .wrap { padding: 20px; max-width: 460px; display: flex; flex-direction: column; gap: 22px; }
  header h2 { margin: 0 0 4px; font-size: 20px; font-weight: 700; }
  header p { margin: 0; color: var(--sg-muted, #64748b); font-size: 13.5px; line-height: 1.5; }
  .block h3 { margin: 0 0 10px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--sg-muted, #64748b); }
  .parent { padding-bottom: 8px; border-bottom: 1px solid var(--sg-border, #e2e8f0); font-weight: 600; }
  .children { display: flex; flex-direction: column; gap: 8px; padding: 10px 0 0 18px; }
  .submit { margin-top: 12px; }
  .btn { font: inherit; font-size: 13px; font-weight: 600; padding: 7px 16px; border: 0; border-radius: 8px; background: var(--sg-accent, #2563eb); color: var(--sg-on-accent, #fff); cursor: pointer; }
</style>
```

[View this example on GitHub](https://github.com/sv-grid/sv-grid/blob/main/examples/src/demos/309-checkbox.svelte)
