|
8 | 8 |
|
9 | 9 | A toggle button component for Vaadin Flow that supports customizable labels and icons on both sides of the toggle. |
10 | 10 |
|
| 11 | +Starting with version 2.0, the add-on is built on top of the core Vaadin [Switch](https://vaadin.com/docs/latest/components/switch) component. The switch itself (keyboard, focus, accessibility, read-only and disabled states) comes from Vaadin, and the add-on adds side labels, icons, label highlighting, theme variants and touch swipe around it. |
| 12 | + |
11 | 13 | ## Features |
12 | 14 |
|
13 | | -* Toggle between two states with a single click |
| 15 | +* Toggle between two states with a click, a tap, a swipe, or the Space key |
14 | 16 | * Customizable left and right labels |
15 | 17 | * Support for icons on both sides via slots |
16 | 18 | * Optional label highlighting: active-side label uses the theme variant color (primary, success, warning, error, or contrast), inactive side is dimmed |
17 | 19 | * Optional icons-inside layout: icons adjacent to the switch, labels on the outer edges |
18 | 20 | * Theme variants: `SMALL`, `MEDIUM`, `LARGE`, `LONGSWIPE`, `PRIMARY`, `SUCCESS`, `WARNING`, `ERROR`, `CONTRAST` |
19 | 21 | * `LONGSWIPE` variant produces a wider switch track, optimized for touch interaction; can be combined with size variants |
| 22 | +* Accessible out of the box: announced as a switch with its on/off state, focusable, and operable with the keyboard |
| 23 | +* Form support: helper text, required indicator, error message, and `Binder` validation |
| 24 | +* Works with both the Lumo and Aura themes |
20 | 25 | * Fluent API for easy configuration |
21 | | -* Full integration with Vaadin's `HasValue`, `HasSize`, `HasLabel`, `HasAriaLabel`, and `HasTooltip` |
| 26 | +* Full integration with Vaadin's `HasValue`, `HasSize`, `HasLabel`, `HasAriaLabel`, `HasTooltip`, `HasHelper`, `HasValidationProperties`, and `Focusable` |
| 27 | + |
| 28 | +## Compatibility |
| 29 | + |
| 30 | +| Add-on version | Vaadin version | Java version | |
| 31 | +|---|---|---| |
| 32 | +| 2.x | 25.3 or newer | 21 | |
| 33 | +| 1.x | 24, and 25.0 to 25.2 | 17 (Vaadin 24), 21 (Vaadin 25) | |
| 34 | + |
| 35 | +Version 2.x requires Vaadin 25.3 or newer, because it is built on the core `Switch` introduced in that release. Version 1.x is in maintenance mode for older Vaadin versions. |
22 | 36 |
|
23 | 37 | ## Online demo |
24 | 38 |
|
@@ -145,8 +159,66 @@ toggle.addThemeVariants(ToggleButtonVariant.CONTRAST); |
145 | 159 | // Long swipe: wider track, optimized for touch (can be combined with size variants) |
146 | 160 | toggle.addThemeVariants(ToggleButtonVariant.LONGSWIPE); |
147 | 161 | toggle.addThemeVariants(ToggleButtonVariant.LONGSWIPE, ToggleButtonVariant.LARGE); |
| 162 | + |
| 163 | +// Helper text, shown below the toggle and read by screen readers |
| 164 | +ToggleButton toggle = new ToggleButton("Audit log retention", true); |
| 165 | +toggle.setHelperText("Included on the Business plan."); |
| 166 | +toggle.setReadOnly(true); |
148 | 167 | ``` |
149 | 168 |
|
| 169 | +### Forms and validation |
| 170 | + |
| 171 | +`ToggleButton` works with `Binder` like any other Vaadin field. A required toggle is valid when it is on: |
| 172 | + |
| 173 | +```java |
| 174 | +ToggleButton confirm = new ToggleButton("I confirm the trip details are correct") |
| 175 | + .setLeftLabel("No") |
| 176 | + .setRightLabel("Yes"); |
| 177 | + |
| 178 | +binder.forField(confirm) |
| 179 | + .asRequired("You must confirm the trip details to continue") |
| 180 | + .bind(Booking::isConfirmed, Booking::setConfirmed); |
| 181 | +``` |
| 182 | + |
| 183 | +The error message can also be set manually with `setErrorMessage(String)` and `setInvalid(boolean)`. |
| 184 | + |
| 185 | +### Accessibility |
| 186 | + |
| 187 | +The toggle is announced by screen readers as a switch with its on/off state. Its accessible name is taken from, in this order: |
| 188 | + |
| 189 | +1. The ARIA label, if set with `setAriaLabel(String)` |
| 190 | +2. The field label |
| 191 | +3. The side labels, joined with " / " (for example, "Off / On") |
| 192 | + |
| 193 | +The side labels and icons are decorative for screen readers. When they carry meaning that "on" and "off" don't convey (for example, "Dark" and "Light"), include it in the field label or the ARIA label. |
| 194 | + |
| 195 | +### Styling |
| 196 | + |
| 197 | +Color variants can be customized with these CSS custom properties on `fc-toggle-button`: `--toggle-button-primary-color`, `--toggle-button-success-color`, `--toggle-button-warning-color`, `--toggle-button-error-color`, and `--toggle-button-contrast-color`. |
| 198 | + |
| 199 | +The switch inside the component is a regular `vaadin-switch`, so it can also be styled with the core [Switch styling properties](https://vaadin.com/docs/latest/components/switch/styling), such as `--vaadin-switch-width` or `--vaadin-switch-background`: |
| 200 | + |
| 201 | +```css |
| 202 | +fc-toggle-button vaadin-switch { |
| 203 | + --vaadin-switch-width: 48px; |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +The rest of the component is rendered as regular elements that can be styled with these class names: `fc-toggle-button-label`, `fc-toggle-button-row`, `fc-toggle-button-left-label`, `fc-toggle-button-right-label`, `fc-toggle-button-left-icon`, `fc-toggle-button-right-icon`, `fc-toggle-button-helper`, and `fc-toggle-button-error`. |
| 208 | + |
| 209 | +## Migrating from 1.x |
| 210 | + |
| 211 | +Version 2.0 keeps the 1.x fluent API and value change events, but it has some breaking changes: |
| 212 | + |
| 213 | +* It requires Vaadin 25.3 or newer and Java 21. |
| 214 | +* `ToggleButton` extends `AbstractField` instead of `AbstractSinglePropertyField`. Value change listeners keep working, and the event type is still `ComponentValueChangeEvent<ToggleButton, Boolean>`. |
| 215 | +* `ToggleButton` no longer implements `HasComponents`, so `add(...)` and `remove(...)` aren't available. Use `setLeftIcon(...)` and `setRightIcon(...)` for icons. |
| 216 | +* Without a size variant, the toggle uses the native switch size of the active theme. Previously, the default was the same as `MEDIUM`. Add `ToggleButtonVariant.MEDIUM` to keep the 1.x size. |
| 217 | +* The color variants write different values to the `theme` attribute: `color-primary`, `color-success`, `color-warning`, `color-error`, and `color-contrast` (previously `primary`, `success`, `warning`, `error`, and `contrast`). The Java constants in `ToggleButtonVariant` don't change. This only affects custom CSS that targets the attribute, such as `fc-toggle-button[theme~="error"]`. The old values collided with the theme names that `Binder` adds and removes for validation errors, so a toggle bound with `Binder` lost its `ERROR` or `WARNING` variant. |
| 218 | +* Read-only and disabled states use the core Switch look. In read-only mode, the track border is solid instead of dashed. A checked read-only toggle with a color variant still shows the variant color, as a tint on the track border. |
| 219 | +* The tooltip is attached to the switch, so it opens when hovering or focusing the switch, not the side labels. |
| 220 | +* When swiping, the marker no longer follows the finger. The toggle changes state when the swipe passes half the track width. |
| 221 | + |
150 | 222 | ## Special configuration when using Spring |
151 | 223 |
|
152 | 224 | By default, Vaadin Flow only includes `com/vaadin/flow/component` to be always scanned for UI components and views. For this reason, the add-on might need to be allowed in order to display correctly. |
|
0 commit comments