Skip to content

Instantly share code, notes, and snippets.

import { useRef, useEffect } from 'react'
interface UseIntersectionObserverProps<T> {
options: IntersectionObserverInit
callback: IntersectionObserverCallback | null
}
export const useIntersectionObserver = <T extends HTMLElement>(
props: UseIntersectionObserverProps<T>,
) => {
import { useRef, useEffect } from 'react'
export const useResizeObserver = <T extends HTMLElement>(
callback: ResizeObserverCallback,
) => {
const ref = useRef<T | null>(null)
useEffect(() => {
const observer = new ResizeObserver(callback)
if (ref.current) {
observer.observe(ref.current)
import * as React from 'react'
import { useMatchMedia } from './use-match-media'
export const usePrefersColorScheme = () => {
const [prefersScheme, setPrefersScheme] = React.useState<'light' | 'dark'>(
'light',
)
const handlePrefersSchemeDarkChange = (event: MediaQueryListEvent) => {
if (event.matches) setPrefersScheme('dark')
import * as React from 'react'
export const useMatchMedia = (
mediaQueryString: string,
handleChange: (e: MediaQueryListEvent) => void,
) => {
React.useEffect(() => {
const mqList = window.matchMedia(mediaQueryString)
mqList.addEventListener('change', handleChange)
const event = new window.MediaQueryListEvent('change', {
interface ThemeSpacings {
none: number
xxsmall: number
xsmall: number
small: number
medium: number
large: number
xlarge: number
xxlarge: number
}
@lndwn
lndwn / theme-colors.ts
Last active May 29, 2021 04:00
TypeScript interfaces for the `colors` key in a System UI theme
interface ColorSequence {
primary: string
secondary?: string
tertiary?: string
quaternary?: string
}
interface BrandColors extends ColorSequence {}
interface ControlColors {
@lndwn
lndwn / toDate.ts
Created May 26, 2020 00:07
ISO datestring to Date()
export const toDate = (datestring: string): Date => {
const options = {year: "numeric", month: "long", day: "numeric"}
return new Date(datestring).toLocaleDateString(undefined, options)
}
@lndwn
lndwn / text-input.sc.ts
Last active February 8, 2020 06:46
HTML input element with focus ring
import styled from 'styled-components'
export const TextInput = styled.input(
({ theme }) => `
display: block;
width: 100%;
box-sizing: content-box;
height: ${theme.sizes[2]};
padding: 2px;
margin-left: -4px;
@lndwn
lndwn / Core
Last active October 12, 2019 03:58
New laptop setup
#!/bin/bash
brew tap homebrew/cask-versions
brew install \
node \
yarn \
zsh \
git \
gpg
.defaults {
font-feature-settings: "ss03", "cv02", "cv03", "cv04", "cv05", "cv06", "cv08", "cv09", "cv10";
}
.tabular { font-feature-settings: "tnum"; }
.fractions { font-feature-settings: "frac"; }
.eyebrow { font-feature-settings: "cpsp"; }