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

Feature request: group unknown props for forwarding #2930

Closed
pshrmn opened this issue Jun 2, 2019 · 1 comment · Fixed by #4489
Closed

Feature request: group unknown props for forwarding #2930

pshrmn opened this issue Jun 2, 2019 · 1 comment · Fixed by #4489

Comments

@pshrmn
Copy link

pshrmn commented Jun 2, 2019

All of the props that are passed to a component are available through $$props, but there isn't a great way to access the "unknown" (non-exported) props. Having the ability to access these props would make it more convenient to forward attributes down to a native DOM element.

Currently, my workaround is to destructure $$props with every known prop name.

<div {...rest} id={computed}>
  <slot></slot>
</div>

<script>
  export let one;
  export let two;

  $: computed = `${one}-${two}`;

  const {
    one: _one,
    two: _two,
    '$$slots': _slots,
    '$$scope': _scope,
    ...rest
  } = $$props;

</script>
<div one='test' two='ing' tabindex="-1">Testing</div>
<!-- <div id="test-ing" tabindex="-1">Testing</div> -->

My ideal use would look more like:

<div {...$$attrs} id={computed}>
  <slot></slot>
</div>

<script>
  export let one;
  export let two;

  $: computed = `${one}-${two}`;
</script>

I used $$attrs since Vue has an $attrs property (based on its inheritedAttrs), but I'd happily type $$theseAreTheUnexportedProps if it saved me from doing the destructuring myself.

If this isn't ideal default behavior, maybe it could be controlled with an option?

<svelte:options inherit={true} />
@Conduitry
Copy link
Member

In 3.20.0, you now have access to $$restProps which is an object of all props not explicitly declared as exports in the component! 🎉

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.

2 participants