Karl Herrick

A technologist and web developer | Posts about technology encountered along the journey.

Using Open

When using the terminal on my Mac, I often use the open command to get a view of the current directory in Finder and generally use it to… open up things. So, while using other operating systems, I have naturally wanted similar functionality. To my recent surprise, I found it within Haiku, and it turns out it’s coming to Debian as well!

Haiku Screenshot


The Web Is Dynamic

HTML, JavaScript, and CSS are key parts of the Open Web Platform as they assist in creating customized, yet reusable solutions that anyone might need to build high quality, robust web applications—without requiring unnecessary complexity.

In this small example, a few support libraries work together to provide efficient templating, as well as an implementation of React’s Hooks API, allowing us to use state of the art features, while still preserving compatibility with the wider ecosystem on the Web.

With the addition of script:

(async () => {
  const { html } = await import('https://unpkg.com/lit-html@^1.0.0/lit-html.js')
  const { component, useState } = await import('https://unpkg.com/haunted@4.7.1/haunted.js')

  function counter({ value }) {
    const [count, setCount] = useState(value);
    const handleSetCount = (val) => () => {
      setCount(val);
      this.setAttribute("value", val);
    };

    return html`
      <style>
        span {
          margin: var(--x-hook-component-span-margin, inherit);
        }
      </style>
      <button
        type="button"
        @click=${handleSetCount(isNaN(count) ? 0 : Number(count) - 1)}
      >
        Decrement
      </button>
      <span id="count">${count}</span>
      <button
        type="button"
        @click=${handleSetCount(isNaN(count) ? 0 : Number(count) + 1)}
      >
        Increment
      </button>
    `;
  }

  if (!customElements.get("x-hook-component")) {
    customElements.define(
      "x-hook-component",
      component(counter, { observedAttributes: ["value"] })
    );
  }
})()

Some basic styles included:

x-hook-component {
  --x-hook-component-span-margin: 0 2rem;

  align-items: center;
  background: #2b2b2b;
  color: #fff;
  display: flex;
  justify-content: center;
  margin: 1rem 0;
  padding: 1rem;
}

And the element rendered (with a default value included even), we can define new and interesting behavior without much trouble at all.

<x-hook-component value="5"></x-hook-component>

Being that custom elements and dynamic import are so widely supported now—adding functionality and content in this way is starting to make a lot more sense for many use cases—especially seeing how once loaded and cached, the cost to reload any support libraries seems barely noticeable. Try this example on CodeSandbox.


Code Highlighting

It seemed like a neat idea to encapsulate syntax highlighting responsibilities for the various code snippets that I share at times, so I went ahead and wrote a custom element to help me take care of the job. To try it out, install x-postpress-code with your favorite package manager and import into your project. If you like going buildless [1, 2], load it from a CDN like unpkg or skypack:

<script
  src="https://unpkg.com/x-postpress-code@1.0.3"
  type="module"
></script>

Once registered, the web component requires a type attribute to identify which supported language should be highlighted. The content of the code itself can either be lazy loaded using the src attribute, or by including the source in the default slot surrounded by pre tags:

<x-postpress-code type="bash"><pre>
#/usr/bin/env bash
for file in *; do
  echo $file
done
</pre></x-postpress-code>

One nice side effect of defining the code this way, is that if JavaScript is disabled for whatever reason, the original is still rendered (although unhighlighted). Try it out at JS Bin, or checkout the project page for more information.