New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

svelte-toggle

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svelte-toggle

Accessible toggle switch component

latest
Source
npmnpm
Version
4.0.1
Version published
Weekly downloads
1.5K
33.33%
Maintainers
1
Weekly downloads
 
Created
Source

svelte-toggle

NPM

Accessible toggle switch component

This Svelte component implements accessibility practices for toggle buttons recommended by the Inclusive Components guide.

Try it in the Svelte REPL.

Installation

# yarn
yarn add -D svelte-toggle

# npm
npm i -D svelte-toggle

# pnpm
pnpm i -D svelte-toggle

Usage

Uncontrolled

The component is toggled by default.

<script>
  import Toggle from "svelte-toggle";

  let isToggled = true;
</script>

<Toggle on:toggle={(e) => (isToggled = e.detail)} />

Toggled? {isToggled}

Two-way binding

The toggled prop supports two way binding.

<script>
  import Toggle from "svelte-toggle";

  let toggled = false;
</script>

<Toggle bind:toggled />

<button on:click={() => (toggled = !toggled)}>
  {toggled ? "Turn off" : "Turn on"}
</button>

Switch descriptors

Customize the toggle switch descriptors using the "on" and "off" props.

<Toggle on="On" off="Off" />

Alternatively, you can override the default slot:

<Toggle let:toggled>
  <strong>{toggled ? "Yes" : "No"}</strong>
</Toggle>

Small variant

Set small to true to use the small size variant.

<Toggle small />

Custom colors

Customize the switch colors:

  • switchColor (default: #fff)
  • toggledColor (default: #0f62fe)
  • untoggledColor (default: #8d8d8d)
<Toggle switchColor="#eee" toggledColor="#24a148" untoggledColor="#fa4d56" />

Custom label

Customize the label text through the label prop.

<Toggle label="Custom label" />

Hidden label

Set hideLabel to true to visually hide the label.

Note: You should still provide a label value for accessibility.

<Toggle hideLabel label="Custom label" />

Disabled

Set disabled to true to use the disabled state.

<Toggle disabled />

Fully controlled

ToggleCore is an unstyled component that provides the accessibility attributes for the label and button elements.

Use this component if you want to style the component yourself.

<script>
  import { ToggleCore } from "svelte-toggle";

  let on = false;
</script>

<ToggleCore toggled={on} let:label let:button>
  <!-- svelte-ignore a11y-label-has-associated-control -->
  <label {...label}>Label</label>
  <button {...button} on:click={() => (on = !on)}>
    {on ? "On" : "Off"}
  </button>
</ToggleCore>

API

API for the default Toggle component.

Props

Prop nameTypeDefault value
idstring"toggle" + Math.random().toString(36)"
labelstring"Label"
hideLabelbooleanfalse
smallbooleanfalse
toggledbooleantrue
disabledbooleanfalse
onstringundefined
offstringundefined
switchColorstring"#fff"
toggledColorstring"#0f62fe"
untoggledColorstring"#8d8d8d"

Dispatched events

  • on:toggle: fired whenever toggled changes
<script>
  import Toggle from "svelte-toggle";

  let events = [];
</script>

<Toggle on:toggle={(e) => (events = [...events, e.detail])} />

on:toggle: {events.join(", ")}

Forwarded events

  • on:click
  • on:focus
  • on:blur

Changelog

CHANGELOG.md

License

MIT

Keywords

svelte

FAQs

Package last updated on 15 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts