This file contains hidden or 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 { useState, useCallback, useLayoutEffect, RefObject, useMemo } from 'react'; | |
import debounce from 'debounce'; | |
export type Dimensions = [number, number]; | |
export default function useRefDimensions(ref: RefObject<HTMLElement>): Dimensions { | |
const [width, setWidth] = useState(0); | |
const [height, setHeight] = useState(0); | |
const computeDimensions = useCallback((elem: Element) => { |
This file contains hidden or 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
function noop() {} | |
type CatchCallback<T> = Parameters<Promise<T>['catch']>[0]; | |
class PromiseChain<T> { | |
#chain: Promise<any> = Promise.resolve(); | |
#values: Array<T> = []; | |
add(promise: Promise<T | void | undefined>, promiseCatch: CatchCallback<T> = noop) { | |
this.#chain = this.#chain | |
.then(() => promise) |
This file contains hidden or 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 isRequestIdleCallbackSupported = | |
'requestIdleCallback' in window && | |
typeof window.requestIdleCallback === 'function'; | |
/** | |
* A higher order function that wraps passed function to run | |
* inside of a requestIdleCallback or "onload" event | |
* depending on what is supported in the browser. | |
* | |
* This is useful for running low priority app level bootstrapping events. |
This file contains hidden or 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
/** | |
* Converts Java SimpleDataFormat to ISO 8601 format | |
* | |
* @example | |
* simpleDateIso('2019-01-24T12:42:17:234-0700') => '2019-01-24T12:42:17.234-0700' | |
* | |
* @param {string} simpleDate - Java SimpleDataFormat string | |
* https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html | |
* @returns {string|null} | |
*/ |
This file contains hidden or 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
/** | |
* Returns rem representation of pixel based on root size. | |
* | |
* @param {number} px - Pixel Size | |
* @param {number} [rootSize=16] - Root Font Size | |
* @returns {string} | |
*/ | |
export default function pxToRem(px: number, rootSize: number = 16): string { | |
return `${px / rootSize}rem`; | |
} |
This file contains hidden or 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 { useReducer } from 'react'; | |
type Toggle = (forceToggle?: boolean) => void; | |
export default function useToggle(initial: boolean = false) { | |
return useReducer( | |
(toggledState: boolean, forceToggle?: boolean) => forceToggle || !toggledState, | |
initial | |
) as [boolean, Toggle]; | |
} |
This file contains hidden or 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
# test directories | |
__tests__ | |
test | |
tests | |
powered-test | |
# asset directories | |
docs | |
doc | |
website |
This file contains hidden or 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 { useReducer } from 'react'; | |
type Reducer<S = any> = (state: S, action: any) => S; | |
type Map = Record<string, Reducer> | |
export default function useReducerMap(map: Map, initialState: unknown) { | |
return useReducer((state, action) => { | |
const reducer = map[action.type]; | |
return reducer(state, action); | |
}, initialState) |
This file contains hidden or 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, { useEffect } from 'react'; | |
export default function useLongPress(onLongPress: (ev: React.TouchEvent) => any, touchDuration: number = 500) { | |
let timer: NodeJS.Timeout; | |
const startTimer = (ev: React.TouchEvent) => { | |
// Re-assigning event so we can keep a ref if react cleans up | |
// (https://reactjs.org/docs/events.html#event-pooling) | |
const event = ev; | |
timer = setTimeout(() => onLongPress(event), touchDuration); |
This file contains hidden or 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
/** | |
* Omit a value from a type | |
*/ | |
export type Omit<Type, Key extends keyof Type> = Pick<Type, Exclude<keyof Type, Key>> |
NewerOlder