Skip to content

FlowingCode/ToggleButton

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Published on Vaadin Directory Stars on vaadin.com/directory Build Status Maven Central Javadoc

Toggle Button Add-On

A toggle button component for Vaadin Flow that supports customizable labels and icons on both sides of the toggle.

Features

  • Toggle between two states with a single click
  • Customizable left and right labels
  • Support for icons on both sides via slots
  • 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
  • LONGSWIPE variant produces a wider switch track, optimized for touch interaction; can be combined with size variants
  • Fluent API for easy configuration
  • Full integration with Vaadin's HasValue, HasSize, HasLabel, HasAriaLabel, and HasTooltip

Online demo

Online demo here

Download release

Available in Vaadin Directory

Maven install

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.

Building and running demo

  • git clone repository
  • mvn clean install jetty:run

To see the demo, navigate to http://localhost:8080/

Release notes

See here

Issue tracking

The issues for this add-on are tracked on its github.com page. All bug reports and feature requests are appreciated.

Contributions

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.

1. Reporting Bugs and Requesting Features

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.

2. Contributing Code via Pull Requests

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.

License & Author

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.

Developer Guide

Getting started

// 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);

Special configuration when using Spring

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.

About

Toggle Button for Vaadin

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors