embed-semantic-navigator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseEmbed Semantic Navigator
嵌入语义导航面板
Choose the package
选择合适的包
| Stack | Package |
|---|---|
| React | |
| Any other / vanilla | |
Install as a devDependency. Do not ship the panel to end users.
| 技术栈 | 包名 |
|---|---|
| React | |
| 其他/原生JS | |
将其安装为devDependency。不要将该面板交付给终端用户。
Keep it out of production
确保不进入生产环境
A top-level static import is not tree-shaken away by wrapping .
Lazy-import the panel (dynamic ) behind a DEV / explicit flag so
production builds drop it.
if (import.meta.env.DEV)import()通过包裹的顶层静态导入无法被 tree-shaking 移除。延迟导入该面板(使用动态),并置于DEV环境或显式标志之后,这样生产构建时就会剔除它。
if (import.meta.env.DEV)import()React
React 场景
tsx
import { lazy, Suspense, useRef } from "react";
const SemanticNavigator = lazy(() =>
import("@real-a11y-dev/react").then((m) => ({
default: m.SemanticNavigator,
})),
);
export function DevAuditOverlay({ children }: { children: React.ReactNode }) {
// Vite: `import.meta.env.DEV`. Webpack/Next: `process.env.NODE_ENV !== "production"`.
if (!import.meta.env.DEV) return <>{children}</>;
const rootRef = useRef<HTMLDivElement>(null);
return (
<div ref={rootRef}>
{children}
<Suspense fallback={null}>
<SemanticNavigator root={rootRef} mode="a11y" floating />
</Suspense>
</div>
);
}root<main>Use as .
<DevAuditOverlay><App /></DevAuditOverlay>Also available: , . Prefer floating props for
overlays; inline layout for split-pane debugging.
useSemanticTreeuseActiveModalNext.js App Router: client-only mount; verify production build has no
/ hydration errors (SSR risk is invisible in a Vite SPA).
Recipe: https://real-a11y.dev/recipes/nextjs
window is not definedtsx
import { lazy, Suspense, useRef } from "react";
const SemanticNavigator = lazy(() =>
import("@real-a11y-dev/react").then((m) => ({
default: m.SemanticNavigator,
})),
);
export function DevAuditOverlay({ children }: { children: React.ReactNode }) {
// Vite: `import.meta.env.DEV`. Webpack/Next: `process.env.NODE_ENV !== "production"`.
if (!import.meta.env.DEV) return <>{children}</>;
const rootRef = useRef<HTMLDivElement>(null);
return (
<div ref={rootRef}>
{children}
<Suspense fallback={null}>
<SemanticNavigator root={rootRef} mode="a11y" floating />
</Suspense>
</div>
);
}root<main>使用方式:。
<DevAuditOverlay><App /></DevAuditOverlay>同时提供以下API:、。对于覆盖层场景,优先使用浮动属性;分栏调试时使用内联布局。
useSemanticTreeuseActiveModalNext.js App Router:仅在客户端挂载;需验证生产构建中不存在或 hydration 错误(在Vite单页应用中SSR风险不可见)。相关方案:https://real-a11y.dev/recipes/nextjs
window is not definedVanilla / any framework
原生JS/任意框架场景
ts
import { createInspector } from "@real-a11y-dev/inspector";
const inspector = createInspector({
root: document.getElementById("app"),
container: document.getElementById("tree-panel"),
viewMode: "a11y", // "dom" | "a11y" | "tab"
theme: "auto",
});
inspector.mount();
// inspector.setRoot / setViewMode / refresh / getTreePrefer when available. Host side-effects (,
etc.) stay opt-in.
mount: "shadow"highlightOnHoverts
import { createInspector } from "@real-a11y-dev/inspector";
const inspector = createInspector({
root: document.getElementById("app"),
container: document.getElementById("tree-panel"),
viewMode: "a11y", // "dom" | "a11y" | "tab"
theme: "auto",
});
inspector.mount();
// inspector.setRoot / setViewMode / refresh / getTree若支持,优先使用。宿主端副作用(如等)保持可选启用。
mount: "shadow"highlightOnHoverRelated
相关资源
- Storybook →
a11y-in-storybook - Assert on trees in tests →
a11y-snapshot-tests
- Storybook →
a11y-in-storybook - 在测试中断言树结构 →
a11y-snapshot-tests