Skip to content

Instantly share code, notes, and snippets.

@djgrant
djgrant / settings.jsonc
Created March 3, 2025 13:56
Live coding editor config
{
"files.exclude": {
"node_modules/": true,
".gitignore": true,
"bun.lockb": true,
".vscode": true
},
"editor.minimap.enabled": false, // removes minimap
"editor.renderWhitespace": "none", // removes indent guides
"editor.renderLineHighlight": "none", // removes line highlight
@djgrant
djgrant / use-in-viewport-effect.ts
Created January 23, 2025 11:06
Hook to observe when element is in the viewport
import { useEffect } from "react";
import { useInView } from "react-intersection-observer";
export function useInViewportEffect(
ref: React.RefObject<Element | null>,
effect: () => (() => void) | void,
) {
const { ref: inViewRef, inView } = useInView();
useEffect(() => {
import z from "zod";
// Using z.ZodType to convert simple TypeScript type to Zod type
const b: z.ZodType<string> = z.string();
// Using Zodify, just for fun!
const a: Zodify<string> = z.string();
// Convert object
type RawSchema = {
async function readFiles(project: string) {
async function walkDir(
dir: string,
callback: (path: string, content: string) => void
) {
const dirPath = path.join(rootDir, project, dir);
const files = await fs.readdir(dirPath, { withFileTypes: true });
for (const file of files) {
const relativeFilePath = path.join(dir, file.name);
@djgrant
djgrant / ordered-p-queue.ts
Created February 2, 2022 06:52
Ordered p-queue
/**
* Enforce sequential ordering of tasks by priority
*
**/
import PQueue, { DefaultAddOptions } from "p-queue";
class QueueClass {
_queue: any[];
_deferredQ: Map<number, any>;

Document

  • emoji
  • icon
  • image
  • prose
  • text

Navigation

  • button
  • breadcrumb
const createPoll = (fn, interval) => {
const subscribers = {};
const intervals = {};
return ({ key, args, onSuccess }) => {
if (!subscribers[key]) {
subscribers[key] = [];
}
const index = subscribers[key].push(onSuccess) - 1;
if (!intervals[key]) {
intervals[key] = setInterval(() => {
@djgrant
djgrant / styled.js
Created August 12, 2019 21:32
styled scales
const scale = [1, 2, 4, 8];
const breakpoints = ['@media (min-width: 20em)', '@media (min-width: 40em)'];
const getValue = value => {
if (Number.isFinite(value)) {
value = `${value}px`;
}
return value;
}
@djgrant
djgrant / example.js
Last active July 30, 2019 00:37
Styled System utils
import styled from '@emotion/styled';
import { variant, space, layout, flexbox } from 'styled-system';
import { css, switchProp, ifProp } from './styled-system-utils';
const Box = styled.div(
space,
layout,
flexbox,
switchProp('gutter', [{ px: [2, 3] }, { px: [3, 4] }, { px: [4, 5] }], 1),
// Alternatively:
@djgrant
djgrant / form.test.tsx
Created June 8, 2019 18:02
React uncontrolled form container
import 'jest-dom/extend-expect';
import React from 'react';
import { render, fireEvent, cleanup } from '@testing-library/react';
import Form from './form';
window.FormData = undefined;
require('formdata-polyfill');
describe('Form container', () => {
afterEach(cleanup);