This commit is contained in:
mario
2025-03-07 13:47:44 +07:00
commit c4efec5a14
3358 changed files with 303774 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
import * as React from 'react';
// https://github.com/reach/reach-ui/blob/dev/packages/utils/src/context.tsx
type ContextProvider<T> = React.FC<React.PropsWithChildren<T>>;
export function createContext<ContextValueType extends object | null>(
rootComponentName: string,
defaultContext?: ContextValueType
): [ContextProvider<ContextValueType>, (callerComponentName: string) => ContextValueType] {
const Ctx = React.createContext<ContextValueType | undefined>(defaultContext);
function Provider(props: React.PropsWithChildren<ContextValueType>) {
const { children, ...context } = props;
const value = React.useMemo(
() => context,
// eslint-disable-next-line react-hooks/exhaustive-deps
Object.values(context)
) as ContextValueType;
return <Ctx.Provider value={value}>{children}</Ctx.Provider>;
}
function useContext(callerComponentName: string) {
const context = React.useContext(Ctx);
if (context) {
return context;
}
if (defaultContext) {
return defaultContext;
}
throw Error(
`${callerComponentName} must be rendered inside of a ${rootComponentName} component.`
);
}
Ctx.displayName = `${rootComponentName}Context`;
Provider.displayName = `${rootComponentName}Provider`;
return [Provider, useContext];
}

View File

@@ -0,0 +1,6 @@
import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}