Files
photon-tbw/src/lib/ui/layout/pages/Tabs.svelte
T

143 lines
3.5 KiB
Svelte

<script lang="ts">
import { page } from '$app/state'
import TabButton from '$lib/ui/form/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
class?: string
}
let {
routes,
currentRoute = undefined,
buildUrl = (_, href) => href,
children,
style = 'header',
margin = true,
class: clazz = '',
}: 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
// me on 2025/10/9: no shi buh
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={[
'tab-bar rounded-full',
style == 'header' ? 'bar-header material-uniform' : 'gap-2',
margin && 'my-2',
margin && style == 'subpage' && 'mb-3',
clazz,
]}
>
{#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
href={buildUrl(currentRoute, route.href)}
class={['tab-item ', selected && 'material-distinct tab-selected']}
>
{route.name}
</a>
{:else}
<TabButton href={buildUrl(currentRoute, route.href)} {selected}>
{route.name}
</TabButton>
{/if}
{/each}
{@render children?.()}
</nav>
<style>
@reference '../../../../app.css';
.tab-bar {
display: flex;
flex-direction: row;
align-items: center;
justify-items: center;
z-index: 20;
overflow-x: auto;
max-width: 100%;
flex-shrink: 0;
@variant sm {
margin-top: 0;
}
}
.tab-bar.bar-header {
padding: calc(var(--spacing) * 0.5);
gap: calc(var(--spacing) * 1) calc(var(--spacing) * 1);
margin-top: calc(var(--spacing) * -1);
margin-left: auto;
margin-right: auto;
@variant sm {
justify-content: start;
}
}
.tab-item {
&:not(.tab-selected) {
color: var(--color-slate-500);
@variant dark {
color: var(--color-zinc-500);
}
border: 1px solid transparent;
}
padding: calc(var(--spacing) * 1.5) calc(var(--spacing) * 4);
border-radius: 999px;
font-weight: var(--font-weight-medium);
z-index: 0;
flex-shrink: 0;
@variant hover {
color: var(--color-slate-900);
background-color: var(--color-slate-100);
@variant dark {
color: var(--color-zinc-100);
background-color: var(--color-zinc-900);
}
}
}
</style>