Skip to content

Instantly share code, notes, and snippets.

View wojtekmaj's full-sized avatar

Wojciech Maj wojtekmaj

View GitHub Profile
@wojtekmaj
wojtekmaj / jest-to-vitest.sh
Last active July 29, 2026 14:08
Automatically migrate Jest project to Vitest
#!/bin/bash
# Ensure we're working on the latest version of the main branch
git switch main
git fetch
git pull
# Create a new branch
git switch -c vitest
@wojtekmaj
wojtekmaj / .gitignore
Last active April 26, 2026 10:06
How to upgrade Yarn to Yarn Modern (v4 at the moment) seamlessly
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
@wojtekmaj
wojtekmaj / add-missing-import-extensions.txt
Last active January 24, 2026 04:16
Visual Studio Code-compatible regular expression to add all missing import extensions
# Find
import\s(([^;]|\n)*)\sfrom\s(['"])(\.{1,2}\/.*)(?<!\.js)(?<!\.(css|pdf|png|jpg|jsx|mjs|mp3|mp4|svg|ttf))(?<!\.(avif|json|webm|webp|woff))(?<!\.woff2)(['"]);
# Replace with
import $1 from $3$4.js$7;
@wojtekmaj
wojtekmaj / find-scroll-container.js
Last active November 27, 2025 15:04
Find the nearest scrollable container.
const findScrollContainer = (element) => {
if (!element) {
return undefined;
}
let parent = element.parentElement;
while (parent) {
const { overflow } = window.getComputedStyle(parent);
if (overflow.split(' ').every(o => o === 'auto' || o === 'scroll')) {
return parent;
@wojtekmaj
wojtekmaj / merge-refs.js
Last active October 21, 2025 08:46
Merge multiple React refs to use them on a single React element.
/**
* Allows to use multiple refs on a single React element.
* Supports both functions and ref objects created using createRef() and useRef().
*
* Usage:
* ```jsx
* <div ref={mergeRefs(ref1, ref2, ref3)} />
* ```
*
* @param {...Array<Function|Object>} inputRefs Array of refs
@wojtekmaj
wojtekmaj / settings.json
Last active February 14, 2025 13:35
VSCode settings worth checking out
{
// Enables breadcrumbs
"breadcrumbs.enabled": true,
// Slightly improves performance if you don't usescreen reader
"editor.accessibilitySupport": "off",
// My favorite font settings
"editor.fontFamily": "'Fira Code', monospace",
"editor.fontLigatures": true,
@wojtekmaj
wojtekmaj / esmOrCjs.js
Created January 28, 2025 22:39
A function to determine if a path is going to be loaded as ESM or CJS
import assert from 'node:assert';
import path from 'node:path';
async function esmOrCjs(filepath) {
const absolutePath = import.meta.resolve(filepath).replace('file://', '');
const ext = path.extname(absolutePath);
switch (ext) {
case '.mjs':
return 'esm';
@wojtekmaj
wojtekmaj / webpack-to-vite.sh
Created March 2, 2023 10:53
Automatically migrate Webpack project to Vite
#!/bin/bash
# Ensure we're working on the latest version of the main branch
git switch main
git fetch
git pull
# Create a new branch
git switch -c vite
@wojtekmaj
wojtekmaj / use.ts
Last active November 25, 2024 04:51
use() hook you can use in React 18 (stable) today
/// <reference types="react/next" />
import { use as originalUse, useContext } from 'react';
const STATUS = {
PENDING: 'pending',
REJECTED: 'rejected',
FULFILLED: 'fulfilled',
} as const;
type TState<T> =
@wojtekmaj
wojtekmaj / react-add-links.js
Created May 2, 2021 21:33
Add links to text in React