Skip to content

Allow $props.id() to be the default value of a property #16069

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

Closed
webJose opened this issue Jun 2, 2025 · 3 comments
Closed

Allow $props.id() to be the default value of a property #16069

webJose opened this issue Jun 2, 2025 · 3 comments

Comments

@webJose
Copy link
Contributor

webJose commented Jun 2, 2025

Describe the problem

In a component library, one might allow a component to accept an HTML element ID, or the component could default to the value of $props.id().

As it stands today, $props.id can only be used to initialize a const variable in the component's script.

Furthermore, even if the component did not accept its ID to be set externally via properties, it is helpful for the component to be able to share its ID by means of a binding.

To achieve this today, one would need to do something like this:

<script>
  let {
    id = $bindable()
  } = $props();

  const cid = $props.id();

  $effect.pre(() => {
    id = cid;
  });
</script>

Describe the proposed solution

It would be nice if one could simply use $props.id() as the default value of a property:

<script>
  let {
    id = $bindable($props.id())
  } = $props();
</script>

Externalizing the value of $props.id() is a must for components that create things like accessible text, where the id is needed elsewhere in a described-by attribute, to name an example that is not the obvious <label for={id}>... example.

Importance

would make my life easier

@paoloricciuti
Copy link
Member

Why not just this?

<script>
	const _id = $props.id();
	let { id = $bindable(_id) } = $props();
</script>

@webJose
Copy link
Contributor Author

webJose commented Jun 2, 2025

Why not just this?

<script> const _id = $props.id(); let { id = $bindable(_id) } = $props(); </script>

Hehe, that is much nicer. I haven't done Svelte at work for months and it is showing, lol.

2 lines of code definitely is better than my version. Feel free to decide whether or not you want to keep this feature request open. I'm OK with this 2-liner solution.

@dummdidumm
Copy link
Member

We can revisit this if the request becomes more common - until then closing, thank you.

@dummdidumm dummdidumm closed this as not planned Won't fix, can't repro, duplicate, stale Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants