Skip to content

Instantly share code, notes, and snippets.

@souporserious
souporserious / lock-scroll.js
Last active May 8, 2024 09:33
lock scrollbars
/**
* Lock all scrollbars by disabling mousewheel and locking scrollbars in position
* Optionally provide an element to allow it to scroll when hovered
*/
const listenerOptions = supportsPassiveEvents
? { capture: true, passive: false }
: true
let lockedScrolls = []
function lockScroll(node) {
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
function extractPort(script: string) {
const portRegex = /next dev.*(?:-p |--port )(\d+)/
const match = script.match(portRegex)
return match ? match[1] : null
}
/* Read package.json and parse the Next.js port number */
/** Get the offset ancestors of a node. */
function getOffsetAncestors(node: HTMLElement): Set<HTMLElement> {
let ancestors = new Set<HTMLElement>()
let current: Element | null = node.offsetParent
while (current) {
if (current instanceof HTMLElement) {
ancestors.add(current)
current = current.offsetParent
} else {
break
'use client'
import { useState, useLayoutEffect, useRef, useMemo } from 'react'
export function VirtualList({
data,
rowHeight,
itemsToShow = 4,
overscanCount = 2,
}: {
data: { text: string; id: number }[]
import * as webpack from 'webpack'
import { glob } from 'glob'
import { dirname, resolve } from 'node:path'
function getPathnameFromFilename(filename: string) {
return (
filename
// Remove file extensions
.replace(/\.[^/.]+$/, '')
// Remove leading "./"
/**
* Lock all scrollbars by disabling mousewheel and locking scrollbars in position
* Optionally provide an element to allow it to scroll when hovered
*/
const listenerOptions = supportsPassiveEvents
? { capture: true, passive: false }
: true
let lockedScrolls = []
function lockScroll(node) {
/** Get the closest element that scrolls */
function getClosestOverflowElement(node: HTMLElement, includeHidden?: boolean) {
if (node) {
const { overflow, overflowX, overflowY } = getComputedStyle(node)
const canScroll = (
includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/
).test(overflow + overflowX + overflowY)
if (node === document.body || canScroll) {
return node
} else {
@souporserious
souporserious / get-type-declarations.ts
Created April 4, 2023 15:19
Utility function to grab type declarations from an NPM package
import fs from 'node:fs/promises'
import path from 'node:path'
/** Fetches the types for a locally installed NPM package. */
export async function getTypeDeclarations(packageName) {
const [parentPackage, submodule] = packageName.split('/')
const parentPackagePath = path.resolve(
process.cwd(),
'node_modules',
parentPackage,
import * as React from 'react'
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
import ts from 'typescript'
import { setupTypeAcquisition } from '@typescript/ata'
import { signal } from '@preact/signals-core'
/** Type definitions for the current project. */
export const types = signal<{ code: string; path: string }[]>([])
const ata = setupTypeAcquisition({
@souporserious
souporserious / changed-pages.mjs
Last active July 11, 2023 16:11
Diffs the current checked out branch against main and returns the Next.js pages that have changed.
import { execSync } from 'node:child_process';
import parseGitDiff from 'parse-git-diff';
import { Node, Project, SyntaxKind } from 'ts-morph';
/**
* We're going to build a NextJS utility that compares two git branches and
* outputs a list of pages that have changed. We'll use TS-Morph to trace
* the references of the pages that are not obvious by the file name and directory
* structure that NextJS uses to render pages.
*/