Reflective properties and cloning elements #741
Replies: 1 comment 2 replies
-
There's no rule, but I generally try to follow a couple simple guidelines:
This depends on how you've created them and whether or not properties have updated since then. In the majority of cases, attributes do not change on their own. If you set I'll admit the line is a bit gray, but I don't believe an element should reflect all its properties by default. Even if you did this with scalar data, you couldn't do it with non-scalar without the use of converters or without littering the DOM with potentially expensive operations (think of a data table with a large array of rows). If you want to clone an element, you should probably iterate the properties you're interested in to make sure they're in sync. Better yet, why not create the element from scratch so you don't have to be concerned with its current state? That guarantees the element is how you intend it to be. Regarding the icon example, the case could be made for reflecting |
Beta Was this translation helpful? Give feedback.
-
Many Shoelace elements have 'reflective' properties, properties which have both a (JavaScript) property value and are also represented as an attribute on the HTML element. Not all properties are reflective. Some can't be reflective (you can't store an HTML element itself inside an attribute value). What is the exact 'rule' for letting a property be a reflective one?
There are some inconsistencies. For example the Button's
disabled
property is reflective, but the Dropdown'sdisabled
property is not.The reason I'd like properties to be reflective is the possibility to clone them. Cloning (see https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode) copies any Node and its attributes, but it will not copy properties. Currently cloning an Icon will not copy its name and/or library properties, because they are not reflective. It will copy its attributes, but these might have changed. See example below.
It is in this example of course possible to set the attributes, instead of the properties. I prefer setting properties, but that does not work for all components. This results in having to use properties in some cases and attributes in others.
Example:
Consider an Icon is created using HTML. The Icon's name property is then changed by JavaScript code. The attribute still shows the 'original' attribute value.
Beta Was this translation helpful? Give feedback.
All reactions