Skip to content

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

Closed as not planned
Closed as not planned
@webJose

Description

@webJose

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions