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 { useState } from "react"; | |
/** | |
* As an API it's the same useRef, | |
* but it uses useState under the hood. | |
* | |
* Used to watch the ref changing between re-renders. | |
* | |
* Source: https://non-traditional.dev/creating-a-stateful-ref-object-in-react-fcd56d9dea58 | |
*/ |
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 { useState } from "react"; | |
import { useMount, usePrevious, useToggle } from "react-use"; | |
export function debounce(func: Function, timeout = 300) { | |
let timer: any; | |
return (...args: any[]) => { | |
clearTimeout(timer); | |
timer = setTimeout(() => { | |
func(...args); | |
}, timeout); |
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
/** | |
* Get PDF page count from a PDF Blob | |
* | |
* Source: https://stackoverflow.com/a/58089003 | |
*/ | |
export const getPdfPageCount = (pdfFile: Blob) => | |
new Promise<number>((resolve, reject) => { | |
const reader = new FileReader(); | |
reader.readAsBinaryString(pdfFile); |
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 { AxiosError, AxiosResponse } from "axios"; | |
/** | |
* Axios types overload | |
*/ | |
export function asyncHelper<ResponseType = any, ErrorType = any>( | |
promise: Promise<AxiosResponse<ResponseType | ErrorType, any>> | null | undefined | false | |
): Promise<{ response: AxiosResponse<ResponseType> | null; error: AxiosError<ErrorType> | null }>; | |
/** |
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
/** | |
* Assuming 1rem = 16px | |
* | |
* 14 => 0.875 | |
*/ | |
export const pxToRem = (val: number, htmlFontSize?: number) => val / (htmlFontSize ?? 16); | |
/** | |
* Assuming 1rem = 16px | |
* |
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
/** | |
* Convert a text to kebab-case format | |
*/ | |
export const toKebabCase = (text: string) => | |
text | |
.trim() | |
.replace(/([a-z])([A-Z])/g, "$1-$2") // insert dash between lowercase and uppercase letters | |
.replace(/\s+/g, "-") // replace spaces with dashes | |
.toLowerCase(); // convert all characters to lowercase |
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
/** | |
* Get from a numeric `enum` just `keys` or just 'values' | |
* | |
* Source: https://www.sharooq.com/how-to-access-keys-and-values-of-an-enum-in-typescript | |
* | |
* @param target | |
* @param NumericEnum | |
* @returns | |
*/ | |
export const getNumericEnum = (target: "keys" | "values", NumericEnum: Record<any, any>) => |
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
/** | |
* Source: https://youtu.be/wsoQ-fgaoyQ | |
*/ | |
export const asyncHelper = (promise) => | |
Promise.allSettled([promise]).then(([{ value, reason }]) => ({ | |
data: value, | |
error: reason, | |
})); | |
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
/** | |
* Source: https://github.com/TanStack/table/blob/v7/examples/filtering/src/makeData.js | |
*/ | |
import namor from 'namor' | |
const range = len => { | |
const arr = [] | |
for (let i = 0; i < len; i++) { | |
arr.push(i) |
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
.rainbow-text { | |
background-image: var(--rainbow-gradient,#fff); | |
background-size: 100%; | |
background-repeat: repeat; | |
-webkit-background-clip: text; | |
-webkit-text-fill-color: transparent; | |
-moz-background-clip: text; | |
-moz-text-fill-color: transparent; | |
filter: drop-shadow(0 0 2rem #000); | |
text-shadow: none; |
NewerOlder