Skip to content

Instantly share code, notes, and snippets.

View jonathantneal's full-sized avatar

Jonathan Neal jonathantneal

View GitHub Profile
@jonathantneal
jonathantneal / CSSStyleObserver.js
Created April 7, 2022 04:34
CSS Style Observer (695 bytes minified, 397 bytes gzipped)
class CSSStyleRecord {
constructor(
/** @type {{ target: Element, propertyName: string, oldValue: string, newValue: string }} */
init
) {
Object.assign(this, init)
}
}
class CSSStyleObserver {
@jonathantneal
jonathantneal / HTMLElement-contentAvailableCallback.js
Last active November 22, 2021 01:33
HTMLElement with contentAvailableCallback (483 bytes / 317 gzip / 252 brotli, CC0-1.0)
{
let NativeHTMLElement = globalThis.HTMLElement, isContentAvailable = (element) => {
if (element.ownerDocument.readyState !== 'loading') return true
while (element) {
if (element.nextSibling) return true
element = element.parentElement
}
}
@jonathantneal
jonathantneal / glob.ts
Created November 2, 2021 13:26
Return a list of files, like import.meta.glob in Vite, without calling the files.
import glob from 'fast-glob'
import nodeURL from 'url'
import process from 'process'
/** Returns pathnames matching the given pattern. */
const sync = (source: string) => glob.sync(source, options()) as string[]
/** Returns pathnames matching the given pattern. */
const async = (source: string) => glob(source, options()) as Promise<string[]>
@jonathantneal
jonathantneal / README.sh
Last active October 29, 2021 15:01
Useful Brew Installs (macOS Homebrew)
# Brew Package Manager
# --------------------
# Brew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# JavaScript & TypeScript runtime
brew install deno
# GitHub manager
@jonathantneal
jonathantneal / Tokens.d.ts
Last active August 21, 2021 14:00
Tokens — manage space-separated tokens for any attribute — 990 bytes / 498 bytes gzipped
/**
* The **Tokens** interface represents a set of space-separated tokens.
*
* **Usage**:
* ```js
* let classTokens = new Tokens(document.body, 'class')
* classTokens.has('js') // boolean
* classTokens.add('js').has('js') // true
* classTokens.remove('js').has('js') // false
* ```
@jonathantneal
jonathantneal / command.js
Last active October 26, 2022 07:20
Executable JavaScript Modules
":" //#;exec /usr/bin/env node --input-type=module - $@<$0
import process from 'process'
const { argv } = process
console.log(argv)
@jonathantneal
jonathantneal / README.md
Last active July 20, 2021 23:28
React Custom Elements

React Custom Elements

Use Web Components as React Components.

Usage

Import reactCustomElements as a module:

import * as reactCustomElements from 'https://gist.githack.com/jonathantneal/b3df57a4c59cd70e0d18437352586794/raw/reactCustomElements.esm.min.js'
@jonathantneal
jonathantneal / getUniqueSelector.js
Last active July 14, 2023 16:15
getUniqueSelector — Return a unique selector for a specific element (151 bytes minified, 144 bytes gzipped)
/** Return a unique selector for a specific element. */
let getUniqueSelector = (/** @type {Element} */ element) => {
/** Unique selector for this element */
let selector = ''
/** @type {Element} */
let parent
/** @type {number} */
let nth
@jonathantneal
jonathantneal / index.xml
Last active June 3, 2021 03:29
Example XML webpage
<!DOCTYPE xml [
<!ATTLIST xsl:stylesheet
id ID #FIXED '!'
xmlns:xsl CDATA #FIXED 'http://www.w3.org/1999/XSL/Transform'
version CDATA #FIXED '1.0'
>
<!ATTLIST xsl:output
doctype-system CDATA #FIXED 'about:legacy-compat'
method CDATA #FIXED 'html'
>
@jonathantneal
jonathantneal / esbuild-loader.js
Last active June 15, 2021 01:49
Node — Loads modules using esbuild. — `node --experimental-loader ./esbuild-loader.js`
import { existsSync } from 'fs'
import { readFile } from 'fs/promises'
import { transform } from 'esbuild'
/** @typedef {{ conditions: string[], parentURL: string }} Context */
/** @typedef {(specifier: string, context: Context, defaultResolve: ResolveHook) => { url: string }} ResolveHook */
/** @typedef {(url: string, context: Context, defaultGetFormat: GetFormatHook) => { format: string }} GetFormatHook */
/** @typedef {(url: string, context: Context, defaultGetSource: GetSourceHook) => Promise<{ source: string | SharedArrayBuffer | Uint8Array }>} GetSourceHook */
/** Matches any JavaScript/TypeScript module. */