Reflex Logo

Intro

Gallery

Hosting

Learn

Components

Recipes

API Reference

Onboarding

Library

/

Chakra

/

Forms

/

Switch

The Switch component is used as an alternative for the Checkbox component. You can switch between enabled or disabled states.

Switch off!

class SwitchState1(rx.State):
    checked: bool = False
    is_checked: bool = "Switch off!"

    def change_check(self, checked: bool):
        self.checked = checked
        if self.checked:
            self.is_checked = "Switch on!"
        else:
            self.is_checked = "Switch off!"


def switch_example():
    return rx.chakra.vstack(
        rx.chakra.heading(SwitchState1.is_checked),
        rx.chakra.switch(
            is_checked=SwitchState1.checked,
            on_change=SwitchState1.change_check,
        ),
    )

You can also change the color scheme of the Switch component by passing the color_scheme argument. The default color scheme is blue.

rx.chakra.hstack(
    rx.chakra.switch(color_scheme="red"),
    rx.chakra.switch(color_scheme="green"),
    rx.chakra.switch(color_scheme="yellow"),
    rx.chakra.switch(color_scheme="blue"),
    rx.chakra.switch(color_scheme="purple"),
)

Toggleable switch component.

PropTypeDefault ValueValues
is_checked
bool
is_disabled
bool
is_focusable
bool
is_invalid
bool
is_read_only
bool
is_required
bool
name
str
value
str
Var.create(True)
spacing
str
placeholder
str
color_scheme
Literal

Event Triggers

TriggerDescription
on_change

Fired when the switch value changes

Did you find this useful?

HomeGalleryChangelogIntroductionHosting