-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Svelte 5: Programmatically creating snippets #9980
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
Comments
I've found the same issues using StoryBook and in developing a custom cell renderer for an AgGrid. const columnDefs = [
...
{
maxWidth: 50,
cellRenderer: cellRendererFactory((c, p) => {
const deleteButton = new Button({
target: c.eGui,
props: {
icon: 'fa-solid:trash',
class: 'px-0 mx-0',
small: true
}
});
deleteButton.$on('click', event => {
removeRow(p.node.rowIndex);
});
return deleteButton;
}),
} But i think that this is a lot of work in respect to something, for example, like this: function deleteButton({c, p}) {
return <Button small icon='fa-solid:trash' class='px-0 mx-0' on:click={event => removeRow(p.node.rowIndex)}/>
}
const columnDefs = [
...
{
maxWidth: 50,
cellRenderer: deleteButton
} Furthermore, if the rendered component should be a "composite" (eg. a span or a div with two buttons as children) i have not found anything simple to use searching in svelte docs or by googling it, so i resorted to the intermediary .svelte file. |
Support this 100%, i would LOVE to have snippets just like in react micro components. Without the need for an extra file for the micro component |
Svelte 5 introduced import { createRawSnippet } from 'svelte';
const greet = createRawSnippet((name) => {
return {
render: () => `
<h1>Hello ${name()}!</h1>
`,
setup: (node) => {
$effect(() => {
node.textContent = `Hello ${name()}!`;
});
}
};
}); |
Closed by #12425 |
Describe the problem
I've had the need to create snippets (specifically for the children prop) both in unit tests as well as in Storybook. My current workaround has been to create a new component which passes what I need as children to the component I want to test. But that creates indirection since I need to place that in a separate file.
Describe the proposed solution
It would be great to have some built-in API to define a snippet function without having to have a .svelte file. This would simplify testing components with @testing-library/svelte as well as passing args as snippet props in Storybook.
Alternatives considered
As mentioned I've been defining new components in new files that pass the snippets I want, but I don't think that's a good long-term solution.
Importance
nice to have
The text was updated successfully, but these errors were encountered: