Skip to content

Instantly share code, notes, and snippets.

View mattmccray's full-sized avatar

Matt McCray mattmccray

View GitHub Profile
@mattmccray
mattmccray / ReadMe.md
Last active August 5, 2023 02:43
css.js -- Small, inline css for components

css.js

A CSS in JS micro-tool that's < 1kb (gzipped).

Usage:

const MyComponent = (props) => {
 return (
@mattmccray
mattmccray / router.ts
Last active October 16, 2022 17:14
A zero dependency, tightly focused, micro router.
interface PathProvider {
getPath(): string
setPath(path: string): void
onPathChange(callback: (path: string) => void): void
}
interface CurrentMatch {
path: string
route: Route | null
params: Record<string, string>
@mattmccray
mattmccray / test.ts
Last active April 10, 2022 15:23
🧪 Tiny Browser Unit Testing (<1kb gzipped)
type LogEntry = {
messages: any[],
level: 'pass' | 'fail' | 'error'
}
class Logger {
messages: LogEntry[] = []
get total() {
return this.messages.length
@mattmccray
mattmccray / css-component.tsx
Last active January 27, 2022 03:11
Simple Component wrapper for use with SolidJS and ./css.ts
import { createMemo, JSX, splitProps } from "solid-js"
import { Dynamic } from 'solid-js/web'
type CssComponent<T extends keyof JSX.IntrinsicElements> = (props: JSX.IntrinsicElements[T]) => JSX.Element
export function component(styles: string): (props: JSX.IntrinsicElements['div']) => JSX.Element
export function component<T extends keyof JSX.IntrinsicElements>(tag: T | string | CssComponent<any>, styles: string): (props: JSX.IntrinsicElements[T]) => JSX.Element
export function component<T extends keyof JSX.IntrinsicElements>(tag?: string | T | CssComponent<any>, styles?: string): CssComponent<any> {
let extraClassNames = ''
if (!styles) {
@mattmccray
mattmccray / css-component.tsx
Created January 25, 2022 01:12
Simple Component wrapper for use with SolidJS and ./css.ts
import { JSX } from "solid-js"
import { Dynamic } from 'solid-js/web'
export interface ComponentProps { children?: any, className?: string, [attr: string]: any }
export type CssComponent = (props: ComponentProps) => JSX.Element
export function component(styles: string): CssComponent
export function component(tag: string | CssComponent, styles: string): CssComponent
export function component(tag?: string | CssComponent, styles?: string): CssComponent {
let extraClassNames = ''
@mattmccray
mattmccray / css.ts
Last active November 28, 2021 21:31
Simple and tiny CSS in JS
interface IConfiguration {
append: 'each' | 'batch'
debug: boolean
appendTo: Element
replaceRegExp: RegExp
}
let _namePrefix = 'css'
let _pendingStyles: string | null = null
let _config: IConfiguration = {
import { useEffect, useMemo, useRef } from "preact/hooks"
type PropsChangeMode = 'notify' | 'recycle' | 'smart'
interface IControllerClass<T, P> {
new(props: P): T
dispose?: () => void
propsDidChange?: (props: P) => void
}
/**
import { useEffect, useRef, useState } from "preact/hooks"; // Or 'react'
interface PromiseResult<T> {
isResolved: boolean
value?: T
error?: any
}
export function usePromise<T>(builder: (...params: any[]) => Promise<T>, params: any[] = []): PromiseResult<T> {
@mattmccray
mattmccray / StateManager.ts
Last active May 30, 2021 03:49
Simple state manager using react and immer.
import React from 'react'
import { Patch, produceWithPatches } from 'immer'
export interface StateManager<T = {}> {
handle<P>(signal: Signal<P>, callback: (state: T, event: P) => void): () => void
getState(): T
onStateChanged: Signal<{ state: T, changes: Patch[] }>
}
@mattmccray
mattmccray / mutt.js
Last active January 15, 2021 19:45
MUTT - Micro Unit Testing Tool (for browsers)
// MUTT - Micro Unit Testing Tool (for browsers)
let _testDepth = 0;
export function test(name, suite) {
const title = `🐶 ${name}`;
const ctx = {
count: 0,
failed: 0
};