Svelte 5: Pass bind:value or function to snippet #15847
Answered
by
brunnerh
artttimiron
asked this question in
Q&A
-
Hey! Is it any way, how I can detect withdraw option change via snippet? Or pass function to here? Sorry, I'm new in Svelte WithdrawOptions component has buttons, and I want to detect changes in my Parent component <script lang="ts">
import { SegmentedPicker } from '$lib';
import type { TabList } from '$lib';
import { WithdrawOptions } from '$lib';
import { UserWalletBalance } from '$lib';
let withdrawOptionId = $state<String | undefined>();
const tabs: TabList = [
{
id: 'withdraw',
label: 'Withdraw',
content: () => withdraw(withdrawOptionId)
},
{
id: 'earnings',
label: 'Earnings',
content: earnings
}
];
let activeTabId = $state('withdraw');
</script>
{#snippet withdraw(param: String | undefined)}
<div class="mt-1">
<WithdrawOptions bind:testParam={param} />
</div>
{/snippet}
{#snippet earnings()}
<div>test me 2</div>
{/snippet}
<div class="bg-transparent">
<UserWalletBalance currentBalance={0.5} lifetimeEarningUsd={0.5} />
<div class="w-full max-w-md mx-auto mt-3">
<SegmentedPicker {tabs} bind:activeTabId />
</div>
</div>
<style>
:global(:root) {
--page-background: linear-gradient(180deg, #FFF 0%, #EDEDEE 100%);
}
</style> and SegmentPicker <script lang="ts">
import { Tabs } from 'bits-ui';
import type { TabList } from '$lib';
interface Props {
tabs: TabList;
activeTabId: string;
}
let { tabs, activeTabId = $bindable(tabs[0]?.id) }: Props = $props();
</script>
<div class="max-w-160">
<Tabs.Root bind:value={activeTabId} class="bg-background-alt min-w-[300px] max-w-[500px] p-3">
<Tabs.List
class="rounded-9px bg-dark-10 shadow-mini-inset dark:bg-background grid w-full grid-cols-2 gap-1 p-1 text-sm font-semibold leading-[0.01em] dark:border dark:border-neutral-600/30"
>
{#each tabs as tab (tab.id)}
<Tabs.Trigger value={tab.id}>
{#snippet child({ props })}
<button
{...props}
class="data-[state=active]:shadow-mini dark:data-[state=active]:bg-muted h-8 rounded-[7px] bg-transparent py-2 data-[state=active]:bg-white"
>
{tab.label}
</button>
{/snippet}
</Tabs.Trigger>
{/each}
</Tabs.List>
{#each tabs as tab (tab.id)}
<Tabs.Content value={tab.id} class="select-none pt-3">
{@render tab.content()}
</Tabs.Content>
{/each}
</Tabs.Root>
</div>
and types.ts import type { Snippet } from 'svelte';
export interface Tab {
id: string;
label: string;
content: Snippet;
}
export type TabList = Tab[]; |
Beta Was this translation helpful? Give feedback.
Answered by
brunnerh
Apr 29, 2025
Replies: 1 comment 6 replies
-
I do not understand the question, and is all that code necessary to illustrate the issue? Additionally, readability could be better via syntax highlighting which can be added by specifying a language after ```js
function test() {
console.log('hi')
}
``` (For Svelte, use |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can either pass in a stateful object and bind to one of its properties or use function binding syntax, e.g.
Object property binding Playground example