Skip to content

Instantly share code, notes, and snippets.

View brandonpittman's full-sized avatar

Brandon Pittman brandonpittman

View GitHub Profile
@brandonpittman
brandonpittman / use-root-data.ts
Created November 30, 2022 04:28
Helper to grab Remix root loader data
import { useRouteData } from "remix-utils";
import type { SerializeFrom } from "@remix-run/server-runtime";
import type { loader } from "~/root";
export let useRootData = () =>
useRouteData<SerializeFrom<typeof loader>>("root");
@brandonpittman
brandonpittman / invalid-params.ts
Created November 18, 2022 02:44
Handle invalid SearchParams with Conform
let removeInvalidSearchParams = (
error: unknown,
request: Request,
prefix = "Unrecognized key(s) in object:"
) => {
let unrecongnizedKeysSchema = z.tuple([
z.tuple([z.string().length(0), z.string().startsWith(prefix)]),
]);
let result = unrecongnizedKeysSchema.safeParse(formatError(error));
@brandonpittman
brandonpittman / ProgressiveEnhancement.tsx
Created November 17, 2022 02:05
Progress enhancement helpers/components for Remix
import type { FetcherWithComponents, FormProps } from "@remix-run/react";
import type { ReactElement, ReactNode } from "react";
import { Form } from "@remix-run/react";
import { useHydrated } from "remix-utils";
export let NoJS = ({ children }: { children: ReactElement }) => {
let isHydrated = useHydrated();
if (!isHydrated) {
import {
createPortal as createReactPortal,
unmountComponentAtNode,
} from "react-dom";
import { useState, useCallback, useEffect } from "react";
export let usePortal = (el: Element = document.body) => {
let [portal, setPortal] = useState<{
render: Function;
remove: Function;
@brandonpittman
brandonpittman / use-presence-with-tailwind.tsx
Last active February 1, 2024 04:23
`usePresence` with Tailwind animation utilities
import { AnimatePresence, usePresence } from "framer-motion";
import { classNames } from "~/lib/utils/class-names";
import { useCounter } from "@kyleshevlin/use-common";
import { useCallback } from "react";
const Box = ({ count }: { count: number }) => {
const [isPresent, safeToRemove] = usePresence();
const onAnimationEnd = useCallback(() => {
if (!isPresent) safeToRemove();
@brandonpittman
brandonpittman / kit-google-translate.ts
Last active November 5, 2021 07:55
Translate selected text into with Google Translate.
// Menu: JA → EN
// Description: Translate selected text into with Google Translate.
// Author: Brandon Pittman
// Shortcut: ctrl j
import "@johnlindquist/kit";
const origin = "https://translate.google.com";
const text = await getSelectedText();
const sl = "ja";
@brandonpittman
brandonpittman / weeks.tsx
Last active August 26, 2021 05:44
4000 Weeks
import clsx from 'clsx';
import { differenceInWeeks } from 'date-fns';
const fillIn = (complete: boolean) =>
clsx(
complete ? 'bg-gray-300' : 'bg-transparent',
'h-1.5 w-1.5 border border-gray-300 rounded-full'
);
const weekCount = differenceInWeeks(new Date(), new Date('8/31/1983'));
@brandonpittman
brandonpittman / tabata.cjs
Last active June 15, 2021 23:58
Script to announce Tabata intervals
#!/usr/bin/env node
const os = require("os");
if (os.platform() !== "darwin") {
console.error("This script only works with macOS.");
process.exit(1);
}
const { spawn } = require("child_process");
await $`mkdir example`
await cd('example')