Hacker News new | ask | show | jobs
by fareesh 1240 days ago
How to create a general-purpose button:

https://twitter.com/Steve8708/status/1620448506766045191

I feel like this should be a lot simpler than it is.

Specifically, the fact that I have to create a component and rely on JS feels incredibly wrong - perhaps this is a pet peeve and isn't as bad as I am making it out to be.

I hope browser vendors are able to provide a roadmap to how these types of problems can be simplified someday in the future.

3 comments

What part of buttons require javascript?

If you want to run javascript when a user clicks a button, yes, that will require javascript. If you want to change the default styling of buttons, you need to write some CSS. This is not 'too hard' - the video points out a single line of CSS that completely removes all default styling.

This video invents a bunch of problems, and then just solves them by just... using standard HTML elements. Links for links, buttons for buttons. The author finally ends the video with a pretty terrible recommendation of trying to hide links vs buttons to users by putting them behind the same component. Not only does this approach teach bad habbits, you can very quickly run into a bunch of different corner cases blowing up that logic even further.

The one annoying part about buttons could be how they default to type=submit, but with the web's strong focus on backwards compat, the ship sailed on that a while ago, and it's not that onerous to just put type=button on buttons you don't want to submit a form.

The notion of wanting to make a div to be used as a button reminds me of this meme https://miro.medium.com/v2/resize:fit:4800/format:webp/1*tBk...
If browsers could let you put something in the document meta or whatever that would turn off all element styling it would be a less annoying lift to use more semantic elements. You can use resets but they’re just okay and still don’t get you to “looks exactly the same in all browsers.”
You don't have to create a component or rely on JS.