Skip to content

Instantly share code, notes, and snippets.

View typeofweb's full-sized avatar
🛒
https://yournextstore.com

Michał Miszczyszyn typeofweb

🛒
https://yournextstore.com
View GitHub Profile
@typeofweb
typeofweb / try_.ts
Last active August 17, 2024 09:56
Turn async values and errors into tuples. https://www.npmjs.com/package/safe-try
// Also available on NPM: https://www.npmjs.com/package/safe-try
// By @typeofweb 2024
// License: MIT
type PromiseToTupleResult<T> = [Error, null] | [null, Awaited<T>];
export const try_ = async <T extends Promise<unknown>>(
promise: T,
): Promise<PromiseToTupleResult<T>> => {
try {
@typeofweb
typeofweb / SuperLink.tsx
Last active June 21, 2024 15:06
Link with on-hover prefetching in Next.js
@typeofweb
typeofweb / Button.tsx
Created February 12, 2024 10:22
`asChild` pattern in React
import { clsx } from "clsx";
import { type ButtonHTMLAttributes } from "react";
import { Slot } from "./Slot";
interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
asChild?: boolean;
children: React.ReactNode;
}
export const Button = ({ asChild = false, ...props }: ButtonProps) => {
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Permission": {
"title": "Permission",
"description": "An enumeration.",
"enum": [
"MANAGE_USERS",
"MANAGE_STAFF",
"IMPERSONATE_USER",
import type { Stripe } from "stripe";
type StripeWebhookEventTypes = Stripe.WebhookEndpointCreateParams.EnabledEvent;
type StripeWebhookEvent<EventType extends StripeWebhookEventTypes, Payload> = {
type: EventType;
data: {
object: Payload;
};
};
import Fetch from 'node-fetch';
interface JJITOffer {
title: string;
street: string;
city: string;
country_code: string;
address_text: string;
marker_icon: string;
workplace_type: string;
const LogicValues = [true, false] as const;
type Logic = typeof LogicValues[number];
const CARDINALITY = LogicValues.length;
type Literal = { __t: "Literal"; symbol: string };
type UnaryOperator = { __t: "Unary"; e: (a: Logic) => Logic };
type BinaryOperator = { __t: "Binary"; e: (a: Logic, b: Logic) => Logic };
type Token = Literal | UnaryOperator | BinaryOperator;
/* eslint-disable functional/no-this-expression -- it's a class */
/* eslint-disable functional/prefer-readonly-type -- it's a class */
export const useBodyFix = () => {
return fixBodyService;
};
class FixBodyService {
private windowOffsetY: number = 0;
private scrollbarWidth: number = 0;
function getHexValue(intInput: number) {
return intInput.toString(16).padStart(2, "0");
}
function getTintValue(tint: number, bgTint: number, alpha: number) {
return Math.min(Math.floor((1 - alpha) * bgTint + alpha * tint), 255);
}
class Color {
constructor(
const getAllCSSVariableNames = () =>
Array.from(document.styleSheets)
.filter((s) => s.href === null || s.href.startsWith(window.location.origin))
.flatMap((s) => Array.from(s.cssRules))
.filter((r): r is CSSStyleRule => r.type === CSSRule.STYLE_RULE)
.flatMap((r) => (r.selectorText === ":root" ? Array.from(r.style) : []))
.filter((name) => name.startsWith("--"))
.sort();
const getCSSColorVariableNames = () =>