From 2eb2b6e70388de549847d925ac85f6c179db60d1 Mon Sep 17 00:00:00 2001 From: Hammad Jutt Date: Tue, 21 Jul 2020 22:43:44 -0600 Subject: [PATCH] Separate shorthand props intro different functions --- src/createBox.ts | 22 +++++++++++--- src/createText.ts | 17 ++++++++--- src/restyleFunctions.ts | 64 +++++++++++++++++++++++++---------------- 3 files changed, 71 insertions(+), 32 deletions(-) diff --git a/src/createBox.ts b/src/createBox.ts index 2fb377bf..631481f9 100644 --- a/src/createBox.ts +++ b/src/createBox.ts @@ -5,6 +5,7 @@ import createRestyleComponent from './createRestyleComponent'; import {BaseTheme} from './types'; import { backgroundColor, + backgroundColorShorthand, opacity, layout, spacing, @@ -20,23 +21,34 @@ import { PositionProps, visible, VisibleProps, + SpacingShorthandProps, + BackgroundColorShorthandProps, + spacingShorthand, } from './restyleFunctions'; -export type BoxProps = BackgroundColorProps & +export type BoxProps< + Theme extends BaseTheme, + EnableShorthand extends boolean = true +> = BackgroundColorProps & OpacityProps & VisibleProps & LayoutProps & SpacingProps & BorderProps & ShadowProps & - PositionProps; + PositionProps & + (EnableShorthand extends true + ? SpacingShorthandProps & BackgroundColorShorthandProps + : never); export const boxRestyleFunctions = [ backgroundColor, + backgroundColorShorthand, opacity, visible, layout, spacing, + spacingShorthand, border, shadow, position, @@ -44,12 +56,14 @@ export const boxRestyleFunctions = [ const createBox = < Theme extends BaseTheme, - Props = React.ComponentProps & {children?: React.ReactNode} + Props = React.ComponentProps & {children?: React.ReactNode}, + EnableShorthand extends boolean = true >( BaseComponent: React.ComponentType = View, ) => { return createRestyleComponent< - BoxProps & Omit>, + BoxProps & + Omit>, Theme >(boxRestyleFunctions, BaseComponent); }; diff --git a/src/createText.ts b/src/createText.ts index 78f88527..cbc06052 100644 --- a/src/createText.ts +++ b/src/createText.ts @@ -16,16 +16,22 @@ import { TextShadowProps, TypographyProps, VisibleProps, + spacingShorthand, + SpacingShorthandProps, } from './restyleFunctions'; import createVariant, {VariantProps} from './createVariant'; -export type TextProps = ColorProps & +export type TextProps< + Theme extends BaseTheme, + EnableShorthand extends boolean = true +> = ColorProps & OpacityProps & VisibleProps & TypographyProps & SpacingProps & TextShadowProps & - VariantProps; + VariantProps & + (EnableShorthand extends true ? SpacingShorthandProps : never); export const textRestyleFunctions = [ color, @@ -33,18 +39,21 @@ export const textRestyleFunctions = [ visible, typography, spacing, + spacingShorthand, textShadow, createVariant({themeKey: 'textVariants'}), ]; const createText = < Theme extends BaseTheme, - Props = React.ComponentProps & {children?: React.ReactNode} + Props = React.ComponentProps & {children?: React.ReactNode}, + EnableShorthand extends boolean = true >( BaseComponent: React.ComponentType = Text, ) => { return createRestyleComponent< - TextProps & Omit>, + TextProps & + Omit>, Theme >(textRestyleFunctions, BaseComponent); }; diff --git a/src/restyleFunctions.ts b/src/restyleFunctions.ts index 1f0b8961..bf4af529 100644 --- a/src/restyleFunctions.ts +++ b/src/restyleFunctions.ts @@ -19,6 +19,9 @@ const spacingProperties = { paddingLeft: true, paddingHorizontal: true, paddingVertical: true, +}; + +const spacingPropertiesShorthand = { m: 'margin', mt: 'marginTop', mr: 'marginRight', @@ -114,11 +117,6 @@ const textShadowProperties = { textShadowRadius: true, }; -const backgroundColorProperties = { - backgroundColor: true, - bg: 'backgroundColor', -}; - export const color = createRestyleFunction({ property: 'color', themeKey: 'colors', @@ -134,28 +132,36 @@ export const visible = createRestyleFunction({ transform: ({value}) => (value === false ? 'none' : 'flex'), }); -export const backgroundColor = getKeys(backgroundColorProperties).map( - property => { - const alias = backgroundColorProperties[property]; +export const backgroundColor = createRestyleFunction({ + property: 'backgroundColor', + themeKey: 'colors', +}); - return createRestyleFunction({ - property, - styleProperty: typeof alias === 'string' ? alias : undefined, - themeKey: 'colors', - }); - }, -); +export const backgroundColorShorthand = createRestyleFunction({ + property: 'bg', + styleProperty: 'backgroundColor', + themeKey: 'colors', +}); export const spacing = getKeys(spacingProperties).map(property => { - const alias = spacingProperties[property]; - return createRestyleFunction({ property, - styleProperty: typeof alias === 'string' ? alias : undefined, themeKey: 'spacing', }); }); +export const spacingShorthand = getKeys(spacingPropertiesShorthand).map( + property => { + const styleProperty = spacingPropertiesShorthand[property]; + + return createRestyleFunction({ + property, + styleProperty, + themeKey: 'spacing', + }); + }, +); + export const typography = getKeys(typographyProperties).map(property => { return createRestyleFunction({ property, @@ -227,8 +233,10 @@ export const textShadow = [ export const all = [ color, opacity, - ...backgroundColor, + backgroundColor, + backgroundColorShorthand, ...spacing, + ...spacingShorthand, ...typography, ...layout, ...position, @@ -248,15 +256,23 @@ export interface VisibleProps { visible?: ResponsiveValue; } -export type BackgroundColorProps = { - [Key in keyof typeof backgroundColorProperties]?: ResponsiveValue< - keyof Theme['colors'], +export interface BackgroundColorProps { + backgroundColor?: ResponsiveValue; +} + +export interface BackgroundColorShorthandProps { + bg?: ResponsiveValue; +} + +export type SpacingProps = { + [Key in keyof typeof spacingProperties]?: ResponsiveValue< + keyof Theme['spacing'], Theme >; }; -export type SpacingProps = { - [Key in keyof typeof spacingProperties]?: ResponsiveValue< +export type SpacingShorthandProps = { + [Key in keyof typeof spacingPropertiesShorthand]?: ResponsiveValue< keyof Theme['spacing'], Theme >;