cirrus, to typescript
@cirrus@mstdn.social avatar

Been going down a rabbit hole today trying to get , , manifests and to all hold hands and sing kumbaya.

Send help.

How's your day going?

inautilo, to react
@inautilo@mastodon.social avatar


HTML attributes vs. DOM properties · What fewer and fewer developers seem to know https://ilo.im/15ynz8


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/

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/

NickEast, to Catroventos
@NickEast@geekdom.social avatar
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/

kerfuffle, to Kotlin
@kerfuffle@mastodon.online avatar

For a hackathon this weekend, we built a small application in #Kotlin, #Javalin and #Lit that uses #Tensorflow to detect if you're about to upload something you might want to reconsider, and then allows stripping Exif metadata for privacy.

We also looked at distorting the image to make it unusable for training an #AI. In one day we could just garble the image beyond human recognition, but a better option would be integrating #Glaze to distort it for AI yet not for the human eye.

chronicallydave, to bloomington_in
@chronicallydave@mastodon.social avatar
xalieri, to DigitalArt
@xalieri@masto.ai avatar
xalieri,
@xalieri@masto.ai avatar
xalieri,
@xalieri@masto.ai avatar
tallship, to history

"I knowed his ma. They was good folks. He was full a hell, sure, like a good boy oughta be…He done a bad thing an’ they hurt ‘im, caught ‘im an’ hurt him so he was mad, an’ the nex’ bad thing he done was mad, an’ they hurt ‘im again. An’ purty soon he was mean-mad."

- J.S., who really just wanted to be a fucking marine biologist, as evidenced in his conributions to Between the Pacific Tides, by his friend Ed Ricketts, yet much to his chagrin was relegated to authorship in depression era grit fiction, notable for his brutally honest naturalism, social commentary on extreme hardship, heartache, and subsequently immortalized in pen and celluloid.

Now you fucking know, bitches.

and , both celebrated and vilified; yet still one of my all time great inspirations as an artist. But just who was it he invoked that was so injuriously transfigured?

Can you haz ? 🍔 Can you? I doubt it. lemme know :p

photo credit: Hanna Shandra - https://presentacionesjhm.blogspot.com/2016/11/flabellina-iodinea.html - My confidence in your inability to grasp this is epic. Prove me wrong.

.

dailydrop.hrbrmstr.dev, to golang

Wails: Coding in Harmony, Not in Distress!

The WPEs are b a c k!

We’re going to be doing a bit with the Wails project this year, so let’s start off nice and slow with a small project to get our feet wet before diving in head first.

Subscribe

Wails…who dis?

https://kagi.com/proxy/1002866_1.jpg?c=yj8bAX9r2L_88xABK6cI9d8B1rJdGat1itBlo-TIavloGGA4HLCOtF-ME2oVH2LbO1w1AIy4nUMx1-OPgtexrCyzS3K4TRB3B-GZs59m9BV-Q1CZhkCdrWMAI2WLYBUlWQ_AaGkJ9ciLwxjGcJl7YrTk6YkUuBRk_H_gIed_zA4im9c_7Gmf6O2xGmiSYyWln7anWa3GD-QKJByuRVNFlqp0233YWseDhhgPDv8kdb8%3D

Wails is a project that enables you to write desktop applications using Go and web technologies. It’s a lightweight and fast Electron alternative for Go, allowing you to build applications with the flexibility and power of Go, combined with a rich, modern frontend. Wails uses a purpose-built library for handling native elements such as Window, Menus, Dialogs, etc., so you can build good-looking, feature-rich desktop applications.

The amount of Go and JavaScript required is minimal, at first, and you can 100% get by with Kagi, Perplexity, or just some keen internet searches (and poking through the Drop history for Go and JavaScript resources). I’ll prove this with the app you’re going to modify.

Wails use of these technologies means you can (for the most part) turn any web app you make into something that you can distribute as an actual app to folks. While I’m more of a fan of native apps, this can save a ton of time and reduce cognitive load…something I think we’ll all need this year.

Getting Started with Wails

https://hrbrmstrsdailydrop.files.wordpress.com/2024/01/image-5.png?w=1024To start with Wails, you need to install it first. You can install Wails using the Go install command:

$ go install github.com/wailsapp/wails/v2/cmd/wails@latest

After that install finishes, you can validate it with:

$ wails doctor

In the wall of output you should see “SUCCESS” near the end.

Now, we can initialize a new project!

For this tutorial, we will use the Lit template:

$ wails init -n wails-tutorial -t lit

As longtime Drop readers know, Lit provides a very lightweight and sane wrapper over standard Web Components.

Now, do:

$ cd wails-tutorial$ wails dev

A bunch of text will scroll by and you should see an app screen that lets you enter in some text that will be parroted back at you.

The directory structure looks like this:

wails-tutorial├── README.md├── app.go├── build│   ├── README.md│   ├── appicon.png│   ├── darwin│   │   ├── Info.dev.plist│   │   └── Info.plist│   └── windows│       ├── icon.ico│       ├── info.json│       ├── installer│       │   ├── project.nsi│       │   └── wails_tools.nsh│       └── wails.exe.manifest├── frontend│   ├── dist│   │   └── gitkeep│   ├── index.html│   ├── package.json│   ├── src│   │   ├── assets│   │   │   ├── fonts│   │   │   │   ├── OFL.txt│   │   │   │   └── nunito-v16-latin-regular.woff2│   │   │   └── images│   │   │       └── logo-universal.png│   │   ├── my-element.js│   │   └── style.css│   ├── vite.config.js│   └── wailsjs│       ├── go│       │   └── main│       │       ├── App.d.ts│       │       └── App.js│       └── runtime│           ├── package.json│           ├── runtime.d.ts│           └── runtime.js├── go.mod├── go.sum├── main.go└── wails.json

Here are the core files we’ll be tweaking:

  • app.go contains the Go back end functions we’ll be exposing to the JavaScript front end
  • frontend/index.html is the entry point for the app
  • my-element.js is the Lit web component that handles the interactivity (we’ll be renaming this).

In the default app, Greet in app.go is called by my-element.js. Here’s what that function looks like:

func (a *App) Greet(name string) string {  return fmt.Sprintf("Hello %s, It's show time!", name)}

The func (a *App) Greet(name string) string is a function signature the Wails dev tools looks for. It will automagically generate glue code for the JavaScript parts, and you can use complex function parameters and return values. This one takes in a string, surrounds it with some basic text, then returns it.

You should take some time to change what’s in the Sprintf, rename Greet (and use the new imported name in the my-element.js) and tweak what the render() returns in my-element.js before continuing. It might also be a good idea to read through the official walkthrough.

Making A “Real” App

https://hrbrmstrsdailydrop.files.wordpress.com/2024/01/image-4.png?w=1024Rather than blather a bunch (it is your WPE, after all), I’m going to link you to Codeberg where there’s a modified version of this template project that:

  • uses a third-party Go module for fetching OpenGraph tags from a website
  • displays parts of those tags using Tachyons for the CSS framework.

The core change to app.go is this function:

func (a *App) FetchOpenGraphTags(postURL string) (map[string]string, error) {fmt.Println("FOGT:", postURL)og, err := opengraph.Fetch(postURL)fmt.Printf("OpenGraph: %+vnError: %vn", og, err)return map[string]string{"title":       og.Title,"description": og.Description,"url":         og.URL.String(),}, err}

I added some printing in there so you can see messages appear in the terminal console while the app is running.

When you run wails dev the glue code is, again, automagically generated so the JavaScript side can call that function via:

async fetchOG() {  let thisURL = this.shadowRoot.querySelector('input#url').value  console.log("fetchOG")  FetchOpenGraphTags(thisURL).then(result => {    console.log('fetch opengraph tags:', result)    this.tags = result;    this.requestUpdate();  });}// I’m showing this here, but we’ll talk about it in the next WPEasync connectedCallback() {  super.connectedCallback();}

The fetchOG function is called in render() when the button is pressed:

<button @click=${this.fetchOG} class="btn">Render</button>

You can use Developer Tools from the running app to watch the console messages go by (very useful for debugging).

Your Mission

Your goal is to (a) successfully get this modified app running and (b) utilize more OG tag fields and display the entire set of OG tags in a much nicer way. Extra points for adding another Golang back end function that you need to call from the JS side.

FIN

Today’s WPE sets a solid foundation for future ones, and we’ll be having tons of fun with it as we layer in DuckDB, SQLite, WebR, Pyodide, and more over the coming weeks/months. Give a shout-out or reply to this post if you have any questions/issues.

The first Bonus Drop goes out this weekend to paid subscribers! (The refunds from the transition from Nazistack should be out soon…I keep checking on the status and will follow up next week if there’s a delay.)

Remember, you can follow and interact with the full text of The Daily Drop’s free posts on Mastodon via @dailydrop.hrbrmstr.dev@dailydrop.hrbrmstr.dev ☮️

https://dailydrop.hrbrmstr.dev/2024/01/05/drop-399-2024-01-05-weekend-project-edition/

#go #golang #javascript #lit #litElement #wails

image/png
image/png

janik_frenzel, to california
@janik_frenzel@sfba.social avatar
mikemathia, to random
@mikemathia@ioc.exchange avatar
khoi, to tech

Who's using Lit or StencilJS

or Web Components in general??

rusticola, to random
@rusticola@hachyderm.io avatar

I’m writing a gov.uk style date input in . It’s pretty sweet for building 😄.

Jbasoo, to 11ty
@Jbasoo@mastodon.social avatar

I've just started learning and oh my days there seems to be a lot of ways to boilerplate this. Should I just use ? It looks like I'd need a bundler which seems overkill for my tiny site.

Also, are there any good courses around?

schizanon, (edited ) to random
@schizanon@mas.to avatar

It's cool that is integrating with .

It feels like we've been circling around this design pattern for decades; they've been called Observables, Pub/Sub, State, Context, etc.

STOKED to hear that there's discussion around standardizing and adding Signals to the !

https://www.youtube.com/live/ri9FEl_hRTc?si=-oztbFGgLG0f9aXJ&t=1636

schizanon,
@schizanon@mas.to avatar

"in a way you can think of a lit element is like a computed effect of a signals although our system kinda works like a push system instead of signals that often have a pull system" ~ @justinfagnani

#lit #litHTML #litElement #webDev #javaScript #web

schizanon, to javascript
@schizanon@mas.to avatar

It's super cool that is supporting standard , but I'm still not going to use them!

Maybe one day when they don't need to be transpiled anymore.

schizanon,
@schizanon@mas.to avatar

I wish I understood why insists on ducking around with decorators. It just seems like a little syntactic sugar to me. I wonder what their rationale for depending on them so much is.

schizanon, (edited ) to CSS
@schizanon@mas.to avatar

I think that #shadowDOM solves #css's problems and #lit is right to scope styles to the element.

#webDev #web #html #litElement #lithtml

schizanon, (edited ) to react
@schizanon@mas.to avatar

is getting a "context" api https://www.npmjs.com/package/@lit/context

It seems to work like , but it's an open standard that other frameworks can apparently implement.

tiberiuichim, to react

So now, it's no longer , no longer or but . Do we jump on this hype train, or wait for the next one?

KeithMills, to random
  • All
  • Subscribed
  • Moderated
  • Favorites
  • anitta
  • khanakhh
  • thenastyranch
  • Youngstown
  • hgfsjryuu7
  • slotface
  • rosin
  • InstantRegret
  • tacticalgear
  • kavyap
  • osvaldo12
  • everett
  • DreamBathrooms
  • PowerRangers
  • tester
  • magazineikmin
  • Durango
  • mdbf
  • ngwrru68w68
  • modclub
  • cubers
  • vwfavf
  • ethstaker
  • cisconetworking
  • GTA5RPClips
  • normalnudes
  • Leos
  • provamag3
  • All magazines