Skip to content

Instantly share code, notes, and snippets.

@colelawrence
colelawrence / qwik-tw-styled-components.tsx
Last active January 2, 2024 18:19
Qwik styled-components with Tailwind merge. To remix for your codebase at your option! Also supports `data-qwik-inspector` pasthrough.
import { Slot, component$ } from "@builder.io/qwik";
import type { JSX } from "@builder.io/qwik/jsx-runtime";
import { twMerge } from "tailwind-merge";
/** Comes from {@link tw} template. */
type TwString = string & {
_tw: true;
};
/** This applies types to somethign like `tw.div` or `tw.h1`, which will leverage the types from `JSX.IntrinsicElements` */
@colelawrence
colelawrence / useFuseSearch.ts
Created December 28, 2023 14:26
Qwik hook for searching items with Fuse.js
import type { NoSerialize, Signal } from "@builder.io/qwik";
import { noSerialize, useSignal, useVisibleTask$ } from "@builder.io/qwik";
import type { IFuseOptions } from "fuse.js";
import Fuse from "fuse.js";
export function useFuseSearch<T>(
config: {
items: Signal<T[]> | T[];
search: Signal<string> | string;
},
@colelawrence
colelawrence / svelte-read.ts
Created December 21, 2023 15:53
Synchronously read a svelte 3/4 store / Readable
import type { Readable } from "svelte/store";
const UNSET = Symbol();
export function read<T>(store: Readable<T>): T {
let value: T | typeof UNSET = UNSET;
store.subscribe((v) => (value = v))();
if (UNSET === value) {
throw new Error("Readable did not react synchronously");
}
return value;
@colelawrence
colelawrence / ChangeNotifier.ts
Created December 21, 2023 15:52
Simple callback tracker for changed values
export class ChangeNotifier {
callbacks: Array<() => void> = [];
onChange(callback: () => void) {
this.callbacks.push(callback);
return () => {
const index = this.callbacks.indexOf(callback);
if (index >= 0) {
this.callbacks.splice(index, 1);
}
};
@colelawrence
colelawrence / DisposePool.ts
Created December 19, 2023 20:53
Dispose Pool (Subscription) class
export interface IDisposable {
dispose(): void;
}
export class DisposePool implements IDisposable {
#fns: null | (() => void)[] = [];
#pool: null | IDisposable[] = [];
get disposed() {
return this.#pool === null;
}
import {
Object3D, PerspectiveCamera, Scene,
WebGLRenderer
} from "three";
import { CleanupFn } from "./CleanupFn";
import { DisposePool, isDisposable } from "./DisposePool";
import { merge } from "./merge";
import { ISheet } from "@theatre/core";
import { TheatreMergeable, theatreMerge } from "./theatreMerge";
import { EventsPool } from "./EventsPool";
@colelawrence
colelawrence / dev-stringify.ts
Last active January 11, 2024 21:21
Tight JSON and JavaScript stringify
function jsReplacer() {
const seen = new WeakSet();
return (key: string, value: any) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) return "[Circular]";
seen.add(value);
}
if (value instanceof RegExp) return { regexp: value.toString() };
if (value instanceof Function) return { function: value.toString() };
if (value instanceof Error) return { error: value.toString() };
@colelawrence
colelawrence / yjs-shared-types.ts
Created September 18, 2023 05:44
YJs, TypeScript, "Shared Types" with zod-like parser inference
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
import * as Y from "yjs";
export interface ZodLikeParser<T> {
_type: T;
parse: (value: unknown) => T;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AnyValueDesign = ZodLikeParser<any> | CollectionDef<any> | RecordDef<any>;
@colelawrence
colelawrence / format-slint-deno.ts
Last active April 26, 2023 12:06
Format slint files using Deno
const ignore: string[] = [
// "ui/generated.slint"
];
const dirs = { ui: "ui" };
const whitespaceRE = /^(\s*)(.*?)(\/*|\/\/)?(.*)$/;
const indentBy = " ";
file: for await (const file of Deno.readDir(dirs.ui)) {
if (
@colelawrence
colelawrence / prosemirror-buildTypedNodeSpec.ts
Last active January 7, 2023 06:08
ProseMirror typed node spec (incomplete source code from Story.ai codebase)
import {
AttributeSpec,
DOMOutputSpec,
Fragment,
Mark,
Node as PMNode,
NodeSpec,
NodeType,
ParseRule,
} from "prosemirror-model";