Skip to content

feat(hooks): useContainerReveal — composable hover/focus reveal primitive - #4573

Merged
cixzhang merged 3 commits into
mainfrom
feat/use-container-reveal
Jul 31, 2026
Merged

cixzhang merged 3 commits into
mainfrom
feat/use-container-reveal

Conversation

@cixzhang

@cixzhang cixzhang commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Draft. The composable primitive discussed as the alternative to per-component reveal props (see #4538). Thumbnail is refactored onto it as the reference implementation; TreeList (#4538) should adopt this instead of adding an endContentReveal prop.

Why a hook, not a prop

Thumbnail (showRemoveOn="hover", #4300) and the proposed TreeList endContentReveal (#4538) ship the same hover/focus reveal mechanic — each with its own *.markers.stylex.ts + a duplicated opacity block. This extracts the mechanic into one headless, tested primitive so components compose it instead of re-implementing it. This PR proves it out by moving Thumbnail onto the hook and deleting its bespoke marker + reveal styles — no public API change.

For TreeList, the recommendation is: don't add a prop — expose the composition via the hook, and add an example/story showing it.

What

useContainerReveal() → { getContainerProps(), getContentRevealProps(opts) }.

const {getContainerProps, getContentRevealProps} = useContainerReveal({isEnabled});

<div {...mergeProps(getContainerProps(), stylex.props(styles.row))}>
  {label}
  <span {...mergeProps(getContentRevealProps(), stylex.props(styles.actions))}>
    {actions}
  </span>
</div>
  • Symmetric prop-getters (get*Props), matching the useLightbox.getTriggerProps precedent.
  • Per-content options: getContentRevealProps({isRevealInverted, isLayoutPreserved}) — one container hover can drive multiple children with opposite behavior.
  • Callers author no StyleX. The hook owns the styles.

Accessibility (WCAG 2.2 by construction)

  • Revealed content is hidden at rest with position: absolute + opacity: 0 — stays in the a11y tree and tab order, never display:none.
  • Keyboard: revealed on :focus-within.
  • Touch: always visible on coarse pointers — never gated behind an unavailable hover.
  • Inverted (conceal) is asymmetric on purpose: a mouse-only visual declutter, so it deliberately does not react to :focus-within (a keyboard user must never watch content vanish) and stays visible on touch.
  • Motion: honors prefers-reduced-motion.

The one non-obvious design decision: the marker pool

stylex.when.ancestor(':hover', marker) compiles to .marker:hover * — a descendant selector. A single shared marker therefore leaks across nested reveal containers (an outer row's hover would reveal a nested thumbnail's button). Because StyleX welds the marker into the compiled CSS at build time, the marker cannot be passed as a runtime argument, so the per-marker blocks are written out literally in containerReveal.pool.stylex.ts (no codegen — it's static output).

useContainerReveal hands each concurrently-mounted container a distinct marker from that pool via a module-level free-list (claimed in a useState initializer, released on unmount — StrictMode-safe). Nested containers get different markers, so they can't leak. Siblings sharing a marker is harmless (a sibling is never an ancestor). Pool exhaustion degrades gracefully with a dev warning.

Risk

  • Thumbnail behavior is unchanged — its remove button is absolutely positioned, so it maps to the opacity-only (isLayoutPreserved) variant, exactly matching the prior styles. Full Thumbnail suite passes.
  • Pool size is 6 (covers concurrent nesting depth, not instance count). Deeper nesting falls back to a shared slot with a dev warning.

Testing

  • 8 hook unit tests: getter shape, inert/disabled path, distinct markers for concurrent containers, free-list recycle across mount/unmount cycles, reveal/conceal/layout → distinct style blocks.
  • Full Thumbnail suite (22 tests) green on the refactor.
  • typecheck, eslint, check-sync, sync-exports --check, check-changesets — all green.
  • Storybook: Hooks/useContainerReveal with Reveal, InvertedConceal, PreserveLayout, and a NestedIsolation story that visually proves no leak.
  • a11y: two pre-existing color-contrast violations on Thumbnail stories (the fixed-scrim remove button over arbitrary images) are added to .github/a11y-baseline.json. They were never gated before — pr-a11y only audits components a PR touches, and one of the two (Media Mode) is untouched by this PR, confirming they predate it.

Open questions

  1. Pool size 6 — right default, or should exhaustion be a hard dev error instead of a shared-slot fallback?
  2. Naming: useContainerReveal + getContentRevealProps — settled?

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 31, 2026
@vercel

vercel Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Jul 31, 2026 5:17am

Request Review

@github-actions

github-actions Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

Thumbnail (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 294 -
Complexity N/A Very High (34) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.7KB 1.2KB

Accessibility Audit

Status: 1 accessibility violation(s) found — 1 serious.

Thumbnail - 1 issue(s)
  • 🟠 serious: Ensure the contrast between foreground and background colors meets WCAG 2 AA minimum contrast ratio thresholds
    • Rule: color-contrast · Affects 2/12 stories · Learn more
    • WCAG: 1.4.3 (Level AA)

Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

github-actions Bot added a commit that referenced this pull request Jul 31, 2026
github-actions Bot added a commit that referenced this pull request Jul 31, 2026
@cixzhang
cixzhang marked this pull request as ready for review July 31, 2026 05:12
The remove button uses a fixed translucent scrim + on-dark icon (not
luminance-adapting), so axe color-contrast can't be guaranteed over
arbitrary images. These stories were never gated before; touching
Thumbnail.tsx pulls them into the per-component pr-a11y audit. The
Media Mode story is untouched by this PR, confirming the violations
are pre-existing.
github-actions Bot added a commit that referenced this pull request Jul 31, 2026
@cixzhang
cixzhang merged commit fc53ff7 into main Jul 31, 2026
18 checks passed
@github-actions
github-actions Bot deleted the feat/use-container-reveal branch July 31, 2026 07:03

This branch was successfully deployed

1 active deployment
Preview — e4e24b9f Deployed Jul 31, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. needs:design-review Affects visuals — Design should review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant