From 5240fccd1f349dcf387199bc72f7572323f60619 Mon Sep 17 00:00:00 2001 From: Roj Date: Sun, 31 Mar 2024 21:09:24 +0300 Subject: [PATCH] Animate modal with Presence --- fresh.gen.ts | 2 -- islands/ConnectivityTest.tsx | 27 ++++++++++++++- islands/Modal.tsx | 65 +++++++++++++++++++----------------- islands/Select.tsx | 26 --------------- 4 files changed, 60 insertions(+), 60 deletions(-) delete mode 100644 islands/Select.tsx diff --git a/fresh.gen.ts b/fresh.gen.ts index 0e614da..ece2f3f 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -26,7 +26,6 @@ import * as $FileIdAnalyzer from "./islands/FileIdAnalyzer.tsx"; import * as $FilterQueryBrowser from "./islands/FilterQueryBrowser.tsx"; import * as $InlineMessageIdUnpacker from "./islands/InlineMessageIdUnpacker.tsx"; import * as $Modal from "./islands/Modal.tsx"; -import * as $Select from "./islands/Select.tsx"; import * as $SessionStringAnalyzer from "./islands/SessionStringAnalyzer.tsx"; import * as $SessionStringConverter from "./islands/SessionStringConverter.tsx"; import * as $SessionStringGenerator from "./islands/SessionStringGenerator.tsx"; @@ -62,7 +61,6 @@ const manifest = { "./islands/FilterQueryBrowser.tsx": $FilterQueryBrowser, "./islands/InlineMessageIdUnpacker.tsx": $InlineMessageIdUnpacker, "./islands/Modal.tsx": $Modal, - "./islands/Select.tsx": $Select, "./islands/SessionStringAnalyzer.tsx": $SessionStringAnalyzer, "./islands/SessionStringConverter.tsx": $SessionStringConverter, "./islands/SessionStringGenerator.tsx": $SessionStringGenerator, diff --git a/islands/ConnectivityTest.tsx b/islands/ConnectivityTest.tsx index af91bb6..ebf0942 100644 --- a/islands/ConnectivityTest.tsx +++ b/islands/ConnectivityTest.tsx @@ -1,4 +1,5 @@ import { IS_BROWSER } from "$fresh/runtime.ts"; +import { ComponentChildren } from "preact"; import { signal, useSignal } from "@preact/signals"; import type { DC } from "mtkruto/mod.ts"; @@ -10,7 +11,6 @@ import { Button } from "../components/Button.tsx"; import { Spinner } from "../components/icons/Spinner.tsx"; import { Alert } from "./Alert.tsx"; -import { Select } from "./Select.tsx"; const localStorage = prefixedLocalStorage("connectivity-test"); @@ -275,3 +275,28 @@ function startTest(all = false) { worker.postMessage([dc, authKey ? decodeHex(authKey) : null]); } } + +export function Select( + { text, caption, checked, onChange }: { + text: ComponentChildren; + caption: string; + checked: boolean; + onChange: (checked: boolean) => void; + }, +) { + return ( +
onChange(!checked)} + tabIndex={0} + onKeyDown={(e) => e.key == "Enter" && onChange(!checked)} + > +
+
{text}
+
{caption}
+
+
+ ); +} diff --git a/islands/Modal.tsx b/islands/Modal.tsx index f07cd9a..4877187 100644 --- a/islands/Modal.tsx +++ b/islands/Modal.tsx @@ -4,6 +4,7 @@ import { useEffect } from "preact/hooks"; import { effect, signal } from "@preact/signals"; import { Button } from "../components/Button.tsx"; +import { Precense } from "../components/Precense.tsx"; export type ModalContent = null | ComponentChildren | (() => ComponentChildren); @@ -26,8 +27,10 @@ IS_BROWSER && effect(() => { } }); +const present = signal(false); + export function isModalVisible() { - return content.value != null; + return present.value; } export function setModalContent( @@ -47,10 +50,11 @@ export function setModalContent( } } content.value = content_; + present.value = true; } export function hideModal() { - setModalContent(null); + present.value = false; } export function displayError(err: unknown, context?: string) { @@ -61,47 +65,46 @@ export function displayError(err: unknown, context?: string) { export function Modal({ onDismiss }: { onDismiss?: () => void }) { useEffect(() => { const onKeyDown = (e: KeyboardEvent) => { - e.key == "Escape" && (content.value = null); + e.key == "Escape" && (hideModal()); }; document.addEventListener("keydown", onKeyDown); return () => document.removeEventListener("keydown", onKeyDown); }, []); - if (!content.value) { - return null; - } function dismiss() { if (onDismiss) { onDismiss(); } else { - content.value = null; + hideModal(); } } return ( -
- e.target == e.currentTarget && autoDismiss.value && dismiss()} - > -
-
- {typeof content.value === "string" - ? ( -

- {content.value} -

- ) - : typeof content.value === "function" - ? content.value() - : content.value} - {showDismissButton.value && ( - - )} + +
+ e.target == e.currentTarget && autoDismiss.value && dismiss()} + > +
+
+ {typeof content.value === "string" + ? ( +

+ {content.value} +

+ ) + : typeof content.value === "function" + ? content.value() + : content.value} + {showDismissButton.value && ( + + )} +
-
+ ); } diff --git a/islands/Select.tsx b/islands/Select.tsx deleted file mode 100644 index 8952b3c..0000000 --- a/islands/Select.tsx +++ /dev/null @@ -1,26 +0,0 @@ -import { ComponentChildren } from "preact"; - -export function Select( - { text, caption, checked, onChange }: { - text: ComponentChildren; - caption: string; - checked: boolean; - onChange: (checked: boolean) => void; - }, -) { - return ( -
onChange(!checked)} - tabIndex={0} - onKeyDown={(e) => e.key == "Enter" && onChange(!checked)} - > -
-
{text}
-
{caption}
-
-
- ); -}