Skip to content

Instantly share code, notes, and snippets.

View rmdort's full-sized avatar

Vinay M rmdort

View GitHub Profile
// Setup
const { onIncreaseIndent, onDecreaseIndent } = useSpreadsheetState()
return (
<CanvasGrid
onIncreaseIndent={onIncreaseIndent}
onDecreaseIndent={onDecreaseIndent}
/>
)
// Option 1 - With useSpreadsheetState hook
// Calculator has to expose the following functions
/*
getPrecedents,
getPrecedentsFromFormulaString,
getDependents,
getGraphNodes,
*/
export const Spreadsheet = () => {
import { useCallback, useRef, useSyncExternalStore } from "react";
export const useAsyncHook = <T>(fn: () => Promise<T> | undefined) => {
const resultRef = useRef<T>();
const getSnapShot = () => resultRef.current;
const subscribe = useCallback(
(onStoreChange: () => void) => {
const executeAsync = async () => {
try {
const result = await fn?.();
@rmdort
rmdort / use-memo-compare.ts
Created March 11, 2022 15:35
Cache a variable across render
import { useRef, useLayoutEffect } from "react";
export const useMemoCompare = <T>(
next: T,
compare: (previous: T | undefined, next: T) => boolean
): T => {
const previousRef = useRef<T>();
const previous = previousRef.current;
const isEqual = compare(previous, next);
@rmdort
rmdort / use-resize-observer.ts
Created February 26, 2022 11:58
use-resizer-observer hook
import { useRef, useLayoutEffect, useState, useCallback } from "react";
type HTMLOrSVGElement = HTMLElement | SVGElement;
export type RectReadOnly = {
readonly x: number;
readonly y: number;
readonly width: number;
readonly height: number;
readonly top: number;
@rmdort
rmdort / index.md
Created June 23, 2021 03:16 — forked from bvaughn/index.md
How to use profiling in production mode for react-dom
@rmdort
rmdort / App.web.tsx
Created June 5, 2021 13:05 — forked from arrygoo/App.web.tsx
React Native Web configuration
import React, {useState} from 'react';
import {View, Text, TouchableOpacity, StyleSheet} from 'react-native';
const App = () => {
const [count, setCount] = useState(0);
return (
<View style={styles.container}>
<Text style={styles.title}>Hello from {'\n'}React Native Web!</Text>
<TouchableOpacity
onPress={() => setCount(count + 1)}
@rmdort
rmdort / viewport.html
Last active May 30, 2021 16:28
Multiple viewports for Dummies
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.header {
background: #eee;
padding: 1rem;
display: flex;
@rmdort
rmdort / topological_sort_dag.ts
Last active May 30, 2021 16:27
Topologically sort (BFS) a DAG
/**
* Visit a graph node
* Sort it topologically and return
* all the dependents
* @param nodes
* @param previsit
* @returns
*/
const topologicalSort = <T>(nodes: T[], children: (node: T) => Set<T>) => {
const stack = new Set<T>();
@rmdort
rmdort / package.json
Created May 11, 2021 13:11 — forked from jayphelps/package.json
TypeScript output es2015, esm (ES Modules), CJS, UMD, UMD + Min + Gzip. Assumes you install typescript (tsc), rollup, uglifyjs either globally or included as devDependencies
{
"scripts": {
"build": "npm run build:es2015 && npm run build:esm && npm run build:cjs && npm run build:umd && npm run build:umd:min",
"build:es2015": "tsc --module es2015 --target es2015 --outDir dist/es2015",
"build:esm": "tsc --module es2015 --target es5 --outDir dist/esm",
"build:cjs": "tsc --module commonjs --target es5 --outDir dist/cjs",
"build:umd": "rollup dist/esm/index.js --format umd --name YourLibrary --sourceMap --output dist/umd/yourlibrary.js",
"build:umd:min": "cd dist/umd && uglifyjs --compress --mangle --source-map --screw-ie8 --comments --o yourlibrary.min.js -- yourlibrary.js && gzip yourlibrary.min.js -c > yourlibrary.min.js.gz",
}
}