Comments (14)
We elected not to support this automatic dash to camel case conversion from
PolymerElement
inLitElement
because it was generally non-platform-y. Instead, lowercase attributes are automatically mapped to the correctly cased property which is what the platform itself does for properties liketabIndex
.This behavior can be customized by importing camelToDashCase from Polymer implementing
static attributeNameForProperty(property) { return camelToDashCase(property); }
Can this decision still be challenged? This fundamentally limits the cross-compatibility with other frameworks like Vue.js which is kind of a bad thing for a web-component-focused framework, isn't it?
from lit-element.
LitElement is about as vanilla as it gets, it is not trying to invent anything already present in the platform. It just adds a few things missing. There are even spec proposals like Template Instantiation, making lit-html even smaller - and making all of this just a tiny bit of convenience.
This is the future of Polymer as well, removing as much of the custom stuff as possible, and what turned out to not make sense, and just use the platform as much as possible, now that it is actually possible to do so.
Expect a lot of the "magic" from Polymer not to be present in LitElement, like reflection, computed values etc
from lit-element.
Doesn't JavaScript already do kebab-case conversion for things like z-index
? I'm not sure spec-wise what difference there is between attribute values (and if this is specifically just for element.styles
) and attribute keys.
I'm also thinking actually the hyphenated attribute data-*
creates its own DOMStringMap which is reflected on the element.dataset
property. Further hyphens are converted to camelCase:
<my-element data-some-attr="foo"></my-element>
element.dataset.someAttr
I think this is somewhat of an outlier since data-
doesn't map exactly to element.data.*
, but would it be too much to reflect hyphenated properties to a DOMStringMap and camelCase after the first instance of the hyphen?
class MyElement extends LitElement {
static get properties() {
return {
foo: { type: DOMStringMap },
};
}
connectedCallback() {
super().connectedCallback();
this.foo.barBaz; // baz
// or perhaps a simple Map which doesn't require any conversion.
this.foo.get('baz-bar'); // bar
}
render() {
return html`<my-element foo-bar-baz="foo" foo-baz-bar="buzz"></my-element>`
}
}
from lit-element.
You can observe the attributes using static get observedAttributes() {return [ "my-name"]}
Then update the properties in
attributeChangedCallback(name, oldValue, newValue) {
this[name] = newValue;
}
If name
is then created as a lit element property, it will automatically re-render
from lit-element.
We elected not to support this automatic dash to camel case conversion from PolymerElement
in LitElement
because it was generally non-platform-y. Instead, lowercase attributes are automatically mapped to the correctly cased property which is what the platform itself does for properties like tabIndex
.
This behavior can be customized by importing camelToDashCase from Polymer implementing
static attributeNameForProperty(property) {
return camelToDashCase(property);
}
from lit-element.
From this comment it seems like this was intentional: kenchris/lit-element#15 (comment)
from lit-element.
so, if I name a property with the name my_name or myName or MyName .... how do I reflect that to the attribute: my-name ?
from lit-element.
@mustafaekim There is no need to reflect it... if you really need to you can set attribute yourself manually via setAttribute. There is no automatic attribute reflection in LitElement.
from lit-element.
OK, reflection may not be a good idea, I am not sure. However I want to re-ask the same question like, I want to initialize my lit-element with some value set via attributes. for instance: <my-element my-name="Mustafa">
and I want the property myName, MyNAme or my_name to be set accordingly. Where is the link between those?
from lit-element.
OH ok, just vanilla web-component. that's good. I am a little bit confused among vanilla, polymer and lit-element
from lit-element.
OK thanks, I like the direction, just a little bit confused about the APIs
from lit-element.
@mustafaekim From my usage experience, <my-element myName="Mustafa">
works fine with property myName
. But the attributes won't actually appear in the DOM.
from lit-element.
@ernsheong true, but for sharable components you most likely want to support attributes as well.
from lit-element.
This codes works in lit-element 2.0.1:
import camel2Dash from '../lib/camel2Dash'
class MyElement extends LitElement {
public static _attributeNameForProperty(name, options) {
return super._attributeNameForProperty(camel2Dash(name), options)
}
// ...
camel2Dash
method (typescript):
export default (str: string): string => {
return str.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase()
}
the camel2Dash
method might not be perfect but it works well for me, i found it here: https://gist.github.com/youssman/745578062609e8acac9f#gistcomment-2304728
here is the method implementation from the lit-element source code: https://github.com/Polymer/lit-element/blob/master/src/lib/updating-element.ts#L360
from lit-element.
Related Issues (20)
- Typescript error on latest build HOT 1
- mixins don't work when using ESNext in tsconfig to compile HOT 1
- Error throw when accessing queryAssignedNodes getter (No version >2.4.0) HOT 1
- Support for contexts? HOT 2
- Persistance? HOT 5
- idempotent/soft customElement decorator HOT 1
- state() is shown as deprecated in LitElement 2.5 HOT 1
- Should not be necessary to copy TemplateResult
- jsdelivr/+esm: [object Object] HOT 1
- Nested components, slot and text styling HOT 1
- Git tags are not in sync with npm tags HOT 2
- No release major/minor branches available for reference. HOT 2
- Move tests and benchmarks to GitHub Actions
- Accessibility => lit-element + Google Chrome Screen Reader Extension => UI Design is Changed HOT 3
- Property does not re-render when fired from custom event HOT 4
- Add a way to know when rendering of an element, and ALL its sub-elements, is finished HOT 3
- Litelement Boolean property returns undefined in when not set HOT 6
- Forward Compatibility With Lit 2 misleading HOT 2
- Starter projects missing in getting started documentation HOT 1
- How do you get the actual click event target? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lit-element.