This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Item = { | |
id: string | |
parentId: string | undefined | |
} | |
type NestedItem = Item & { | |
children: NestedItem[] | |
} | |
const getNestedItems = (items: Item[], parentId: string | undefined = undefined) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react' | |
export type ModalResolveFn<T = unknown> = (answer?: T) => void | |
// the shape of a modal definition. A unique id and an element | |
export interface ModalDefinition<T> { | |
id: string | |
element: React.ReactNode | |
handleClose: ModalResolveFn<T> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react' | |
// the shape of a modal definition. A unique id and an element | |
interface ModalDefinition { | |
id: string | |
content: React.ReactNode | |
} | |
type AddModalFn = (def: ModalDefinition) => void | |
type RemoveModalFn = (id: string) => void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clientX/Y: 0,0 is top left of the browser window (under the toolbar/bookmarks bar) | |
offsetX/Y: 0,0 is top left of clicked element | |
screenX/Y: 0,0 is top left of monitor/display | |
pageX/Y: 0,0 is top left of page content, including scroll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const obj = { | |
str: 'hello', | |
num: 1, | |
bool: true | |
} | |
type MiscObject = typeof obj | |
const fun = <T extends keyof MiscObject>(key: T): MiscObject[T] => { | |
return obj[key] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const prefix = { | |
1: 'First', | |
2: 'Second', | |
3: 'Third', | |
4: 'Fourth', | |
5: 'Fifth', | |
6: 'Sixth', | |
7: 'Seventh', | |
8: 'Eighth', | |
9: 'Ninth', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useRef } from "react" | |
const useAutoFocus = (enabled: boolean) => { | |
const ref = useRef<HTMLElement>() | |
useEffect(() => { | |
if (ref.current && enabled) { | |
setTimeout(() => ref.current?.focus(), 100) | |
} | |
}, [enabled]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { isBefore, setHours, setMinutes, setSeconds, addMinutes, setMilliseconds } from 'date-fns' | |
const setTime = (x, h = 0, m = 0, s = 0, ms = 0) => setHours(setMinutes(setSeconds(setMilliseconds(x, ms), s), m), h) | |
const from = setTime(new Date(), 9) | |
const to = setTime(new Date(), 17) | |
const step = (x) => addMinutes(x, 30) | |
const blocks = [] | |
let cursor = from |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const daysEnum = { | |
1: 'Sun', | |
'Sun': 1, | |
2: 'Mon', | |
'Mon': 2, | |
4: 'Tue', | |
'Tue': 4, | |
8: 'Wed', | |
'Wed': 8, | |
16: 'Thu', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useRef, useEffect } from 'react' | |
// found: https://stackoverflow.com/a/21015393 | |
function getTextWidth(text, font) { | |
// re-use canvas object for better performance | |
var canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement('canvas')) | |
var context = canvas.getContext('2d') | |
context.font = font | |
var metrics = context.measureText(text) | |
return metrics.width |
NewerOlder