Skip to content

Instantly share code, notes, and snippets.

View alexanderson1993's full-sized avatar
๐Ÿš€
Spaceshippin...

Alex Anderson alexanderson1993

๐Ÿš€
Spaceshippin...
View GitHub Profile
@alexanderson1993
alexanderson1993 / CurrencyInput.tsx
Created July 29, 2022 13:12
A currency input field built with Chakra-UI
function CurrencyInput({
value,
onChange,
max,
}: {
value: number;
max: number;
onChange: (val: number) => void;
}) {
const [error, setError] = useState('');
@alexanderson1993
alexanderson1993 / countLines.ts
Created July 15, 2022 15:03
A tool for recursively counting the number of lines in each file in a directory
import fs from 'fs/promises';
console.time('Get file sizes');
const items = await fs.readdir('.');
const ignore = [
'_templates',
'.git',
'.github',
// This is a React component that loads and inlines an SVG image, making it easier to style
// with CSS. It renders an <img> first, and then renders the inline SVG once it's been loaded.
import {useState, useEffect, memo, ComponentPropsWithoutRef} from "react";
export const SVGImageLoader: React.FC<
{url: string} & ComponentPropsWithoutRef<"img">
> = memo(({url, alt, ...props}) => {
const [data, setData] = useState<string | null>(null);
useEffect(() => {
@alexanderson1993
alexanderson1993 / copyToClipboard.ts
Created January 28, 2021 21:43
A component that copies some text to the clipboard when it is clicked.
import * as React from "react";
const CopyToClipboard:React.FC<{text:string} & React.HTMLAttributes<HTMLButtonElement>> = ({text, ...props}) => {
const copyToClipboard = (event:React.MouseEvent<HTMLButtonElement>, str:string) => {
const el = Object.assign(document.createElement('textarea'),{value:str});
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
@alexanderson1993
alexanderson1993 / correct.ts
Created August 17, 2020 14:40
What not to do with proxies.
// Create a symbol for our proxy
let isProxy = Symbol("isProxy");
const handler = {
get(target, key) {
// Override access if we are checking if the object is already a proxy
if (key === isProxy) return true;
if (
// Don't wrap it again if the object is already a proxy
@alexanderson1993
alexanderson1993 / looseApplyPatches.ts
Created August 1, 2020 12:01
Apply immer patches loosely to an object, in case the object doesn't match the object which the patches were created against in the first place.
import { produce, applyPatches, enablePatches } from "immer";
enablePatches();
function setToValue(obj, value, path) {
var i;
path = path.split(".");
for (i = 0; i < path.length - 1; i++) {
if (!obj[path[i]]) {
obj[path[i]] = isNaN(parseInt(path[i], 10)) ? {} : [];
import i18next from "i18next";
import {initReactI18next} from "react-i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import codegen from "codegen.macro";
import debounce from "lodash.debounce";
i18next
.use(LanguageDetector)
.use(initReactI18next) // passes i18n down to react-i18next
.init({
resources: {},
@alexanderson1993
alexanderson1993 / app.tsx
Last active April 19, 2021 15:10
Deno React SSR
import {
React,
} from "https://unpkg.com/es-react";
declare global {
namespace JSX {
interface IntrinsicElements {
h1: any;
div: any;
h2: any;
@alexanderson1993
alexanderson1993 / useUSBDMX.ts
Last active September 7, 2024 02:57
A React hook which uses the WebUSB API to send DMX messages to a connected ENTTEC Pro device
// A React hook which uses the WebUSB API to send DMX messages to a connected ENTTEC Pro device
// This will only work in Chrome.
// If you want to just try it, run it through the TypeScript compiler here https://www.typescriptlang.org/play/index.html
import React from "react";
declare global {
interface Navigator {
// This USB type comes from @types/w3c-web-usb
readonly usb: USB;
@alexanderson1993
alexanderson1993 / machine.js
Last active April 23, 2020 23:43
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions