question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow hyphenated properties to be defined in a component

See original GitHub issue

Is your feature request related to a problem? Please describe.

Given the usage of the following hypothetical component:

<Icon data-tooltip="foobar" />

I receive a warning that data-tooltip has not been declared as a property on the Icon component. However, there is no way to do that since it is hyphenated.

Describe the solution you’d like

Inside my component, it would be nice if I could declare the property like this:

export let dataTooltip = null

Describe alternatives you’ve considered

I know that the properties are also available in $$props. But that doesn’t really solve the problem.

const dataTooltip = $$props['data-tooltip']

While this would give me access to the value, it doesn’t remove the in browser warning.

How important is this feature to you?

It’s annoying. Important? Well, certainly not a deal killer. And for now I just am using the form: <Icon tooltip="foobar" />. And then I am passing the property to the hyphenated version inside the component.

The reason this came about was I was trying to just use a spread operator and pass props thru. But, here I cannot because I don’t want to keep seeing those warnings all over the place.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:35
  • Comments:26 (7 by maintainers)

github_iconTop GitHub Comments

22reactions
oranmorcommented, Jul 30, 2020

@osamamaruf I’ve faced this problem too. As temporary solution I’ve written my own wrapper for CustomElement, where all camelCased attributes are converted to dash-case:

import MyCustomComponent from './MyCustomComponent.svelte';

class MyCustomComponentWrapper extends MyCustomComponent {
  static get observedAttributes() {
    return (super.observedAttributes || []).map(attr => attr.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase());
  }

  attributeChangedCallback(attrName, oldValue, newValue) {
    attrName = attrName.replace(/-([a-z])/g, (_, up) => up.toUpperCase());
    super.attributeChangedCallback(attrName, oldValue, newValue);
  }
}

customElements.define('my-custom-component', MyCustomComponentWrapper);

MyCustomComponent.svelte

<script>
  export let someDashProperty;
</script>

<svelte:options tag={null} />

{someDashProperty}

Then you can use it in this way:

<my-custom-component some-dash-property="hello"></my-custom-component>

Hope it helps until fix is coming

20reactions
sawdencommented, Jun 28, 2021

2 years and the workaround is still the best solution? 😕

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I reference a JavaScript object property with a hyphen ...
Save this answer. Show activity on this post. Hyphenated style properties are referenced via camelCase in JavaScript, so use style. textAlign .
Read more >
vue/attribute-hyphenation
Rule Details #. This rule enforces using hyphenated attribute names on custom components in Vue templates.
Read more >
hyphens - CSS: Cascading Style Sheets - MDN Web Docs
The hyphens CSS property specifies how words should be hyphenated when text wraps across multiple lines. It can prevent hyphenation entirely,  ...
Read more >
hyphens | CSS-Tricks
The hyphens property controls hyphenation of text in block level elements. You can prevent hyphenation from happening at all, allow it, ...
Read more >
Angular coding style guide
Do use a hyphenated, lowercase element selector value; for example, admin-users . Do use a custom prefix for a component selector. For example,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found