Invental/Review lessons/Accessibility and UI behaviour

Accessibility and UI behaviour

A div posing as a button owns its keyboard

Replacing a <button> with a div to avoid nesting means you now own everything the button did for free: focus, Enter and Space, disabled state, and not swallowing events meant for the real buttons inside it.

01 · Before and afterThe pattern

TSX
// trigger is a div so the chip's remove can be a real <button>
<div role="button" tabIndex={disabled ? -1 : 0} aria-disabled={disabled}
  onClick={(e) => { if ((e.target as HTMLElement).closest('[data-no-open]')) return; e.preventDefault(); open(); }}
  onKeyDown={(e) => { if (e.target !== e.currentTarget) return; if (e.key === 'Enter' || e.key === ' ') open(); }}>
  {label}<button type="button" data-no-open onClick={onRemove} aria-label={`Remove ${label}`}>×</button>
</div>

02 · Run it on your appThe check

Can a keyboard-only user remove a chip or tag in your UI without opening the menu it sits in?

If the honest answer is no or not sure, this is worth a look before it reaches production.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.