From 3bba0a5b70967233d0e61b3eac5c4a0b7abe0b44 Mon Sep 17 00:00:00 2001 From: Eason Date: Sat, 20 Jul 2024 09:29:33 +0800 Subject: [PATCH] feat: change font listener --- dictionary-web-app/src/lib/Header.svelte | 52 ++++++++++++++++-------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/dictionary-web-app/src/lib/Header.svelte b/dictionary-web-app/src/lib/Header.svelte index c16e322..672eb4b 100644 --- a/dictionary-web-app/src/lib/Header.svelte +++ b/dictionary-web-app/src/lib/Header.svelte @@ -4,11 +4,20 @@ const fontList: string[] = ['Sans Serif', 'Serif', 'Mono']; const dispatch = createEventDispatcher(); let isDarkMode: boolean = false; + let showFontSelects: boolean = false; let activeFontIndex: number = 0; + const hideFontSelects = (event: MouseEvent) => { + showFontSelects = false; + }; $: activeFont = fontList[activeFontIndex]; $: dispatch('fontChange', { newFontStyle: activeFont, }); + $: if (showFontSelects) { + window.addEventListener('click', hideFontSelects); + } else { + window.removeEventListener('click', hideFontSelects); + }
@@ -17,25 +26,36 @@
- -
- {#each fontList as font, index} - - {/each} -
+ {#if showFontSelects} +
+ {#each fontList as font, index} + + {/each} +
+ {/if}