From 9fb20915e890be0993c15ad19b59105b78cf8f12 Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 13 Apr 2024 12:19:21 +0100 Subject: [Embed] Don't reuse DOM when changing embed (#3530) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Don't reuse DOM when changing embed * add skeleton loading state 💀 * autoselect text --------- Co-authored-by: Samuel Newman --- bskyembed/src/components/container.tsx | 6 ++--- bskyembed/src/screens/landing.tsx | 49 +++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/bskyembed/src/components/container.tsx b/bskyembed/src/components/container.tsx index a96addc8c..5b1b2b7fb 100644 --- a/bskyembed/src/components/container.tsx +++ b/bskyembed/src/components/container.tsx @@ -8,7 +8,7 @@ export function Container({ href, }: { children: ComponentChildren - href: string + href?: string }) { const ref = useRef(null) const prevHeight = useRef(0) @@ -39,7 +39,7 @@ export function Container({ ref={ref} className="w-full bg-white hover:bg-neutral-50 relative transition-colors max-w-[600px] min-w-[300px] flex border rounded-xl" onClick={() => { - if (ref.current) { + if (ref.current && href) { // forwardRef requires preact/compat - let's keep it simple // to keep the bundle size down const anchor = ref.current.querySelector('a') @@ -48,7 +48,7 @@ export function Container({ } } }}> - + {href && }
{children}
) diff --git a/bskyembed/src/screens/landing.tsx b/bskyembed/src/screens/landing.tsx index f10100baa..0c5508935 100644 --- a/bskyembed/src/screens/landing.tsx +++ b/bskyembed/src/screens/landing.tsx @@ -1,7 +1,7 @@ import '../index.css' import {AppBskyFeedDefs, AppBskyFeedPost, AtUri, BskyAgent} from '@atproto/api' -import {Fragment, h, render} from 'preact' +import {h, render} from 'preact' import {useEffect, useMemo, useRef, useState} from 'preact/hooks' import arrowBottom from '../../assets/arrowBottom_stroke2_corner0_rounded.svg' @@ -30,6 +30,7 @@ render(, root) function LandingPage() { const [uri, setUri] = useState('') const [error, setError] = useState(null) + const [loading, setLoading] = useState(false) const [thread, setThread] = useState( null, ) @@ -37,6 +38,8 @@ function LandingPage() { useEffect(() => { void (async () => { setError(null) + setThread(null) + setLoading(true) try { let atUri = DEFAULT_URI @@ -98,6 +101,8 @@ function LandingPage() { } catch (err) { console.error(err) setError(err instanceof Error ? err.message : 'Invalid Bluesky URL') + } finally { + setLoading(false) } })() }, [uri]) @@ -122,16 +127,39 @@ function LandingPage() { -
- {uri && !error && thread && } - {!error && thread && } - {error && ( -
-

{error}

+ {loading ? ( + + ) : ( +
+ {!error && thread && uri && } + {!error && thread && } + {error && ( +
+

{error}

+
+ )} +
+ )} + + ) +} + +function Skeleton() { + return ( + +
+
+
+
+
+
- )} +
+
+
+
- + ) } @@ -195,6 +223,9 @@ function Snippet({thread}: {thread: AppBskyFeedDefs.ThreadViewPost}) { className="border rounded-lg py-3 w-full px-4" readOnly autoFocus + onFocus={() => { + ref.current?.select() + }} />