-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.env | ||
node_modules | ||
node_modules | ||
.idea |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |