218 points · by 1337shadow · 6y ago · 110 comments
A couple days ago, I released Shoelace 2.0, an open source library of common UI components.
These components work with any framework, can be loaded via CDN, are fully customizable with CSS (no preprocessor required), and install easily with a simple script + stylesheet. They were built with Stencil.js, which is a fantastic tool. The end result compiles down to vanilla web components.
Each component was designed from scratch to be lean, customizable, and easy to use. Accessibility is a common question folks have about component libraries. I’m definitely not an expert here, but I've spent a lot of time trying to get it right. I would like to echo the experts and say that accessibility only starts with components, but hopefully having a good foundation to build on will encourage others to think about it more at higher levels.
I hope you'll take a second to check it out! I'm happy to answer any questions you have about the project, how it's built, etc.
Bootstrap is great for when I just want to quickly prototype and build a decent looking interface, but it feels confined when I start wanting to style things myself. When designing MPAs I typically use Bootstrap.
Tailwindcss is great for styling anything and everything, but I find sometimes I waste too much time styling little things that other CSS frameworks already have nice defaults for (and I am a terrible designer so this compounds my dificulties). I often use tailwind for SPAs.
Forgive me if my question is a little naive, but where could I fit Shoelace in for pet projects? Do you see it as being a suitable replacement for one of these usecases? Complementing them? Does it lie somewhere in the middle?
One thing Shoelace doesn't provide right now is a set of base styles. I've considered offering these as an opt-in CSS class or a <sl-prose> component, but I'm not sure it belongs tightly coupled with the library.
On one hand, it makes sense to provide a single "theme" that styles both components AND the underlying website/app. On the other hand, this will make themes very opinionated and potentially overbearing. I still need to think about this more.
In the meantime, you can use something like Tailwind's typography plugin [1] to handle base styles and use Shoelace in lieu of Bootstrap. IMO Shoelace + Tailwind is a great combination for those who prefer utility classes.
<button class="btn btn-outline-secondary">...</button>
would be like: <bs-button outline color="secondary">...</bs-button>
While this doesn't sound all that useful, I think this would be very handy for the interactive components like popovers, tooltips, carousels that need a lot of tag soup and javascript: <bs-popover>
Hello World
</bs-popover>Also there are small version of buttons but none for other form controls which is quite odd.
Seems nice and lightweight overall. I would love to see a dark version too.
I can't seem to reproduce this for other components, but I'll check again.
> Also there are small version of buttons but none for other form controls which is quite odd.
Button, input, textarea, and select all have size variations. It's missing from checkboxes, radios, and switches but I'm not sure how useful that is. Feel free to submit an issue so folks can vote on it.
Edit: this has been identified and fixed in dialogs!
I'm new to web components though, so I was surprised when I saw in the docs that you need to use the CSS `::part` selector to style components, rather than simply using more "traditional", well-known CSS. Is this a result of using a shadow DOM? If so, what benefits does using a shadow DOM use for these components?
Thanks!
- Customizations can be made to components with explicit selectors, such as ::part(icon), rather than implicit selectors, such as .button > div > span + .icon, that are much more fragile.
- The internal structure of a component will likely change as it evolves. By exposing component parts through an API, the internals can be reworked without fear of breaking customizations as long as its parts remain intact.
- It encourages us to think more about how components are designed and how customizations should be allowed before users can take advantage of them. Once we opt a part into the component's API, it's guaranteed to be supported and can't be removed until a major version of the library is released.
1. https://shoelace.style/getting-started/customizing?id=compon...
I had another look at the docs, but I still don't see anything about the benefits of shadow DOM for this library (unless the benefit is the "part thing"?)
From Stencil's website [1]:
> Stencil is a compiler that generates Web Components (more specifically, Custom Elements) and builds high performance web apps. Stencil combines the best concepts of the most popular frameworks into a simple build-time tool.
> Stencil takes features such as Virtual DOM, async rendering (inspired by React Fiber), reactive data-binding, TypeScript, JSX, and Static Site Generation (SSG) and then generates standards-based Web Components and web apps with these features baked in.
¹ Requiring JavaScript harms accessibility, especially for web pages (as distinct from web apps). It makes pages take longer to load, not be as spider-friendly, and be more fragile when JavaScript fails to load, whether deliberately or accidentally—and simple network failures and the likes break things more often than you might imagine. I myself browse the web with JavaScript disabled via uMatrix, mostly because it speeds the web up a lot on average. Privacy improvements are a distant secondary reason. When I encounter pages that require JavaScript, I either give up on them or open them in a Private Browsing window, where I don’t have the uMatrix extension enabled; or if it’s something I expect I may be using more than once or twice, I see about more precise whitelisting in uMatrix.
I have found I write far less markup than bootstrap (despite I only have a few custom css), can do custom styling easily, can do theming easily, etc. And with htmx I could move nearly 95% of all to the server side, yet have close to the same experience than with vue.
Considering that a lot of stuff still need to travel to the backend have the logic on the client have not been that useful for my needs.
My only gripe? I need to use npm to run the production builds with tailwind for have less size, but, I have (finally!) small css!
I've never used web components before, and I use SSR a lot (ASP. NET Core) - are you unable to use SSR with all web components, or only those that use shadow DOM? What's the reason I can't just render an `<sl-whatever>` and render the `value` attribute at the server?
Apologies if I've misunderstood or got some of this wrong!
So if you have a component looking like this in your dev tools:
<sl-emoticon which="smile">
#shadow-root (open)
:-)
</sl-emoticon>
Its HTML serialisation will still be <sl-emoticon which="smile"></sl-emoticon>, never with a :-).Look, there are ways around this lack of serialisation of shadow roots; the examples in https://github.com/skatejs/ssr succinctly show early experiments in the field, where it serialises the shadow root as an element, which it then turns into a shadow root with an inline script—but if the JavaScript doesn’t run, that won’t work and you’ll be left with a weirdly broken element, so I disqualify it and say “Shadow DOM is incompatible with SSR”.
But for Web Components in general, there’s nothing stopping you serialising them with their light DOM, though if your component goes adding children to itself when or after you connect it you might need your connectedCallback to detect if this has effectively already happened (on the server side), and wire things up instead of changing its children. This process (wiring a JS-side component tree to an already-existing DOM, rather than rendering from scratch) is known as rehydration and applies to more than just Web Components.
I concur that normal content like news articles should still be accessible without activating any JS at all.
The idea of real web components not bound to any of these fancy JS frameworks that die and get replaced so quickly nowadays is amazing in my opinion.
People talk about the good of web components not tied to any framework, but in practice it doesn’t tend to pan out that way. These things are a framework themselves, so now you probably have a whole extra framework for every such thing you use (though admittedly each will be a lot lighter than React or similar), and it forces you to work in a different way from what you’re used to within your main framework. People routinely end up wanting (or sometimes needing, especially when React is the root framework) tighter integrations between their frameworks, so they go wrapping the web components in a new layer, e.g. to get better types and tooling support, or to route events through their own system rather than the plain DOM events system, or to do something or other involving types that aren’t just strings. That last one is a major limitation and shortcoming of Web Components as a fully-general solution: if you’re passing in attributes, for example, it’s strings all the way, but people often want and sometimes need more complex things. As an example where this causes substantial inefficiency, imagine a chart element that needs an array of data; if it’s straight web components, you’ll be nudged firmly towards turning it into a bunch of elements, or serialising it as JSON and setting it as an attribute, where setting it as a property (something you can only do via JS, so you’ve lost the declarative nature of the element) will be way more efficient.
- file uploads, the browser fails to indicate that an upload is in progress, the user clicks "submit" again and again because they don't see that the browser is uploading,
- file uploads when part of a form with other fields: if some text field doesn't validate, then the form will be shown again and the user will have to upload his file again, unless you go through the length of partially saving the form ... in which case you give up on having a nice transaction that saves the form at once
- selects with at least thousands of choices, they will freeze the browser, you need an autocomplete
So, as long as your website requires a file upload, or offers to select an option for a foreign key on a table with a lot of data then JS is required. Now, that might only be the case for a page or two, that doesn't prevent the rest of your site from working without JS because webcomponents aren't as invasive as typical JS frameworks that basically want to own all of your page.
If you really want to pass JSON to your webcomponent it's pretty easy:
<your-component><script type="text/json" slot="your-data">.. dump your json here</script></your-component>
But, you could also render your stuff directly and let the webcomponent inspect the DOM and build its own data structure.
Anyway, I fail to see the shortcoming here.
WebComponents are not tied to any framework once they are built, or, if they were coded without.
Shoelace doesn't require loading of any framework, but it's built with the brilliant StencilJS library: required to build, not to use.
For me the only shortcoming is with browser based ES modules but they are being fixed with the ImportMap proposal.
I personally appreciated the introduction to Custom Elements and an explanation of the philosophy of the framework. So much of what we do as developers involves tight coupling and lock-in to a full-stack.
I am in love with the tooltip :)
It's basically a law: anything RDF touches, will fail to get adoption.
Another place where “components” generally fall-over is when a component needs to act as a parent or container for arbitrary markup: yes, there’s the “slot” system but those are still defined and controlled by the component, not the consumer - even though ultimately it’s the consumer’s application.
I compare the situation to the component ecosystem for VB6 and WinForms - which to be fair was a lot worse as consumers often had little to no control over how those components rendered, appeared or behaved - but (no pun intended) elements of the same problem exist today.
Another example is WPF - which has major unaddressed issues still - which to be fair is worlds apart from WinForms in terms of what it’s templating system is capable of. A nice thing about WPF was that a component (Control’s) XAML was transparent and editable at design-time in VS, so the consuming application’s developer could get what they want out of it, the catch was that WPF templates are horrendously complex: getting VS to expose the XAML for even a <Button> would dump 50KB+ of markup into your application XAML because it was an all-or-nothing system and you needed XAML for every button state and behaviour - right down to the pulsating button glow effect that was only ever used in Windows Vista...
For example, Shoelace's <sl-select> component mimics the browser's <select> component. It has single select, multi-select, size variations, shape variations, type-ahead selection, validation states, etc. This component is a composition of <sl-dropdown>, <sl-input>, <sl-menu>, etc. and its entire source is less than 500 LOC (including doc comments and prettier formatting).
This is significantly less than many independent <select> alternatives I've seen, and the component itself doesn't have to worry about positioning, styling, etc. because that's handled by lower-level components.
> Another place where “components” generally fall-over is when a component needs to act as a parent or container for arbitrary markup: yes, there’s the “slot” system but those are still defined and controlled by the component, not the consumer - even though ultimately it’s the consumer’s application.
With web components, slotted content stays in the light DOM and is therefore controlled by the user. It's actually kind of a pain point when building them because, internally, you don't have that much control over arbitrary markup. (You can access markup through slot.assignedNodes() and style things with ::slotted() but it's not what I'd call a first-class experience for component developers.)
It'll happen to you!
So it's probably likely that it's younger developers (such as myself) that are used to using emoji and like to use a bit of emoji to add colour and personality to the page.
I'm rather stereotyping here though, it could be some older person who just loves emoji too
It's definitely high up on the list of components that are challenging to get right!
Anyway, that's the feature to pre-render everything: https://stenciljs.com/docs/hydrate-app
What is it exactly that you can't pre-render ? That's not very clear to me. You can put any kind of content inside your web-component tags:
<your-paragraph><p>your test</p></your-paragraph>
If the browser is recent enough to support TLS 1.3 (which we all want to move on too don't we !) then it is recent enough to support web-components for sure.
So, that's pretty much how I see it: browsers have to upgrade for security reasons, if we're getting features like the ability to do some components without having to load a fat frontend framework, components that integrate perfectly with my favorite server side framework, then my question is why: why should we not haz nice things ? we're getting them for free after all ! Without even webpack to make a 1MB bundle ! I'd like your help to better understand what's the bad news here ...
EDIT: don't understand the downvotes, seeing as we built Stencil. Stencil can definitely be used in a more traditional server-side environment like ASP.Net or PHP
Just tested with Firefox 78 and verified it's working now. You may have an older cache.
You can probably achieve that with UIkit if you make one bundle per page with webpack or something, but with web components such as Shoelace you get that for free. So there's that, wether it matters to you or not is up to you of course.
UIkit sprinkles CSS and JS on top of existing HTML elements: <div uk-dropdown></div>
Shoelace defines web components, which are custom elements: <sl-dropdown>
https://stenciljs.com/docs/browser-support/
It seems it is necessary for browsers that have been abandoned and that are never going to have TLS 1.3 support: IE11, Edge 16-18
Personally I'd leave it just to push users onto abandoning those browsers that will never support TLS 1.3.