RC RANDOM CHAOS

Rebuilding a native <button> from scratch: a lesson in why you shouldn't

· via Hacker News

Original source

If you want to create a button from scratch, you must first create the universe

Hacker News →

Web accessibility advice usually starts with two rules: don’t use ARIA if you can avoid it, and always prefer a native HTML element over a hand-rolled one. This piece justifies that advice by walking through what it actually takes to reconstruct something as mundane as a button. Native elements ship with a long list of built-in guarantees, and a custom replacement has to reproduce every one to satisfy WCAG 2.2 — a proper button role and accessible label, keyboard focusability, activation via mouse, touch, stylus, Space and Enter, form participation (type, name, value, validation, reset/submit), disabled and other states, plus hooks for newer platform features like the Popover and Invoker Commands APIs.

The author builds a custom element step by step to show how quickly the complexity compounds. An icon-only button using an emoji, for example, gets announced by screen readers as ‘pile of poo’ and may render as a replacement glyph, so it needs an explicit aria-label plus the emoji hidden as presentational. Making it focusable requires tabindex=“0”; making it activate requires separately wiring mouseup, touchend, and pointerup, then keydown and keyup for the keyboard. Moving the markup into a JavaScript class with ElementInternals and shadow DOM tidies the HTML but adds its own layer of complexity.

The takeaway is that even after all this scaffolding, the reimplementation only reaches parity with what

Read the full article

Continue reading at Hacker News →

This is an AI-generated summary. Read the original for the full story.