Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/dist-release/src/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const LICENSE = {
// What each tarball may hold besides package.json, README.md and LICENSE, whatever its `files` says.
export const PACKAGE_ALLOW = {
default: ["dist/"],
"docs-theme": ["src/index.css", "src/brand.css", "assets/"],
"docs-theme": ["src/index.css", "src/brand.css", "src/color-vision.css", "assets/"],
};

export const KIT_NAME = "coder-docs-builder";
Expand Down
1 change: 1 addition & 0 deletions apps/dist-release/src/contents.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ test("the allowlist comes from config, not from the package's files field", () =
checkPackageEntries("docs-theme", [
"package/src/index.css",
"package/src/brand.css",
"package/src/color-vision.css",
"package/assets/favicons/favicon-dark.svg",
"package/src/tokens.ts",
]),
Expand Down
4 changes: 3 additions & 1 deletion apps/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,9 @@ Unpause the project only after that setting is on `main`, or the next push to `m
The pages are `/`, `/admin/users`, `/admin/reference`, `/@0.1/admin`, and the `/authoring` section, whose pages carry tabs (with labels of several words), fenced code in several languages with comments, wide tables (two of them under one heading), a table with an empty header cell and a ✅/❌ support matrix, every callout type with inline code in it (some highlighted), feature-state pills, a GFM task list, and an image with alt text.
Two of them, `/` and `/authoring/tables`, are also scanned at a 390px mobile viewport with touch, in both modes, as `mobile-light` and `mobile-dark`.
Every desktop page is also scanned with the Contrast setting on More, as `more-light` and `more-dark`, with axe's `color-contrast-enhanced` rule on, so More is held to WCAG AAA contrast (7:1 for text).
The open search dialog is scanned in every mode, and the open display settings popover in the four desktop modes, as `/#search` and `/#settings`.
The pages with callouts, state pills, ✅/❌ and a diff (`/authoring/callouts`, `/authoring/states`, `/authoring/code`, `/authoring/tables`) are scanned again with Color vision on Protanopia and deuteranopia, as `vision-light` and `vision-dark`, and with More contrast on top, as `vision-more-light` and `vision-more-dark`.
The same pages are scanned with Color vision on Tritanopia, as `vision-tritan-light`, `vision-tritan-dark`, `vision-tritan-more-light` and `vision-tritan-more-dark`.
The open search dialog is scanned in every desktop and mobile mode, and the open display settings popover in the four desktop modes and the eight color vision modes, as `/#search` and `/#settings`.

It fails on any WCAG violation not listed in `.github/a11y-allowlist.json`.
Each entry is `<owner> axe <mode> <page> <rule>`, where the owner is `pr-<number>` for the open pull request that fixes it, or `tracked-<topic>` for a known failure whose fix has no pull request yet.
Expand Down
8 changes: 8 additions & 0 deletions apps/docs/fixtures/docs/authoring/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,11 @@ No highlighting here.
```

Inline code such as `coder server --help` sits in running text.

## Diff

```diff
- replicaCount: 1
+ replicaCount: 2
image: codercom/coder
```
2 changes: 1 addition & 1 deletion apps/docs/fixtures/docs/authoring/states.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

A page tagged with feature states in the manifest, so its title carries the state pills.

The feature is in early access and beta at once, which exercises two pill tones side by side.
The page carries a pill of every tone, early access, beta, deprecated, enterprise, and premium, side by side.
2 changes: 1 addition & 1 deletion apps/docs/fixtures/docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{
"title": "Feature states",
"path": "./authoring/states.md",
"state": ["early access", "beta"]
"state": ["early access", "beta", "deprecated", "enterprise", "premium"]
}
]
}
Expand Down
30 changes: 28 additions & 2 deletions apps/docs/scripts/a11y-axe-helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ export const MODES = ["light", "dark"];
export const MOBILE_MODES = MODES.map((mode) => `mobile-${mode}`);
// The More contrast setting, held to WCAG AAA contrast.
export const MORE_MODES = MODES.map((mode) => `more-${mode}`);
// The color vision setting (protanopia and deuteranopia), alone and with More contrast.
export const VISION_MODES = MODES.map((mode) => `vision-${mode}`);
export const VISION_MORE_MODES = MODES.map((mode) => `vision-more-${mode}`);
// The same, for Tritanopia.
export const VISION_TRITAN_MODES = MODES.map((mode) => `vision-tritan-${mode}`);
export const VISION_TRITAN_MORE_MODES = MODES.map((mode) => `vision-tritan-more-${mode}`);
// The pages with callouts, state pills, ✅/❌ and a diff, where the setting recolors content.
export const COLOR_VISION_PAGES = [
"/authoring/callouts",
"/authoring/states",
"/authoring/code",
"/authoring/tables",
];
const AAA_CONTRAST_RULE = "color-contrast-enhanced";
export const MOBILE_VIEWPORT = { width: 390, height: 844 };

Expand Down Expand Up @@ -91,9 +104,22 @@ export const violationId = (mode, page, rule) => `axe ${mode} ${page} ${rule}`;

export const idOf = (v) => violationId(v.mode, v.page, v.rule);

export const colorSchemeOf = (mode) => mode.replace(/^(?:mobile|more)-/, "");
export const colorSchemeOf = (mode) =>
mode.replace(/^(?:mobile-|more-|vision-tritan-more-|vision-tritan-|vision-more-|vision-)/, "");

export const contrastOf = (mode) =>
MORE_MODES.includes(mode) ||
VISION_MORE_MODES.includes(mode) ||
VISION_TRITAN_MORE_MODES.includes(mode)
? "more"
: undefined;

export const contrastOf = (mode) => (MORE_MODES.includes(mode) ? "more" : undefined);
export function colorVisionOf(mode) {
if (VISION_MODES.includes(mode) || VISION_MORE_MODES.includes(mode)) return "protan-deuter";
if (VISION_TRITAN_MODES.includes(mode) || VISION_TRITAN_MORE_MODES.includes(mode))
return "tritan";
return undefined;
}

export function axeOptions(mode) {
return {
Expand Down
35 changes: 32 additions & 3 deletions apps/docs/scripts/a11y-axe.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DEFAULT_MOBILE_PAGES,
MOBILE_MODES,
MOBILE_VIEWPORT,
COLOR_VISION_PAGES,
MODES,
MORE_MODES,
SEARCH_LABEL,
Expand All @@ -13,9 +14,14 @@ import {
SETTINGS_LABEL,
SWITCHER_DOCUMENT,
SWITCHER_LABEL,
VISION_MODES,
VISION_MORE_MODES,
VISION_TRITAN_MODES,
VISION_TRITAN_MORE_MODES,
applyAllowlist,
axeOptions,
colorSchemeOf,
colorVisionOf,
contrastOf,
parseArgs,
splitBestPractice,
Expand Down Expand Up @@ -77,13 +83,20 @@ async function scan({
}) {
const scheme = colorSchemeOf(mode);
const contrast = contrastOf(mode);
const colorVision = colorVisionOf(mode);
const mobile = MOBILE_MODES.includes(mode)
? { viewport: MOBILE_VIEWPORT, isMobile: true, hasTouch: true, deviceScaleFactor: 3 }
: {};
const context = await browser.newContext({ colorScheme: scheme, bypassCSP: true, ...mobile });
// The key is DISPLAY_PREFERENCES.contrast.storageKey in docs-next; the data-contrast check below catches a drift.
if (contrast)
await context.addInitScript((value) => localStorage.setItem("docs-contrast", value), contrast);
// Likewise DISPLAY_PREFERENCES.colorVision.storageKey, with the data-color-vision check below.
if (colorVision)
await context.addInitScript(
(value) => localStorage.setItem("docs-color-vision", value),
colorVision,
);
try {
const page = await context.newPage();
if (switcher)
Expand All @@ -93,8 +106,13 @@ async function scan({
const rendered = await page.evaluate(() => ({
dark: document.documentElement.classList.contains("dark"),
contrast: document.documentElement.getAttribute("data-contrast") ?? undefined,
colorVision: document.documentElement.getAttribute("data-color-vision") ?? undefined,
}));
if (rendered.dark !== (scheme === "dark") || rendered.contrast !== contrast)
if (
rendered.dark !== (scheme === "dark") ||
rendered.contrast !== contrast ||
rendered.colorVision !== colorVision
)
throw new Error(`${pagePath} did not render in ${mode} mode`);
await page.addScriptTag({ content: axeSource });
if (search) await openSearchWithMatch(page);
Expand Down Expand Up @@ -138,15 +156,26 @@ async function main() {
executablePath: options.chromium ?? process.env.A11Y_CHROMIUM ?? undefined,
});
const desktopModes = [...MODES, ...MORE_MODES];
const visionModes = [
...VISION_MODES,
...VISION_MORE_MODES,
...VISION_TRITAN_MODES,
...VISION_TRITAN_MORE_MODES,
];
const runs = [
...options.pages.flatMap((page) => desktopModes.map((mode) => ({ page, mode }))),
...COLOR_VISION_PAGES.flatMap((page) => visionModes.map((mode) => ({ page, mode }))),
...mobilePages.flatMap((page) => MOBILE_MODES.map((mode) => ({ page, mode }))),
...[...desktopModes, ...MOBILE_MODES].map((mode) => ({
page: SEARCH_PAGE,
mode,
search: true,
})),
...desktopModes.map((mode) => ({ page: SEARCH_PAGE, mode, settings: true })),
...[...desktopModes, ...visionModes].map((mode) => ({
page: SEARCH_PAGE,
mode,
settings: true,
})),
...desktopModes.map((mode) => ({ page: SEARCH_PAGE, mode, switcher: true })),
];
const found = [];
Expand Down Expand Up @@ -175,7 +204,7 @@ async function main() {
}
for (const entry of stale) console.log(`stale allowlist entry, remove it: ${entry}`);
console.log(
`\n${runs.length} scans (${options.pages.length} pages x ${desktopModes.length} modes, ${mobilePages.length} x ${MOBILE_MODES.length} at ${MOBILE_VIEWPORT.width}px, the open search dialog in all ${desktopModes.length + MOBILE_MODES.length}, and the open display settings and version switcher in ${desktopModes.length} each): ` +
`\n${runs.length} scans (${options.pages.length} pages x ${desktopModes.length} modes, ${COLOR_VISION_PAGES.length} x ${visionModes.length} with color vision on, ${mobilePages.length} x ${MOBILE_MODES.length} at ${MOBILE_VIEWPORT.width}px, the open search dialog in all ${desktopModes.length + MOBILE_MODES.length}, and the open display settings in all ${desktopModes.length + visionModes.length}, and the open version switcher in ${desktopModes.length}): ` +
`${violations.length} WCAG rule violations, ${allowed.length} allowlisted, ${failures.length} new; ` +
`${bestPractice.length} best-practice findings (reported, not failing).`,
);
Expand Down
55 changes: 55 additions & 0 deletions apps/docs/scripts/a11y-axe.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { test } from "node:test";
import {
DEFAULT_MOBILE_PAGES,
DEFAULT_PAGES,
COLOR_VISION_PAGES,
MORE_MODES,
VISION_MODES,
VISION_MORE_MODES,
VISION_TRITAN_MODES,
VISION_TRITAN_MORE_MODES,
SEARCH_LABEL,
SEARCH_PAGE,
SEARCH_QUERY,
Expand All @@ -15,6 +20,7 @@ import {
applyAllowlist,
axeOptions,
colorSchemeOf,
colorVisionOf,
contrastOf,
idOf,
parseArgs,
Expand Down Expand Up @@ -169,6 +175,55 @@ test("More contrast modes scan both color schemes with the AAA contrast rule ena
assert.deepEqual(axeOptions("light").runOnly, axeOptions("more-light").runOnly);
});

test("color vision modes scan both color schemes, standard and More, on the pages that carry color", () => {
assert.deepEqual(VISION_MODES, ["vision-light", "vision-dark"]);
assert.deepEqual(VISION_MORE_MODES, ["vision-more-light", "vision-more-dark"]);
for (const mode of [...VISION_MODES, ...VISION_MORE_MODES])
assert.equal(colorVisionOf(mode), "protan-deuter", mode);
assert.deepEqual([...VISION_MODES, ...VISION_MORE_MODES].map(colorSchemeOf), [
"light",
"dark",
"light",
"dark",
]);
assert.deepEqual(VISION_MORE_MODES.map(contrastOf), ["more", "more"]);
assert.equal(contrastOf("vision-light"), undefined);
assert.equal(colorVisionOf("more-dark"), undefined);
assert.equal(colorVisionOf("light"), undefined);
assert.deepEqual(axeOptions("vision-more-dark").rules, {
"color-contrast-enhanced": { enabled: true },
});
assert.equal(axeOptions("vision-dark").rules, undefined);
for (const page of COLOR_VISION_PAGES) assert.ok(DEFAULT_PAGES.includes(page), page);
for (const page of [
"/authoring/callouts",
"/authoring/states",
"/authoring/code",
"/authoring/tables",
])
assert.ok(
COLOR_VISION_PAGES.includes(page),
`${page} carries callouts, pills, a diff or ✅/❌`,
);
});

test("tritan modes scan both color schemes, standard and More", () => {
assert.deepEqual(VISION_TRITAN_MODES, ["vision-tritan-light", "vision-tritan-dark"]);
assert.deepEqual(VISION_TRITAN_MORE_MODES, [
"vision-tritan-more-light",
"vision-tritan-more-dark",
]);
const all = [...VISION_TRITAN_MODES, ...VISION_TRITAN_MORE_MODES];
for (const mode of all) assert.equal(colorVisionOf(mode), "tritan", mode);
assert.deepEqual(all.map(colorSchemeOf), ["light", "dark", "light", "dark"]);
assert.deepEqual(all.map(contrastOf), [undefined, undefined, "more", "more"]);
assert.equal(colorVisionOf("vision-dark"), "protan-deuter");
assert.deepEqual(axeOptions("vision-tritan-more-dark").rules, {
"color-contrast-enhanced": { enabled: true },
});
assert.equal(axeOptions("vision-tritan-dark").rules, undefined);
});

test("splitBestPractice fails the AAA contrast rule", () => {
const { wcag } = splitBestPractice([
{ rule: "color-contrast-enhanced", tags: ["cat.color", "wcag2aaa", "wcag146"] },
Expand Down
21 changes: 21 additions & 0 deletions packages/docs-next/src/display-preferences.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ afterEach(() => {
if (storageDescriptor) Object.defineProperty(globalThis, "localStorage", storageDescriptor);
localStorage.clear();
document.documentElement.removeAttribute("data-contrast");
document.documentElement.removeAttribute("data-color-vision");
document.documentElement.removeAttribute("data-motion");
document.documentElement.removeAttribute("class");
});
Expand Down Expand Up @@ -62,6 +63,26 @@ test("an unknown stored value is ignored", () => {
assert.equal(html.hasAttribute("data-motion"), false);
});

test("a stored color-vision choice is copied onto <html>, and anything else is ignored", () => {
localStorage.setItem("docs-color-vision", "protan-deuter");
assert.equal(runRenderedScript().getAttribute("data-color-vision"), "protan-deuter");
document.documentElement.removeAttribute("data-color-vision");
localStorage.setItem("docs-color-vision", "tritan");
assert.equal(runRenderedScript().getAttribute("data-color-vision"), "tritan");
document.documentElement.removeAttribute("data-color-vision");
for (const stored of ["tritanopia", "Tritan", "Protan-Deuter", "", "system"]) {
localStorage.setItem("docs-color-vision", stored);
assert.equal(runRenderedScript().hasAttribute("data-color-vision"), false, stored);
}
});

test("color vision uses its own key and leaves contrast and motion alone", () => {
localStorage.setItem("docs-color-vision", "protan-deuter");
const html = runRenderedScript();
assert.equal(html.hasAttribute("data-contrast"), false);
assert.equal(html.hasAttribute("data-motion"), false);
});

test("one preference set leaves the other off", () => {
localStorage.setItem("docs-motion", "reduced");
const html = runRenderedScript();
Expand Down
8 changes: 7 additions & 1 deletion packages/docs-next/src/display-preferences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ export const DISPLAY_PREFERENCES = {
attribute: "data-contrast",
values: ["standard", "more"],
},
colorVision: {
storageKey: "docs-color-vision",
attribute: "data-color-vision",
values: ["standard", "protan-deuter", "tritan"],
},
motion: MOTION_PREFERENCE,
} as const;

export type DisplayPreference = keyof typeof DISPLAY_PREFERENCES;
export type ContrastPreference = (typeof DISPLAY_PREFERENCES.contrast.values)[number];
export type ColorVisionPreference = (typeof DISPLAY_PREFERENCES.colorVision.values)[number];
export type MotionPreference = (typeof DISPLAY_PREFERENCES.motion.values)[number];

/** The next-themes key and the only values the theme layer accepts; anything else is cleared before it can reach `<html>`. */
Expand All @@ -22,7 +28,7 @@ export const THEME_PREFERENCE = {
} as const;

/** Clears an unrecognised stored theme, then copies the stored preferences onto `<html>` before first paint; a storage error means follow the system. */
export const DISPLAY_PREFERENCES_SCRIPT = `(function(){var d=document.documentElement;var t=["theme",["system","light","dark"]];try{var s=localStorage.getItem(t[0]);if(s!==null&&t[1].indexOf(s)<0)localStorage.removeItem(t[0])}catch(e){}[["docs-contrast","data-contrast",["standard","more"]],["docs-motion","data-motion",["full","reduced"]]].forEach(function(p){try{var v=localStorage.getItem(p[0]);if(p[2].indexOf(v)>=0)d.setAttribute(p[1],v)}catch(e){}})})()`;
export const DISPLAY_PREFERENCES_SCRIPT = `(function(){var d=document.documentElement;var t=["theme",["system","light","dark"]];try{var s=localStorage.getItem(t[0]);if(s!==null&&t[1].indexOf(s)<0)localStorage.removeItem(t[0])}catch(e){}[["docs-contrast","data-contrast",["standard","more"]],["docs-color-vision","data-color-vision",["standard","protan-deuter","tritan"]],["docs-motion","data-motion",["full","reduced"]]].forEach(function(p){try{var v=localStorage.getItem(p[0]);if(p[2].indexOf(v)>=0)d.setAttribute(p[1],v)}catch(e){}})})()`;

/** The inline script above, for a root layout's `<head>`. */
export function DisplayPreferencesScript(): ReactElement {
Expand Down
Loading
Loading