joe, to javascript

In last week’s post, I said, “That is because you can not pass an array directly into a web component”. I said that I might take a moment at some point to talk about how you could do that. Well, today is the day we are doing that.

Let’s start with a basic Lit example.

See the Pen by Joe Steinbring (@steinbring)
on CodePen.

You will notice that the ArrayList class has an items property that is an array type. Lit won’t let you do something like <array-list items = ['Item 1', 'Item 2', 'Item 3']></array-list> but it is fine with you passing it in using javascript. That means that myList.items = ['Item 1', 'Item 2', 'Item 3']; does the job, fine.

So, how can we do that with a Vue.js array?

See the Pen by Joe Steinbring (@steinbring)
on CodePen.

It is the same thing except you need to set the value of the list in an onMounted() lifecycle hook.

What about if we want to do it with React?

See the Pen by Joe Steinbring (@steinbring)
on CodePen.

With React, you just set the value in a useEffect() React hook.

https://jws.news/2024/how-to-pass-an-array-as-a-property-into-a-web-component/

DavidDarnes, to random
@DavidDarnes@mastodon.design avatar

I want light DOM <slot>s for , but they allow WCs to insert content into light DOM elements

DavidDarnes, to random
@DavidDarnes@mastodon.design avatar

Newly added to my collection: <link-peek>!

Turn any regular anchor link into a rich preview (also known as an ‘unfurled’ link). Can be used with any JSON API and allows for custom templating.

Prefect for creating more visual interest on links without filling the page with additional HTML noise:

✍🏻 https://darn.es/link-peek-web-component/
📦 https://www.npmjs.com/package/@daviddarnes/link-peek

davatron5000,
@davatron5000@mastodon.social avatar

@DavidDarnes I love this! I feel like between you and @zachleat we're finally getting closer to what 's full potential.

DavidDarnes, to random
@DavidDarnes@mastodon.design avatar

TIL that Zapier uses to allow people to embed zaps on their web pages ⚡️

smallcircles, to webassembly
@smallcircles@social.coop avatar

@enhance_dev Backend agnostic server-side rendering (SSR) for Web Components

> https://enhance.dev is an HTML-first full-stack web framework that gives you everything you need to build standards-based multi-page web apps that perform and scale. apps and their elements are server-side rendered for incredible performance and seamless progressive enhancement.

https://begin.com/blog/posts/2024-04-08-introducing-enhance-wasm

heybran, to javascript
@heybran@mastodon.online avatar

Using inline events handlers inside vanilla side project is kinda nice, what do you think? Love the freedom and joy of writing vanilla .

https://heybran.cn/blog/web-components-inline-event-handlers/

joe, (edited ) to javascript

Earlier this week, when I wrote about how to build an autocomplete using Vue.js, it was less about exploring how to do it and more about documenting recent work that used Vuetify. I wanted to use today’s post to go in the other direction. Recently, I discovered the value of using Lit when writing Web Components. I wanted to see if we could go from the HTML / CSS example to a proper web component.

First crack at it

Lit is powerful. You can do a lot with it. Let’s start with a rewrite of Tuesday’s final example to one that uses just Lit.

See the Pen by Joe Steinbring (@steinbring)
on CodePen.

The first thing that we do in this is to import LitElement, html, css from a CDN. Our CountySelector class extends LitElement and then customElements.define('county-selector', CountySelector) at the bottom of the page is what turns our class into a tag. The static styles definition is how you style the tag. You will notice that there aren’t any styles outside of that. The markup is all defined in render() near the bottom. The async fetchCounties() method gets the list of counties from the data.jws.app site that I created last week.

This works but web components are supposed to be reusable and this isn’t reusable enough, though.

How do we increase reusability?

As you might remember from last month’s post about web components, you can use properties with a web component. This means that the placeholder and the options for the autocomplete can be passed in as properties in the markup.

See the Pen by Joe Steinbring (@steinbring)
on CodePen.

You will notice that the big difference between this version and the first one is that we dropped the API call and replaced it with a countyList property that defines the options. We can do better, though.

See the Pen by Joe Steinbring (@steinbring)
on CodePen.

In this next version, we eliminate all explicit references to counties since a person might presumably want to use the component for something other than counties. You might want to use it to prompt a user for ice cream flavors or pizza toppings.

How do you use Vue with a web component?

Unfortunately, you aren’t going to be able to use something like v-model with a web component. There are other ways to bind to form inputs, though. Let’s take a look.

See the Pen by Joe Steinbring (@steinbring)
on CodePen.

In the above example, optionsList and selectedOption are defined as Refs. The ref object is mutable (you can assign new values to .value) and it is reactive (any read operations to .value are tracked, and write operations will trigger associated effects). The options list can be passed into the web component using the :optionsList property. You might notice, though that the example is using .join(', ') to convert the array to a comma-delimited list. That is because you can not pass an array directly into a web component. That is likely going to be the subject of a future article. You might also notice that it is triggering the when you click on a suggestion and onBlur. The dispatchEvent() method sends an event to the object, invoking the affected event listeners in the appropriate order. That should trigger updateSelectedOption when you select a county or finish typing one that isn’t in the list.

So, what do you think? Do you have any questions? Feel free to drop a comment, below.

https://jws.news/2024/how-to-implement-an-autocomplete-using-web-components/

#JavaScript #Lit #VueJs #WebComponents

enhance_dev, to webassembly
@enhance_dev@fosstodon.org avatar

🎉 Introducing Enhance WASM

Say hello to Enhance WASM — backend agnostic server-side rendering for web components.

by @brianleroux

https://begin.com/blog/posts/2024-04-08-introducing-enhance-wasm

DavidDarnes, to random
@DavidDarnes@mastodon.design avatar

New version of <mastodon-post> Web Component just dropped!

You can now access nested data returned from the Mastodon public API using dot notation, e.g. account.display_name or account.avatar.

📦 https://www.npmjs.com/package/@daviddarnes/mastodon-post
✍🏻 https://darn.es/mastodon-post-web-component/

DavidDarnes,
@DavidDarnes@mastodon.design avatar

Turned on sponsoring via Buy Me a Coffee so you can donate for my work

https://buymeacoffee.com/daviddarnes

pablolarah, to random
@pablolarah@mastodon.social avatar

🟢Custom pseudo-classes for web components with the CustomStateSet API
by Ollie Williams @hypeddev

https://fullystacked.net/custom-pseudo-classes/

DavidDarnes, to random
@DavidDarnes@mastodon.design avatar

Been working on an update to my <mastodon-post> Web Component to allow you to target nested data within a post using dot notation.

If you have a moment I'd appreciate a check over what I've changed just to make sure there isn't a regression and I'm not over engineering it. Thanks!

https://github.com/daviddarnes/mastodon-post/pull/2

khalidabuhakmeh, to webdev
@khalidabuhakmeh@mastodon.social avatar
DavidDarnes, to random
@DavidDarnes@mastodon.design avatar

My friend from my Nord design system days @WickyNilliams has created a set of for displaying calendars called ‘cally’.

You should definitely check it out:
https://wicky.nillia.ms/cally/

stuffbreaker, to random
@stuffbreaker@mastodon.social avatar

Storybook Helpers 2.0 have landed!

https://www.npmjs.com/package/wc-storybook-helpers

Changes include:

  • Storybook 8 support
  • Removed v6 support
  • A new sandbox for testing
  • Improved syntax and formatting when showing code
  • Updated configuration API

Check it out!

joe, to javascript

Yesterday, we looked at how to define our own web components and how to use web components that were defined using Shoelace. Today, I figured that we should take a quick look at Lit. Lit is a simple library for building fast, lightweight web components.

Let’s see what yesterday’s “hello world” demo would look like with Lit.

See the Pen by Joe Steinbring (@steinbring)
on CodePen.

The syntax is a lot more pleasing in my humble opinion. Let’s see how to do something a little more complex, though.

See the Pen by Joe Steinbring (@steinbring)
on CodePen.

This example defines <blog-feed></blog-feed> with a json parameter. Lit has a <a href="https://lit.dev/docs/components/lifecycle/#connectedcallback">connectedCallback()</a> lifecycle hook that is invoked when a component is added to the document’s DOM. Using that, we are running this.fetchBlogFeed() which in turn running await fetch(). It is then using https://lit.dev/docs/components/rendering/ to render the list items.

I am kind of digging Lit.

https://jws.news/2024/playing-more-with-web-components/

#JavaScript #Lit #WebComponents

schizanon, to random
@schizanon@mas.to avatar

I'm not rich, but I don't spend more than I earn in interest on my Bitcoin, so I'm having a hard time motivating myself to look for a job. I've been a web developer for about 25 years, and though I like the work, I can't stand the people. They are ego-maniacal, bad at communication, and humorless in general. There are exceptions, but they get cowed by louder more aggressive ones. "So contract" I hear you say; but here's the weird thing; I actually like working with people.

schizanon,
@schizanon@mas.to avatar

But sitting around arguing with fucks on social media isn't gonna help me find that. If anything I'm becoming more like them the more I do.

So I try to think of some silly project to build, something to keep my skills sharp. I know the web, and there's always stuff to learn. and have arrived and there's so much potential to create building blocks from those that other people can use.

joe, to javascript

We briefly played with web components once before on here but it has been a few years and I wanted to go a little deeper. Web components are a suite of different technologies that allow developers to create custom, reusable, encapsulated HTML tags for use in web pages and web apps. Essentially, they let you create your own HTML elements with their own functionality, independent of the rest of your codebase.

Let’s start by taking a look at a very basic example.

See the Pen by Joe Steinbring (@steinbring)
on CodePen.

In this example, the MyGreeting class extends HTMLElement to create a custom element. The constructor then creates a shadow DOM for encapsulation and adds a <span>element with a greeting message (which uses the name attribute for customization). The customElements.define method then registers the custom element with the browser, associating it with the tag <my-greeting>.

So, what can we do with this? You might have heard of Shoelace / Web Awesome. That is just a collection of cool web components. Let’s take a look at a quick example.

See the Pen by Joe Steinbring (@steinbring)
on CodePen.

As you can see above, you just include the activates Shoelace’s autoloader and then registers components on the fly as you use them. Let’s look at a slightly more complicated example.

See the Pen by Joe Steinbring (@steinbring)
on CodePen.

If you flip open the JavaScript panel, you will see that it still neeeds event listeners for the open and close buttons but it is not as complex as if you were writing this frome scratch.

https://jws.news/2024/playing-with-web-components/

joe, to random
@joe@toot.works avatar

That's intriguing. 🤔 #FontAwesome #WebComponents

bridgetown, to ruby
@bridgetown@ruby.social avatar

Hey folks, we're kicking off a new pledge drive to accelerate the development of Bridgetown, an "alt " web framework which starts off in the and vanilla-first, -first, -friendly development, but provides the ability to scale up to dynamic fullstack applications & publications.

Version 2.0 is underway with modernization, performance, and of course new features all on the table!

Consider sponsoring today to ensure 2.0 absolutely rocks. 🤘

https://github.com/sponsors/jaredcwhite

noleli, to random
@noleli@mastodon.social avatar

Question for the folks: say I’m building a simple component. It’s in a design system context, so I want to leave it flexible in terms of what goes inside. I also want to leave the component’s styles reasonably encapsulated so that page authors don’t accidentally (or purposefully) do anything too weird. Here are the four approaches I’ve thought of, along with some pros and cons. Am I missing anything obvious? https://codepen.io/noleli/pen/vYMgmLx

westbrook, to webdev
@westbrook@mastodon.social avatar

Lots of very interesting ideas today at the 's Demo Days no. 1! 🔥

If you missed out, you can find more info at https://github.com/w3c/webcomponents-cg/issues/84

If you never want to miss out again, join the convo at https://discord.gg/s933rmKR9R

If you want to see Demo Days no. 2, share you availability and subscribe to https://calendar.google.com/calendar/u/0/embed?src=o25bim5rvcu42mfnqilirpmp44@group.calendar.google.com

See you there! 👋

frankel, to random
@frankel@mastodon.top avatar
westbrook, to random
@westbrook@mastodon.social avatar

The #webComponentsCG has two community protocols https://github.com/webcomponents-cg/community-protocols/pull/15 and https://github.com/webcomponents-cg/community-protocols/pull/45 that we have resolved to raise as "Proposal" protocols on the 25th of this month. Share your thoughts, today!

How might you leverage these techniques in your #webComponents?

Any visible blockers to these interoperating with the contexts in which you work?

Community Protocols target optional, knowable APIs for broad interoperability across UI delivery, don't get locked-in to you tools, today!

  • All
  • Subscribed
  • Moderated
  • Favorites
  • megavids
  • thenastyranch
  • rosin
  • GTA5RPClips
  • osvaldo12
  • love
  • Youngstown
  • slotface
  • khanakhh
  • everett
  • kavyap
  • mdbf
  • DreamBathrooms
  • ngwrru68w68
  • provamag3
  • magazineikmin
  • InstantRegret
  • normalnudes
  • tacticalgear
  • cubers
  • ethstaker
  • modclub
  • cisconetworking
  • Durango
  • anitta
  • Leos
  • tester
  • JUstTest
  • All magazines