From 39735309b2f2831ea8fe94104139fa0e9f6ace56 Mon Sep 17 00:00:00 2001
From: ivarlovlie <git@ivarlovlie.no>
Date: Sun, 12 Jun 2022 14:39:37 +0200
Subject: refactor: Close blowout toolbelt on escape or click outside of
 [data-blowout-toolbelt-element]

---
 .../src/components/blowout-toolbelt.svelte           | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

(limited to 'apps/web-shared/src/components/blowout-toolbelt.svelte')

diff --git a/apps/web-shared/src/components/blowout-toolbelt.svelte b/apps/web-shared/src/components/blowout-toolbelt.svelte
index 2927d70..b83048c 100644
--- a/apps/web-shared/src/components/blowout-toolbelt.svelte
+++ b/apps/web-shared/src/components/blowout-toolbelt.svelte
@@ -1,4 +1,5 @@
 <script>
+	import { onMount } from "svelte";
 	import ThemeSwitcher from "./theme-switcher.svelte";
 	import ThemeSwitcherIcon from "./theme-switcher-icon.svelte";
 	import LocaleSwitcher from "./locale-switcher.svelte";
@@ -15,6 +16,19 @@
 		show: false,
 		selection: "system"
 	};
+
+	onMount(() => {
+		document.addEventListener("keydown", (e) => {
+			if (e.code === "Escape") {
+				expanded = false;
+			}
+		});
+		document.addEventListener("click", (e) => {
+			if (e.target.closest("[data-blowout-toolbelt-element]") === null) {
+				expanded = false;
+			}
+		});
+	});
 </script>
 <style>
     .blowout {
@@ -25,21 +39,25 @@
         right: 0 !important;
     }
 </style>
-<aside class="blowout position-fixed bg-light inner-glow shadow-xs padding-xxs bottom-50% right-0 z-index-popover {expanded ? 'expanded' : ''}">
+<aside class="blowout position-fixed bg-light inner-glow shadow-xs padding-xxs bottom-50% right-0 z-index-popover {expanded ? 'expanded' : ''}" data-blowout-toolbelt-element>
     <LocaleSwitcher bind:show="{localeSwitcher.show}"
                     glow="{false}"
+                    data-blowout-toolbelt-element
                     bind:selection="{localeSwitcher.selection}"/>
     <ThemeSwitcher bind:show="{themeSwitcher.show}"
                    glow="{false}"
+                   data-blowout-toolbelt-element
                    bind:selection="{themeSwitcher.selection}"/>
     <div class="flex flex-row gap-sm justify-end">
         {#if !expanded}
             <div class="flex ld-switch-btn"
+                 data-blowout-toolbelt-element
                  on:click={() => expanded = true}>
                 <ChevronsLeftIcon/>
             </div>
         {:else}
             <div class="flex ld-switch-btn"
+                 data-blowout-toolbelt-element
                  on:click={() => expanded = false}>
                 <ChevronsRightIcon/>
             </div>
-- 
cgit v1.3

