Skip to content

Commit

Permalink
setup
Browse files Browse the repository at this point in the history
  • Loading branch information
gertvdb committed Jun 29, 2024
1 parent 13ece27 commit a99ac8d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
node_modules
node_modules
.idea
Empty file added build/.gitkeep
Empty file.
78 changes: 78 additions & 0 deletions src/accessibility/preferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
export type prefersReducedMotion = "no-preference" | "reduce";
export type prefersContrast = "no-preference" | "more" | "less" | "custom";
export type prefersReducedTransparency = "no-preference" | "reduce";
export type prefersColorSchema = "light" | "dark";
export type invertedColors = "none" | "inverted";

export class BrowserAccessibilityPreferences {
readonly prefersReducedMotion: prefersReducedMotion;
readonly prefersContrast: prefersContrast;
readonly prefersReducedTransparency: prefersReducedTransparency;
readonly prefersColorSchema: prefersColorSchema;
readonly invertedColors: invertedColors;

constructor(
prefersReducedMotion: prefersReducedMotion,
prefersContrast: prefersContrast,
prefersReducedTransparency: prefersReducedTransparency,
prefersColorSchema: prefersColorSchema,
invertedColors: invertedColors,
) {
this.prefersReducedMotion = prefersReducedMotion;
this.prefersContrast = prefersContrast;
this.prefersReducedTransparency = prefersReducedTransparency;
this.prefersColorSchema = prefersColorSchema;
this.invertedColors = invertedColors;
}

prefersDarkMode(): boolean {
return this.prefersColorSchema === "dark";
}

prefersHighContrast(): boolean {
return this.prefersContrast === "more";
}

prefersLowContrast(): boolean {
return this.prefersContrast === "less";
}
}

const useBrowserAccessibilityPreferences = (): BrowserAccessibilityPreferences => {
const reducedMotion = matchMedia("(prefers-reduced-motion: reduce)").matches
? "reduce"
: "no-preference";

const prefersContrast = matchMedia("(prefers-contrast: more)").matches
? "more"
: matchMedia("(prefers-contrast: less)").matches
? "less"
: matchMedia("(prefers-contrast: custom)").matches
? "custom"
: "no-preference";

const prefersReducedTransparency = matchMedia(
"(prefers-reduced-transparency: reduce)",
).matches
? "reduce"
: "no-preference";

const prefersColorSchema = matchMedia("(prefers-color-scheme: dark)")
.matches
? "dark"
: "light";

const invertedColors = matchMedia("(inverted-colors: inverted)").matches
? "inverted"
: "none";

return new BrowserAccessibilityPreferences(
reducedMotion,
prefersContrast,
prefersReducedTransparency,
prefersColorSchema,
invertedColors,
);
};

export { useBrowserAccessibilityPreferences };
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type { prefersReducedMotion, prefersContrast, prefersReducedTransparency, prefersColorSchema, invertedColors } from "@/accessibility/preferences";
export { BrowserAccessibilityPreferences } from "@/accessibility/preferences";
export { useBrowserAccessibilityPreferences } from "@/accessibility/preferences";

0 comments on commit a99ac8d

Please sign in to comment.