Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions frontend/src/app/main/ui/ds/controls/switch.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@
:disabled disabled?})]

[:> :div props
[:div {:id id
:class (stl/css :switch-track)}
[:div {:class (stl/css :switch-thumb)}]]
(when has-label?
[:label {:for id
:class (stl/css :switch-label)}
label])

[:div {:id id
:class (stl/css :switch-track)}
[:div {:class (stl/css :switch-thumb)}]]]))
label])]))
38 changes: 19 additions & 19 deletions frontend/src/app/main/ui/ds/controls/switch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,42 @@ import * as Switch from "./switch.stories";

# Switch

The `switch*` component is a toggle control that allows users to switch between two mutually exclusive options.
The `switch*` component is a toggle control. It allows users to switch between two mutually exclusive states (`false` or `true`), while also accepting an initial third state (`nil`).

<Canvas of={Switch.Default} />

## Anatomy

The switch component consists of three main parts:

- **Label** (optional): text that describes what the switch controls
- **Track**: the pill-shaped background that indicates the current state
- **Thumb**: the circular knob that moves between positions
- **Label** (optional): the text that describes what the switch controls. Clicking on this text also works for toggling.
- **Track**: the pill-shaped background.
- **Thumb**: the knob that moves between positions.

## Accesibility

When no visible label is provided, use `aria-label` for accessibility.

## Variants

### With Label
## Basic usage

```clj
[:> switch* {:label "Toggle dark mode"
:default-checked false}]
[:> switch* {:label "Toggle something"
:default-checked nil
:on-change handle-change
:disabled false}]
```

<Canvas of={Switch.WithLabel} />
## Accesibility

When no visible label is provided, use `:aria-label` for accessibility.

## Usage Guidelines

### When to Use

- For binary settings that take effect immediately
- In preference panels and configuration screens
- For boolean settings that take effect immediately.
- In preference panels and configuration screens.
- For ternary states where the third state is only relevant at the beginning (e.g., selection of multiple elements with opposite states).

### When Not to Use

- For actions that require confirmation (use buttons instead)
- For multiple choice selections (use radio buttons or select)
- For temporary states that need explicit "Apply" action
- For actions that require confirmation (use buttons instead).
- For multiple choice selections (use radio buttons or select).
- For temporary states that need explicit "Apply" action.
- For ternary states that require passing through all three states.
24 changes: 12 additions & 12 deletions frontend/src/app/main/ui/ds/controls/switch.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ export default {
title: "Controls/Switch",
component: Switch,
argTypes: {
defaultChecked: {
control: { type: "boolean" },
description: "Default checked state for uncontrolled mode",
},
label: {
control: { type: "text" },
description: "Label text displayed next to the switch",
Expand All @@ -27,33 +23,37 @@ export default {
}
},
args: {
defaultChecked: false,
disabled: false
},
parameters: {
controls: { exclude: ["id", "class", "aria-label", "on-change"] },
controls: { exclude: ["id", "class", "aria-label", "default-checked", "on-change"] },
},
render: ({ ...args }) => (
<Switch {...args} />
),
};

export const Default = {};
export const Default = {
args: {
label: "Toggle something",
disabled: false,
},
render: ({ ...args }) => (
<Switch {...args} />
),
};

export const WithLabel = {
export const WithoutLabel = {
args: {
defaultChecked: false,
label: "Enable something",
disabled: false,
},
render: ({ ...args }) => (
<Switch {...args} aria-label="Enable notification"/>
<Switch {...args} />
),
};

export const WithLongLabel = {
args: {
defaultChecked: false,
label: "This is a very long label that demonstrates how the switch component handles text wrapping and layout when the label content is extensive",
disabled: false,
},
Expand Down