Switch

Style-agnostic toggle switch component using CSS custom properties.

python 7 params

Preview

name
checked
python
switch(name='notifications', label='Enable notifications', checked=True, action='toggle_switch')

Usage

views.py
python
from djust import LiveView
from djust.decorators import event_handler
from djust.components import Switch


class MyView(LiveView):
    template_name = "my_template.html"

    def mount(self, request, **kwargs):
        self.component = Switch(name='notifications', label='Enable notifications', checked=True, action='toggle_switch')

    @event_handler()
    def toggle_switch(self, **kwargs):
        self.component.checked = not self.component.checked
my_template.html
django
{{ component }}

Events this component sends to the view: toggle_switch.

Parameters

NameTypeDefaultDescription
namestr''Form field name
checkedboolFalseInitial checked state
labelstr | NoneNoneLabel text displayed next to the switch
disabledboolFalseWhether the switch is disabled
actionstr | NoneNonedjust event handler name (for dj-change)
custom_classstr''Additional CSS classes
**kwargs— passed to `Component.__init__`: `id=` sets `component.id`, and any other keyword is kept as state——
Source & styles
FilePathNotes
moduledjust.components.components.switchSubclass it, or pass custom_class, to change how it renders.
cssdjust_components/components.cssDefines 5 of this component's classes at line 2646, 2647, 2648, 2654. Override those rules, or the custom properties they read, in a stylesheet loaded after it.