Skip to content

Instantly share code, notes, and snippets.

View olibooty's full-sized avatar
🔥

Oli Booty olibooty

🔥
  • Somerset, UK
View GitHub Profile
import { useState } from 'react';
interface TabItem {
id: string;
title: React.ReactNode;
body: React.ReactNode;
}
interface TabPanelProps {
items: TabItem[];
@olibooty
olibooty / useWindowSize.ts
Last active May 20, 2025 15:11
useWindowSize.ts
import { useSyncExternalStore } from "react";
function subscribe(onStoreChange: () => void) {
console.log("called");
window.addEventListener("resize", onStoreChange);
return () => window.removeEventListener("resize", onStoreChange);
}
function getServerSnapshot() {
return 0;
import { renderHook } from '@testing-library/react';
import { act } from 'react-dom/test-utils';
import { useLocalStorage } from './useLocalStorage';
describe('useLocalStorage', () => {
const key = 'test-key';
const value = 'test-value';
beforeEach(() => {
jest.clearAllMocks();
/**
* Renders supplied properties of an object required
*/
type Requirer <T, K extends keyof T> = T & { [P in K]-?: T[P] }
interface Foo {
opt?: string;
}
type ReqFoo = Requirer<Foo, 'opt'>
declare const __brand: unique symbol;
type Branded<Type, Brand> = Type & {
readonly [__brand]: Brand;
};
type RouteName = `/${string}`;
type BrandedRouteName = Branded<RouteName, 'RouteName'>;
export const filterByPropertyKey = <TObj extends Object,>(
collection: Readonly<TObj[]>,
filters: Record<string, unknown>,
) => {
if (Object.keys(filters).length === 0) {
return collection;
}
return collection.filter((item) => {
let include = true;
interface PromiseRes <TObj>{
fulfilled: TObj[];
rejects: PromiseRejectedResult[];
}
/**
* Given an array of promises it will wait for every item to be resolved or rejected
* and returns the fulfilled and rejected objects in two separate arrays
*/
/**
*
* Selects elements from target object by provided keys
*/
export const pick = <T extends object, K extends keyof T = keyof T>(
object: T,
keys: K[]
): Pick<T, K> => {
let output: any = {};
/**
* Groups elements in an array by a given key (if that key exists within each
* iteratee)
*
* *Optional: you can provide a getter function to access a value within each
* item of the array*
*/
const groupBy = <T extends Record<string, any>, U = T[keyof T]>(
key: keyof T,
array: T[],
@olibooty
olibooty / prefers-reduced-motion.scss
Last active October 6, 2023 16:07
A simple way to handle reduced motion preferences with a few lines and one media query
:root {
--prefers-reduced-motion: 1;
@media (prefers-reduced-motion) {
--prefers-reduced-motion: 0;
}
}
@function animationMs($speed) {
@return calc(#{$speed} * var(--prefers-reduced-motion))ms;