Skip to content

Commit 9329dcd

Browse files
marcin-hoaDamianBrzezinskiHoA
authored andcommitted
feat: added theme switch and fixed cookies consent logic
1 parent e97641f commit 9329dcd

File tree

6 files changed

+26
-30
lines changed

6 files changed

+26
-30
lines changed

libs/blog/app-theme/data-access-app-theme/src/app-theme.store.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface AppThemeStore {
1010

1111
export const AppThemeStore = signalStore(
1212
{ providedIn: 'root' },
13-
withState<AppThemeStore>({ theme: 'light' }),
13+
withState<AppThemeStore>({ theme: 'dark' }),
1414
withMethods(
1515
(
1616
store,
@@ -19,15 +19,19 @@ export const AppThemeStore = signalStore(
1919
) => ({
2020
syncWithSystemTheme: () => {
2121
if (isPlatformBrowser(platformId)) {
22-
const theme = getSystemTheme();
22+
const theme =
23+
(localStorage.getItem('theme') as Theme) ?? getSystemTheme();
2324
ccConsumer.setThemeAttribute(theme);
2425
patchState(store, { theme: theme });
2526
}
2627
},
2728
toggleTheme: () => {
28-
const theme = store.theme() === 'dark' ? 'light' : 'dark';
29-
ccConsumer.setThemeAttribute(theme);
30-
patchState(store, { theme: theme });
29+
if (isPlatformBrowser(platformId)) {
30+
const newTheme = store.theme() === 'dark' ? 'light' : 'dark';
31+
ccConsumer.setThemeAttribute(newTheme);
32+
localStorage.setItem('theme', newTheme);
33+
patchState(store, { theme: newTheme });
34+
}
3135
},
3236
}),
3337
),
@@ -44,5 +48,13 @@ function getSystemTheme(): Theme {
4448
export class CCAppThemeConsumer {
4549
setThemeAttribute(theme: Theme): void {
4650
document.documentElement.setAttribute('data-theme', theme);
51+
52+
const classList = document.documentElement.classList;
53+
54+
if (theme === 'dark') {
55+
classList.add('cc--darkmode');
56+
} else {
57+
classList.remove('cc--darkmode');
58+
}
4759
}
4860
}

libs/blog/articles/ui-article-content/src/lib/article-content/article-content.component.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
margin-top: 1.6rem;
66
}
77

8-
@media (prefers-color-scheme: dark) {
8+
:root[data-theme='dark'] {
99
.shiki,
1010
.shiki span {
1111
color: var(--shiki-dark) !important;

libs/blog/articles/ui-article-content/src/lib/article-content/article-content.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { DomSanitizer } from '@angular/platform-browser';
1212
selector: 'al-article-content',
1313
templateUrl: './article-content.component.html',
1414
styleUrl: './article-content.component.scss',
15-
imports: [],
1615
encapsulation: ViewEncapsulation.None,
1716
changeDetection: ChangeDetectionStrategy.OnPush,
1817
})

libs/shared/assets/src/lib/styles/cookies-consent.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/** Also make toggles the same color as the button **/
1818
--cc-toggle-on-bg: var(--cc-btn-primary-bg);
1919

20-
@media (prefers-color-scheme: light) {
20+
:root[data-theme='light'] {
2121
--cc-btn-secondary-color: rgba(44, 47, 49, 1);
2222
}
2323
}

libs/shared/assets/src/lib/styles/main.scss

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
@tailwind components;
55
@tailwind utilities;
66

7+
@custom-variant dark (&:where([data-theme="dark"], [data-theme="dark"] *));
8+
@custom-variant light (&:where([data-theme="light"], [data-theme="light"] *));
9+
710
@layer base {
811
:root[data-theme='dark'] {
912
--primary: 255 0 106;
@@ -28,7 +31,7 @@
2831
--foreground: 20 21 27;
2932
--primary-foreground: 0 0 0;
3033
--muted: 25 25 25;
31-
--border: 200 200 200;
34+
--border: 229 231 235;
3235
--card: 255 255 255;
3336
--background: 255 255 255;
3437
--grey: 241 241 241;
@@ -87,8 +90,8 @@ html {
8790
}
8891
}
8992

90-
@layer components {
93+
@layer base {
9194
.al-link {
92-
@apply text-blue-600 hover:underline;
95+
@apply text-red-500 hover:underline dark:text-blue-400;
9396
}
9497
}

tailwind.preset.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
const {
2-
themeVariants,
3-
prefersLight,
4-
prefersDark,
5-
} = require('tailwindcss-theme-variants');
6-
71
/** @type {import('tailwindcss').Config} */
82
module.exports = {
3+
darkMode: 'selector',
94
theme: {
105
extend: {
116
translate: {
@@ -51,17 +46,4 @@ module.exports = {
5146
},
5247
},
5348
},
54-
plugins: [
55-
require('@tailwindcss/container-queries'),
56-
themeVariants({
57-
themes: {
58-
light: {
59-
mediaQuery: prefersLight /* "@media (prefers-color-scheme: light)" */,
60-
},
61-
dark: {
62-
mediaQuery: prefersDark /* "@media (prefers-color-scheme: dark)" */,
63-
},
64-
},
65-
}),
66-
],
6749
};

0 commit comments

Comments
 (0)