fact: svelte 5's class reactiveness is balls

This commit is contained in:
Xylight committed 2025-09-16 17:48:37 -07:00
1 parent 97469f9a3e
commit 76e989f122
5 files changed
+100 -80

No files matched your search

@@ -136,8 +136,8 @@
)} )}
<div <div
class={[ class={[
buttonColor.secondary, buttonColor.ghost,
'rounded-3xl h-full font-medium flex relative', 'rounded-3xl h-full font-medium flex relative hover:bg-transparent!',
voteRatio < 85 && settings.voteRatioBar && 'vote-ratio', voteRatio < 85 && settings.voteRatioBar && 'vote-ratio',
]} ]}
aria-label={$t('aria.vote.group')} aria-label={$t('aria.vote.group')}
@@ -104,7 +104,7 @@
size="custom" size="custom"
href="{postLink(post.post)}#comments" href="{postLink(post.post)}#comments"
class="text-inherit! h-full px-3 relative" class="text-inherit! h-full px-3 relative"
color="secondary" color="ghost"
rounding="pill" rounding="pill"
target={settings.openLinksInNewTab ? '_blank' : ''} target={settings.openLinksInNewTab ? '_blank' : ''}
aria-label={$t('post.actions.comments')} aria-label={$t('post.actions.comments')}
@@ -130,7 +130,7 @@
onclick={() => (debug = true)} onclick={() => (debug = true)}
title="Debug" title="Debug"
size="custom" size="custom"
color="secondary" color="ghost"
rounding="pill" rounding="pill"
class={buttonSquare} class={buttonSquare}
icon={BugAnt} icon={BugAnt}
@@ -143,7 +143,7 @@
<Button <Button
{@attach attachment} {@attach attachment}
size="custom" size="custom"
color="secondary" color="ghost"
rounding="pill" rounding="pill"
loading={acting} loading={acting}
class={buttonSquare} class={buttonSquare}
@@ -165,7 +165,7 @@
}} }}
size="custom" size="custom"
class={buttonSquare} class={buttonSquare}
color="secondary" color="ghost"
rounding="pill" rounding="pill"
loading={saving} loading={saving}
disabled={saving} disabled={saving}
@@ -179,7 +179,7 @@
<Button <Button
{@attach popover} {@attach popover}
title={$t('post.actions.more.label')} title={$t('post.actions.more.label')}
color="secondary" color="ghost"
rounding="pill" rounding="pill"
size="custom" size="custom"
class={buttonSquare} class={buttonSquare}
+12 -9
View File
@@ -81,9 +81,11 @@
<!--svelte-ignore a11y_click_events_have_key_events--> <!--svelte-ignore a11y_click_events_have_key_events-->
<div <div
role="dialog" role="dialog"
class="overflow-hidden fixed top-0 left-0 w-screen h-screen z-100 class={[
flex flex-col items-center justify-center backdrop-blur-xs 'overflow-hidden fixed top-0 left-0 w-screen h-screen z-100',
bg-white/50 dark:bg-black/50 box-border p-4" 'flex flex-col items-center justify-center backdrop-blur-xs',
'bg-white/50 dark:bg-black/50 box-border p-4',
]}
transition:fade|global={{ duration: 100 }} transition:fade|global={{ duration: 100 }}
onclick={(e) => { onclick={(e) => {
// @ts-expect-error html node hell // @ts-expect-error html node hell
@@ -98,11 +100,12 @@ bg-white/50 dark:bg-black/50 box-border p-4"
easing: backOut, easing: backOut,
duration: 300, duration: 300,
}} }}
class="w-full border border-slate-200 border-b-slate-300 class={[
dark:border-zinc-900 'w-full border border-slate-200 border-b-slate-300 dark:border-zinc-900',
rounded-2xl max-w-lg box-border mx-auto overscroll-contain shadow-lg overflow-auto 'rounded-2xl max-w-lg box-border mx-auto overscroll-contain shadow-lg overflow-auto',
p-5 flex flex-col gap-2 dark:bg-zinc-950 'p-5 flex flex-col gap-2 dark:bg-zinc-950 bg-slate-50 relative',
bg-slate-50 relative {clazz}" clazz,
]}
> >
{#if dismissable} {#if dismissable}
<Button <Button
@@ -114,7 +117,7 @@ bg-white/50 dark:bg-black/50 box-border p-4"
></Button> ></Button>
{/if} {/if}
{#if title !== null} {#if title !== null}
<h1 class="font-semibold text-xl max-w-full"> <h1 class="font-medium tracking-tight text-xl leading-5 max-w-full">
{#if customTitle} {#if customTitle}
{@render customTitle?.()} {@render customTitle?.()}
{:else if title} {:else if title}
@@ -32,26 +32,30 @@
import { expoOut } from 'svelte/easing' import { expoOut } from 'svelte/easing'
import { fly } from 'svelte/transition' import { fly } from 'svelte/transition'
import CommentProvider from './CommentProvider.svelte' import CommentProvider from './CommentProvider.svelte'
import { ReactiveState } from '$lib/util.svelte'
let { data } = $props() let { data } = $props()
onMount(() => { onMount(() => {
data.meta.then((i) => data.post == i.post_view) data.data.value.meta.then((i) => data.data.value.post == i.post_view)
resumables.add({ resumables.add({
name: data.post.post.name, name: data.data.value.post.post.name,
type: 'post', type: 'post',
url: page.url.toString(), url: page.url.toString(),
avatar: data.post.post.thumbnail_url, avatar: data.data.value.post.post.thumbnail_url,
}) })
if (!(data.post.read && settings.markPostsAsRead) && profile.current?.jwt) { if (
!(data.data.value.post.read && settings.markPostsAsRead) &&
profile.current?.jwt
) {
client() client()
.markPostAsRead({ .markPostAsRead({
read: true, read: true,
post_ids: [data.post.post.id], post_ids: [data.data.value.post.post.id],
}) })
.then(() => (data.post.read = true)) .then(() => (data.data.value.post.read = true))
} }
}) })
@@ -59,28 +63,28 @@
async function reloadComments() { async function reloadComments() {
loading = true loading = true
data.comments = client() data.data.value.comments = client()
.getComments({ .getComments({
page: 1, page: 1,
limit: 25, limit: 25,
type_: 'All', type_: 'All',
post_id: data.post.post.id, post_id: data.data.value.post.post.id,
sort: settings.defaultSort.comments, sort: settings.defaultSort.comments,
max_depth: data.post.counts.comments > 100 ? 1 : 3, max_depth: data.data.value.post.counts.comments > 100 ? 1 : 3,
}) })
.then((i) => i.comments) .then((i) => i.comments)
loading = false loading = false
data.thread.singleThread = false data.data.value.thread.singleThread = false
} }
let tags = $derived.by<{ title?: string; tags: Tag[] }>(() => { let tags = $derived.by<{ title?: string; tags: Tag[] }>(() => {
const parsed = parseTags(data.post.post.name) const parsed = parseTags(data.data.value.post.post.name)
return { return {
title: parsed.title, title: parsed.title,
tags: [ tags: [
...parsed.tags, ...parsed.tags,
...(data.post.flair_list?.map((i) => ({ ...(data.data.value.post.flair_list?.map((i) => ({
content: i.flair_title, content: i.flair_title,
color: i.background_color, color: i.background_color,
icon: null, icon: null,
@@ -94,31 +98,40 @@
<svelte:head> <svelte:head>
<title> <title>
{data.post.community.title} | {data.post.post.name} {data.data.value.post.community.title} | {data.data.value.post.post.name}
</title> </title>
<meta property="og:title" content={data.post.post.name} /> <meta property="og:title" content={data.data.value.post.post.name} />
{#if !data.post.post.local} {#if !data.data.value.post.post.local}
<meta name="robots" content="noindex, follow" /> <meta name="robots" content="noindex, follow" />
{/if} {/if}
<link rel="canonical" href={data.post.post.ap_id} /> <link rel="canonical" href={data.data.value.post.post.ap_id} />
{#if data.post.post.body} {#if data.data.value.post.post.body}
<meta property="description" content={data.post.post.body.slice(0, 500)} /> <meta
property="description"
content={data.data.value.post.post.body.slice(0, 500)}
/>
<meta <meta
property="og:description" property="og:description"
content={data.post.post.body.slice(0, 500)} content={data.data.value.post.post.body.slice(0, 500)}
/> />
<meta <meta
property="twitter:description" property="twitter:description"
content={data.post.post.body.slice(0, 500)} content={data.data.value.post.post.body.slice(0, 500)}
/> />
{/if} {/if}
<meta property="og:url" content={page.url.toString()} /> <meta property="og:url" content={page.url.toString()} />
{#if mediaType(data.post.post.url) == 'image'} {#if mediaType(data.data.value.post.post.url) == 'image'}
<meta property="og:image" content={data.post.post.url} /> <meta property="og:image" content={data.data.value.post.post.url} />
<meta property="twitter:card" content={data.post.post.url} /> <meta property="twitter:card" content={data.data.value.post.post.url} />
{:else if data.post.post.thumbnail_url} {:else if data.data.value.post.post.thumbnail_url}
<meta property="og:image" content={data.post.post.thumbnail_url} /> <meta
<meta property="twitter:card" content={data.post.post.thumbnail_url} /> property="og:image"
content={data.data.value.post.post.thumbnail_url}
/>
<meta
property="twitter:card"
content={data.data.value.post.post.thumbnail_url}
/>
{/if} {/if}
</svelte:head> </svelte:head>
@@ -126,49 +139,50 @@
<header class="flex flex-col gap-2"> <header class="flex flex-col gap-2">
<div class="flex flex-row items-center gap-2 flex-wrap"> <div class="flex flex-row items-center gap-2 flex-wrap">
<PostMeta <PostMeta
community={data.post.community} community={data.data.value.post.community}
user={data.post.creator} user={data.data.value.post.creator}
subscribed={profile.current.user?.follows.find( subscribed={profile.current.user?.follows.find(
(i) => i.community.id == data.post.community.id, (i) => i.community.id == data.data.value.post.community.id,
) )
? 'Subscribed' ? 'Subscribed'
: 'NotSubscribed'} : 'NotSubscribed'}
badges={{ badges={{
deleted: data.post.post.deleted, deleted: data.data.value.post.post.deleted,
removed: data.post.post.removed, removed: data.data.value.post.post.removed,
locked: data.post.post.locked, locked: data.data.value.post.post.locked,
featured: featured:
data.post.post.featured_community || data.post.post.featured_local, data.data.value.post.post.featured_community ||
nsfw: data.post.post.nsfw, data.data.value.post.post.featured_local,
saved: data.post.saved, nsfw: data.data.value.post.post.nsfw,
admin: data.post.creator_is_admin, saved: data.data.value.post.saved,
moderator: data.post.creator_is_moderator, admin: data.data.value.post.creator_is_admin,
moderator: data.data.value.post.creator_is_moderator,
}} }}
published={publishedToDate(data.post.post.published)} published={publishedToDate(data.data.value.post.post.published)}
edited={data.post.post.updated} edited={data.data.value.post.post.updated}
title={data.post.post.name} title={data.data.value.post.post.name}
style="width: max-content;" style="width: max-content;"
tags={tags.tags} tags={tags.tags}
/> />
</div> </div>
<h1 class="font-medium text-xl leading-5"> <h1 class="font-medium text-xl leading-5">
<Markdown source={tags.title ?? data.post.post.name} inline /> <Markdown source={tags.title ?? data.data.value.post.post.name} inline />
</h1> </h1>
</header> </header>
<PostMedia <PostMedia
type={mediaType(data.post.post.url)} type={mediaType(data.data.value.post.post.url)}
post={data.post.post} post={data.data.value.post.post}
opened opened
view="cozy" view="cozy"
/> />
{#if data.post.post.body} {#if data.data.value.post.post.body}
<div class="text-base text-slate-800 dark:text-zinc-300 leading-normal"> <div class="text-base text-slate-800 dark:text-zinc-300 leading-normal">
<Markdown source={data.post.post.body} /> <Markdown source={data.data.value.post.post.body} />
</div> </div>
{/if} {/if}
<div class="w-full relative"> <div class="w-full relative">
<PostActions <PostActions
bind:post={data.post} bind:post={data.data.value.post}
onedit={() => onedit={() =>
toast({ toast({
content: 'The post was edited successfully.', content: 'The post was edited successfully.',
@@ -176,7 +190,7 @@
})} })}
/> />
</div> </div>
{#await data.meta then meta} {#await data.data.value.meta then meta}
{@const crossposts = meta.cross_posts} {@const crossposts = meta.cross_posts}
{#if crossposts?.length > 0} {#if crossposts?.length > 0}
<Expandable class="text-base mt-2 w-full cursor-pointer"> <Expandable class="text-base mt-2 w-full cursor-pointer">
@@ -203,8 +217,8 @@
{/if} {/if}
{/await} {/await}
</article> </article>
{#await data.comments then comments} {#await data.data.value.comments then comments}
{#if data.thread.showContext || data.thread.singleThread} {#if data.data.value.thread.showContext || data.data.value.thread.singleThread}
<div <div
class="sticky mx-auto z-50 max-w-md min-w-0 flex flex-col items-center overflow-auto gap-1 class="sticky mx-auto z-50 max-w-md min-w-0 flex flex-col items-center overflow-auto gap-1
bg-slate-50/50 dark:bg-zinc-900/50 backdrop-blur-xl border border-slate-200/50 dark:border-zinc-800/50 bg-slate-50/50 dark:bg-zinc-900/50 backdrop-blur-xl border border-slate-200/50 dark:border-zinc-800/50
@@ -215,17 +229,17 @@
rounding="pill" rounding="pill"
{loading} {loading}
disabled={loading} disabled={loading}
href={data.thread.showContext href={data.data.value.thread.showContext
? `/comment/${ ? `/comment/${
// split first comment path to get 5 before // split first comment path to get 5 before
comments[0].comment.path.split('.').slice(-5)[1] comments[0].comment.path.split('.').slice(-5)[1]
}` }`
: data.thread.singleThread : data.data.value.thread.singleThread
? postLink(data.post.post) ? postLink(data.data.value.post.post)
: undefined} : undefined}
class="hover:bg-white/50 dark:hover:bg-zinc-800/30" class="hover:bg-white/50 dark:hover:bg-zinc-800/30"
> >
{data.thread.showContext {data.data.value.thread.showContext
? $t('routes.post.thread.context') ? $t('routes.post.thread.context')
: $t('routes.post.thread.allComments')} : $t('routes.post.thread.allComments')}
{#snippet suffix()} {#snippet suffix()}
@@ -241,7 +255,7 @@
{$t('routes.post.commentCount')} {$t('routes.post.commentCount')}
{#snippet action()} {#snippet action()}
<span class="font-bold"> <span class="font-bold">
<FormattedNumber number={data.post.counts.comments} /> <FormattedNumber number={data.data.value.post.counts.comments} />
</span> </span>
{/snippet} {/snippet}
</EndPlaceholder> </EndPlaceholder>
@@ -253,7 +267,7 @@
</Button> </Button>
</noscript> </noscript>
{/if} {/if}
{#await data.comments} {#await data.data.value.comments}
<div class="flex flex-col gap-4"> <div class="flex flex-col gap-4">
{#each new Array(10) as _, index} {#each new Array(10) as _, index}
{_} {_}
@@ -276,10 +290,10 @@
{:then comments} {:then comments}
<CommentProvider <CommentProvider
{comments} {comments}
post={data.post} post={data.data.value.post}
focus={data.thread.focus} focus={data.data.value.thread.focus}
onupdate={reloadComments} onupdate={reloadComments}
bind:sort={data.params.comments.sort} bind:sort={data.data.value.params.comments.sort}
virtualize={!page.url.searchParams.get('noVirtualize')} virtualize={!page.url.searchParams.get('noVirtualize')}
/> />
{#if comments.length == 0} {#if comments.length == 0}
@@ -290,12 +304,12 @@
></Placeholder> ></Placeholder>
{/if} {/if}
{/await} {/await}
{#if data.post.counts.comments > 5} {#if data.data.value.post.counts.comments > 5}
<EndPlaceholder> <EndPlaceholder>
{$t('routes.post.commentCount')} {$t('routes.post.commentCount')}
{#snippet action()} {#snippet action()}
<span class="text-black dark:text-white font-bold"> <span class="text-black dark:text-white font-bold">
{data.post.counts.comments} {data.data.value.post.counts.comments}
</span> </span>
<Button <Button
color="tertiary" color="tertiary"
@@ -4,6 +4,7 @@ import { client } from '$lib/client/lemmy.svelte'
import CommunityCard from '$lib/components/lemmy/community/CommunityCard.svelte' import CommunityCard from '$lib/components/lemmy/community/CommunityCard.svelte'
import { feed } from '$lib/lemmy/feeds/feed.svelte.js' import { feed } from '$lib/lemmy/feeds/feed.svelte.js'
import { settings } from '$lib/settings.svelte' import { settings } from '$lib/settings.svelte'
import { ReactiveState } from '$lib/util.svelte.js'
import { redirect } from '@sveltejs/kit' import { redirect } from '@sveltejs/kit'
function buildContext(thread?: string) { function buildContext(thread?: string) {
@@ -92,7 +93,8 @@ export async function load({ params, url, route }) {
} }
}) })
const loaded = await feedData.load({ const loaded = new ReactiveState(
await feedData.load({
comments: { comments: {
post_id: Number(params.id), post_id: Number(params.id),
type_: 'All', type_: 'All',
@@ -103,15 +105,16 @@ export async function load({ params, url, route }) {
}, },
posts: { id: Number(params.id) }, posts: { id: Number(params.id) },
preload: cachedPost, preload: cachedPost,
}) }),
)
return { return {
...loaded, data: loaded,
slots: { slots: {
sidebar: { sidebar: {
component: CommunityCard, component: CommunityCard,
props: { props: {
community_view: loaded.meta.then((i) => i.community_view), community_view: loaded.value.meta.then((i) => i.community_view),
}, },
}, },
}, },