A toggle button component for Vaadin Flow that supports customizable labels and icons on both sides of the toggle.
Starting with version 2.0, the add-on is built on top of the core Vaadin 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.
- Toggle between two states with a click, a tap, a swipe, or the Space key
- Customizable left and right labels
- Support for icons on both sides, set with
setLeftIconandsetRightIcon - Optional label highlighting: active-side label uses the theme variant color (primary, success, warning, error, or contrast), inactive side is dimmed
- Optional icons-inside layout: icons adjacent to the switch, labels on the outer edges
- Theme variants:
SMALL,MEDIUM,LARGE,LONGSWIPE,PRIMARY,SUCCESS,WARNING,ERROR,CONTRAST LONGSWIPEvariant produces a wider switch track, optimized for touch interaction; can be combined with size variants- Accessible out of the box: announced as a switch with its on/off state, focusable, and operable with the keyboard
- Form support: helper text, required indicator, error message, and
Bindervalidation - Works with both the Lumo and Aura themes
- Fluent API for easy configuration
- Full integration with Vaadin's
HasValue,HasSize,HasLabel,HasAriaLabel,HasTooltip,HasHelper,HasValidationProperties, andFocusable
| Add-on version | Vaadin version | Java version |
|---|---|---|
| 2.x | 25.3 or newer | 21 |
| 1.x | 24, and 25.0 to 25.2 | 17 (Vaadin 24), 21 (Vaadin 25) |
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.
Add the following dependencies in your pom.xml file:
<dependency>
<groupId>com.flowingcode.vaadin.addons</groupId>
<artifactId>toggle-button-addon</artifactId>
<version>X.Y.Z</version>
</dependency>Release versions are available from Maven Central repository. For SNAPSHOT versions see here.
- git clone repository
- mvn clean install jetty:run
To see the demo, navigate to http://localhost:8080/
See here
The issues for this add-on are tracked on its github.com page. All bug reports and feature requests are appreciated.
Contributions are welcome. There are two primary ways you can contribute: by reporting issues or by submitting code changes through pull requests. To ensure a smooth and effective process for everyone, please follow the guidelines below for the type of contribution you are making.
Creating an issue is a highly valuable contribution. If you've found a bug or have an idea for a new feature, this is the place to start.
- Before creating an issue, please check the existing issues to see if your topic is already being discussed.
- If not, create a new issue, choosing the right option: "Bug Report" or "Feature Request". Try to keep the scope minimal but as detailed as possible.
A Note on Bug Reports
Please complete all the requested fields to the best of your ability. Each piece of information, like the environment versions and a clear description, helps us understand the context of the issue.
While all details are important, the minimal, reproducible example is the most critical part of your report. It's essential because it removes ambiguity and allows our team to observe the problem firsthand, exactly as you are experiencing it.
As a first step, please refer to our Development Conventions page to find information about Conventional Commits & Code Style requirements.
Then, follow these steps for creating a contribution:
- Fork this project.
- Create an issue to this project about the contribution (bug or feature) if there is no such issue about it already. Try to keep the scope minimal.
- Develop and test the fix or functionality carefully. Only include minimum amount of code needed to fix the issue.
- For commit message, use Conventional Commits to describe your change.
- Send a pull request for the original project.
- Comment on the original issue that you have implemented a fix for it.
This add-on is distributed under Apache License 2.0. For license terms, see LICENSE.txt.
Toggle Button Add-On is written by Flowing Code S.A.
// Basic toggle button
ToggleButton toggle = new ToggleButton();
// With a field label (shown above the toggle)
ToggleButton toggle = new ToggleButton("Notifications");
// With a field label and initial value
ToggleButton toggle = new ToggleButton("Dark mode", true);
// With labels
ToggleButton toggle = new ToggleButton()
.setLeftLabel("Off")
.setRightLabel("On");
// With icons
ToggleButton toggle = new ToggleButton()
.setLeftLabel("Dark")
.setRightLabel("Light")
.setLeftIcon(new Icon(VaadinIcon.MOON))
.setRightIcon(new Icon(VaadinIcon.SUN_O));
// Listen to value changes
toggle.addValueChangeListener(e ->
Notification.show("Toggle is now: " + (e.getValue() ? "on" : "off")));
// Enable label highlighting (active side uses the theme variant color, inactive side is dimmed)
ToggleButton toggle = new ToggleButton()
.setLeftLabel("Off")
.setRightLabel("On")
.withHighlightLabel();
// Icons inside: [label] [icon] [switch] [icon] [label] (default is [icon] [label] [switch] [label] [icon])
ToggleButton toggle = new ToggleButton()
.setLeftIcon(new Icon(VaadinIcon.MOON))
.setLeftLabel("Dark")
.setRightLabel("Light")
.setRightIcon(new Icon(VaadinIcon.SUN_O))
.withIconsInside();
// Apply theme variants
toggle.addThemeVariants(ToggleButtonVariant.PRIMARY);
toggle.addThemeVariants(ToggleButtonVariant.CONTRAST);
// Long swipe: wider track, optimized for touch (can be combined with size variants)
toggle.addThemeVariants(ToggleButtonVariant.LONGSWIPE);
toggle.addThemeVariants(ToggleButtonVariant.LONGSWIPE, ToggleButtonVariant.LARGE);
// Helper text, shown below the toggle and read by screen readers
ToggleButton toggle = new ToggleButton("Audit log retention", true);
toggle.setHelperText("Included on the Business plan.");
toggle.setReadOnly(true);ToggleButton works with Binder like any other Vaadin field. A required toggle is valid when it is on:
ToggleButton confirm = new ToggleButton("I confirm the trip details are correct")
.setLeftLabel("No")
.setRightLabel("Yes");
binder.forField(confirm)
.asRequired("You must confirm the trip details to continue")
.bind(Booking::isConfirmed, Booking::setConfirmed);The error message can also be set manually with setErrorMessage(String) and setInvalid(boolean).
The toggle is announced by screen readers as a switch with its on/off state. Its accessible name is taken from, in this order:
- The elements referenced with
setAriaLabelledBy(String), if set - The ARIA label, if set with
setAriaLabel(String) - The field label
- The side labels, read in order (for example, "Off On")
The side labels and icons aren't read on their own, only as part of the name when there is no field label or ARIA label. When there is one and the side labels carry meaning that "on" and "off" don't convey (for example, "Dark" and "Light"), include it in the field label or the ARIA label.
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.
The switch inside the component is a regular vaadin-switch, so it can also be styled with the core Switch styling properties, such as --vaadin-switch-width or --vaadin-switch-background:
fc-toggle-button vaadin-switch {
--vaadin-switch-width: 48px;
}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.
Version 2.0 keeps the 1.x fluent API and value change events, but it has some breaking changes:
- It requires Vaadin 25.3 or newer and Java 21.
ToggleButtonextendsAbstractFieldinstead ofAbstractSinglePropertyField. Value change listeners keep working, and the event type is stillComponentValueChangeEvent<ToggleButton, Boolean>.ToggleButtonno longer implementsHasComponents, soadd(...)andremove(...)aren't available. UsesetLeftIcon(...)andsetRightIcon(...)for icons.- Without a size variant, the toggle uses the native switch size of the active theme. Previously, the default was the same as
MEDIUM. AddToggleButtonVariant.MEDIUMto keep the 1.x size. - The color variants write different values to the
themeattribute:color-primary,color-success,color-warning,color-error, andcolor-contrast(previouslyprimary,success,warning,error, andcontrast). The Java constants inToggleButtonVariantdon't change. This only affects custom CSS that targets the attribute, such asfc-toggle-button[theme~="error"]. The old values collided with the theme names thatBinderadds and removes for validation errors, so a toggle bound withBinderlost itsERRORorWARNINGvariant. - Read-only and disabled states use the core Switch look. In read-only mode, the track border is solid instead of dashed. Color variants are kept as in 1.x: a checked read-only toggle shows a desaturated variant color on the track border, and a checked disabled toggle shows a faded variant color on the track.
- The tooltip is attached to the switch, so it opens when hovering or focusing the switch, not the side labels.
- When swiping, the marker no longer follows the finger. The toggle changes state when the swipe passes half the track width.
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.
To do so, just add com.flowingcode to the vaadin.allowed-packages property in src/main/resources/application.properties, like:
vaadin.allowed-packages = com.vaadin,org.vaadin,dev.hilla,com.flowingcode
More information on Spring scanning configuration here.