127 lines
4.4 KiB
TypeScript
127 lines
4.4 KiB
TypeScript
import type { Preview, Decorator } from "@storybook/react";
|
|
import { withThemeByClassName } from "@storybook/addon-themes";
|
|
import React from "react";
|
|
import { I18nextProvider } from "react-i18next";
|
|
import { getDirection, supportedLocales } from "@toolctrl/i18n";
|
|
import { getStorybookI18n, setStorybookLocale } from "./i18n";
|
|
import "./preview.css";
|
|
|
|
const LOCALE_TOOLBAR_DESCRIPTION = "Active language - flips Tailwind rtl: variants, <html lang/dir>, and the in-canvas i18next instance";
|
|
const RTL_PREVIEW_TITLE = "AR (RTL preview)";
|
|
|
|
// i18n decorator (#62 Task F + #72 i18n-foundation).
|
|
//
|
|
// Two responsibilities:
|
|
// 1. Sync `<html lang>` / `<html dir>` so any `rtl:` Tailwind variants
|
|
// light up exactly as they would in the running app.
|
|
// 2. Switch the in-canvas i18next instance to the picked locale and wrap
|
|
// every story in `<I18nextProvider>` so `useTranslation` calls in
|
|
// future stories (and any real `packages/ui` components we import
|
|
// once the Tailwind v4 pipeline is wired) resolve against the seeded
|
|
// `apps/web/public/locales` tree.
|
|
//
|
|
// The instance is lazy-built on first render — for the existing 21 stories
|
|
// (none of which call `useTranslation`) this is effectively a no-op beyond
|
|
// updating the html attributes.
|
|
const withLocale: Decorator = (Story, context) => {
|
|
const locale = (context.globals.locale ?? "en") as string;
|
|
const dir = getDirection(locale);
|
|
if (typeof document !== "undefined") {
|
|
document.documentElement.setAttribute("lang", locale);
|
|
document.documentElement.setAttribute("dir", dir);
|
|
}
|
|
setStorybookLocale(locale);
|
|
const i18n = getStorybookI18n();
|
|
return (
|
|
<I18nextProvider i18n={i18n}>
|
|
<Story />
|
|
</I18nextProvider>
|
|
);
|
|
};
|
|
|
|
const withWrapper: Decorator = (Story) => (
|
|
<div className="text-foreground bg-background min-h-[60px] p-6 font-sans text-sm">
|
|
<Story />
|
|
</div>
|
|
);
|
|
|
|
const preview: Preview = {
|
|
parameters: {
|
|
layout: "padded",
|
|
controls: {
|
|
matchers: {
|
|
color: /(background|color)$/i,
|
|
date: /Date$/i,
|
|
},
|
|
},
|
|
backgrounds: {
|
|
default: "light",
|
|
values: [
|
|
{ name: "light", value: "#ffffff" },
|
|
{ name: "dark", value: "#0b0b0c" },
|
|
],
|
|
},
|
|
a11y: {
|
|
// WCAG-2.2-AA target ruleset for live in-Storybook accessibility
|
|
// feedback. Mirrors packages/ui-app/test-setup/axe.ts so devs see the
|
|
// same violations during design as their unit tests will catch.
|
|
//
|
|
// Disable per-story only with `parameters.a11y.disable = true` and a
|
|
// code-comment justification. Spec:
|
|
// docs/improvement-plan/10-compliance-polish/wcag-internal.md
|
|
element: "#storybook-root",
|
|
manual: false,
|
|
config: {
|
|
rules: [
|
|
// High-signal contrast rule — Storybook can compute real CSS so we
|
|
// keep it enabled (jsdom can't, which is why vitest-axe disables it).
|
|
{ id: "color-contrast", enabled: true },
|
|
{ id: "color-contrast-enhanced", enabled: false },
|
|
// Region landmarks aren't meaningful for an isolated component
|
|
// story; verified separately by route-level audits.
|
|
{ id: "region", enabled: false },
|
|
],
|
|
},
|
|
options: {
|
|
runOnly: {
|
|
type: "tag",
|
|
values: ["wcag2a", "wcag2aa", "wcag21a", "wcag21aa", "wcag22aa", "best-practice"],
|
|
},
|
|
reporter: "v2",
|
|
},
|
|
},
|
|
},
|
|
globalTypes: {
|
|
locale: {
|
|
name: "Locale",
|
|
description: LOCALE_TOOLBAR_DESCRIPTION,
|
|
defaultValue: "en",
|
|
toolbar: {
|
|
icon: "globe",
|
|
items: [
|
|
...supportedLocales.map((code) => ({ value: code, title: code.toUpperCase() })),
|
|
// RTL preview locale. We don't ship Arabic translations yet, but
|
|
// the decorator still flips direction so we can spot layout breaks.
|
|
{ value: "ar", title: RTL_PREVIEW_TITLE },
|
|
],
|
|
showName: true,
|
|
dynamicTitle: true,
|
|
},
|
|
},
|
|
},
|
|
decorators: [
|
|
// `withThemeByClassName` registers its own `theme` global + toolbar item
|
|
// (key `theme`, values `light`/`dark`) and applies the class to <html>.
|
|
// We intentionally don't re-declare it in `globalTypes` above to avoid
|
|
// a duplicate toolbar entry — the addon owns that surface.
|
|
withThemeByClassName({
|
|
themes: { light: "light", dark: "dark" },
|
|
defaultTheme: "light",
|
|
}),
|
|
withLocale,
|
|
withWrapper,
|
|
],
|
|
};
|
|
|
|
export default preview;
|