Toggle
SwiftUI's Toggle, the iOS switch. Under a finger its thumb lifts into a glass droplet that can be dragged across or off the switch; the value changes when the finger lifts. Switch is the same component under its UIKit name.
import { Toggle } from "@adamexu/glass"; // or { Switch }Basic
With no label, name it with aria-label.
<Toggle defaultOn aria-label="Wi-Fi" />
<Toggle aria-label="Bluetooth" />Label, tint and state
Children are the label, drawn at the leading edge with the switch at the trailing one, as in a List row. labelsHidden keeps the label for screen readers only.
Wi-FiTintedDisabledisOn = true
const [wifi, setWifi] = useState(true);
<Toggle isOn={wifi} onChange={setWifi}>Wi-Fi</Toggle>
<Toggle defaultOn tint="#ff5a1f">Tinted</Toggle>
<Toggle disabled>Disabled</Toggle>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| isOn | boolean | - | SwiftUI isOn: a controlled value. Leave it out and the toggle keeps its own. |
| defaultOn | boolean | false | The initial value when uncontrolled. |
| onChange | (isOn: boolean) => void | - | Runs when the finger lifts, never during a drag. |
| tint | string | - | SwiftUI .tint(_:): any CSS colour, for the on state. |
| disabled | boolean | false | SwiftUI .disabled(_:): stops responding and dims. |
| children | ReactNode | - | The label, at the leading edge. |
| labelsHidden | boolean | false | SwiftUI .labelsHidden(): the label is only spoken. |
| idiom | "phone" | "pad" | "duo" | "phone" | The device whose feel to match. The look is the same; on iPad a drag has a gate before the thumb moves. |
| id, aria-label | string | - | Go on the role="switch" button. |
| className, style | string, CSSProperties | - | On the outer box. |
Behaviour
The toggle is a role="switch" button, so Space and Enter flip it. It is drawn for Chromium, where the lifted thumb refracts the track and whatever is behind it.
SwiftUI
| SwiftUI | @adamexu/glass |
|---|---|
| Toggle(isOn:) | <Toggle isOn onChange> |
| Toggle("Wi-Fi", isOn:) | <Toggle isOn onChange>Wi-Fi</Toggle> |
| .tint(.orange) | tint="orange" |
| .labelsHidden() | labelsHidden |
| .disabled(true) | disabled |