Skip to content

Instantly share code, notes, and snippets.

View esamattis's full-sized avatar

Esa-Matti Suuronen esamattis

View GitHub Profile
@esamattis
esamattis / elements.js
Last active August 12, 2024 09:39
jsdoc type guards for dom elements
/**
* @template {keyof HTMLElementTagNameMap} T
* @param {any} el
* @param {T} elName
* @returns {el is HTMLElementTagNameMap[T]}
*/
function isElement(el, elName) {
return el?.constructor?.name === elName;
}
@esamattis
esamattis / type.scpt
Last active May 30, 2024 10:47
Type file contents using Apple Script on macOs
-- usage: osascript type.scpt <file>
on run {filePath}
set posixFilePath to filePath
set fileContent to do shell script "cat " & quoted form of posixFilePath
log "Will start typing in 3 seconds..."
delay 3
tell application "System Events"
set initialAppName to name of first application process whose frontmost is true
beep 1
log "Typing into: " & initialAppName
$ cat Cargo.toml
[package]
name = "rustdepstest"
version = "0.1.0"
edition = "2021"
[dependencies]
swc = "0.264.38"
$ cargo tree
@esamattis
esamattis / ColdWater
Last active February 9, 2023 11:31
Reading Hydrodigit-S1 water meter with iM871A-USB and wmbusmeters to Home Asisstant sensor
# /etc/wmbusmeters.d/ColdWater
name=ColdWater
# the id printed on the meter
id=030xxxxx
key=NOKEY
driver=hydrodigit
@esamattis
esamattis / trpc-test-link.ts
Last active January 12, 2023 08:45
tRPC local / test link for testing
@esamattis
esamattis / Dockerfile
Last active December 21, 2023 20:21
How to deploy a single app to a container from a pnpm monorepo using `pnpm deploy` in Github Actions
FROM node:14-slim
ENV NODE_ENV=production
COPY pnpm-deploy-output /app
WORKDIR /app
ENTRYPOINT ["/app/entrypoint.sh"]
@esamattis
esamattis / scroll-into-view.ts
Created August 24, 2022 21:59
scrollIntoViewIfNeeded() that works with `overflow: scroll` parent divs etc. as well
function getScrollContainer(node: HTMLElement | null): HTMLElement | null {
if (!node) {
return null;
}
if (node.scrollHeight > node.clientHeight) {
if (node === document.body) {
return document.documentElement;
}
return node;
@esamattis
esamattis / view.tsx
Last active June 7, 2022 22:16
Correctly typed "as" prop for React with working refs
interface ViewProps<T> {
as?: T;
ref?: React.Ref<
T extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[T] : T
>;
children?: React.ReactNode;
}
declare function ViewType<T extends ElementType = "div">(
props: React.ComponentPropsWithoutRef<T> & ViewProps<T>,
@esamattis
esamattis / NoHydrate.tsx
Created June 5, 2022 14:54
Prevent React hydration on specific element (HACK)
/**
* Render a div which preserves its server-side content on browser hydration.
*/
function NoHydrate(props: { id?: string; children: React.ReactNode }) {
const id = props.id ?? "no-hydrate";
const container = useRef<HTMLDivElement>(null);
const save = useRef<Node[]>();
// During the first render capture clones of the children. Reading the DOM
// during React render is a hack but the only way to access the DOM before
@esamattis
esamattis / typed-loaders.ts
Created May 19, 2022 20:59
TypeScript inference for Remix.run loaders and actions
import { json } from "@remix-run/node";
import { useActionData, useLoaderData } from "@remix-run/react";
export function useTypedLoaderData<T extends (arg: any) => any>(): Awaited<
ReturnType<T>
> {
return useLoaderData();
}
export function useTypedActionData<T extends (arg: any) => any>():