Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svelte 5: Access snippets from context module #10350

Open
scampos-s3d opened this issue Jan 31, 2024 · 2 comments · May be fixed by #14315
Open

Svelte 5: Access snippets from context module #10350

scampos-s3d opened this issue Jan 31, 2024 · 2 comments · May be fixed by #14315
Assignees
Milestone

Comments

@scampos-s3d
Copy link

scampos-s3d commented Jan 31, 2024

Describe the problem

Can't send/find/reference/use a snippet defined in a component from a <script context="module"> block in that component

Example of what I'm trying to do

<script lang="ts" context="module">
    import type { Snippet } from "svelte";

    let modals = $state<Array<{snippet: Snippet | Snippet<any>, isFullScreen: boolean, arg?: any }>>([]);

    export function openModal(snippet: Snippet | Snippet<any>, isFullScreen: boolean, arg?: any)
    {
        modals.push({snippet, isFullScreen, arg});
        modals = modals;
    }

    export function closeModal()
    {
        modals.pop();
    }

    export function confirm(): Promise<boolean>
    {
        let resolvePromise: (result: boolean) => void;
        let confirmPromise = new Promise<boolean>((resolve) =>
        {
            resolvePromise = resolve;
        });
        
        openModal(confirmModal, false, resolvePromise!);

        return confirmPromise;
    }
</script>

{#each modals as {snippet, isFullScreen, arg} }
    {#if isFullScreen}
        <div class="full-screen-modal">
            {@render snippet(arg)}
        </div>
    {:else}
        <div class="dialog-modal-wrapper">
            <div class="dialog-modal">
                {@render snippet(arg)}
            </div>
        </div>
    {/if}
{/each}

{#snippet confirmModal(resolvePromise: (value: boolean | PromiseLike<boolean>) => void)}
    <button on:click={() => resolvePromise(true)}>Accept</button>
    <button on:click={() => resolvePromise(false)}>Cancel</button>
{/snippet}

Describe the proposed solution

Maybe make snippets visible from <script context="module"> block, the possibility of define {#snippet coso() context="module"}, javascript snippets or anything like that.

Importance

would make my life easier

@dummdidumm
Copy link
Member

Related: People want to export snippets, which should be doable as long as they don't use anything that's in scope of the instance component

@JReinhold
Copy link

To chime in here, for Svelte CSF in Storybook, this would allow us to call setTemplate(snippet) directly in the existing module script instead of creating a new script just for this purpose.

https://github.com/storybookjs/addon-svelte-csf/blob/next/README.md#default-snippet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants