2023-07-04 #javascript / frantic.im

JavaScript Gom Jabbar

You stop to count how many tools and parsers work on your codebase: TypeScript, esbuild, swc, babel, eslint, prettier, jest, webpack, rollup, terser. You are not sure if you missed any. You are not sure if you want to know. The level of pain is so high you forget about anything else.

We've come a long way, but we're not there yet.


2022-10-28 #javascript

Local or session storage?

Local storage tends to be the obvious place to persist data locally in a web application. We tend to grab straight for localStorage, but it's not the only tool in our workbox. Another option is sessionStorage. Let's review their similarities and differences, and determine when to use which.

Read more


2021-04-07 #alpine #javascript / ryangjchandler.co.uk

Reusable Alpine Components by Ryan Chandler

I've been using Alpine often lately. Ryan has written a lot of good stuff on Alpine, but his reusable components post is what really got me kickstarted.

You should be careful to not abstract too early. If you are finding it difficult to manage your Alpine component from the x-data attribute, this one is definitely for you.

The way this article builds up was very helpful: only use the level of abstraction you need:

  1. No abstractions
  2. Extract to a component function
  3. Use x-spread
  4. Mix in other data

2021-04-05 #javascript

The window.matchMedia API

To execute a piece of code when the viewport is a certain size, the first thing that comes to mind is adding a resize event listener to the window object. This is one way, but not always the best. Sometimes the lesser known window.matchMedia API is a better fit.

Read more


2020-10-21 #web-components #javascript #vanilla-js

Embed a web page with a web component and the shadow DOM

Today, my colleague Freek asked for help embedding the webview of an email campaign in an iframe. He needed it in an iframe because embedding the HTML directly caused layout issues because the website's CSS clashed with it.

After setting up the iframe, we needed to find a way to dynamically resize it based on its contents to avoid double scrollbars on the page. While possible, it required some icky scripting.

I took a step back. The problem at hand was that the CSS needed to be scoped somehow. While iframes were the only solution for a long time, these days we have the shadow DOM.

Read more


2020-10-01 #spatie #laravel #php #javascript

The revamped Spatie guidelines

I created the original Spatie guidelines site three years ago. Last month, we consolidated a few of our subsites to our main spatie.be site, including the guidelines.

Read more


2020-09-20 #javascript #performance / css-tricks.com

A Tale of Client Side Framework Performance

Jeremy Wagner measured some performance differences between React and vanilla JavaScript. While I'm not surprised that running your code on bare metal code is faster than a framework, it's interesting to see some exact numbers.

React and ReactDOM total about 120 KiB of minified JavaScript, which definitely contributes to slow startup time. When client-side rendering in React is relied upon entirely, it churns. Even if you render components on the server and hydrate them on the client, it still churns because component hydration is computationally expensive.

If it sounds like I have a grudge against React, then I must confess that I really like its componentization model. It makes organizing code easier. I think JSX is great. Server rendering is also cool—even if that’s just how we say “send HTML over the network” these days.

Even with server-side rendering, React and other virtual DOM frameworks add loading time because they need to load more code and hydrate the page before it's interactive.

A vanilla addEventListener callback performs about 40 times faster (!) than an event handled in a React component. React's overhead worth when you have a lot of complex state, not "simple state" like menu toggles.

Read the full article on CSS-Tricks.


2020-09-15 #javascript #oss

Moment.js, thank you for your service

If you've dealt with dates in the browser, chances are you've used Moment.js. Moment has been the date library for JavaScript in the past years. In a humble prelude of the documentation, the Moment maintainers talk about the (near) deprecated status of the project.

Read more


2020-05-04 #javascript #vanilla-js #javascript-framework-diet

Enter & leave transitions

Now that we've built a dropdown list, lets add some transitions to create open & close animations.

Read more


2020-04-27 #npm #javascript

Shotgun upgrade your npm dependencies with yarn upgrade --latest

Every now and then I do a quick checkup of a project's npm dependencies. I like to keep them up to date by often doing small upgrades. It's a lot less painful than doing large upgrades once a year.

One annoying part of this process is ensuring every dependency is on the latest major version. For example, if a project requires lint-staged@^8.0.0, yarn upgrade won't upgrade it to lint-staged@^9.0.0 (luckily of course, it's the behaviour I want during everyday development).

Today I learned about yarn upgrade --latest, which will upgrade all dependencies to the highest available version, despite the version constraints in your package.json file. lint-staged@^8.0.0 would happily upgrade to lint-staged@^9.0.0, even if it breaks semver boundaries.

Read more


2020-03-05 #javascript #vanilla-js #javascript-framework-diet

Dropdowns

On to our first component: a dropdown list. I'm going to walk through the implementation I landed on in a recent project. There are many ways to build dropdowns, and you might want to shape the API your way, so use this post as a source of inspiration.

Read more


2020-02-19 #javascript #vanilla-js #javascript-framework-diet

File structure

In the previous posts, we've gone through our first few utility functions. We now have enough in our toolbox to move on to our first component. However, where do all these functions belong?

Read more


2020-02-13 #javascript #vanilla-js #javascript-framework-diet

Event delegation

After learning how to select elements in the DOM, it's time to zoom into events. This is the third post in the JavaScript Framework Diet series.

Read more


2020-02-06 #javascript #vanilla-js #javascript-framework-diet

Selecting elements (part 1)

Lets get warmed up! Before we can get productive, we need two small helpers that we'll be using in most components we'll build from here on. I'm talking about $ and $$, which are wrappers around document.querySelector and document.querySelectorAll.

Read more


2020-02-06 #javascript #vanilla-js #javascript-framework-diet

Selecting elements (part 2)

In Selecting elements (part 1) we learned how to select elements from the DOM, and how to find elements inside other elements, both with our own $ and $$ helpers.

In part 2, we're going to review two DOM element instance methods: closest and matches.

Read more