UNPKG

jacaranda

Version:

A lightweight styling library for React Native

75 lines (74 loc) 3.33 kB
/** * ISC License * Copyright 2025 Javier Diaz Chamorro * * Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby * granted, provided that the above copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE * OR PERFORMANCE OF THIS SOFTWARE. */ import { type ComponentType } from 'react'; import { ImageStyle, TextStyle, ViewStyle, StyleProp } from 'react-native'; type ResolvedStyle = ViewStyle & TextStyle & ImageStyle; type StyleObject = { [K in keyof ResolvedStyle]?: ResolvedStyle[K] | (string extends ResolvedStyle[K] ? `$${string}` : never) | (number extends ResolvedStyle[K] ? `$${string}` : never); }; type VariantOptions<V> = { [P in keyof V]: { [K in keyof V[P]]: StyleObject; }; }; type CompoundVariant<V> = { variants: Partial<{ [P in keyof V]: keyof V[P] | boolean | Array<keyof V[P]>; }>; style: StyleObject; }; type DefaultVariants<V> = Partial<{ [P in keyof V]: keyof V[P] | boolean; }>; type VariantStyleConfig<V extends VariantOptions<V>> = { base?: StyleObject; variants: V; compoundVariants?: CompoundVariant<V>[]; defaultVariants?: DefaultVariants<V>; }; export type OmitUndefined<T> = T extends undefined ? never : T; /** * Helper type to make all properties optional if they have a default value */ type OptionalIfHasDefault<Props, Defaults> = Omit<Props, keyof Defaults> & Partial<Pick<Props, Extract<keyof Defaults, keyof Props>>>; export type VariantProps<T> = T extends (props?: infer P) => StyleProp<ResolvedStyle> ? P : never; /** * Creates a function that generates styles based on variants */ declare function styles<V extends VariantOptions<V>>(config: VariantStyleConfig<V>): (props?: OptionalIfHasDefault<{ [P in keyof V]: boolean | (string & {}) | keyof V[P]; }, Partial<{ [P_1 in keyof V]: boolean | keyof V[P_1]; }>>) => StyleProp<ResolvedStyle>; /**** Tokens definition */ interface AllowedTokenCategories { colors: string; space: number; fontSize: number; fonts: string; lineHeight: number; } type TokenConfig = { [K in keyof AllowedTokenCategories]?: Record<string, AllowedTokenCategories[K]>; }; type ComponentProps<T> = T extends ComponentType<infer P> ? P : never; type StyledFunction = <C extends ComponentType<any>>(Component: C) => <P extends object = {}>(styleObject: StyleObject | ((props: P & Omit<ComponentProps<C>, 'style'>) => StyleObject)) => ComponentType<P & Omit<ComponentProps<C>, 'style'> & { style?: StyleProp<ViewStyle | TextStyle | ImageStyle>; }>; interface CreateTokensReturn { sva: typeof styles; tokens: TokenConfig; styled: StyledFunction; } /** * Creates a token system and returns the styles function */ export declare function defineTokens<T extends TokenConfig>(tokenConfig: T): CreateTokensReturn; export {};