native-variants
Version:
A library for handling variants in React Native components with theme support.
48 lines (47 loc) • 1.43 kB
TypeScript
import React from "react";
/**
* Creates a type-safe context with provider and hook.
* Useful for creating component-level contexts for sharing props.
*
* @template T - The type of props to share via context
* @returns Object containing CTXProvider component and useCTX hook
*
* @example
* ```ts
* // Define props type
* type ButtonContextProps = {
* size: "sm" | "md" | "lg";
* variant: "primary" | "secondary";
* disabled?: boolean;
* };
*
* // Create context
* const { CTXProvider: ButtonProvider, useCTX: useButtonContext } =
* createCTX<ButtonContextProps>();
*
* // Use in parent component
* function ButtonGroup({ children }) {
* return (
* <ButtonProvider props={{ size: "md", variant: "primary" }}>
* {children}
* </ButtonProvider>
* );
* }
*
* // Use in child component
* function ButtonText() {
* const context = useButtonContext();
* // context?.size, context?.variant, etc.
* }
* ```
*/
export declare function createCTX<T>(): {
/** Provider component that makes context available to children */
CTXProvider: ({ children, props, }: React.PropsWithChildren<{
props: T;
}>) => React.JSX.Element;
/** Hook to optionally consume context (returns undefined if outside provider) */
useCTX: () => T | undefined;
/** Hook that requires context (throws if outside provider) */
useRequiredCTX: (componentName?: string) => T;
};