TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How we use web components

260 pointsby prakhargurunaniabout 4 years ago

19 comments

jjcmabout 4 years ago
I’ve been using webcomponents heavily in my latest project. Built out a full SPA using vanilla js. They are not without problems, but GitHub is using them perfectly. GitHub isn’t pushing them to do what they aren’t yet good at. They’re amazingly suited for consistent components, especially view oriented ones. GitHub also has in my opinion made the correct choice to make their own base element rather than using a standard library one like lit-html. A good base element has two things in my mind - styled component functions and shared util functions specific to your app. It’s amazingly easy to make a simple set of styled component base functions. Here’s the one I made for my project: <a href="https:&#x2F;&#x2F;github.com&#x2F;jjcm&#x2F;soci-frontend&#x2F;blob&#x2F;master&#x2F;components&#x2F;soci-component.js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jjcm&#x2F;soci-frontend&#x2F;blob&#x2F;master&#x2F;components...</a> (lines 6-32 for the styled component functions). The rest of your component should be shared functions specific to your use case. Once that base class is there they’re so easy to use.<p>Two caveats with webcomponents however. The first is Safari will be the bane of your existence. You should dev in safari if you can. If it works there it will work in all other browsers. The second is stay away from form elements, for now. Or if you do, use an encapsulating component that inserts a slotted input into the light dom. The form registration api isn’t fully supported yet except by chrome, and even then it’s a bit rough around the edges.
1337shadowabout 4 years ago
WebComponents are an amazing replacement for jQuery, get the best of frontend Component based development without the bloat and the JS toolchain! Vanilla JS is great with WebComponents. We even write them in python with Ryzom&#x27;s transpiler:<p>```<p>class DeleteButton(Component): tag = &#x27;delete-button&#x27;<p><pre><code> class HTMLElement: def connectedCallback(self): this.addEventListener(&#x27;click&#x27;, this.delete.bind(this)) async def delete(self, event): csrf = document.querySelector(&#x27;[name=&quot;csrfmiddlewaretoken&quot;]&#x27;) await fetch(this.attributes[&#x27;delete-url&#x27;].value, { method: &#x27;delete&#x27;, headers: {&#x27;X-CSRFTOKEN&#x27;: csrf.value}, redirect: &#x27;manual&#x27;, }).then(lambda response: print(response))</code></pre> ```<p>Speaking of which, Rails ViewComponent looks a lot like Python Ryzom, except the later optionnaly supports data-binding over websockets.<p>But writing JS in Python is extremely satisfying if you already like writing Python more than you like writing JS ;)
评论 #27049870 未加载
pvsukale3about 4 years ago
For my sideproject (<a href="https:&#x2F;&#x2F;profilehunt.net" rel="nofollow">https:&#x2F;&#x2F;profilehunt.net</a>) I have been using a somewhat similar approach. I use Github ViewComponents (<a href="https:&#x2F;&#x2F;github.com&#x2F;github&#x2F;view_component" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;github&#x2F;view_component</a>) for componentization of Rails views. It helps with more modular views which are easier to debug (errors usually point directly to the line in view code, unlike Rails views). I use StimulusJS (<a href="https:&#x2F;&#x2F;stimulus.hotwire.dev&#x2F;" rel="nofollow">https:&#x2F;&#x2F;stimulus.hotwire.dev&#x2F;</a>) for Javascript interactivity. Stimulus is great, it allows me to write more structured reusable JS controllers without global event listeners and ids. For my project I have heavily used these two to create a Trello like board which behaves like an SPA. State and routing is still on server. I just fetch HTML on clicks and render it using Stimulus. It is usually fast enough as I only fetch small components and append them to DOM, but in case it&#x27;s not I use loading animations. If you want to take a look at how it is done, this blog post (<a href="https:&#x2F;&#x2F;boringrails.com&#x2F;articles&#x2F;hovercards-stimulus&#x2F;" rel="nofollow">https:&#x2F;&#x2F;boringrails.com&#x2F;articles&#x2F;hovercards-stimulus&#x2F;</a>) explains it really well.<p>edit: Fixed a link and grammar.
评论 #27047672 未加载
评论 #27047715 未加载
claytongulickabout 4 years ago
I do pretty much everything with Web Components these days.<p>It&#x27;s so nice to be able to pick things I need from different frameworks, like Ionic, vaadin, Shoelace, etc... and they all just work together.<p>I tend to implement vanilla web components, I enjoy managing the life cycle and don&#x27;t find it particularly burdensome. Of course. Lit-html is a big reason why that&#x27;s easy to do. I use it for rendering. I generally avoid using the shadow dom unless I have a really good reason.<p>My components all self-register, and are pretty encapsulated. They all maintain internal state, but for more global state I use a library I wrote called ApplicationState [1].<p>It&#x27;s hard to imagine going back to the framework churn after experiencing the freedom of Web Components.<p>It really feels like we&#x27;ve finally realized the promise of composability, reusability, encapsulation and performance with Web Components.<p>[1] <a href="https:&#x2F;&#x2F;claytongulick.github.io&#x2F;applicationstate&#x2F;" rel="nofollow">https:&#x2F;&#x2F;claytongulick.github.io&#x2F;applicationstate&#x2F;</a>
评论 #27047784 未加载
deergomooabout 4 years ago
I was really excited about web components until I discovered that slots and CSS isolation are a package deal.<p>Absolutely great for the developer that wants to ship pre-built components that cannot be affected by the CSS of the site they&#x27;re used in; absolutely useless when you want to make re-usable components for your <i>own</i> app, where you very much want slot content to be styled using the CSS you already have.
评论 #27050526 未加载
评论 #27050427 未加载
评论 #27050390 未加载
kazoomongerabout 4 years ago
I&#x27;m currently working on testing a UI with Selenium that uses Shadow DOM all over the place. It&#x27;s hot garbage, and I have no idea why anybody thought this was a good idea.<p>Something goes wrong in CI and you&#x27;d like to dump the DOM to figure out why? Too bad, the entire page is sitting inside a Shadow DOM, and you get to see an empty body tag and a timeout waiting for an element to appear.<p>Want to click that button that&#x27;s nested 5 Shadow DOMs deep? Have fun creating a monster JS query to manually trawl though the Shadow DOMs to click it, because Selenium has no idea how to access the button. And that&#x27;s if you&#x27;re lucky and the Shadow DOM is marked &quot;open&quot;. If it&#x27;s marked closed, you can go sit on your thumb.<p>It&#x27;s like someone looked at iframes and thought &quot;hey, that&#x27;s a great idea!&quot; and never stopped to think why they were considered bad practice already 20 years ago.
评论 #27050959 未加载
评论 #27052748 未加载
评论 #27049213 未加载
hardwaresoftonabout 4 years ago
Would love to see why they <i>did not</i> choose lit-element, it was one of the best I tried in a recent experiment of ~5 libs.<p>The big thing holding back webcompinents at this point is a data sharing strategy, and I think we could get away with actually using components to store and manage data:<p><a href="https:&#x2F;&#x2F;www.vadosware.io&#x2F;post&#x2F;sade-pattern-services-as-dom-elements&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.vadosware.io&#x2F;post&#x2F;sade-pattern-services-as-dom-e...</a><p>The github repo (which also happens to contain how to write a web component in lit-element, slim.js, tonic, vue, svelte):<p><a href="https:&#x2F;&#x2F;gitlab.com&#x2F;mrman&#x2F;services-as-dom-elements" rel="nofollow">https:&#x2F;&#x2F;gitlab.com&#x2F;mrman&#x2F;services-as-dom-elements</a>
评论 #27048050 未加载
评论 #27048055 未加载
jpangs88about 4 years ago
At my work web components were purposed recently for creating a ui component library of which I was skeptical. On the whole I was skeptical of web components viability and future but this post relieves some of that tension.<p>The other thing I was worried about was that it was planned to after writing this web component lib, to wrap these components in React. Does anyone have any experience or insights into a React wrapped web component lib?
评论 #27047731 未加载
评论 #27047627 未加载
评论 #27047452 未加载
评论 #27050568 未加载
评论 #27049989 未加载
slverabout 4 years ago
I&#x27;ve refreshed myself on web components, templates and shadow DOM, and I understand how each of them works, but I only see immediate benefits from templates. The rest seems a bit like abstraction for abstraction&#x27;s sake and syntax sugar over existing capabilities.<p>What am I missing, what&#x27;s the elevator pitch for web-components and shadow DOM?<p>Scoped styles seems to be something that&#x27;s pushed with shadow DOM a lot, but isn&#x27;t &lt;style&gt; in the page body illegal HTML (despite yes, it works)?
评论 #27050449 未加载
评论 #27050628 未加载
评论 #27049664 未加载
评论 #27051471 未加载
systematicalabout 4 years ago
For those who have used tools like Hotwire, what has your experience been like? How does it compare to something like Vue or React?
评论 #27046421 未加载
评论 #27047764 未加载
评论 #27050342 未加载
m0ngr31about 4 years ago
We&#x27;ve started converting everything to web components using Stencil. Allows us flexibility to have the same look and feel no matter what framework we&#x27;re using.<p>Took a minute to figure out how to get React&#x2F;Vue components to render inside of a web component, but now we&#x27;re all set.
scoofyabout 4 years ago
I have a very dumb question. I&#x27;m currently trying to implement web components into a pretty basic flask site, and I&#x27;m really having trouble. Does anyone know any resources about basic implementation that I can review?<p>I was interested in Lit, and am try to bundle that, but it&#x27;s not working at all, and I really feel like I&#x27;m missing something.
评论 #27047999 未加载
评论 #27047759 未加载
timdaubabout 4 years ago
I started using ficusjs for some experiments [2]. I built a signature button for Metamask [1].<p>What&#x27;s great is that:<p>- I use preact&#x27;s htm as a renderer [3], which is JSX but as template strings.<p>- The API is like (p)react but a bit more generalized. I like it.<p>- The web component concept is great. Especially for mixing server-side rendering and JavaScript-powered components.<p>That last one IMO is web components killer feature. I can now wrote a mini component and then I tugg it in with the other 99% of my page that is rendered server side.<p>It means, I&#x27;m able to serve my users quickly. I have SEO&#x27;d everything too. Cool!<p>-1: <a href="https:&#x2F;&#x2F;github.com&#x2F;TimDaub&#x2F;web3-sign-msg" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;TimDaub&#x2F;web3-sign-msg</a><p>- 2: <a href="https:&#x2F;&#x2F;docs.ficusjs.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;docs.ficusjs.org&#x2F;</a><p>-3: <a href="https:&#x2F;&#x2F;github.com&#x2F;developit&#x2F;htm" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;developit&#x2F;htm</a>
评论 #27049654 未加载
aitchnyuabout 4 years ago
In my side project I use Vue CLI&#x27;s async webcomponents target to generate a webcomponents.js can be imported into a script tag and &lt;some-element props=&quot;123&quot;&#x2F;&gt;. I was at a loss how to include compiled SFCs (components) into a server rendered app. IME, Vue tooling assumes a Node JAMstack SPA and the Django boilerplates were a pain. Guys who wanted to sprinkle interactivity to their apps were at a loss.
lgesslerabout 4 years ago
Something I&#x27;ve never understood how to handle with vanilla web components: if there&#x27;s some state that needs to be shared and kept in sync between multiple components, how do you do it?<p>(For comparison, in React you can pass the state down as props from a common ancestor, and in Clojure frameworks all app state is in a giant object referenced by components as needed.)
评论 #27046953 未加载
评论 #27047567 未加载
评论 #27047966 未加载
评论 #27047170 未加载
fareeshabout 4 years ago
How well do web components work in the context of SEO and pagespeed scores?
评论 #27046564 未加载
评论 #27046525 未加载
hitekkerabout 4 years ago
On the topic of web components, does anyone know if AMP is still using them? I heard they switched over to Preact but I couldn&#x27;t find an official announcement.
评论 #27046573 未加载
chrisweeklyabout 4 years ago
51 comments and no mention of Stencil? hmm.
agluszakabout 4 years ago
&quot;At GitHub, we pride ourselves on delivering a first-class developer experience.&quot;<p>Ugh, every time I see such PR-ese language, I want to stop reading any further