Skip to content

docs(useFocusTrap): add a quick edit panel example block - #9513

Merged
cixzhang merged 4 commits into
meta:mainfrom
MannXo:docs/9485-usefocustrap-example
Nov 3, 2026
Merged

cixzhang merged 4 commits into
meta:mainfrom
MannXo:docs/9485-usefocustrap-example

Conversation

@MannXo

@MannXo MannXo commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Reader impact

A builder reading the useFocusTrap docs has no runnable example of the hook. After this change the hook's page shows a custom quick-edit panel, not built on Dialog, that traps focus, and astryx template useFocusTrapHookUsage <path> copies it.

Source of truth

  • useFocusTrap and its HookDoc (packages/core/src/hooks/useFocusTrap.ts, useFocusTrap.doc.mjs).
  • architecture:knowledge-contracts INV28.

Correction

Adds blocks/components/Hooks/useFocusTrapHookUsage (exampleFor: 'useFocusTrap'). A Quick edit button opens a Card panel (role="dialog", aria-label="Quick edit") with a Title field and Cancel/Save. The panel spreads containerRef from useFocusTrap({isActive: isOpen, onEscape: close}) and calls focusFirst in an effect on open, as in the hook's JSDoc example. Closing deactivates the trap, which returns focus to the Quick edit button.

Evidence

  • Real Chromium (Playwright against the docsite dev server, /components/useFocusTrap). Opening focuses the Title input. Tab moves Cancel, Save, then back to Title. Shift+Tab from Title goes to Save. Escape closes the panel and focus is on Quick edit.
  • Test (packages/cli/api/template/use-focus-trap-hook-usage.test.tsx, outside assets/templates/ so the template viewer does not load it as a template). Opening moves focus into the panel, and Escape and Cancel both close it with focus back on Quick edit. With the hook replaced by a plain ref, all three fail.
  • The test does not assert which control focusFirst picks. In the CLI project's jsdom, querySelectorAll with the focusable selector list returns matches grouped by selector rather than in document order, so focusFirst lands on Cancel there. In Chromium it lands on the input, as above.

Scope

  • This PR changes documentation only, or names every necessary generated artifact. The generated artifacts are the new block's route in internal/shadcn-registry/routes.lock.json and its sandbox route in apps/sandbox/scripts/next-export-routes.json.
  • Runtime, API, visual, and policy changes were removed or split.
  • Public text and artifacts contain no internal Meta context.

Testing

  • vitest run --project node packages/cli/api/template/use-focus-trap-hook-usage.test.tsx apps/sandbox/src/route-manifest.test.ts: 9 passed, 0 failed.
  • vitest run --project node packages/cli/foundation/discovery packages/cli/api/template packages/cli/assets/templates internal/shadcn-registry: 1105 passed, 0 failed.
  • apps/docsite: node scripts/generate-data.mjs, then vitest run src/__tests__/data-extraction.test.ts src/__tests__/example-coverage.test.ts: 133 passed, 0 failed. The route lock check is clean after UPDATE_SHADCN_ROUTE_LOCK=1 added the new route.
  • pnpm -F @astryxdesign/cli typecheck (template docs and strict): clean.
  • eslint (strict tier) and prettier --check on the touched files: clean. pnpm lint:strict repo-wide: 0 errors (106 warnings, none in these files).
  • node scripts/release/changeset-coverage.mjs pr --base upstream/main --head HEAD --released v0.7.5: passes. Changeset is @astryxdesign/cli patch, [docs].
  • Not run: the full pnpm test suite.

Fixes #9485

@vercel

vercel Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 3, 2026 8:54pm UTC

Request Review

@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge needs:design-review Affects visuals — Design should review labels Nov 2, 2026
@astracat-bot

astracat-bot Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 3, 2026, 9:02 PM UTC

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

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example covers what INV28 asks of a hook example and behaves as described in Chromium, but two things are needed before merge:

  • Add a short comment above the useEffect that calls focusFirst() saying it must run after commit, once the panel exists and useFocusTrap has recorded the opener; a callback ref would run too early, and focus would not return after close.
  • Check the failed docs deploy for this commit, then fix the cause or redeploy until it passes.

[Automated review]

onEscape: close,
});

useEffect(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Effect is the right place for focusFirst(), but it needs a comment saying why. The panel exists only after commit, and by then useFocusTrap has recorded the opener for focus return. A callback ref looks simpler, but it runs before that record, so after Escape focus lands on <body>. For example:

// Focus moves into the panel after commit, when the panel exists.
// By then useFocusTrap has recorded the opener, so closing returns focus to it.
// A callback ref runs before that record, and focus then drops to the body.

A custom quick-edit panel, not built on Dialog, traps focus with
useFocusTrap: focusFirst moves focus to its field on open, Tab and
Shift+Tab stay inside it, Escape closes it through onEscape, and closing
returns focus to the Quick edit button.

Fixes meta#9485
… of templates, and say why focusFirst runs in an effect
@MannXo
MannXo force-pushed the docs/9485-usefocustrap-example branch from 5e0a7c4 to b3f4f41 Compare November 3, 2026 18:53
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · 675d36a

Storybook · Sandbox · built from this PR's head

Package size: @astryxdesign/cli 3.32 MB packed (+642 B); no new dependencies.

Not run on this PR: add ci:run-visual, ci:run-a11y or ci:run-rtl to run them.


Generated by PR Enrichment workflow | View full report

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks ready to merge: the Quick edit panel gives useFocusTrap the runnable example that architecture:knowledge-contracts INV28 asks for, and its behavior comes from the hook itself.

  • The comment above the focusFirst() Effect, asked for in the earlier review, is in place and says why it runs after commit.
  • The repository's CI jobs have not run on this commit yet; they start once a maintainer adds the ci:run label.

[Automated review]

@cixzhang cixzhang added the ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) label Nov 3, 2026
@cixzhang cixzhang closed this Nov 3, 2026
@cixzhang cixzhang reopened this Nov 3, 2026
Main gained meta#9509's useAnnounce example beside this one; both route
manifests keep both entries.
The new example block matches `astryx search close`, so the match count
goes from 46 to 47; the top 20 results are unchanged.

Co-authored-by: Cindy Zhang <[email protected]>

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks ready to merge: the Quick edit panel gives useFocusTrap the runnable example that architecture:knowledge-contracts INV28 asks for, and its focus behavior comes from the hook itself.

  • The search close golden count now includes the new block.
  • Some CI test jobs had not finished yet; confirm they pass before merging.

[Automated review]

@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Nov 3, 2026
@cixzhang
cixzhang merged commit 1404bf8 into meta:main Nov 3, 2026
60 checks passed

This branch was successfully deployed

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

Labels

ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team) needs:design-review Affects visuals — Design should review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs(useFocusTrap): add a minimal live example

2 participants