Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Proposal]$snippet  —  Snippet adapters in code#16238

Refzlund started this conversation inIdeas
Discussion options

$snippet

Use snippets programatically by wrapping them inside another snippet via$snippet.

The ability to compose logic related to the snippet and creating adapters for snippets.

Introduction

Snippets are extremely versatile, ergo why they're a strong pattern.

However, a caveat is that you can only provide the snippet with props when you're in control of the@render.
This is fine, as component variables can be declared — however it adds easily 2n lines of code.

<scriptlang='ts'>import {chevron }from'Icons.svelte'importButtonfrom'Button.svelte'</script><Button>    {#snippeticon()}        {@renderchevron({ rotate:90 })}    {/snippet}    {#snippetchildren()}        Some text    {/snippet}</Button>

That's a lot of extra work to provide these parameters.

Today, we can "technically" create snippet adapters via code, however, it's not pretty and messes with Svelte internals.

<scriptlang='ts'module>declareconst tooltip: (_internal:CommentNode,text: ()=>string)const createTooltip:Snippet= (text:string)=> {// Additional optional logic here.return (...args)=>tooltip(args[0], ()=>text)    }export {createTooltip }</script>{#snippettooltip(text:string)}    ...{/snippet}<!---------><scriptlang='ts'>importComponentfrom'./Component.svelte'import {createTooltip,tooltip }from'./Tooltip.svelte'</script><Componentsnippet={createTooltip(`Here's a tooltip!`)}>    Hello there</Component>

Proposal

My proposal here, is to formalize the process of adapting snippets.

<scriptlang='ts'>import {chevron }from'Icons.svelte'importButtonfrom'Button.svelte'</script><Buttonicon={$snippet(()=>chevron({ rotate:90 }))}>    Some text</Button>

Or by code

<scriptlang='ts'module>const createTooltip= (text:string)=>$snippet(()=>tooltip(text))</script>{#snippettooltip(text:string)}    ...{/snippet}

We could adapt parameters, to fit other ones;

<scriptlang='ts'module>// nameDisplay: Snippet<[firstName: string, lastName: string]>importNamefrom'./Name.svelte'// Snippet<[name: string]>import {name }from'./DisplaySnippets.svelte'</script><PersonnameDisplay={$snippet((firstName,lastName)=>name(`${firstName} ${lastName}`))}>    This person is magnificant.</Person>

And so.

Tip

Additionally, we could also provide a way to get theComment Node, that the snippet is attached to;

$snippet(()=>{constnode=$snippet.node// e.g. the `comment` node snippet representing the snippet    ...crawlTree})

as this could be used for tree-crawling

Alternatively, we could allow calling Snippet's outside of the@render scope:

{#snippetchevron({...})} ... {/snippet}<Buttonicon={chevron({ rotate:90 })}>

Now all said and done, this is clearly a nice-to-have, and just an idea. I do personally think it would fit into the Svelte 5 environment.

You must be logged in to vote

Replies: 0 comments

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Ideas
Labels
None yet
1 participant
@Refzlund

[8]ページ先頭

©2009-2025 Movatter.jp