Coder Social home page Coder Social logo

metonym / svelte-toggle Goto Github PK

View Code? Open in Web Editor NEW
42.0 3.0 5.0 484 KB

Accessible toggle switch component

Home Page: https://metonym.github.io/svelte-toggle

License: MIT License

JavaScript 9.97% Svelte 59.47% TypeScript 30.56%
toggle switch button accessible wai-aria svelte svelte-component a11y typescript

svelte-toggle's People

Contributors

dependabot[bot] avatar metonym avatar ronen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

svelte-toggle's Issues

Vite: Conflicting results in svelte module resolution.

The current resolution for svelte modules used for the toggle component has some conflicting results. This is not a breaking problem, but it may cause them in the future.

A good alternative would be to use conditional exports as explained here, since they're supported by Node and bundlers like Vite and simplify the implementation for those resolutions.

Support group binding

Svelte supports a feature where instead of one separate variable per each checkbox you can have an array and each checkbox will add/remove its name attribute from that array when toggled.

https://svelte.dev/tutorial/group-inputs

Is there any way this can be supported with svelte-toggle?

e.g.

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

  let items = [];
</script>

<Toggle bind:group={items} name="x" />
<Toggle bind:group={items} name="y" />
<Toggle bind:group={items} name="z" />

Toggling all three should result in items = ['x','y','z']

p.s. Maybe not helpful but I found this: https://svelte.dev/repl/faabda4cabd544bd858a8a8abd0095f5?version=3.12.1

Usage with writable store

Is there a clean way to bind the toggle to a writable store?

The following does not work:

<script>
import { writable } from 'svelte/store'
import Toggle from 'svelte-toggle'

const toggled = writable(false)
</script>

<Toggle bind:toggled />

Neither does bind:$toggled, bind:value={$toggled} or bind:checked={$toggled}.

My workaround is to add the toggled property and listen to the toggle event to update the store:

<script>
import { writable } from 'svelte/store'
import Toggle from 'svelte-toggle'

const toggled = writable(false)

function onToggle({ detail }) {
  toggled.set(detail)
}

</script>

<Toggle toggled={$toggled} on:toggle={onToggle} />

This isn't terrible, but I wonder why it's not as simple as binding a store to a native checkbox, like so:

<script>
import { writable } from 'svelte/store'
import Toggle from 'svelte-toggle'

const toggled = writable(false)
</script>

<input type="checkbox" bind:checked={$toggled} />

two toggles in the same svelte file: second does not work

Hi!
When I use more than one Toggle components in the same```
svelte file, the second one does not work.

<script>                                                                        
  import Toggle from "svelte-toggle";                                           
  let toggled;                                                                  
  let toggled2;                                                                 
</script>                                                                  
<Toggle bind:toggled />                                                                                                                           
Toggled? {toggled}                                                              
<Toggle bind:toggled2 />                                                        
Toggled2? {toggled2}

Here toggles2 remains undefined.
If I define both variables to false, toggled 2 remains false even when i toggle the component.

See this repl https://svelte.dev/repl/03dfe3c0bb1b4e818edc50353b6ff24b?version=3.46.4 based on your example.

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.