-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #583 from getformwork/feature/improved-color-schemes
Improve color schemes handling
- Loading branch information
Showing
19 changed files
with
141 additions
and
81 deletions.
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
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
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,19 @@ | ||
<?php | ||
|
||
namespace Formwork\Users; | ||
|
||
enum ColorScheme: string | ||
{ | ||
case Light = 'light'; | ||
case Dark = 'dark'; | ||
case Auto = 'auto'; | ||
|
||
public function getCompatibleSchemes(): string | ||
{ | ||
return match ($this) { | ||
self::Light => 'light', | ||
self::Dark => 'dark', | ||
self::Auto => 'light dark', | ||
}; | ||
} | ||
} |
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
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
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
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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,30 +1,59 @@ | ||
import { getCookies, setCookie } from "../utils/cookies"; | ||
import { $ } from "../utils/selectors"; | ||
import { app } from "../app"; | ||
|
||
type ColorSchemeName = "light" | "dark"; | ||
type ColorSchemePreference = ColorSchemeName | "light dark"; | ||
|
||
export class ColorScheme { | ||
constructor() { | ||
const setPreferredColorScheme = () => { | ||
const getSupportedColorSchemes = (): ColorSchemePreference => { | ||
const meta = $("meta[name=color-scheme]") as HTMLMetaElement; | ||
if (!meta) { | ||
return "light dark"; | ||
} | ||
switch (meta.content) { | ||
case "light": | ||
return "light"; | ||
case "dark": | ||
return "dark"; | ||
default: | ||
return "light dark"; | ||
} | ||
}; | ||
|
||
const setColorScheme = (colorScheme: ColorSchemeName) => { | ||
document.documentElement.classList.remove("color-scheme-light", "color-scheme-dark"); | ||
document.documentElement.classList.add(`color-scheme-${colorScheme}`); | ||
}; | ||
|
||
const setPreferredColorScheme = (event: Event) => { | ||
const cookies = getCookies(); | ||
const cookieName = "formwork_preferred_color_scheme"; | ||
const oldValue = cookieName in cookies ? cookies[cookieName] : null; | ||
let value: "light" | "dark" = "light"; | ||
let colorScheme: ColorSchemeName = "light"; | ||
|
||
if (window.matchMedia("(prefers-color-scheme: light)").matches) { | ||
value = "light"; | ||
colorScheme = "light"; | ||
} else if (window.matchMedia("(prefers-color-scheme: dark)").matches) { | ||
value = "dark"; | ||
colorScheme = "dark"; | ||
} | ||
|
||
if (value && value !== oldValue) { | ||
setCookie(cookieName, value, { | ||
if (colorScheme && colorScheme !== oldValue) { | ||
setCookie(cookieName, colorScheme, { | ||
"max-age": 2592000, // 1 month | ||
path: app.config.baseUri, | ||
samesite: "strict", | ||
}); | ||
|
||
if (event.type === "change" && getSupportedColorSchemes() === "light dark") { | ||
setColorScheme(colorScheme); | ||
} | ||
} | ||
}; | ||
|
||
window.addEventListener("beforeunload", setPreferredColorScheme); | ||
window.addEventListener("pagehide", setPreferredColorScheme); | ||
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", setPreferredColorScheme); | ||
} | ||
} |
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
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
Oops, something went wrong.