-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Split attrs and props #3015
Comments
i think is very important to support this fature; |
I would love to see something like this in Svelte! Ember has something similar when you use the <SomeComponent @argumentName class="class-from-parent" /> Inside the component template you can "spread" the html attributes like this (splattributes): <div class="component-class" ...attributes>content</div> To have the following rendered html
The main use case for this is passing in classes to position the component inside other components. Or passing in data-test attributes for testing purposes. In Svelte you would have to manually code this for each component, which isn't ideal. |
Yesterday, while building an <!-- TextInput.svelte -->
<script>
export let value;
export let large = false;
</script>
<input type="text" class:large bind:value {...$attrs} />
<!-- $attrs becomes { maxlength: "128" } as defined in App.svelte --> <!-- App.svelte -->
<script>
import TextInput from './TextInput.svelte';
let name = '';
</script>
<TextInput bind:value={name} maxlength="128" large/> |
And how did you solve this problem? |
Using a reactive declaration like this: <script>
export let large = false;
export let value;
$: props = omit($$props, 'value', 'large');
</script>
<input type="text" class:large bind:value {...props} /> However, with that approach, you will bump into this bug . |
More or less a duplicate of #2930. I think if we were to have something like this, it would probably take the form of a magic global |
This approach makes us duplicate prop names. It might be better to use the internal api: import { current_component } from 'svelte/internal';
export function omit(obj) {
const keysToOmit = Object.keys(current_component.$$.props);
return Object.keys(obj).reduce((acc, key) => {
if (keysToOmit.indexOf(key) === -1) acc[key] = obj[key]
return acc
}, {})
} and then inside svelte component $: attrs = omit($$props); |
There is no need to find new ways of doing this. $$rest is on its way |
Oh thats cool! |
Quick search through issues haven't results, so I will create this.
Is it technically possible to split native attributes and properties into $$attrs and $$props for cases where we need to bind only native attributes to some element?
Something like in Vue realization:
In Vue it's much cleaner, because component have only one root element. So it's logical to bind attributes to first element. On the other hand, in Svelte it's possible to have several root elements and this is great ability, but may be it's a good idea to allow developers manually control native attributes bindings?
Some stupid example:
I think this may be useful in cases when we have many properties and don't want to see all properties zoo in markup. Or it may contain something like unregistered properties, which will exclude all explicitly exported props.
However, if we back to the tutorial. It can be bad idea, because of:
What do you think?
The text was updated successfully, but these errors were encountered: