-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTML First Interface - v4 #30
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JRJurman
force-pushed
the
v4
branch
2 times, most recently
from
September 13, 2023 20:43
4134d30
to
0eb0438
Compare
JRJurman
changed the title
New Component Effect, deprecate script behavior in Component Definitions - v4
HTML First Interface - v4
Sep 15, 2023
…ew ControlledInput, testing methods for TramLite watchers
…e console logs, new examples
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This version is available by pointing to
tram-lite@beta
.https://unpkg.com/tram-lite@beta
This version focuses on an HTML first interface, allowing developers to build complex behaviors in web-components using special
tl-
attributes.This is in-part inspired by the success of HTMX, and the attractiveness of the new template syntax found in Tram-Lite v3.
Example Component
Codepen: https://codepen.io/JRJurman/pen/YzdrKQR
Notable things to highlight
<template tl-definition>
The
tl-definition
indicates a template tag that has a list of component definitions to process.This is the HTML interface for
TramLite.define
Rather than using a custom built-in (which isn't universally supported), we now watch for a
tl-definition
attribute using a mutation observer, regardless of platform. Eventually, when a universal solution is implemented here for adding behavior for existing tags (potentiallyhas
?) then we can look into migrating to that!<input tl-controlled>
The
tl-controlled
attribute sets up the 2-way data binding that was common for inputs (and related components). The value for these elements will be bound to an attribute on the host component (and vice-versa).This is the HTML interface for
TramLite.updateRootAttr
.There are three additional attributes that work with
tl-controlled
:tl-trigger
- the event you want to update the host attribute on. E.g.input
,keyup
, etc... Defaults tochange
.tl-hostattr
- the attribute name for the host component. Defaults tovalue
tl-targetattr
- the attribute on this input to copy over. Defaults tovalue
, but can bechecked
for checkboxes, or anything else.In the above example, we have the two inputs:
This sets up two controlled inputs. Both update on
input
events. The first one has a defaulttl-hostattr
andtl-targetattr
ofvalue
, so the host element will have an attributevalue
bound to this input. The second input has atl-hostattr="max"
, which means that updates to this input will set themax
attribute on the host web-component.Note: while we use
tl-controlled
here with input elements, other elements can use this attribute (e.g. select).<script tl-effect>
The
tl-effect
attribute is a special binding for script-tags that allows them to reference their host component. This is similar to the previous implementation of script tags in Tram-Lite definitions (but now should work in any web-component!).This is the HTML interface for
TramLite.addAttributeListener
.Notably, we've removed support for external script tags (using the
src
attribute). Tram-Lite support here was previously unclear, and it's not obvious how we might support these in the ways we support inline javascript.Scripts with the
tl-effect
also support two additional attributes:tl-dependencies
- a list of attributes that will cause the inline script to reruntl-hold
- an attribute that indicates that the component scripts should not trigger. If this attribute is present, we won't run the script. By default, Tram-Lite sets the value here to"component-mount"
and will remove the attribute when the component has mounted. We may support other options in the future (likeconstruction
orDOMContentLoaded
)In the above example, we have the following script tag
This script will re-trigger whenever
value
ormax
is updated.API Changes
Removed
html
andsvg
While these methods were useful in some cases, they didn't directly help with building web-components or enhancing web-component behavior. Their implementation often had unpredictable side-effects (as an artifact of setting
innerHTML
).There is a use-case here that offering this makes us more inline with tools like JSX, which offer a more elegant way to build elements (and namely lists of elements) in javascript, but we should seek to offer an HTML interface first, rather than an exclusive JS implementation.
Moved functions into static methods
Rather than having top-level functions that are defined on the window object, we now expose all the Javascript methods from the
TramLite
class. They are...TramLite.define
TramLite.updateRootAttr
TramLite.addAttributeListener
Now that the library is more focused on an HTML first interface, these functions don't need to be as readily available.
There is also a utility function,
TramLite.appendShadowRootProcessor
, which is used to bind the functionality fortl-effect
andtl-controlled
. Developers could potentially use this for upgrading other kinds of components with their own attributes, in the same way we process our own attributes.Checklist
PR AnnotationsMigration Guide(see Migration guide from v3 to v4 #31)