Web-components via <template>
tags
#2825
mindplay-dk
started this conversation in
3. Feature Ideas
Replies: 2 comments 3 replies
-
@mindplay-dk Hey, I've tried out your snippet and it works like a charm. Thanks so so much :) :) |
Beta Was this translation helpful? Give feedback.
1 reply
-
@mindplay-dk Hm, there was one problem I have been seeing: Somehow, See this minimal example: https://jsfiddle.net/ra15tbmv/ Do you have any idea what is going wrong here? :) All the best, |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'd like to have reusable UI components, in some form. 🤔
I discovered this package, which attempts to leverage the
<template>
tag, as a means of generating reusable web-components - it was surprisingly simple, but sadly also defunct due to breaking changes in the Alpine API.After a few attempts, here's a basic, reusable version of the dropdown from the tutorial:
https://jsfiddle.net/mindplay/vw86ebyo/
Just a very basic proof of concept at this point, but this already hints at how powerful this feature could be - having styles locally defined inside the component, using local selectors, and exposing options via CSS variables, it's already reminiscent of the ergonomics of CSS-in-JS and larger UI frameworks, but mostly just leveraging native web platform capabilities.
There is obviously a lot more to unpack here - I don't know what the idea with the attribute stuff in
alpine-webcomponent
was, there was no examples, tests or documentation. I'm not sure if web-components should communicate via attributes, or maybe property getter/setters, or directly via Alpine's data tree somehow... probably the latter?I honestly don't know much about Alpine yet. I could just see this really simple HTML-based approach working just as well inside web-components, why not - so I gave it a shot, and I think it looks promising.
I like the rugged, pragmatic approach of Alpine. But if I'm building a complex, interactive UI, I think there are other considerations besides elements being visible on screen before they get bootstrapped for interactivity. The ergonomics of having to debug a page with 50 copies of custom UI elements with all their markup - it could get heavy. In terms of performance, I would be surprised if parsing the same markup 50 times is actually faster than native
cloneNode
from a<template>
tag.And sure, maybe Alpine isn't on a mission to replace the big frameworks. But wouldn't it be fun if it could? You already have most of what is needed - state, conditionals, loops, etc... all we need is some way to package a section of Alpine markup for reuse. 🙂
Is anything like this in the stars for Alpine?
Beta Was this translation helpful? Give feedback.
All reactions