Netflix / x-element

A dead simple starting point for custom elements.
Apache License 2.0
28 stars 12 forks source link

Discussion: Add syntactic sugar for boolean attribute bindings to Template Engine. #121

Closed theengineear closed 1 year ago

theengineear commented 1 year ago

Authors can already leverage a boolean updater to accomplish a boolean attribute binding. Adding syntactic sugar may potentially be a more ergonomic solution.

Why we might add it:

Why we might not add it:

Proposal

Users would be able to declare something like the following to bind an attribute as a boolean (see the ? in there?):

static template(html) {
  return ({ trueOrFalse }) => {
    return html`<div ?data-true="${trueOrFalse}">Hi There.</div>`;
  };
}

Currently

…for comparison, this is how you would do this currently.

static template(html, { boolean }) {
  return ({ trueOrFalse }) => {
    return html`<div data-true="${boolean(trueOrFalse)}">Hi There.</div>`;
  };
}

Note that the what to do (i.e., bind an attribute) is nicely separated from the how to do it (i.e., in a boolean fashion).

theengineear commented 1 year ago

I’m on the fence for this one. At a minimum, I’d like to start without support for the ? sugar. One of the main goals of x-element is to stay minimal — this functionality is strictly not necessary.

Additionally, I think that using boolean is more favorable since it exposes users to what’s really going on and will better-prepare them for leveraging things like defining a custom updater later.

theengineear commented 1 year ago

@klebba — Based on our conversation, we've decided to go with the ? syntax and not expose the a boolean updater.