Skip to content

Instantly share code, notes, and snippets.

@hnordt
hnordt / counter.tsx
Created November 16, 2025 19:04
A type-safe, semantic custom counter element built with Custom Elements, the Invoker Commands API, and preact-render-to-string.
import { renderToStaticMarkup } from "preact-render-to-string";
declare global {
namespace preact.JSX {
interface IntrinsicElements {
"el-counter": preact.HTMLAttributes<HTMLElement>;
}
}
}
type TMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
type THandler = (
req: Request,
match: URLPatternResult | null,
) => Response | Promise<Response>;
class Route {
constructor(
private pattern: URLPatternInput,
@hnordt
hnordt / deno.json
Last active November 12, 2025 22:29
{
"compilerOptions": {
"lib": ["dom", "deno.ns"],
"jsx": "precompile",
"jsxImportSource": "preact"
},
"tasks": {
"dev": "deno run --allow-net --watch server.tsx",
},
"imports": {
import React from "react";
function usePress(onEnd: (isCancelled: boolean) => void, onStart?: () => void) {
return {
onPointerUp(e: React.PointerEvent<Element>) {
const rect = e.currentTarget.getBoundingClientRect();
onEnd(
!(
e.clientX >= rect.left &&
@hnordt
hnordt / Router.tsx
Last active September 17, 2025 00:33
import { useReducer, useEffect } from "react";
// pathToRegexp can be replaced with URLPattern once browser support improves
// https://developer.mozilla.org/en-US/docs/Web/API/URLPattern
import pathToRegexp from "path-to-regexp";
const Router = {
PushStateEventType: "pushstate",
ReplaceStateEventType: "replacestate",
PopStateEventType: "popstate",
import React from "react"
import { useLocation, useLoaderData, useFetcher } from "remix"
export default function useLoaderDataWithWindowFocusRefetching<LoaderData>() {
const loaderData = useLoaderData<LoaderData>()
const fetcher = useFetcher<LoaderData>()
const location = useLocation()
const pathnameRef = React.useRef(location.pathname)
React.useEffect(() => {
import React from "react"
import { useLocation, useNavigate } from "remix"
export default function useWindowFocusRefetching() {
const location = useLocation()
const navigate = useNavigate()
const pathnameRef = React.useRef(location.pathname)
React.useEffect(() => {
// add to your tailwind.config.js
const plugin = require("tailwindcss/plugin")
const radixPlugin = plugin(({ addVariant }) => {
const dataStates = [
"open",
"closed",
"active",
"inactive",
@hnordt
hnordt / machine.js
Last active August 20, 2020 16:02
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: "booking",
context: {
newsletterIndex: null,
placementId: null,
availabilityId: null,
reservationId: null
},
type: "parallel",
states: {
@hnordt
hnordt / machine.js
Last active August 19, 2020 02:26
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: "booking",
context: {
newsletterIndex: null,
placementId: null,
availabilityId: null,
},
type: "parallel",
states: {
booking: {