Skip to content

Commit c4813e6

Browse files
committed
do theme change by listening to theme change event in Giscus component
1 parent 1089657 commit c4813e6

File tree

6 files changed

+27
-13
lines changed

6 files changed

+27
-13
lines changed

docs/working-notes/todo2.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ https://github.dev/syhily/yufan.me
177177
// interesting design
178178
https://github.com/joshmedeski/joshmedeski.com
179179

180+
// clean styles, no github repo
181+
// astrowind zapravo data-aw-social-share
182+
https://thezal.dev/blog/
183+
180184
// notion cms
181185
https://feather.so/showcase
182186
https://bhanuteja.dev/blog

docs/working-notes/todo3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,5 +409,6 @@ search with fuse.js astro-paper
409409
fix lint, types, format
410410
put images beside .mdx in content, slug...
411411
style giscus, tags and share
412+
move giscus mode, add another theme change event handler
412413
```
413414

src/components/Giscus.astro

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,25 @@
2323
import 'giscus';
2424

2525
import { SELECTORS } from '@/constants/dom';
26-
import { getCurrentMode } from '@/utils/dom';
26+
import { THEME_CONFIG } from '@/constants/themes';
27+
import { getCurrentMode, sendModeToGiscus } from '@/utils/dom';
28+
29+
import type { ChangeThemeCustomEvent } from '@/types/constants';
2730

2831
const { GISCUS_WIDGET_SELECTOR } = SELECTORS;
32+
const { CHANGE_EVENT } = THEME_CONFIG;
2933

3034
// will run only on page load
3135
const giscusWidget = document.querySelector(GISCUS_WIDGET_SELECTOR);
3236
const mode = getCurrentMode();
3337

3438
if (giscusWidget) giscusWidget.setAttribute('theme', mode);
39+
40+
// will run on button click
41+
document.addEventListener(CHANGE_EVENT, (event) => {
42+
const customEvent = event as ChangeThemeCustomEvent;
43+
const { mode } = customEvent.detail.theme;
44+
45+
sendModeToGiscus(mode);
46+
});
3547
</script>

src/components/ThemeToggle.astro

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import { Icon } from 'astro-icon/components';
1111

1212
<script>
1313
import { THEME_CONFIG } from '@/constants/themes';
14-
import { getNextTheme, sendModeToGiscus } from '@/utils/dom';
14+
import { getNextTheme } from '@/utils/dom';
15+
16+
import type { ChangeThemeCustomEvent } from '@/types/constants';
1517

1618
const { CHANGE_EVENT } = THEME_CONFIG;
1719

@@ -33,18 +35,13 @@ import { Icon } from 'astro-icon/components';
3335
() => {
3436
// shift theme
3537
const nextTheme = getNextTheme();
36-
const themeChangeEvent = new CustomEvent(CHANGE_EVENT, {
37-
detail: { theme: nextTheme },
38-
});
38+
const payload = { detail: { theme: nextTheme } } as ChangeThemeCustomEvent;
39+
const themeChangeEvent = new CustomEvent(CHANGE_EVENT, payload);
3940
// dispatch event -> ThemeProvider.astro
4041
document.dispatchEvent(themeChangeEvent);
4142

4243
// set the aria-checked attribute
4344
// button.setAttribute('aria-checked', String(isDarkMode()));
44-
45-
// send message with delay to Giscus here
46-
// ThemeScript has <script is:inline /> and can't import files
47-
setTimeout(() => sendModeToGiscus(), 50);
4845
},
4946
{ signal }
5047
);

src/types/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ export type ImageSizes = {
1919
FIXED: Record<string, Pick<LocalImageProps, 'width' | 'height'>>;
2020
RESPONSIVE: Record<string, Pick<LocalImageProps, 'widths' | 'sizes'>>;
2121
};
22+
23+
export type ChangeThemeCustomEvent = CustomEvent<{ theme: Theme }>;

src/utils/dom.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SELECTORS } from '@/constants/dom';
22
import { DEFAULT_THEMES, MODES, THEME_CONFIG, THEMES } from '@/constants/themes';
33

4-
import type { Theme } from '@/types/constants';
4+
import type { Mode, Theme } from '@/types/constants';
55

66
const { MODE_CLASS, DATA_ATTRIBUTE } = THEME_CONFIG;
77

@@ -39,7 +39,7 @@ export const getNextTheme = () => {
3939

4040
const { GISCUS_WIDGET_SELECTOR, GISCUS_IFRAME_SELECTOR } = SELECTORS;
4141

42-
export const sendModeToGiscus = (): void => {
42+
export const sendModeToGiscus = (mode: Mode): void => {
4343
const giscusIframeUrl = 'https://giscus.app';
4444

4545
const shadowHost = document.querySelector(GISCUS_WIDGET_SELECTOR);
@@ -49,7 +49,5 @@ export const sendModeToGiscus = (): void => {
4949
const iframe = shadowRoot.querySelector(GISCUS_IFRAME_SELECTOR) as HTMLIFrameElement;
5050
if (!iframe?.contentWindow) return;
5151

52-
const mode = getCurrentMode();
53-
5452
iframe.contentWindow.postMessage({ giscus: { setConfig: { theme: mode } } }, giscusIframeUrl);
5553
};

0 commit comments

Comments
 (0)