be-enhanced
TypeScript icon, indicating that this package has built-in type declarations

0.0.151 • Public • Published

be-enhanced [WIP]

How big is this package in your project? NPM version

be-enhanced provides a base class that attaches a custom class instance onto a third party (built-in or custom) element. It supplements be-hive. The pair of packages enables "casting spells", or enhancing server-rendered DOM elements, based on cross-cutting custom attributes. These base classes can also be used during template instantiation for a more optimal repeated web component scenario, via "non verbal spells".

be-enhanced, which focuses on adding custom properties to an element, and be-hive, which focuses on attaching and forwarding attributes values to the custom property based on a list of one or more attributes to observe, together form a simplified userland implementation of this proposal. Based on the really nice idea of supporting multiple attributes, this component in particular, be-enhanced is oblivious to any attributes, leaving the magic of tying the knot between attributes and associated enhancements to be-hive exclusively.

be-enhanced, together with be-hive, provide a much more "conservative" alternative approach to enhancing existing DOM elements, in place of the controversial "is"-based customized built-in element standard-ish. There are, however, a small number of use cases where the is-based built-in approach may be the preferred one.

In contrast to the "is" approach, we can apply multiple behaviors / decorators / enhancements to the same element:

#shadow-root (open)
<black-eyed-peas 
    be-on-the-next-level=11
    be-rocking-over-that-bass-tremble
    be-chilling-with-my-motherfuckin-crew
></black-eyed-peas>

which seems more readable than:

<is-on-the-next-level level=11>
    <is-rocking-over-that-base-tremble>
        <is-chilling-with-my-motherfunckin-crew>
            <black-eyed-peas></black-eyed-peas>
        </is-chilling-with-my-motherfuckin-crew>
    </is-rocking-over-that-base-tremble>
</is-on-the-next-level>

Not to mention concerns about performance. And then there's this.

Priors

be-enhanced's goals are quite similar to what is achieved via things that go by many names.

We prefer (progressive) "enhancement" as the term, but "decorator", "[cross-cutting] custom attribute", "directive", "behavior" are also acceptable terms.

Differences to these solutions (perhaps):

  1. This can be used independently of any framework (web component based).
  2. Each decorator can be imported independently of others via an ES6 module.
  3. Definition is class-based, but with functional reactive ways of organizing the code ("FROOP")
  4. Applies exclusively within Shadow DOM realms.
  5. Reactive properties are managed declaratively via JSON syntax.
  6. Namespace collisions easily avoidable within each shadow DOM realm.
  7. Use of namespaced properties allows multiple vendors to apply different enhancements simultaneously.
  8. be-enhanced provides "isomorphic" support for using the same declarative syntax while transforming templates during template instantiation, as well as while the DOM is sitting in the live DOM tree. But the critical feature is that if the library is not yet loaded during template instantiation, nuk ka problem, the live DOM decorator can apply the logic progressively when the library is loaded. Meaning we can punt during template instantiation, so that render blocking is avoided. And if the library is loaded prior to template instantiation, it can still be supplemented by the live DOM decorator, but the initial work performed during the template instantiation can be skipped by the live DOM decorator.

Prior to that, there was the heretical htc behaviors.

Examples of constructing a be-enhanced progressive enhancement decorator:

Enhancement Purpose Code
be-adoptive Adopt styles from parent shadow realm code
be-a-beacon Announce arrival of (last) element in HTML Stream code
be-based Adjust URLs from a base URL code
be-bound Provide two-way binding code
be-buttoned-up Make a button link to button menu in popover code
be-channeling Channel events to the host element. code
be-clonable Make DOM element be clonable code
be-committed Trigger a button click on keyboard "enter" code
be-computed Compute values from other HTML signals via local script tags. code
be-counted Track and share how many times button has been clicked. code
be-definitive Define web component from existing DOM in tree. code
be-delible Make DOM element be deletable code
be-derived Derive state from server-rendered HTML code
be-detail-oriented Make the fieldset expandable. code
be-dispatching Dispatch event with specified name. code
be-elevating Elevate local property value to upstream element. code
be-entrusting Derive initial value from server streamed semantic HTML. code
be-exportable Allow script tag to export itself as a module code
be-fetching Fetch resource based on url property code
be-formidable Enhance the form's validation abilities. code
be-functional Connect script to DOM elements. code
be-importing Import Static, Declarative HTML Web Components with Streaming HTML code
be-inclusive Include content from nearby templates code
be-intl Format numbers using intl.NumberFormat. code
be-invoking Invoke method on upstream peer element or the host. code
be-it Forward props to adjacent element. code
be-lazy Load template when it scrolls into view code
be-linked Connect HTML (web) components together with readabe syntax code
be-literate Enhance the input element so it can read local files code
be-memed Cache templates contained within templates code
be-observant One-way-bind from the host or a peer element. code
be-promising Run enhancements in a predictable order code
be-prop-slotting Transfer values from light children to host. code
be-propagating Publish property changes to adorned element. code
be-repeated Repeat section of HTML code
be-scoped Create an EventTarget associated with the adorned element that can hold scoped state. code
be-searching Make a DOM element searchable code
be-sharing Share values from the adorned element to peer elements. code
be-switched Lazy loads content when conditions are met. code
be-typed Allow the user to customize input element during run time. code
be-value-added Add a value property used for formatting. code
be-valued Reflect value of input to the value attribute on input event. code
be-written Stream a url to a target DOM element. code

The be-enhancement api

be-enhancement commits the cardinal sin of attaching a custom property gateway, "beEnhanced" on all Elements. Being that the platform has shown little to no interest in providing support for progressive enhancement over many decades when such solutions have proven useful, we should feel no guilt whatsoever.

Having this property gateway is a life-saver as far as performance and providing an easy way of integrating enhancements into (some) frameworks.

To set a value on a namespaced property (e.g. via a framework), do the following:

await customElements.whenDefined('be-enhanced');
const base = myElement.beEnhanced.by.aButterBeerCounter;
Object.assign(base, {count: 7});

This should work just fine even if the enhancement a-butter-beer-counter hasn't loaded yet. The enhancement will absorb the settings the moment it becomes attached to the element it is enhancing.

Server-rendered HTML vs Template Instantiated HTML

If the HTML we are working with is rendered by the server, the most effective way of activating the custom enhancement is via the associated attribute:

<button be-counted>Count me</button>

However, activating enhancements via attributes is not ideal when using client side api's to build the API, such as during template instantiation.

As we stated so very long ago, the way to do this most effectively programmatically is:

import('be-counted/be-counted.js');
await customElements.whenDefined('be-enhanced');
const base = myElement.beEnhanced.by.beCounted;
Object.assign(base, {value: 7});

In the example above, we are importing the dependency asynchronously using the dynamic import. This means that depending on the timing the hydration of the enhancement may be done during template instantiation (for example), or after the element being enhanced has been added to the live DOM tree.

Using dynamic import as shown above has the benefit that the dependency will not cause the template instantiation to be render blocked.

Event Notifications

Be-enhancement element decorators/behaviors typically don't, by default, emit events that get bubbled up the DOM tree.

To subscribe to an event:

const myEnhancement = await myElement.beEnhanced.whenResolved(emc);
myEnhancement.addEventListener('resolved', e => {

})

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.0.151111latest

Version History

VersionDownloads (Last 7 Days)Published
0.0.151111
0.0.15077
0.0.14970
0.0.14877
0.0.14713
0.0.1462
0.0.1452
0.0.1445
0.0.1436
0.0.1425
0.0.1410
0.0.1400
0.0.1396
0.0.1384
0.0.1370
0.0.1360
0.0.1350
0.0.1340
0.0.1334
0.0.1321
0.0.1312
0.0.1300
0.0.1290
0.0.1280
0.0.1270
0.0.1260
0.0.1250
0.0.1240
0.0.1230
0.0.1220
0.0.1210
0.0.1200
0.0.1191
0.0.1185
0.0.1170
0.0.1166
0.0.1156
0.0.1141
0.0.1130
0.0.1121
0.0.1110
0.0.1100
0.0.1090
0.0.1080
0.0.1070
0.0.1060
0.0.1050
0.0.1040
0.0.1030
0.0.1020
0.0.1010
0.0.1000
0.0.990
0.0.980
0.0.970
0.0.960
0.0.950
0.0.940
0.0.930
0.0.920
0.0.910
0.0.900
0.0.890
0.0.880
0.0.870
0.0.860
0.0.850
0.0.840
0.0.830
0.0.820
0.0.810
0.0.800
0.0.790
0.0.780
0.0.770
0.0.760
0.0.750
0.0.740
0.0.731
0.0.720
0.0.710
0.0.700
0.0.692
0.0.680
0.0.671
0.0.660
0.0.650
0.0.640
0.0.630
0.0.622
0.0.611
0.0.600
0.0.590
0.0.583
0.0.570
0.0.560
0.0.550
0.0.540
0.0.530
0.0.521
0.0.516
0.0.500
0.0.490
0.0.480
0.0.470
0.0.460
0.0.450
0.0.440
0.0.430
0.0.420
0.0.410
0.0.400
0.0.390
0.0.380
0.0.370
0.0.360
0.0.350
0.0.340
0.0.330
0.0.320
0.0.310
0.0.300
0.0.290
0.0.280
0.0.270
0.0.260
0.0.251
0.0.240
0.0.231
0.0.220
0.0.210
0.0.200
0.0.191
0.0.180
0.0.170
0.0.160
0.0.150
0.0.140
0.0.130
0.0.120
0.0.110
0.0.100
0.0.90
0.0.80
0.0.70
0.0.60
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10
0.0.00

Package Sidebar

Install

npm i be-enhanced

Weekly Downloads

425

Version

0.0.151

License

MIT

Unpacked Size

141 kB

Total Files

48

Last publish

Collaborators

  • bahrus