Docs Components Forms

Switch

React example follows below.
ts
import { Switch } from "@grassroot/ui-react";
import * as React from "react";
const switchFixture = {
id: "switch",
label: "Deploy on merge",
description: "Ship prod when CI passes on main",
checked: true,
} satisfies {
id: string;
label: string;
description: string;
checked: boolean;
};
export default function SwitchDemo() {
const [on, setOn] = React.useState<boolean>(switchFixture.checked);
return (
<div>
<Switch
label={switchFixture.label}
description={switchFixture.description}
checked={on}
onChange={(e) => setOn(e.target.checked)}
/>
</div>
);
}
ts
<script setup lang="ts">
import { ref } from "vue";
import { Switch } from "@grassroot/ui-vue";
const switchFixture = {
id: "switch",
label: "Deploy on merge",
description: "Ship prod when CI passes on main",
checked: true,
} satisfies {
id: string;
label: string;
description: string;
checked: boolean;
};
const on = ref(switchFixture.checked);
function onChange(event: Event) {
on.value = (event.target as HTMLInputElement).checked;
}
</script>
<template>
<Switch :checked="on" @change="onChange">
<template #label>{{ switchFixture.label }}</template>
<template #description>{{ switchFixture.description }}</template>
</Switch>
</template>
ts
import { Component, signal } from "@angular/core";
import { GrassrootSwitch } from "@grassroot/ui-angular";
const switchFixture = {
id: "switch",
label: "Deploy on merge",
description: "Ship prod when CI passes on main",
checked: true,
} satisfies {
id: string;
label: string;
description: string;
checked: boolean;
};
@Component({
selector: "app-switch-demo",
standalone: true,
imports: [GrassrootSwitch],
template: `
<grassroot-switch
[checked]="on()"
[label]="fixture.label"
[description]="fixture.description"
(checkedChange)="on.set($event.checked)"
/>
`,
})
export class SwitchDemoComponent {
protected readonly fixture = switchFixture;
protected readonly on = signal<boolean>(switchFixture.checked);
}
export default SwitchDemoComponent;
ts
<script lang="ts">
import { Switch } from "@grassroot/ui-svelte";
const switchFixture = {
id: "switch",
label: "Deploy on merge",
description: "Ship prod when CI passes on main",
checked: true,
} satisfies {
id: string;
label: string;
description: string;
checked: boolean;
};
let on = $state(switchFixture.checked);
function onChange(event: Event) {
on = (event.target as HTMLInputElement).checked;
}
</script>
<Switch
label={switchFixture.label}
description={switchFixture.description}
checked={on}
{onChange}
/>
ts
/** @jsxImportSource solid-js */
import { createSignal } from "solid-js";
import { Switch } from "@grassroot/ui-solid";
const switchFixture = {
id: "switch",
label: "Deploy on merge",
description: "Ship prod when CI passes on main",
checked: true,
} satisfies {
id: string;
label: string;
description: string;
checked: boolean;
};
export default function SwitchDemo() {
const [on, setOn] = createSignal<boolean>(switchFixture.checked);
return (
<>
<Switch
label={switchFixture.label}
description={switchFixture.description}
checked={on()}
onChange={(e) => setOn((e.target as HTMLInputElement).checked)}
/>
</>
);
}

Installation

API Reference

Import

import { Switch } from "@grassroot/ui-react";
import * as React from "react";

Props

Prop
Type
Default
Requirement
Description
checked
boolean | undefined
optional
defaultChecked
boolean | undefined
optional
disabled
boolean | undefined
optional
readOnly
boolean | undefined
optional
name
string | undefined
optional
value
string | undefined
optional
required
boolean | undefined
optional
id
string | undefined
optional
Seeds the part scope, so the hidden input/label/description ids are all deterministic from it [baseline #10].
tone
Tone | undefined
"accent"
optional
Brand colour of the "on" track.
onCheckedChange
((detail: SwitchChangeDetail) => void) | undefined
optional
onChange
React.ChangeEventHandler<HTMLInputElement, Element> | undefined
optional
"aria-label"
string | undefined
optional
Accessible name when neither `label` nor `description` is rendered.
className
string | undefined
optional
Prop
checked
Type
boolean | undefined
Default
Requirement
optional
Description
Prop
defaultChecked
Type
boolean | undefined
Default
Requirement
optional
Description
Prop
disabled
Type
boolean | undefined
Default
Requirement
optional
Description
Prop
readOnly
Type
boolean | undefined
Default
Requirement
optional
Description
Prop
name
Type
string | undefined
Default
Requirement
optional
Description
Prop
value
Type
string | undefined
Default
Requirement
optional
Description
Prop
required
Type
boolean | undefined
Default
Requirement
optional
Description
Prop
id
Type
string | undefined
Default
Requirement
optional
Description
Seeds the part scope, so the hidden input/label/description ids are all deterministic from it [baseline #10].
Prop
tone
Type
Tone | undefined
Default
"accent"
Requirement
optional
Description
Brand colour of the "on" track.
Prop
onCheckedChange
Type
((detail: SwitchChangeDetail) => void) | undefined
Default
Requirement
optional
Description
Prop
onChange
Type
React.ChangeEventHandler<HTMLInputElement, Element> | undefined
Default
Requirement
optional
Description
Prop
"aria-label"
Type
string | undefined
Default
Requirement
optional
Description
Accessible name when neither `label` nor `description` is rendered.
Prop
className
Type
string | undefined
Default
Requirement
optional
Description

Events

EventPayload
onCheckedChange((detail: SwitchChangeDetail) => void) | undefined

Content regions

  • children
  • description
  • label

Native attributes

Standard DOM and ARIA attributes not listed above spread onto the root element.

Import

import { ref } from "vue";
import { Switch } from "@grassroot/ui-vue";

Props

Prop
Type
Default
Requirement
checked
boolean | undefined
undefined
optional
defaultChecked
boolean | undefined
undefined
optional
disabled
boolean | undefined
undefined
optional
readOnly
boolean | undefined
undefined
optional
name
string | undefined
undefined
optional
value
string | undefined
undefined
optional
required
boolean | undefined
undefined
optional
id
string | undefined
undefined
optional
tone
Tone
"accent"
optional
Prop
checked
Type
boolean | undefined
Default
undefined
Requirement
optional
Prop
defaultChecked
Type
boolean | undefined
Default
undefined
Requirement
optional
Prop
disabled
Type
boolean | undefined
Default
undefined
Requirement
optional
Prop
readOnly
Type
boolean | undefined
Default
undefined
Requirement
optional
Prop
name
Type
string | undefined
Default
undefined
Requirement
optional
Prop
value
Type
string | undefined
Default
undefined
Requirement
optional
Prop
required
Type
boolean | undefined
Default
undefined
Requirement
optional
Prop
id
Type
string | undefined
Default
undefined
Requirement
optional
Prop
tone
Type
Tone
Default
"accent"
Requirement
optional

Events

EventPayload
checked-change(_detail: SwitchChangeDetail) => true
change(_event: Event) => true

Content regions

  • default
  • description
  • label

Import

import { Component, signal } from "@angular/core";
import { GrassrootSwitch } from "@grassroot/ui-angular";

Props

Prop
Type
Default
Requirement
checked
boolean
optional
defaultChecked
boolean
optional
disabled
boolean
optional
readOnly
boolean
optional
name
string
optional
value
string
optional
required
boolean
optional
id
string
optional
label
string
optional
description
string
optional
tone
Tone
"accent"
optional
Prop
checked
Type
boolean
Default
Requirement
optional
Prop
defaultChecked
Type
boolean
Default
Requirement
optional
Prop
disabled
Type
boolean
Default
Requirement
optional
Prop
readOnly
Type
boolean
Default
Requirement
optional
Prop
name
Type
string
Default
Requirement
optional
Prop
value
Type
string
Default
Requirement
optional
Prop
required
Type
boolean
Default
Requirement
optional
Prop
id
Type
string
Default
Requirement
optional
Prop
label
Type
string
Default
Requirement
optional
Prop
description
Type
string
Default
Requirement
optional
Prop
tone
Type
Tone
Default
"accent"
Requirement
optional

Events

EventPayload
checkedChangeSwitchChangeDetail
changeEvent

Content regions

  • default
  • description
  • label

Import

import { Switch } from "@grassroot/ui-svelte";

Props

Prop
Type
Default
Requirement
checked
boolean
optional
defaultChecked
boolean
optional
disabled
boolean
optional
readOnly
boolean
optional
name
string
optional
value
string
optional
required
boolean
optional
id
string
optional
tone
Tone
"accent"
optional
className
string
optional
onCheckedChange
(detail: SwitchChangeDetail) => void
optional
onChange
(event: Event) => void
optional
Prop
checked
Type
boolean
Default
Requirement
optional
Prop
defaultChecked
Type
boolean
Default
Requirement
optional
Prop
disabled
Type
boolean
Default
Requirement
optional
Prop
readOnly
Type
boolean
Default
Requirement
optional
Prop
name
Type
string
Default
Requirement
optional
Prop
value
Type
string
Default
Requirement
optional
Prop
required
Type
boolean
Default
Requirement
optional
Prop
id
Type
string
Default
Requirement
optional
Prop
tone
Type
Tone
Default
"accent"
Requirement
optional
Prop
className
Type
string
Default
Requirement
optional
Prop
onCheckedChange
Type
(detail: SwitchChangeDetail) => void
Default
Requirement
optional
Prop
onChange
Type
(event: Event) => void
Default
Requirement
optional

Events

EventPayload
onCheckedChange(detail: SwitchChangeDetail) => void
onChange(event: Event) => void

Content regions

  • children
  • description
  • label

Import

import { createSignal } from "solid-js";
import { Switch } from "@grassroot/ui-solid";

Props

Prop
Type
Default
Requirement
checked
boolean
optional
defaultChecked
boolean
optional
disabled
boolean
optional
readOnly
boolean
optional
name
string
optional
value
string
optional
required
boolean
optional
id
string
optional
tone
Tone
"accent"
optional
onCheckedChange
(detail: SwitchChangeDetail) => void
optional
onChange
(event: Event) => void
optional
className
string
optional
Prop
checked
Type
boolean
Default
Requirement
optional
Prop
defaultChecked
Type
boolean
Default
Requirement
optional
Prop
disabled
Type
boolean
Default
Requirement
optional
Prop
readOnly
Type
boolean
Default
Requirement
optional
Prop
name
Type
string
Default
Requirement
optional
Prop
value
Type
string
Default
Requirement
optional
Prop
required
Type
boolean
Default
Requirement
optional
Prop
id
Type
string
Default
Requirement
optional
Prop
tone
Type
Tone
Default
"accent"
Requirement
optional
Prop
onCheckedChange
Type
(detail: SwitchChangeDetail) => void
Default
Requirement
optional
Prop
onChange
Type
(event: Event) => void
Default
Requirement
optional
Prop
className
Type
string
Default
Requirement
optional

Events

EventPayload
onCheckedChange(detail: SwitchChangeDetail) => void
onChange(event: Event) => void

Content regions

  • children
  • description
  • label