DEV Community

Cover image for Create a custom toggle component in Vue.js. πŸŽ›

Create a custom toggle component in Vue.js. πŸŽ›

Vaibhav Khulbe on September 11, 2020

Making a custom component shouldn't be a mess. And what's better than using Vue's native features to create one! So let's do it. Let's make a custo...
Collapse
 
thepassle profile image
Pascal Schilp

This switch is not accessible, and not usable with keyboard navigation. You should really consider reading up on how to make custom components like these accessible before sharing wrong information.

Here are some good resources:
w3.org/TR/wai-aria-1.1/#switch
scottaohara.github.io/aria-switch-...
erikkroes.nl/blog/accessible-html-...

Collapse
 
jwkicklighter profile image
Jordan Kicklighter

And I would add to this: styling based on an ID is not considered good practice, nor is using an ID like "on" or "off" in a component that will be reused. According to spec, ID should be unique on the whole page.

Collapse
 
vaibhavkhulbe profile image
Vaibhav Khulbe

Hey Jordan, I agree, but this is not a full-fledged application. It's just a demo of how to start writing a custom component.

Also, please provide some external resources to back off your statements, this would help others to understand more.

Thread Thread
 
jwkicklighter profile image
Jordan Kicklighter • Edited

Sure, I recognize that it's just a demo and there may be some merit in just getting things out there for people to see. That said, it's a demo targeted toward a fairly beginner level audience. These subtle behaviors are often not addressed in anything new programmers will learn, and I feel that it's a disservice to allow poor programming habits to show up in an example meant for teaching people. Arguably, you should never be okay with poor practices in any code you write, if they could be easily avoidable; but this problem is even worse when you have an audience that may have never seen any of the techniques and will learn bad habits while trying to understand everything going on in the example.

Short version: it being a demo is precisely the reason you should follow best practices.

Edit: first sentence in this doc developer.mozilla.org/en-US/docs/W...

Thread Thread
 
thepassle profile image
Pascal Schilp

Exactly β€” this may not be a full-fledged application, but if a new dev comes here, reads this, recreates this toggle with non-unique IDs, and reuse a bunch of that component on their page/app, they will end up with multiple non-unique IDs on their page. Its a bad practice, and a poor example for new developers.

Collapse
 
vaibhavkhulbe profile image
Vaibhav Khulbe • Edited

Hi Pascal! You're right accessibility is highly important! But as you can see, this article is just focussed on "how to make a custom component". It's just about coding the component, adding other things like accessibility can surely be done. This was just to get a demo or a quick-start. :)

Also, thanks for adding the resources, they will definitely help!

Collapse
 
thepassle profile image
Pascal Schilp

Making it not accessible is not how you make a component. Youve just excluded a lot of people from being able to use the component, and even worse, youve taught a bunch of other people the wrong way to make a component.

If its not accessible, its not finished. β€œAdding” accessibility is not a bonus; its a requirement

Collapse
 
binotaliu profile image
γƒ“γƒŽγ‚Ώ

As another comment said that it is not a11y friendly. I strongly recommend to use input[type=checkbox] and use css to style it. Or, you can put an input[type=checkbox] that only visible to screen readers, then hide your divs on screen readers.

Collapse
 
vaibhavkhulbe profile image
Vaibhav Khulbe

Thanks for the info!

Collapse
 
spez profile image
Abhigyan

How did you create that GIF at the topmost?

Collapse
 
vaibhavkhulbe profile image
Vaibhav Khulbe

Using Canva :)

Collapse
 
vaibhavkhulbe profile image
Vaibhav Khulbe

Thanks Ayushi! Your comment is highly appreciated. 😊