impr: restructure src/lib

This commit is contained in:
Xylight committed 2025-09-24 17:12:19 -07:00
1 parent d85b889409
commit 75f907743e
376 files changed
+1000 -1033

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
<script lang="ts">
import type { Snippet } from 'svelte'
import Avatar from '../Avatar.svelte'
interface Props {
href?: string
icon?: string
title: string
detail?: string
orientation: 'vertical' | 'horizontal'
children?: Snippet
}
let {
href,
icon,
title,
detail,
children,
orientation = 'horizontal',
}: Props = $props()
</script>
<div
class={[
'flex gap-2 p-5',
orientation == 'horizontal'
? 'flex-row items-center'
: 'flex-col items-staRT',
]}
>
<Avatar url={icon} alt={title} width={detail ? 32 : 24} circle={false} />
<a class="flex-1 flex flex-col group" {href}>
<h3
class="font-medium text-base overflow-hidden text-ellipsis leading-5 group-hover:underline"
>
{title}
</h3>
<p class="text-sm text-slate-600 dark:text-zinc-400">
{detail}
</p>
</a>
<div class="flex flex-row gap-2 items-center">
{@render children?.()}
</div>
</div>
+159
View File
@@ -0,0 +1,159 @@
<script lang="ts" generics="T">
import type { Snippet } from 'svelte'
import type { ClassValue } from 'svelte/elements'
const sizeDelay: Record<'lg' | 'md' | 'sm' | 'xs', number> = {
lg: 120,
md: 80,
sm: 40,
xs: 0,
}
interface Props {
items?: T[]
item?: Snippet<[T, number]>
children?: Snippet
animate?: boolean
size?: 'lg' | 'md' | 'sm' | 'xs'
class?: ClassValue
selected?: (item: T) => boolean
}
let {
items,
item: itemSnippet,
children,
animate = true,
size = 'sm',
class: clazz,
selected,
}: Props = $props()
</script>
<ul class="space-y-1">
{#if items}
{#each items as item, index (item)}
<li
class={[
'group/li',
animate && 'animate',
size == 'xs' && 'xs',
selected?.(item) && 'selected',
clazz,
]}
style="--i: {index < 10 ? index * sizeDelay[size] : 0}ms;"
>
{@render itemSnippet?.(item, index)}
</li>
{/each}
{/if}
{@render children?.()}
</ul>
<style>
ul > :global(li) {
transition: background-color 0.1s;
background-color: var(--color-white);
container-type: inline-size;
border-radius: var(--radius-md) var(--radius-md);
&:first-child {
border-top-left-radius: var(--radius-2xl);
border-top-right-radius: var(--radius-2xl);
}
&:last-child {
border-bottom-left-radius: var(--radius-2xl);
border-bottom-right-radius: var(--radius-2xl);
}
&:hover {
background-color: var(--color-slate-25);
}
}
ul > :global(li:not(.xs) > *) {
padding-block: calc(var(--spacing) * 3);
padding-inline: calc(var(--spacing) * 3.5);
@media screen and (min-width: 40rem) {
padding-block: calc(var(--spacing) * 3);
padding-inline: calc(var(--spacing) * 4);
}
}
ul > :global(li.xs > *) {
padding-block: calc(var(--spacing) * 0.5);
padding-block: calc(var(--spacing) * 1);
}
ul > :global(li).selected {
background-color: color-mix(
in oklab,
var(--color-white),
var(--color-slate-100)
);
}
:global(.dark) ul > :global(li).selected {
background-color: color-mix(
in oklab,
var(--color-zinc-925),
var(--color-zinc-800)
);
}
:global(.dark) ul > :global(li) {
background-color: color-mix(
in oklab,
var(--color-zinc-925),
var(--color-zinc-900)
);
&:hover {
background-color: var(--color-zinc-900);
}
&:active {
background-color: var(--color-zinc-950);
}
}
@keyframes pop-in {
from {
transform: translateY(16px);
opacity: 0;
}
to {
transform: translateY(0px);
opacity: 1;
}
}
.animate {
--delay: var(--i, 0ms);
}
:global(.animate > *:only-child) {
animation: pop-in 1s forwards cubic-bezier(0.16, 1, 0.3, 1);
animation-delay: var(--delay, 0ms);
opacity: 0;
}
:global(.animate:not(:has(> * + *))) {
animation: pop-in 1s forwards cubic-bezier(0.16, 1, 0.3, 1);
animation-delay: var(--delay, 0ms);
opacity: 0;
}
@media (prefers-reduced-motion: reduce) {
:global(.animate > *:only-child) {
animation: none;
opacity: 1;
}
:global(.animate:not(:has(> * + *))) {
animation: none;
opacity: 1;
}
}
</style>
+71
View File
@@ -0,0 +1,71 @@
<script lang="ts">
import type { ClassValue } from 'svelte/elements'
const sizes = {
xs: 'text-xs',
sm: 'text-sm',
md: 'text-base',
lg: 'text-lg',
}
const margins = {
none: '',
sm: 'mt-3 mb-1 px-3',
md: 'mt-4 mb-2',
lg: 'mt-6 mb-2',
'bottom-sm': 'mb-1 px-3',
'bottom-md': 'mb-2',
'bottom-lg': 'mb-3',
}
const colors = {
subtle: 'text-slate-600 dark:text-zinc-400',
none: '',
}
interface Props {
class?: ClassValue
size?: keyof typeof sizes
color?: keyof typeof colors
margin?: keyof typeof margins
border?: boolean
children?: import('svelte').Snippet
action?: import('svelte').Snippet
element?: string
}
let {
class: clazz = '',
children,
action,
border = true,
size = 'sm',
color = 'subtle',
margin = 'none',
element = 'h3',
}: Props = $props()
</script>
<div
class={[
'flex flex-row items-center gap-2 flex-wrap',
sizes[size],
colors[color],
margins[margin],
clazz,
]}
>
<svelte:element
this={element}
class="font-medium text-left flex flex-row gap-1 items-center"
>
{@render children?.()}
</svelte:element>
<div
class={[
'flex-1 border-slate-200/70 dark:border-zinc-800 border-b',
!border && 'opacity-0',
]}
></div>
{@render action?.()}
</div>
+108
View File
@@ -0,0 +1,108 @@
<script lang="ts">
import { t } from '$lib/app/i18n'
import { Button } from 'mono-svelte'
import { ChevronLeft, ChevronRight, Icon } from 'svelte-hero-icons'
import { backOut } from 'svelte/easing'
import { fly } from 'svelte/transition'
import { page as pageData } from '$app/state'
import { invalidate } from '$app/navigation'
import { SvelteURLSearchParams } from 'svelte/reactivity'
interface Props {
page?: number
cursor?: { next?: string; back?: string } | undefined
hasMore?: boolean
children?: import('svelte').Snippet
href?: (current: number | string) => string
back?: boolean
}
let {
page = $bindable(0),
cursor = undefined,
hasMore = true,
children,
href,
back = true,
}: Props = $props()
let customHref = (href?: string) => {
if (href?.startsWith('?')) {
const current = new SvelteURLSearchParams(pageData.url.searchParams)
const newParams = new SvelteURLSearchParams(href)
current.delete(Array.from(newParams.keys())[0])
current.append(
Array.from(newParams.entries())[0][0],
Array.from(newParams.entries())[0][1],
)
return `?${current.toString()}`
} else return href
}
</script>
{#if hasMore || page != 1}
<nav
aria-label={$t('aria.pagination.nav')}
class={[
'flex flex-row gap-2 items-center justify-center bg-white dark:bg-zinc-900',
'border border-slate-200 dark:border-zinc-800 rounded-full overflow-hidden w-max mx-auto p-0.5',
'shadow-md',
]}
>
{#if children}
<span class="text-sm text-slate-600 dark:text-zinc-400 font-medium">
{@render children?.()}
</span>
<hr class="border-slate-200 dark:border-zinc-800 flex-1" />
{/if}
{#if back}
<Button
href={customHref(href?.(cursor?.back ?? page - 1))}
color="tertiary"
onclick={() => invalidate(pageData.url)}
title={$t('common.back')}
rounding="pill"
size="custom"
class="text-inherit dark:text-inherit p-1"
disabled={(cursor?.back == undefined && cursor?.next != undefined) ||
page <= 1}
>
{#snippet suffix()}
<Icon src={ChevronLeft} size="24" mini />
{/snippet}
</Button>
{/if}
{#if page}
<div style="display: grid;">
{#key page}
<div
in:fly={{ y: 8, easing: backOut, duration: 400, delay: 100 }}
out:fly={{ y: 8, easing: backOut, duration: 400 }}
class="text-lg font-medium"
style="grid-column: 1; grid-row: 1;"
>
{page}
</div>
{/key}
</div>
{/if}
<Button
href={customHref(href?.(cursor?.next ?? page + 1))}
color="tertiary"
onclick={() => invalidate(pageData.url)}
title={$t('common.next')}
size="custom"
rounding="pill"
class="text-inherit dark:text-inherit p-1"
disabled={!hasMore}
>
{#snippet suffix()}
<Icon src={ChevronRight} size="24" mini />
{/snippet}
</Button>
</nav>
{/if}
+35
View File
@@ -0,0 +1,35 @@
<script lang="ts">
import { navigating, page } from '$app/state'
import { t } from '$lib/app/i18n'
import { Button, TextInput } from 'mono-svelte'
import { MagnifyingGlass } from 'svelte-hero-icons'
interface Props {
query?: string | null
}
let { query = $bindable() }: Props = $props()
let searchElement = $state<HTMLInputElement>()
</script>
<div class="flex gap-2 flex-row items-center w-full text-base h-10">
<TextInput
bind:value={query}
bind:element={searchElement}
name="q"
placeholder={$t('routes.search.query')}
size="lg"
class="flex-1 rounded-full! h-full text-base!"
/>
<Button
submit
color="primary"
size="custom"
class="shrink-0 h-full aspect-square shadow-md"
title="Search"
rounding="pill"
loading={navigating.to?.route.id == page.url.pathname}
icon={MagnifyingGlass}
></Button>
</div>
+109
View File
@@ -0,0 +1,109 @@
<script lang="ts">
import { settings } from '$lib/settings.svelte.js'
import type { Snippet } from 'svelte'
import type {
ClassValue,
HTMLAttributes,
UIEventHandler,
} from 'svelte/elements'
import { scrollY } from 'svelte/reactivity/window'
interface Props extends HTMLAttributes<HTMLDivElement> {
children?: Snippet
navbar?: Snippet<[{ class: ClassValue; style: string }]>
sidebar?: Snippet<[{ class: ClassValue; style: string }]>
main?: Snippet<[{ class: ClassValue; style: string }]>
suffix?: Snippet<[{ class: ClassValue; style: string }]>
}
let { children, navbar, sidebar, main, suffix }: Props = $props()
let previousTop = 0
const onscroll: UIEventHandler<Window> = () => {
if (!settings.dock.autoHide) {
dockVisible = true
return
}
dockVisible = (scrollY?.current ?? 0) <= previousTop
previousTop = scrollY?.current ?? 0
}
let dockVisible = $state(true)
</script>
<svelte:window {onscroll} />
{@render children?.()}
<div class="min-h-screen flex flex-col">
<div
class={[
'fixed md:sticky bottom-0 md:top-0',
!dockVisible && 'max-md:-bottom-24',
'w-full z-50 pointer-events-none',
]}
style="transition: bottom .4s cubic-bezier(0.075, 0.82, 0.165, 1);"
aria-hidden="true"
>
{@render navbar?.({
class: [
'bg-slate-100/70 dark:bg-zinc-950/90 border-slate-200/50 dark:border-zinc-900',
'pointer-events-auto backdrop-blur-xl border md:border-t-0 md:border-x-0',
],
style: '',
})}
</div>
<div class={['content flex-1', settings.newWidth && 'limit-width']}>
{@render sidebar?.({
class: `hidden md:flex sticky top-0 lg:top-16 left-0 max-h-[calc(100vh-4rem)] bg-slate-100 dark:bg-zinc-950 z-40`,
style: 'grid-area: sidebar; width: 100% !important;',
})}
{@render main?.({
class: `w-full bg-slate-50 dark:bg-zinc-925 justify-self-center border-x border-slate-200 dark:border-zinc-900 max-lg:pb-22 pb-3 lg:pb-6`,
style: 'grid-area: main',
})}
{@render suffix?.({
class: `max-lg:hidden flex w-full sticky top-0 lg:top-16 left-0 max-h-[calc(100vh-4rem)] overflow-auto bg-slate-100 dark:bg-zinc-950 z-40`,
style: 'grid-area: suffix;',
})}
</div>
</div>
<style>
.content {
width: 100%;
display: grid;
height: 100%;
margin-left: auto;
margin-right: auto;
grid-area: content;
grid-template-columns: 1fr;
grid-template-areas: 'main';
justify-items: start;
}
.content.limit-width {
max-width: 100rem;
}
@media (min-width: 48rem) {
.content {
grid-template-columns: 16rem 1fr;
justify-items: end start;
grid-template-areas: 'sidebar main';
}
}
@media (min-width: 64rem) {
.content {
grid-template-columns: 20% 60% 20%;
justify-items: end center start;
grid-template-areas: 'sidebar main suffix';
}
.content.limit-width {
width: 100%;
grid-template-columns: 2fr 5fr 2fr;
}
}
</style>
+7
View File
@@ -0,0 +1,7 @@
export { default as Header } from './pages/Header.svelte'
export { default as Tabs } from './pages/Tabs.svelte'
export { default as CommonList } from './CommonList.svelte'
export { default as SearchBar } from './SearchBar.svelte'
export { default as Shell } from './Shell.svelte'
export { default as Pageination } from './Pageination.svelte'
export { default as PostListShell } from './pages/PostListShell.svelte'
+56
View File
@@ -0,0 +1,56 @@
<script lang="ts">
import { t } from '$lib/app/i18n'
const sizes = {
sm: 'text-2xl',
md: 'text-3xl',
lg: 'text-4xl',
xl: 'text-6xl',
}
interface Props {
pageHeader?: boolean
style?: string
class?: string
size?: keyof typeof sizes
children?: import('svelte').Snippet
extended?: import('svelte').Snippet
}
let {
pageHeader = false,
style = '',
class: clazz = '',
size = 'lg',
children,
extended,
}: Props = $props()
</script>
<header
class={[
pageHeader &&
`w-[calc(100%+1.5rem)] sm:w-[calc(100%+3rem)]
bg-slate-100 dark:bg-zinc-950 -mx-3 sm:-mx-6 sm:px-6 sm:pb-6 px-4 pb-4 -mt-64 pt-64
border-b border-slate-200 dark:border-zinc-900 margin z-0 mb-3 sm:mb-6`,
]}
{style}
aria-label={$t('aria.element.pageHeader')}
>
{#if children}
<h1
class={[
sizes[size],
'flex gap-2 w-full tracking-tight font-medium',
clazz,
]}
>
{@render children?.()}
</h1>
{/if}
{#if extended}
<div class="flex flex-col gap-3 mt-3">
{@render extended?.()}
</div>
{/if}
</header>
@@ -0,0 +1,114 @@
<script lang="ts">
import { browser } from '$app/environment'
import { page } from '$app/state'
import type { GetPosts, ListingType, PostView } from '$lib/api/types'
import Location from '$lib/feature/filter/Location.svelte'
import Sort from '$lib/feature/filter/Sort.svelte'
import ViewSelect from '$lib/feature/filter/ViewSelect.svelte'
import PostFeed from '$comp/lemmy/post/feed/PostFeed.svelte'
import VirtualFeed from '$comp/lemmy/post/feed/VirtualFeed.svelte'
import { Header } from '$comp/ui/layout'
import Pageination from '$comp/ui/layout/Pageination.svelte'
import { settings } from '$lib/settings.svelte.js'
import type { SortType } from 'lemmy-js-client'
import Button from 'mono-svelte/button/Button.svelte'
import type { Snippet } from 'svelte'
import { ArrowRight, Icon } from 'svelte-hero-icons'
interface Props {
posts: PostView[]
cursor?: string
params: {
location?: ListingType
sort?: SortType
}
title?: string
extended?: Snippet
getParams: GetPosts
client: {
lastSeen?: number
itemHeights?: (number | null)[]
}
header?: boolean
}
let {
params,
posts = $bindable(),
cursor = $bindable(),
title,
extended: passedExtended,
getParams,
client = $bindable(),
header = true,
}: Props = $props()
$effect(() => {
if (params.sort) settings.defaultSort.sort = params.sort
})
const FeedComponent = $derived(
settings.infiniteScroll && browser && !settings.posts.noVirtualize
? VirtualFeed
: PostFeed,
)
</script>
<div class="flex flex-col gap-2 max-w-full w-full min-w-0">
{#if header}
<Header pageHeader>
{#if title}
{title}
{/if}
{#snippet extended()}
{@render passedExtended?.()}
<form class="" method="get" action={page.url.pathname}>
<div class="flex flex-row gap-2">
{#if params.location}
<Location name="type" navigate bind:selected={params.location} />
{/if}
{#if params.sort}
<Sort
placement="bottom"
name="sort"
navigate
bind:selected={params.sort}
/>
{/if}
<ViewSelect placement="bottom" />
<noscript>
<Button
class="self-end h-[34px] aspect-square"
size="custom"
submit
>
<Icon src={ArrowRight} size="16" micro />
</Button>
</noscript>
</div>
</form>
{/snippet}
</Header>
{/if}
<FeedComponent
bind:posts
bind:lastSeen={() => client.lastSeen ?? 0, (v) => (client.lastSeen = v)}
bind:params={getParams}
virtualList={{ itemHeights: client?.itemHeights ?? [] }}
/>
<svelte:element
this={settings.infiniteScroll && !settings.posts.noVirtualize
? 'noscript'
: 'div'}
class="mt-auto flex flex-col"
>
<Pageination
cursor={{ next: cursor }}
href={(page) =>
typeof page == 'number' ? `?page=${page}` : `?cursor=${page}`}
back={false}
/>
</svelte:element>
</div>
+92
View File
@@ -0,0 +1,92 @@
<script lang="ts">
import { invalidateAll } from '$app/navigation'
import { page } from '$app/state'
import TabButton from '$comp/input/TabButton.svelte'
import { SvelteURL } from 'svelte/reactivity'
interface Props {
routes: {
href: string
name: string
}[]
currentRoute?: SvelteURL | undefined
buildUrl?: (currentRoute: SvelteURL | undefined, href: string) => string
children?: import('svelte').Snippet
style?: 'header' | 'subpage'
margin?: boolean
}
let {
routes,
currentRoute = undefined,
buildUrl = (_, href) => href,
children,
style = 'header',
margin = true,
}: Props = $props()
let matchType = $derived<('search' | 'pathname')[]>(
routes.map((i) =>
new URL(`https://example.com${i.href}`)?.search != ''
? 'search'
: 'pathname',
),
)
// i am on 2 hours of sleep i apologize
function isSelected(url: URL, href: SvelteURL, type: 'search' | 'pathname') {
const currentSearch = new Set(url.searchParams.values())
const hrefSearch = new Set(href.searchParams.values())
// checks if there is any
const usePathname = !routes.some(
(i) =>
new Set(
new URL(`https://example.com${i.href}`).searchParams.values(),
).intersection(currentSearch).size > 0,
)
const hasSearchParam = hrefSearch.intersection(currentSearch).size > 0
if (hasSearchParam && !usePathname) return hasSearchParam
else if ((url.search == '' || usePathname) && type == 'pathname')
return href.pathname == url.pathname
}
</script>
<nav
class={[
'flex flex-row items-center sm:mt-0 flex-wrap z-20',
style == 'header'
? 'p-1 gap-x-8 gap-y-4 justify-center sm:justify-start'
: 'gap-2',
margin && 'my-2',
margin && style == 'subpage' && 'mb-3',
]}
>
{#each routes as route, index (route.href)}
{@const selected = isSelected(
page.url,
new SvelteURL(`${page.url.origin}${route.href}`),
matchType[index],
)}
{#if style == 'header'}
<a
onclick={() => invalidateAll()}
href={buildUrl(currentRoute, route.href)}
class={[
'rounded-full font-medium transition-colors duration-100 relative z-0 shrink-0 hover:text-slate-900 hover:dark:text-zinc-100',
selected
? 'text-primary-900 dark:text-primary-100'
: 'text-slate-500 dark:text-zinc-500',
]}
>
{route.name}
</a>
{:else}
<TabButton href={buildUrl(currentRoute, route.href)} {selected}>
{route.name}
</TabButton>
{/if}
{/each}
{@render children?.()}
</nav>