suiss/uim · React Native Component
Toggle
A two-state button that can be turned on or off, for formatting and filter controls.
Toggle is an open-source mobile component from suiss/uim for React Native and Expo, with SwiftUI and Jetpack Compose versions built from the same design tokens. The @esuiss/uim CLI copies its source into your project, so you own the code.


import { TextBIcon } from "phosphor-react-native"
import * as React from "react"
import { View } from "react-native"
import { Toggle, ToggleIcon } from "@/components/ui/toggle"
type ToggleDemoProps = Omit<
React.ComponentProps<typeof Toggle>,
"pressed" | "onPressedChange"
> & {
initial?: boolean
}
function ToggleDemo({ initial = false, children, ...props }: ToggleDemoProps) {
const [pressed, setPressed] = React.useState(initial)
return (
<Toggle pressed={pressed} onPressedChange={setPressed} {...props}>
{children}
</Toggle>
)
}
export function ToggleExample() {
return (
<View className="flex-row">
<ToggleDemo accessibilityLabel="Toggle bold">
<ToggleIcon as={TextBIcon} />
</ToggleDemo>
</View>
)
}import EsuissUI
import PhosphorSwift
import SwiftUI
struct ToggleExample: View {
var body: some View {
ToggleDemo(label: "Toggle bold") { Ph.textB.regular }
}
}
struct ToggleDemo<Icon: View>: View {
let variant: ToggleVariant
let size: ToggleSize
let label: LocalizedStringKey
let icon: Icon
@State private var isOn: Bool
init(
initial: Bool = false,
variant: ToggleVariant = .default,
size: ToggleSize = .default,
label: LocalizedStringKey,
@ViewBuilder icon: () -> Icon
) {
self.variant = variant
self.size = size
self.label = label
self.icon = icon()
_isOn = State(initialValue: initial)
}
var body: some View {
Toggle(isOn: $isOn) {
icon.frame(width: 16, height: 16)
}
.toggleStyle(.variant(variant, size: size))
.accessibilityLabel(Text(label))
}
}import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.adamglin.PhosphorIcons
import com.adamglin.phosphoricons.Regular
import com.adamglin.phosphoricons.regular.TextB
import com.esuiss.ui.components.Icon
import com.esuiss.ui.components.Toggle
import com.esuiss.ui.components.ToggleSize
import com.esuiss.ui.components.ToggleVariant
@Composable
fun ToggleDemo(
label: String,
initial: Boolean = false,
variant: ToggleVariant = ToggleVariant.Default,
size: ToggleSize = ToggleSize.Default,
enabled: Boolean = true,
content: @Composable RowScope.() -> Unit,
) {
var pressed by rememberSaveable { mutableStateOf(initial) }
Toggle(
pressed,
{ pressed = it },
Modifier.semantics { contentDescription = label },
variant = variant,
size = size,
enabled = enabled,
content = content,
)
}
@Composable
fun ToggleRow(content: @Composable () -> Unit) {
Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically) {
content()
}
}
@Composable
fun ToggleBold(initial: Boolean = false) {
ToggleRow {
ToggleDemo("Toggle bold", initial = initial) {
Icon(PhosphorIcons.Regular.TextB, contentDescription = null)
}
}
}Toggle is a Pressable with an on and off state. When on, it shows a selected background and its text turns to the link color; it scales down slightly while pressed. It has two variants, default and outline, and three sizes.
ToggleIcon is an Icon sized for use inside a toggle.
Installation
$ pnpm dlx @esuiss/uim@latest add toggle$ pnpm dlx @esuiss/uim@latest add toggle --platform swift$ pnpm dlx @esuiss/uim@latest add toggle --platform kotlinThe CLI also adds what it builds on: Icon, Text.
Manual
- Install the dependencies:
npx expo install class-variance-authority cn-
Add the pieces it builds on: Icon, Text, and the primitives
components/primitives/toggle.tsx,components/primitives/use-render.tsx. -
Copy the source into
components/ui/toggle.tsx:
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import { Pressable } from "react-native"
import {
Toggle as TogglePrimitive,
type ToggleState,
} from "@/components/primitives/toggle"
import { resolveClassName } from "@/components/primitives/use-render"
import { Icon } from "@/components/ui/icon"
import { TextClassContext } from "@/components/ui/text"
const toggleVariants = cva(
"flex-row items-center justify-center gap-1 rounded-lg",
{
variants: {
variant: {- Update the import paths to match your project.
- Install the dependencies:
npx expo install class-variance-authority cn-
Add the pieces it builds on: Icon, Text, and the primitives
components/primitives/toggle.tsx,components/primitives/use-render.tsx. -
Copy the source into
components/ui/toggle.tsx:
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import { Pressable } from "react-native"
import {
Toggle as TogglePrimitive,
type ToggleState,
} from "@/components/primitives/toggle"
import { resolveClassName } from "@/components/primitives/use-render"
import { Icon } from "@/components/ui/icon"
import { TextClassContext } from "@/components/ui/text"
const toggleVariants = cva(
"flex-row items-center justify-center gap-1 rounded-lg",
{
variants: {
variant: {- Update the import paths to match your project.
-
Add the pieces it builds on: Icon, Text, and the primitives
components/primitives/toggle.tsx,components/primitives/use-render.tsx. -
Copy the source into
components/ui/toggle.tsx:
import type * as React from "react"
import {
Pressable,
type StyleProp,
StyleSheet,
type TextStyle,
type ViewStyle,
} from "react-native"
import {
Toggle as TogglePrimitive,
type ToggleState,
} from "@/components/primitives/toggle"
import { resolveStyle } from "@/components/primitives/use-render"
import { Icon } from "@/components/ui/icon"
import { TextStyleContext } from "@/components/ui/text"
import {- Update the import paths to match your project.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
.package(url: "https://github.com/phosphor-icons/swift", from: "2.1.0")- Copy the theme files every view uses:
import SwiftUI
import UIKit
struct RGBA: Sendable {
let red: CGFloat
let green: CGFloat
let blue: CGFloat
let alpha: CGFloat
init(_ hex: UInt32, _ alpha: CGFloat = 1) {
red = CGFloat((hex >> 16) & 0xFF) / 255
green = CGFloat((hex >> 8) & 0xFF) / 255
blue = CGFloat(hex & 0xFF) / 255
self.alpha = alpha
}import SwiftUI
extension Color {
public static let surface = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0x000000))
public static let surfaceSecondary = Color.adaptive(light: .init(0xF5F5F7), dark: .init(0x161617))
public static let surfaceTertiary = Color.adaptive(light: .init(0xE8E8ED), dark: .init(0x2C2C2E))
public static let surfaceRaised = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0x1D1D1F))
public static let label = Color.adaptive(light: .init(0x1D1D1F), dark: .init(0xF5F5F7))
public static let labelSecondary = Color.adaptive(light: .init(0x6E6E73), dark: .init(0xA1A1A6))
public static let labelTertiary = Color.adaptive(light: .init(0x86868B), dark: .init(0x86868B))
public static let labelQuaternary = Color.adaptive(light: .init(0xD2D2D7), dark: .init(0x424245))
public static let control = Color.adaptive(light: .init(0x000000, 0.04), dark: .init(0xFFFFFF, 0.1))
public static let controlPressed = Color.adaptive(light: .init(0x000000, 0.12), dark: .init(0xFFFFFF, 0.22))
public static let controlSelected = Color.adaptive(light: .init(0x1D1D1F), dark: .init(0xF5F5F7))
public static let controlDisabled = Color.adaptive(light: .init(0x000000, 0.03), dark: .init(0xFFFFFF, 0.05))
public static let onControlSelected = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0x000000))- Copy the source and every file it uses:
import SwiftUI
public enum ToggleVariant: CaseIterable, Sendable {
case `default`, outline
}
public enum ToggleSize: CaseIterable, Sendable {
case `default`, sm, lg
var height: CGFloat {
switch self {
case .default: 36
case .sm: 32
case .lg: 40
}
}import SwiftUI
public struct VariantButtonStyle: ButtonStyle {
public enum Variant: CaseIterable, Sendable {
case `default`, outline, secondary, tinted, ghost, plain, destructive, link
}
public enum Size: CaseIterable, Sendable {
case `default`, xs, sm, lg, xl, icon, iconXs, iconSm, iconLg, iconXl
var height: CGFloat {
switch self {
case .xs, .iconXs: 24
case .sm, .iconSm: 32
case .default, .icon: 36
case .lg, .iconLg: 44import SwiftUI
extension EnvironmentValues {
@Entry var buttonGroup: Axis? = nil
@Entry var buttonGroupSize: CGSize = .zero
}
enum ButtonGroupSpace: Hashable {
case group
}
struct ButtonGroupFrame {
let bounds: Anchor<CGRect>
let outlined: Bool
}import PhosphorSwift
import SwiftUI
extension Shadow {
static let control = Shadow(color: .black.opacity(0.08), radius: 0.5, y: 0.5)
static let thumb = Shadow(color: .black.opacity(0.12), radius: 1.5, y: 1)
}
struct SelectionIndicator<Mark: View>: View {
let checked: Bool
let pressed: Bool
let cornerRadius: CGFloat
let mark: Mark
@Environment(\.isEnabled) private var isEnabled
@Environment(\.isInvalid) private var invalidimport SwiftUI
extension EnvironmentValues {
@Entry public var isInvalid = false
@Entry var fieldInset = false
@Entry var fieldGroupInset = false
@Entry var fieldSetInset = false
}
extension View {
public func invalid(_ isInvalid: Bool = true) -> some View {
environment(\.isInvalid, isInvalid)
}
}
public enum FieldOrientation: CaseIterable, Sendable {import SwiftUI
extension EnvironmentValues {
@Entry public var hapticsEnabled = true
}
extension View {
public func haptics(_ enabled: Bool) -> some View {
environment(\.hapticsEnabled, enabled)
}
func hapticFeedback<Trigger: Equatable>(_ feedback: SensoryFeedback, trigger: Trigger) -> some View {
modifier(HapticFeedbackModifier(feedback: feedback, trigger: trigger) { _, _ in true })
}
func hapticFeedback<Trigger: Equatable>(import SwiftUI
public struct Separator: View {
let orientation: Axis
public init(_ orientation: Axis = .horizontal) {
self.orientation = orientation
}
public var body: some View {
Rectangle()
.fill(Color.separator)
.frame(width: orientation == .vertical ? 1 : nil, height: orientation == .horizontal ? 1 : nil)
.frame(maxWidth: orientation == .horizontal ? .infinity : nil, maxHeight: orientation == .vertical ? .infinity : nil)
.accessibilityHidden(true)
}import SwiftUI
public enum TextVariant: CaseIterable, Sendable {
case `default`, h1, h2, h3, h4, p, blockquote, code, lead, large, small, muted
var style: TextStyle {
switch self {
case .h1: .xl3
case .h2: .xl2
case .h3: .xl
case .h4, .lead, .large: .lg
case .code, .small, .muted: .sm
default: .base
}
}- Add the files to your app target.
- Set
minSdkto 24 or higher and add the dependencies:
implementation(platform("androidx.compose:compose-bom:2026.09.00"))
implementation("androidx.compose.foundation:foundation")
implementation("androidx.compose.ui:ui")
implementation("com.adamglin:phosphor-icon-android:1.0.0")- Copy the theme files every view uses:
package com.esuiss.ui.theme
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.dropShadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.shadow.Shadow as DropShadow
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.LineHeightStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpOffsetpackage com.esuiss.ui.theme
import androidx.compose.runtime.Immutable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
@Immutable
data class Colors(
val surface: Color,
val surfaceSecondary: Color,
val surfaceTertiary: Color,
val surfaceRaised: Color,
val label: Color,
val labelSecondary: Color,
val labelTertiary: Color,
val labelQuaternary: Color,- Copy the source and every file it uses:
package com.esuiss.ui.components
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.selection.toggleablepackage com.esuiss.ui.components
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.selection.triStateToggleable
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Colorpackage com.esuiss.ui.components
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
val LocalContentStyle = compositionLocalOf { TextStyle.Default }
val LocalContentColor = compositionLocalOf { Color.Unspecified }
val LocalIconSize = compositionLocalOf { 20.dp }package com.esuiss.ui.components
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.LayoutScopeMarker
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextpackage com.esuiss.ui.components
import android.provider.Settings
import androidx.compose.animation.core.CubicBezierEasing
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.text.selection.TextSelectionColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Sizepackage com.esuiss.ui.components
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.hapticfeedback.HapticFeedback
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalHapticFeedback
val LocalHapticsEnabled = staticCompositionLocalOf { true }
@Composable
fun ProvideHaptics(enabled: Boolean, content: @Composable () -> Unit) {
CompositionLocalProvider(LocalHapticsEnabled provides enabled, content = content)
}package com.esuiss.ui.components
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.Dp
import com.esuiss.ui.theme.EsuissTheme
@Composable
fun Icon(
imageVector: ImageVector,package com.esuiss.ui.components
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.esuiss.ui.theme.EsuissTheme
import com.esuiss.ui.theme.Typography
@Composablepackage com.esuiss.ui.components
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.esuiss.ui.theme.EsuissTheme
@Composable
fun Separator(- Update the
com.esuiss.uipackage names to match your project.
Usage
import { Toggle, ToggleIcon } from "@/components/ui/toggle"<View className="flex-row">
<ToggleDemo accessibilityLabel="Toggle bold">
<ToggleIcon as={TextBIcon} />
</ToggleDemo>
</View>Control the state with pressed and onPressedChange(pressed, details), or leave it uncontrolled with defaultPressed. Put text in a Text and icons in ToggleIcon.
Icon-only toggles need an accessibilityLabel. To group toggles with single or multiple selection, use Toggle Group. toggleVariants, toggleStateClassName and toggleTextClassName are exported for reuse in custom components.
Examples
Pressed
An icon toggle that starts in the on state.


import { TextBIcon } from "phosphor-react-native"
import * as React from "react"
import { View } from "react-native"
import { Toggle, ToggleIcon } from "@/components/ui/toggle"
type ToggleDemoProps = Omit<
React.ComponentProps<typeof Toggle>,
"pressed" | "onPressedChange"
> & {
initial?: boolean
}
function ToggleDemo({ initial = false, children, ...props }: ToggleDemoProps) {
const [pressed, setPressed] = React.useState(initial)
return (
<Toggle pressed={pressed} onPressedChange={setPressed} {...props}>
{children}
</Toggle>
)
}
export function TogglePressedExample() {
return (
<View className="flex-row">
<ToggleDemo initial accessibilityLabel="Toggle bold">
<ToggleIcon as={TextBIcon} />
</ToggleDemo>
</View>
)
}import EsuissUI
import PhosphorSwift
import SwiftUI
struct TogglePressedExample: View {
var body: some View {
ToggleDemo(initial: true, label: "Toggle bold") { Ph.textB.regular }
}
}
struct ToggleDemo<Icon: View>: View {
let variant: ToggleVariant
let size: ToggleSize
let label: LocalizedStringKey
let icon: Icon
@State private var isOn: Bool
init(
initial: Bool = false,
variant: ToggleVariant = .default,
size: ToggleSize = .default,
label: LocalizedStringKey,
@ViewBuilder icon: () -> Icon
) {
self.variant = variant
self.size = size
self.label = label
self.icon = icon()
_isOn = State(initialValue: initial)
}
var body: some View {
Toggle(isOn: $isOn) {
icon.frame(width: 16, height: 16)
}
.toggleStyle(.variant(variant, size: size))
.accessibilityLabel(Text(label))
}
}import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.adamglin.PhosphorIcons
import com.adamglin.phosphoricons.Regular
import com.adamglin.phosphoricons.regular.TextB
import com.esuiss.ui.components.Icon
import com.esuiss.ui.components.Toggle
import com.esuiss.ui.components.ToggleSize
import com.esuiss.ui.components.ToggleVariant
@Composable
fun TogglePressedExample() {
ToggleBold(initial = true)
}
@Composable
fun ToggleDemo(
label: String,
initial: Boolean = false,
variant: ToggleVariant = ToggleVariant.Default,
size: ToggleSize = ToggleSize.Default,
enabled: Boolean = true,
content: @Composable RowScope.() -> Unit,
) {
var pressed by rememberSaveable { mutableStateOf(initial) }
Toggle(
pressed,
{ pressed = it },
Modifier.semantics { contentDescription = label },
variant = variant,
size = size,
enabled = enabled,
content = content,
)
}
@Composable
fun ToggleRow(content: @Composable () -> Unit) {
Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically) {
content()
}
}
@Composable
fun ToggleBold(initial: Boolean = false) {
ToggleRow {
ToggleDemo("Toggle bold", initial = initial) {
Icon(PhosphorIcons.Regular.TextB, contentDescription = null)
}
}
}Variants
The default and outline variants.


import { TextItalicIcon } from "phosphor-react-native"
import * as React from "react"
import { View } from "react-native"
import { Toggle, ToggleIcon } from "@/components/ui/toggle"
type ToggleDemoProps = Omit<
React.ComponentProps<typeof Toggle>,
"pressed" | "onPressedChange"
> & {
initial?: boolean
}
function ToggleDemo({ initial = false, children, ...props }: ToggleDemoProps) {
const [pressed, setPressed] = React.useState(initial)
return (
<Toggle pressed={pressed} onPressedChange={setPressed} {...props}>
{children}
</Toggle>
)
}
export function ToggleVariantsExample() {
return (
<View className="flex-row gap-2">
<ToggleDemo accessibilityLabel="Toggle italic">
<ToggleIcon as={TextItalicIcon} />
</ToggleDemo>
<ToggleDemo variant="outline" accessibilityLabel="Toggle italic">
<ToggleIcon as={TextItalicIcon} />
</ToggleDemo>
</View>
)
}import EsuissUI
import PhosphorSwift
import SwiftUI
struct ToggleVariantsExample: View {
var body: some View {
HStack(spacing: 8) {
ToggleDemo(label: "Toggle italic") { Ph.textItalic.regular }
ToggleDemo(variant: .outline, label: "Toggle italic") { Ph.textItalic.regular }
}
}
}
struct ToggleDemo<Icon: View>: View {
let variant: ToggleVariant
let size: ToggleSize
let label: LocalizedStringKey
let icon: Icon
@State private var isOn: Bool
init(
initial: Bool = false,
variant: ToggleVariant = .default,
size: ToggleSize = .default,
label: LocalizedStringKey,
@ViewBuilder icon: () -> Icon
) {
self.variant = variant
self.size = size
self.label = label
self.icon = icon()
_isOn = State(initialValue: initial)
}
var body: some View {
Toggle(isOn: $isOn) {
icon.frame(width: 16, height: 16)
}
.toggleStyle(.variant(variant, size: size))
.accessibilityLabel(Text(label))
}
}import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.adamglin.PhosphorIcons
import com.adamglin.phosphoricons.Regular
import com.adamglin.phosphoricons.regular.TextItalic
import com.esuiss.ui.components.Icon
import com.esuiss.ui.components.Toggle
import com.esuiss.ui.components.ToggleSize
import com.esuiss.ui.components.ToggleVariant
@Composable
fun ToggleDemo(
label: String,
initial: Boolean = false,
variant: ToggleVariant = ToggleVariant.Default,
size: ToggleSize = ToggleSize.Default,
enabled: Boolean = true,
content: @Composable RowScope.() -> Unit,
) {
var pressed by rememberSaveable { mutableStateOf(initial) }
Toggle(
pressed,
{ pressed = it },
Modifier.semantics { contentDescription = label },
variant = variant,
size = size,
enabled = enabled,
content = content,
)
}
@Composable
fun ToggleRow(content: @Composable () -> Unit) {
Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically) {
content()
}
}
@Composable
fun ToggleVariants() {
ToggleRow {
ToggleDemo("Toggle italic") {
Icon(PhosphorIcons.Regular.TextItalic, contentDescription = null)
}
ToggleDemo("Toggle italic", variant = ToggleVariant.Outline) {
Icon(PhosphorIcons.Regular.TextItalic, contentDescription = null)
}
}
}Sizes
Outline toggles in the sm, default and lg sizes.


import { TextBIcon } from "phosphor-react-native"
import * as React from "react"
import { View } from "react-native"
import { Toggle, ToggleIcon } from "@/components/ui/toggle"
type ToggleDemoProps = Omit<
React.ComponentProps<typeof Toggle>,
"pressed" | "onPressedChange"
> & {
initial?: boolean
}
function ToggleDemo({ initial = false, children, ...props }: ToggleDemoProps) {
const [pressed, setPressed] = React.useState(initial)
return (
<Toggle pressed={pressed} onPressedChange={setPressed} {...props}>
{children}
</Toggle>
)
}
export function ToggleSizesExample() {
return (
<View className="flex-row items-center gap-2">
<ToggleDemo size="sm" variant="outline" accessibilityLabel="Small">
<ToggleIcon as={TextBIcon} />
</ToggleDemo>
<ToggleDemo variant="outline" accessibilityLabel="Default">
<ToggleIcon as={TextBIcon} />
</ToggleDemo>
<ToggleDemo size="lg" variant="outline" accessibilityLabel="Large">
<ToggleIcon as={TextBIcon} />
</ToggleDemo>
</View>
)
}import EsuissUI
import PhosphorSwift
import SwiftUI
struct ToggleSizesExample: View {
var body: some View {
HStack(spacing: 8) {
ToggleDemo(variant: .outline, size: .sm, label: "Small") { Ph.textB.regular }
ToggleDemo(variant: .outline, label: "Default") { Ph.textB.regular }
ToggleDemo(variant: .outline, size: .lg, label: "Large") { Ph.textB.regular }
}
}
}
struct ToggleDemo<Icon: View>: View {
let variant: ToggleVariant
let size: ToggleSize
let label: LocalizedStringKey
let icon: Icon
@State private var isOn: Bool
init(
initial: Bool = false,
variant: ToggleVariant = .default,
size: ToggleSize = .default,
label: LocalizedStringKey,
@ViewBuilder icon: () -> Icon
) {
self.variant = variant
self.size = size
self.label = label
self.icon = icon()
_isOn = State(initialValue: initial)
}
var body: some View {
Toggle(isOn: $isOn) {
icon.frame(width: 16, height: 16)
}
.toggleStyle(.variant(variant, size: size))
.accessibilityLabel(Text(label))
}
}import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.adamglin.PhosphorIcons
import com.adamglin.phosphoricons.Regular
import com.adamglin.phosphoricons.regular.TextB
import com.esuiss.ui.components.Icon
import com.esuiss.ui.components.Toggle
import com.esuiss.ui.components.ToggleSize
import com.esuiss.ui.components.ToggleVariant
@Composable
fun ToggleDemo(
label: String,
initial: Boolean = false,
variant: ToggleVariant = ToggleVariant.Default,
size: ToggleSize = ToggleSize.Default,
enabled: Boolean = true,
content: @Composable RowScope.() -> Unit,
) {
var pressed by rememberSaveable { mutableStateOf(initial) }
Toggle(
pressed,
{ pressed = it },
Modifier.semantics { contentDescription = label },
variant = variant,
size = size,
enabled = enabled,
content = content,
)
}
@Composable
fun ToggleRow(content: @Composable () -> Unit) {
Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically) {
content()
}
}
@Composable
fun ToggleSizes() {
ToggleRow {
listOf(ToggleSize.Sm to "Small", ToggleSize.Default to "Default", ToggleSize.Lg to "Large").forEach { (size, label) ->
key(label) {
ToggleDemo(label, variant = ToggleVariant.Outline, size = size) {
Icon(PhosphorIcons.Regular.TextB, contentDescription = null)
}
}
}
}
}With text
An outline toggle with an icon and a text label.


import { BookmarkSimpleIcon } from "phosphor-react-native"
import * as React from "react"
import { View } from "react-native"
import { Text } from "@/components/ui/text"
import { Toggle, ToggleIcon } from "@/components/ui/toggle"
type ToggleDemoProps = Omit<
React.ComponentProps<typeof Toggle>,
"pressed" | "onPressedChange"
> & {
initial?: boolean
}
function ToggleDemo({ initial = false, children, ...props }: ToggleDemoProps) {
const [pressed, setPressed] = React.useState(initial)
return (
<Toggle pressed={pressed} onPressedChange={setPressed} {...props}>
{children}
</Toggle>
)
}
export function ToggleWithTextExample() {
return (
<View className="flex-row">
<ToggleDemo variant="outline" accessibilityLabel="Bookmark">
<ToggleIcon as={BookmarkSimpleIcon} />
<Text>Bookmark</Text>
</ToggleDemo>
</View>
)
}import EsuissUI
import PhosphorSwift
import SwiftUI
struct WithTextDemo: View {
@State private var isOn = false
var body: some View {
Toggle(isOn: $isOn) {
Label {
Text("Bookmark")
} icon: {
Ph.bookmarkSimple.regular
}
}
.toggleStyle(.variant(.outline))
}
}import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.adamglin.PhosphorIcons
import com.adamglin.phosphoricons.Regular
import com.adamglin.phosphoricons.regular.BookmarkSimple
import com.esuiss.ui.components.Icon
import com.esuiss.ui.components.Text
import com.esuiss.ui.components.Toggle
import com.esuiss.ui.components.ToggleSize
import com.esuiss.ui.components.ToggleVariant
@Composable
fun ToggleDemo(
label: String,
initial: Boolean = false,
variant: ToggleVariant = ToggleVariant.Default,
size: ToggleSize = ToggleSize.Default,
enabled: Boolean = true,
content: @Composable RowScope.() -> Unit,
) {
var pressed by rememberSaveable { mutableStateOf(initial) }
Toggle(
pressed,
{ pressed = it },
Modifier.semantics { contentDescription = label },
variant = variant,
size = size,
enabled = enabled,
content = content,
)
}
@Composable
fun ToggleRow(content: @Composable () -> Unit) {
Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically) {
content()
}
}
@Composable
fun ToggleWithText() {
ToggleRow {
ToggleDemo("Bookmark", variant = ToggleVariant.Outline) {
Icon(PhosphorIcons.Regular.BookmarkSimple, contentDescription = null)
Text("Bookmark")
}
}
}Disabled
Disabled toggles in the off and on states.


import { TextBIcon, TextItalicIcon } from "phosphor-react-native"
import * as React from "react"
import { View } from "react-native"
import { Toggle, ToggleIcon } from "@/components/ui/toggle"
type ToggleDemoProps = Omit<
React.ComponentProps<typeof Toggle>,
"pressed" | "onPressedChange"
> & {
initial?: boolean
}
function ToggleDemo({ initial = false, children, ...props }: ToggleDemoProps) {
const [pressed, setPressed] = React.useState(initial)
return (
<Toggle pressed={pressed} onPressedChange={setPressed} {...props}>
{children}
</Toggle>
)
}
export function ToggleDisabledExample() {
return (
<View className="flex-row gap-2">
<ToggleDemo disabled accessibilityLabel="Toggle bold">
<ToggleIcon as={TextBIcon} />
</ToggleDemo>
<ToggleDemo
disabled
initial
variant="outline"
accessibilityLabel="Toggle italic"
>
<ToggleIcon as={TextItalicIcon} />
</ToggleDemo>
</View>
)
}import EsuissUI
import PhosphorSwift
import SwiftUI
struct ToggleDisabledExample: View {
var body: some View {
HStack(spacing: 8) {
ToggleDemo(label: "Toggle bold") { Ph.textB.regular }
ToggleDemo(initial: true, variant: .outline, label: "Toggle italic") { Ph.textItalic.regular }
}
.disabled(true)
}
}
struct ToggleDemo<Icon: View>: View {
let variant: ToggleVariant
let size: ToggleSize
let label: LocalizedStringKey
let icon: Icon
@State private var isOn: Bool
init(
initial: Bool = false,
variant: ToggleVariant = .default,
size: ToggleSize = .default,
label: LocalizedStringKey,
@ViewBuilder icon: () -> Icon
) {
self.variant = variant
self.size = size
self.label = label
self.icon = icon()
_isOn = State(initialValue: initial)
}
var body: some View {
Toggle(isOn: $isOn) {
icon.frame(width: 16, height: 16)
}
.toggleStyle(.variant(variant, size: size))
.accessibilityLabel(Text(label))
}
}import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.adamglin.PhosphorIcons
import com.adamglin.phosphoricons.Regular
import com.adamglin.phosphoricons.regular.TextB
import com.adamglin.phosphoricons.regular.TextItalic
import com.esuiss.ui.components.Icon
import com.esuiss.ui.components.Toggle
import com.esuiss.ui.components.ToggleSize
import com.esuiss.ui.components.ToggleVariant
@Composable
fun ToggleDemo(
label: String,
initial: Boolean = false,
variant: ToggleVariant = ToggleVariant.Default,
size: ToggleSize = ToggleSize.Default,
enabled: Boolean = true,
content: @Composable RowScope.() -> Unit,
) {
var pressed by rememberSaveable { mutableStateOf(initial) }
Toggle(
pressed,
{ pressed = it },
Modifier.semantics { contentDescription = label },
variant = variant,
size = size,
enabled = enabled,
content = content,
)
}
@Composable
fun ToggleRow(content: @Composable () -> Unit) {
Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically) {
content()
}
}
@Composable
fun ToggleDisabled() {
ToggleRow {
ToggleDemo("Toggle bold", enabled = false) {
Icon(PhosphorIcons.Regular.TextB, contentDescription = null)
}
ToggleDemo("Toggle italic", initial = true, variant = ToggleVariant.Outline, enabled = false) {
Icon(PhosphorIcons.Regular.TextItalic, contentDescription = null)
}
}
}API Reference
Toggle
The pressable two-state button.
React.ComponentProps<typeof TogglePrimitive>- About
- Visual style:
defaultoroutline. - Type
"default""outline"- Default
- "default"
- About
- Height and padding:
default,smorlg. - Type
"default""sm""lg"- Default
- "default"
ToggleIcon
An icon sized for a toggle.
React.ComponentProps<typeof Icon>Accessibility
- Uses the
togglebuttonrole and reportscheckedanddisabledinaccessibilityState. - Inside a single-select Toggle Group, it uses the
radiorole instead. - Icon-only toggles need an
accessibilityLabel.
Notes
- Changing the state triggers a selection haptic when a haptics adapter is provided through
HapticsContext. - Uses the
bg-item-selected,bg-item-pressed,bg-control-disabled,text-linkandborder-separatortokens. - Light and dark colors follow the system appearance.