Left Liquid Background
Right Liquid Background

Switch

Switch toggles are interactive binary controls used for enabling or disabling a specific option. Ideal for settings, preferences, and form controls, the Switch component ensures clear, intuitive state management.

Installation

Install the switch individually or as part of the complete @aristobyte-ui library.

Installation of Switch

The above command installs the Switch component individually. You may skip this step if @aristobyte-ui is already installed globally.

Package Manager

Individual Package

Install only the switch if you need specific functionality.

Full Library

Skip this if you have the complete @aristobyte-ui already installed.

Import

Choose between individual package imports or global library imports based on your project's needs and bundle size requirements.

Import Switch

You can import the Switch component either as a standalone module or from the global AristoByteUI package. The import method depends on how your project structure is configured.

Import Method

;

Individual Package

Import directly from the switch package for optimal tree-shaking and smaller bundle sizes.

Recommended for component-specific usage

Global Library

Import from the main library package when using multiple components from AristoByte UI.

Recommended for component-specific usage

Basic Usage Example

Basic implementation of the component using default properties. Suitable for general-purpose interactions.

Switch Variants

The Switch component supports multiple color variants such as default, primary, secondary, success, error, and warning. These variants reflect the intent of the toggle action and align with your application's color semantics.

Preview

Guidelines

    Semantic Mapping
  • Use primary for high-visibility, core settings.

  • Use success/error for domain-specific semantics.

  • Map variants to backend meaning when toggles correspond to persistent feature flags.

  • Reserve warning colors for toggles that may cause visible UX changes or require confirmation.

    Accessibility Notes
  • Respect contrast and WCAG state colors.

  • Avoid alarming colors for benign toggles.

  • Ensure variant colors are still distinguishable for color-blind users (use shape + color).

  • Provide tokenized theming for variant overrides so clients can maintain high contrast.

Initially Checked

Use the checked prop to define the initial on/off state of the Switch. When set to true, the switch renders in the active (checked) state on initial mount. This prop is ideal for controlled or semi-controlled usage patterns, particularly in form presets and settings UIs.

Preview

Guidelines

    When to Use
  • Default to the most common safe option.

  • Reflect server-sourced defaults in settings pages.

  • Use checked to mirror persisted user preferences when rendering forms.

  • Avoid pre-checking toggles that result in billing or destructive behavior.

    Best Practices
  • Avoid dark patterns; never pre-enable risky toggles.

  • Sync initial state with persisted user preferences.

  • Provide explicit documentation for initial checked semantics to prevent integration errors.

  • When migrating defaults, communicate breaking changes in release notes and deprecation paths.

Labeled Switch

Pass a text string using the label prop to display a caption alongside the Switch component. This label improves clarity and accessibility, making it easier for users to understand the function being toggled. It's especially effective in form fields, settings panels, or anywhere context is needed.

Preview

Guidelines

    Usage Scenarios
  • Use labels to clarify domain meaning of the toggle.

  • Localize labels to match app language settings.

  • Prefer concise labels (2-4 words) that map directly to backend fields.

  • When space permits, include a short helper to explain complex toggle behavior.

    Accessibility & Implementation
  • Associate label via

  • Keep label copy concise (2-4 words).

  • Ensure label changes are announced by screen readers when state toggles programmatically.

  • Provide test IDs for labels to support UI automation and telemetry correlation.

Align Label

The alignLabel prop allows you to control the positioning of the label relative to the switch thumb. Supported values are horizontal and vertical. The default behavior is vertical, placing the label above or below the switch depending on styling. Use horizontal when you want the label to be inline with the switch for compact layouts.

Preview

Guidelines

    Layout Patterns
  • Horizontal for dense settings lists or tables.

  • Vertical for forms and stacked layouts.

  • Use responsive alignment to shift between horizontal/vertical based on available width.

  • Prefer vertical alignment for forms where label wrapping is likely to improve readability.

    Implementation Notes
  • Ensure click target includes label and control.

  • Maintain consistent alignment across a section.

  • When aligning horizontally, ensure the label remains accessible via keyboard navigation.

  • Document expected layout fallback for third-party CSS resets that might alter alignment behavior.

Disabled

The disabled prop visually disables the switch, indicating that it cannot be interacted with by the user. While the switch becomes non-clickable, it can still reflect a checked or unchecked state, allowing developers to communicate a specific status that remains static. This is particularly useful in read-only contexts or in UI flows where toggling is conditionally locked, but the user still needs to observe the current value. Disabled switches maintain their configured variant, size, and label alignment.

Preview

Guidelines

    Operational Use
  • Reflect permission gating or system-enforced states.

  • Use when prerequisites are unmet in forms.

  • Use disabled state to indicate server-derived read-only configuration in settings pages.

  • Apply disabled when dependent asynchronous validations are pending to avoid inconsistent toggles.

    Developer Guidance
  • Explain disabled state with helper text or tooltip.

  • Avoid disabling core settings without rationale.

  • Prefer programmatic reasons (data-disable-reason) over visual-only indicators for maintainability.

  • Do not rely on visual disabled state for security — enforce permission checks server-side.

Switch Sizes

The Switch component comes in five scalable sizes — xsm, sm, md, lg, and xlg. These sizes provide layout flexibility and can be adapted to match form densities, mobile-first designs, or compact UI patterns.

Preview

Guidelines

    When to Use
  • Use smaller sizes in dense data tables.

  • Use large/x-large on mobile-first screens.

  • Select sizes that maintain a minimum thumb grab area for touch devices.

  • Prefer consistent vertical metrics between switches and adjacent inputs for optical alignment.

    Developer Tips
  • Ensure thumb size remains easily grabbable.

  • Avoid resizing text without adjusting control size.

  • Document how size impacts layout and provide examples for responsive design.

  • Create snapshot tests for each size to catch visual regressions across themes.

Controlled Switch

The controlled version of the Switch component offers explicit control over its state using React state management. This pattern ensures that the checked value is fully managed by the parent component via useState and updated through the onChange callback. It's particularly useful when you need to synchronize the switch state with business logic, form handlers, or other UI elements.

Preview

Guidelines

    Integration Patterns
  • Coordinate state with forms, stores, or URL params.

  • Drive from a single source of truth in complex UIs.

  • When integrating with form libraries, map onChange to library adapters to avoid duplicate renders.

  • Use controlled pattern for toggles that affect other components or persist to remote stores.

    Developer Notes
  • Debounce writes if persisting remotely.

  • Always implement onChange with idempotent handlers.

  • Consider optimistic UI updates with rollback paths for failed persistence.

  • Document expected event payload structure to ensure consistent consumer implementations.

Track Icons

The trackIcon prop enables the display of icons within the switch track itself, offering enhanced visual context for toggled states. You can define icons for both checked and unchecked states by passing a config object like { checked: IconOn, unchecked: IconOff }. These icons render respectively on the left and right sides of the track. It's optional—providing only one or none is fully supported. This feature is useful for reinforcing switch semantics such as enabling/disabling, visibility toggles, and more.

Preview

Guidelines

    Design Considerations
  • Use icons to reinforce domain meaning (e.g., eye/eye-off).

  • Use sparingly to avoid visual noise.

  • Ensure icons are legible across sizes and themes; consider stroke weight consistency.

  • Prefer subtle icons that act as affordances rather than decorative elements.

    Best Practices
  • Keep icons subtle and size-aligned to the track.

  • Ensure icons remain legible at all sizes.

  • Provide non-icon alternatives for assistive tech consumers (aria-label fallback).

  • Avoid adding heavy SVGs that increase bundle size for frequently-rendered switches.

Thumb Icon

The thumbIcon prop allows you to inject a custom icon directly into the switch's thumb element, offering a more expressive and branded interaction model. This prop accepts a single React.ElementType and renders the icon inside the movable thumb regardless of its state. It's ideal for use cases where a consistent icon (e.g., a power symbol, lock, or eye) enhances the user's understanding of the control. If omitted, the thumb renders without any icon by default.

Preview

Guidelines

    When to Use
  • Use for strong branding or explicit affordance.

  • Avoid when minimalism and clarity are preferred.

  • Use for clearly semantic controls like power toggles where icon adds immediate recognition.

  • Avoid for generic settings where icon could reduce readability.

    Developer Tips
  • Keep contrast sufficient against the thumb color.

  • Do not animate icons independently of the thumb.

  • Provide scalable vector icons to avoid pixelation at large sizes.

  • Document accessibility expectations when thumb icons replace textual cues.

Props Reference

Complete reference for all props available on Switch component(s), including type definitions, default values, and usage examples.

  • Switch

    • Core Props
      • onChange

        () => void

      • label

        string

      • checked

        boolean

    • Visual Props
      • alignLabel

        'horizontal' | 'vertical'default: 'vertical'

      • switchSize

        'xsm' | 'sm' | 'md' | 'lg' | 'xlg'default: 'md'

      • variant

        'default' | 'primary' | 'secondary' | 'success' | 'error' | 'warning'default: 'default'

    • State Props
      • disabled

        booleandefault: false

    • Advanced Props
      • trackIcon

      • thumbIcon

      • className

        stringdefault: ''

      • style

        React.CSSPropertiesdefault: {}

Viewing Unit with ID switch