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

lwc:spread applies props rather than attributes #4425

Open
nolanlawson opened this issue Jul 29, 2024 · 2 comments
Open

lwc:spread applies props rather than attributes #4425

nolanlawson opened this issue Jul 29, 2024 · 2 comments

Comments

@nolanlawson
Copy link
Collaborator

Description

lwc:spread applies property names to an element (similar to Object.assign()), but not attributes, which may be unexpected.

For example, a developer might assume that this will render as class="red":

<h1 lwc:spread={props}>Hello world!</h1>
export default class extends LightningElement {
  props = {
    'class': 'red',
  }
}

However, it does not. Instead, you must use className:

export default class extends LightningElement {
  props = {
    'className': 'red',
  }
}

Similarly, you cannot apply attributes that do not have a property equivalent:

  props = {
    'data-foo': 'foo', // will not render the `data-foo` attribute
  }

Steps to Reproduce

See repro

Potential workaround

Use the property format (e.g. className instead of class), or use renderedCallback to manually call setAttribute() on the element.

Potential solution

This would be an overhaul of how lwc:spread currently works and would have the potential for backwards-incompatible breakages. For the time being, though, this issue can at least serve as documentation about the current behavior.

@ekashida
Copy link
Member

For context, this behavior is by design. I recall having a dev sync discussion where the outcome was to keep it as simple as possible and to avoid any special handling.

@ekashida
Copy link
Member

The documentation could be better--it feels like we could add more detail around event handlers and IDL attributes that reflect as content attributes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants