Skip to content

Instantly share code, notes, and snippets.

@brookback
brookback / router.ts
Created February 21, 2024 14:36
Super simple URLPattern based router.
interface Route {
pattern: URLPattern;
handler: Handler;
options: {
method: Method;
};
}
export type Handler = (req: Request) => Promise<Response> | Response;
@brookback
brookback / Popover.tsx
Created December 15, 2023 15:09
Native popovers in Preact.
import * as preact from 'preact';
import { DOMAttrs, DOMEvent, JSXElement, CSS, Ref, RefObject } from './types';
import { useContext, useEffect, useId, useMemo, useRef, useState } from 'preact/hooks';
import { classNames } from '@lookback/shared';
import { forwardRef, memo } from 'preact/compat';
interface Props {
button: JSXElement | ((isExpanded: boolean) => JSXElement);
style?: CSS;
}
fn main() {
let lol: Option<&str> = Some("lol");
if lol.is_none() {
// do some error handling
return;
}
// I'd love if `lol` was asserted to be Some("lol")
// below this if clause.
@brookback
brookback / use-cycling-focus.ts
Created September 18, 2020 11:44
A React hook for managing focus of elements in a list based on keyboard arrows (up and down).
import * as React from 'react';
type FocusIndex = number | null;
/**
* A React hook for managing focus of elements in a list based on keyboard
* arrows (up and down). Great for a11y and UX.
*
* This hook does not deal with the actual imperative `.focus()` part at all.
* It solely calculates the which index in a list which should currently be
// tslint:disable no-let
/** Return:
*
* - `< 0` if `a` comes before `b`.
* - `0` if `a` and `b` are equal.
* - `> 0` if `a` comes after `b`.
*/
type Comparator<T> = (a: T, b: T) => number;
@brookback
brookback / Tooltip.tsx
Created March 5, 2020 08:52
Tooltip.tsx wrapping custom reach-tooltip-modified.tsx.
import React, { Fragment, cloneElement } from 'react';
import {
useTooltip,
TooltipPopup,
TooltipProps,
Position,
} from './reach-tooltip';
interface Props {
isVisible?: boolean;
@brookback
brookback / input.css
Created February 26, 2020 22:25
PostCSS plugin which finds all hex or rgb colors defined on :root, and converts them to Display P3 versions and adds to a @supports rule.
:root {
--sans: Quicksand, sans-serif;
--serif: Neuton, serif;
--font-size: 24px;
--lineheight: 1.6;
--spacing: calc(var(--lineheight) * 1rem);
/* Colours */
const easeInOutQuad = (t) => t<.5 ? 2*t*t : -1+(4-2*t)*t
const easeInOutCubic = (t) => t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1
const easeInOutQuart = (t) => t<.5 ? 8*t*t*t*t : 1-8*(--t)*t*t*t
const easeInOutQuint = (t) => t<.5 ? 16*t*t*t*t*t : 1+16*(--t)*t*t*t*t
// OLIKA FUNKTIONER
@brookback
brookback / query.js
Last active October 28, 2019 07:55
Conditionally fetching a GraphQL field based on the presence of required variable.
/*
This query will crash when run if incoming userId prop is undefined. It's because
it's marked as required for the `user(id)` field. So that will force me to mark it
as required as a variable in my `Foo` client query below.
But I want to *conditionally* include the whole user field if only if userId prop
is a string in the component.
How?!
[alias]
# View commits in local branch vs. master
current = !"git log $(git rev-parse --abbrev-ref HEAD) ^master --no-merges"
# View unpushed commits
unpushed = !git log @{u}..HEAD
# View all unpushed commits on all branches
all-unpushed = !git log --branches --not --remotes --oneline
# View latest unpushed commits on all branches
unpushed-branches = !git log --branches --not --remotes --oneline --decorate --simplify-by-decoration