Skip to content

Commit a6a6ca6

Browse files
committed
theme switch: Improve AlpineJS's code formatting
1 parent 91caf14 commit a6a6ca6

1 file changed

Lines changed: 26 additions & 22 deletions

File tree

src/web/FloraWeb/Components/Navbar.hs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,35 @@ import FloraWeb.Pages.Templates.Types
1414

1515
navbar :: FloraHTML
1616
navbar = do
17+
let xData =
18+
[str|
19+
{
20+
updateTheme() {
21+
const customTheme = document.documentElement.getAttribute('data-theme');
22+
const isSystemDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
23+
const applyTheme = (theme) => {
24+
document.documentElement.setAttribute('data-theme', theme);
25+
(async () => { await cookieStore.set('theme', theme) })();
26+
};
27+
switch (customTheme) {
28+
case 'light':
29+
applyTheme('dark');
30+
break;
31+
case 'dark':
32+
applyTheme('light');
33+
break;
34+
default:
35+
isSystemDark ? applyTheme('light') : applyTheme('dark');
36+
break;
37+
}
38+
}
39+
}
40+
|]
41+
1742
ActiveElements{aboutNav, packagesNav} <- asks activeElements
1843
nav_
1944
[ class_ "top-navbar"
20-
, xData_
21-
"{ updateTheme() { \
22-
\ const customTheme = document.documentElement.getAttribute('data-theme'); \
23-
\ const isSystemDark = window.matchMedia('(prefers-color-scheme: dark)').matches; \
24-
\ const applyTheme = (theme) => { \
25-
\ document.documentElement.setAttribute('data-theme', theme); \
26-
\ console.log(\"theme switch\"); \
27-
\ (async () => { await cookieStore.set('theme', theme) })(); \
28-
\ }; \
29-
\ switch (customTheme) { \
30-
\ case 'light': \
31-
\ applyTheme('dark'); \
32-
\ break; \
33-
\ case 'dark': \
34-
\ applyTheme('light'); \
35-
\ break; \
36-
\ default: \
37-
\ isSystemDark ? applyTheme('light') : applyTheme('dark'); \
38-
\ break; \
39-
\ } \
40-
\ } \
41-
\}"
45+
, xData_ xData
4246
]
4347
$ do
4448
div_ [class_ "navbar-content"] $ do

0 commit comments

Comments
 (0)