Skip to content

Avatar: optional interactivity (href/onClick) + single-tab-stop roving focus in AvatarGroup #4170

Closed

Description

@cixzhang

Summary

Make Avatar optionally interactive — support href and onClick so an avatar can be a link (to a profile) or a button (open a menu/dialog). When interactive avatars are inside an AvatarGroup, the group should be a single tab stop with roving arrow-key navigation via useListFocus (with a keyboard hint), rather than N separate tab stops.

Motivation

Today Avatar renders a static <div role="img"> — no way to make it clickable without a consumer wrapping it in their own <a>/<button>, which loses the component's styling, focus ring, and a11y wiring. Profile avatars that navigate, or avatars that open an actions menu, are extremely common. Avatar should own this the same way Button does.

And when a row of avatars is interactive, tabbing through every single one is poor keyboard UX. Composite widgets should be one tab stop with arrow-key navigation — the pattern useListFocus({hasRovingTabIndex: true}) already implements.

Proposed behavior

Avatar (interactive mode)

  • href + as → renders as a link, using useLinkComponent() for the app's router (same precedent as Button: href, as: LinkComponentType, target, rel).
  • onClick → renders as a <button> when no href.
  • Neither → stays the current static <div role="img"> (no behavior change, no focusability).
  • Interactive avatars get the correct role, keyboard activation (Enter/Space for button, Enter for link), a visible :focus-visible ring, and hover/active affordances. name/alt remains the accessible name (coordinate with the default-tooltip work in Avatar: show name tooltip by default, auto-disable when wrapped in a tooltip/hovercard #4164).
  • Follows the same "component wins on contract props, compose consumer handlers via composeEventHandlers" forwarding rules as the rest of core.

AvatarGroupOverflow (interactive parity)

  • The "+N" indicator already renders as a <button> when onClick is passed, but has no link support. Give it the same interactive contract as Avatar: add href + as (via useLinkComponent) so "+N" can navigate (e.g. to a full member list), keep onClick for the button case, and stay a static <span> when neither is provided.
  • Same role/keyboard/focus-ring/activation treatment as an interactive Avatar, and it participates in the group's roving tab order (below).

AvatarGroup (single tab stop)

  • When its avatars are interactive, AvatarGroup uses useListFocus with hasRovingTabIndex: true so the group is one tab stop: one avatar is tabindex="0", the rest -1, and Left/Right (horizontal orientation) move focus, with Home/End for boundaries. The hook already stamps/repairs the roving tab stop as items mount/unmount.
  • The AvatarGroupOverflow "+N" indicator participates in the roving order when it's interactive (onClick or href).
  • Keyboard hint: surface a hint that the group is arrow-navigable (e.g. an aria-description/visually-hidden instruction, or a documented pattern) so screen-reader and keyboard users know to use arrows. (Exact hint mechanism is a spec question — see below.)
  • Correct container semantics (the group already has role="group" + label; may need to move toward a listbox/toolbar-style role depending on whether avatars are links vs buttons).

Design notes / open questions (for spec)

  • Reuse, don't reinvent. Button already solves link-vs-button rendering (href/as/useLinkComponent, aria-disabled focus handling) and useListFocus already solves the single-tab-stop roving pattern (hasRovingTabIndex, orientation, Home/End, RTL). This should compose those, not duplicate them. Consider whether the interactive shell should come from the shared Clickable primitive proposed in Add generic Clickable and ClickableContainer components #3704.
  • Role choice in a group: a group of link-avatars vs button-avatars implies different container semantics (navigation list vs toolbar). Spec should pin this to a WAI-ARIA pattern.
  • Hint delivery: decide between a visually-hidden instruction, aria-description, or relying on the roving pattern alone. Don't double-announce.
  • Non-interactive avatars in a mixed group — define behavior when only some avatars are interactive.

Contract additions (relevant to #4163)

  • Avatar: href?, as?: LinkComponentType, target?, rel?, onClick? — new public props to spec + document, mirroring Button's link props.
  • AvatarGroup: any prop needed to opt into roving focus (or infer it from interactive children) + the hint.
  • AvatarGroupOverflow: add href?, as?: LinkComponentType, target?, rel? (it already has onClick?) — mirror Avatar's link props.

Acceptance criteria

  • <Avatar href="/u/jane" name="Jane" /> renders a link (via useLinkComponent), keyboard-activatable, with a focus ring.
  • <Avatar onClick={…} name="Jane" /> renders a focusable button with Enter/Space activation.
  • <Avatar name="Jane" /> (neither) is unchanged — static role="img", not focusable.
  • An AvatarGroup of interactive avatars is a single tab stop; Left/Right move focus between them; Home/End jump to ends.
  • AvatarGroupOverflow supports href (renders a link via useLinkComponent) as well as onClick, and stays a static <span> when neither is set.
  • Interactive AvatarGroupOverflow (link or button) participates in the roving order.
  • A keyboard/AT hint communicates arrow-navigation; accessible name is not double-announced.
  • Non-interactive avatars unaffected; mixed groups behave per spec.
  • Works across themes + light/dark; focus ring visible in both.

Notes

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions