Open sorvell opened 1 year ago
A good workaround for spread attributes:
Instead of spreading attributes onto your element, have a function that applies the common attributes, and accepts additional customization props.
Thus, this:
const commonProps = {
class: 'my-link',
target: '_blank',
};
html`<a href="#" ...${commonProps}>${label}</a>` // not supported
turns into this:
function CommonLink({href, children}:{href:string, children:TemplateResult|string}): TemplateResult {
return html`<a class="my-link" target="_blank" href=${href}>${children}</a>`;
}
/// later:
return CommonLink({href: '#', label: label })
Even better, CommonLink now supports spread syntax (for limited set of props), and you get good type-checking:
const commonProps = { href: '#' };
return CommonLink({...commonProps, label: label })
Motivation
This has been a long standing request and is generally useful when passing around blobs of related data that should be handed down between elements together.
Supporting this capability could also be used to satisfy [RRFC] Better Declarative Host Manipulation.
Example
How
Here is a slightly opinionated prototype of a
spread
directive. Sample usage with a bit of explanation inline:Opinions (favoring simplicity)
in
checkIn addition, the prototype provides a
host
directive that can be used inChildPart
position, e.g.And a
tag
directive that obviates the need to usestatic-html
, also building uponspread
, e.g.Caveats
Values that are spread into elements can conflict with otherwise bound values. While not necessarily harmful and following a simple "last one wins" rule, this behavior could be unexpected or a potential foot-gun. It's up to the user to understand this and ensure proper configuration.