-
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
1 parent
ae42d45
commit 529936e
Showing
7 changed files
with
76 additions
and
0 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
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 { set_prefers_color_scheme } from "./prefers_color_scheme"; |
26 changes: 26 additions & 0 deletions
26
packages/~/app/ui/src/scheme/scripts/prefers_color_scheme.test.ts
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,26 @@ | ||
// | ||
|
||
import { expect, test } from "bun:test"; | ||
import "htmx.org"; | ||
import _hyperscript from "hyperscript.org"; | ||
import { set_prefers_color_scheme } from "./prefers_color_scheme"; | ||
|
||
// | ||
|
||
test("set_prefers_color_scheme", () => { | ||
document.body.innerHTML = ` | ||
<html _="${set_prefers_color_scheme}" ></html> | ||
`; | ||
|
||
_hyperscript.processNode(document.body); | ||
|
||
// | ||
|
||
const html = document.querySelector("html")!; | ||
expect(html.outerHTML).toEqual("My html"); | ||
Check failure on line 20 in packages/~/app/ui/src/scheme/scripts/prefers_color_scheme.test.ts
|
||
// expect(button.getAttribute("disabled")).toBeNull(); | ||
// button.click(); | ||
// expect(button.getAttribute("disabled")).toBe("true"); | ||
// button.dispatchEvent(new Event("htmx:afterOnLoad")); | ||
// expect(button.getAttribute("disabled")).toBeNull(); | ||
}); |
41 changes: 41 additions & 0 deletions
41
packages/~/app/ui/src/scheme/scripts/prefers_color_scheme.ts
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,41 @@ | ||
// | ||
|
||
// | ||
// Inspired by https://github.com/GouvernementFR/dsfr/blob/fe9d66b4ccaa7c3a3c0cb07eca69ddd6787965c4/src/scheme/script/scheme/scheme-boot.js#L14 | ||
// | ||
|
||
export const set_prefers_color_scheme = ` | ||
-- from https://github.com/sqwxl/harfang/blob/afb20cbc60c9eb44d2ce0501a7f754afc7395915/app/templates/partials/theme_toggle.html#L59C33-L84C1 | ||
def nextTheme() | ||
set currentTheme to localStorage.theme | ||
set prefersDark to window.matchMedia("(prefers-color-scheme: dark)").matches | ||
if prefersDark | ||
-- auto(dark) -> light -> dark | ||
if no currentTheme | ||
return "light" | ||
else if currentTheme is "light" | ||
return "dark" | ||
else | ||
return null | ||
end | ||
else | ||
-- auto(light) -> dark -> light | ||
if no currentTheme | ||
return "dark" | ||
else if currentTheme is "dark" | ||
return "light" | ||
else | ||
return null | ||
end | ||
end | ||
end | ||
on load | ||
set theme to nextTheme() | ||
if no theme | ||
call localStorage.removeItem("theme") | ||
else | ||
set localStorage.theme to theme | ||
end | ||
`; |