Skip to content

Commit

Permalink
Fix typo information for props (#216)
Browse files Browse the repository at this point in the history
Co-authored-by: Franz Moreno <[email protected]>
  • Loading branch information
fortmarek and FranzMoreno authored Jan 27, 2023
1 parent f12a5c2 commit 11a9812
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Next

- Better TypeScript type support [#216](https://github.com/Shopify/restyle/pull/216) by [FranzMoreno](https://github.com/FranzMoreno) & [fortmarek](https://github.com/fortmarek)

## 2.2.0 - 2023-01-24

- Upgrade React Native to 0.71 in [#207](https://github.com/Shopify/restyle/pull/207) by [mattfrances](https://github.com/mattfrances)
Expand Down
5 changes: 4 additions & 1 deletion src/createVariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ export type VariantProps<
K extends keyof Theme,
Property extends keyof any = 'variant',
> = {
[key in Property]?: ResponsiveValue<keyof Omit<Theme[K], 'defaults'>, Theme>;
[key in Property]?: ResponsiveValue<
keyof Omit<Theme[K], 'defaults'>,
Theme['breakpoints']
>;
};

export default createVariant;
2 changes: 1 addition & 1 deletion src/hooks/useResponsiveProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import useTheme from './useTheme';

const useResponsiveProp = <Theme extends BaseTheme, TVal extends PropValue>(
propValue: ResponsiveValue<TVal, Theme>,
propValue: ResponsiveValue<TVal, Theme['breakpoints']>,
) => {
const theme = useTheme<Theme>();
const dimensions = useWindowDimensions();
Expand Down
8 changes: 4 additions & 4 deletions src/responsiveHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const getValueForScreenSize = <Theme extends BaseTheme, TVal>({
breakpoints,
dimensions,
}: {
responsiveValue: AtLeastOneResponsiveValue<TVal, Theme>;
responsiveValue: AtLeastOneResponsiveValue<TVal, Theme['breakpoints']>;
breakpoints: Theme['breakpoints'];
dimensions: Dimensions;
}): TVal | undefined => {
Expand Down Expand Up @@ -62,9 +62,9 @@ export const getValueForScreenSize = <Theme extends BaseTheme, TVal>({
};

export const isResponsiveObjectValue = <Theme extends BaseTheme, TVal>(
val: ResponsiveValue<TVal, Theme>,
val: ResponsiveValue<TVal, Theme['breakpoints']>,
theme: Theme,
): val is AtLeastOneResponsiveValue<TVal, Theme> => {
): val is AtLeastOneResponsiveValue<TVal, Theme['breakpoints']> => {
if (!val) return false;
if (typeof val !== 'object') return false;
return getKeys(val).reduce((acc: boolean, key) => {
Expand All @@ -77,7 +77,7 @@ export const getResponsiveValue = <
Theme extends BaseTheme,
K extends keyof Theme | undefined,
>(
propValue: ResponsiveValue<TVal, Theme>,
propValue: ResponsiveValue<TVal, Theme['breakpoints']>,
{
theme,
transform,
Expand Down
42 changes: 24 additions & 18 deletions src/restyleFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,97 +272,103 @@ export const all = [
];

export interface ColorProps<Theme extends BaseTheme> {
color?: ResponsiveValue<keyof Theme['colors'], Theme>;
color?: ResponsiveValue<keyof Theme['colors'], Theme['breakpoints']>;
}
export interface OpacityProps<Theme extends BaseTheme> {
opacity?: ResponsiveValue<number, Theme>;
opacity?: ResponsiveValue<number, Theme['breakpoints']>;
}

export interface VisibleProps<Theme extends BaseTheme> {
visible?: ResponsiveValue<boolean, Theme>;
visible?: ResponsiveValue<boolean, Theme['breakpoints']>;
}

export interface BackgroundColorProps<Theme extends BaseTheme> {
backgroundColor?: ResponsiveValue<keyof Theme['colors'], Theme>;
backgroundColor?: ResponsiveValue<
keyof Theme['colors'],
Theme['breakpoints']
>;
}

export interface BackgroundColorShorthandProps<Theme extends BaseTheme> {
bg?: ResponsiveValue<keyof Theme['colors'], Theme>;
bg?: ResponsiveValue<keyof Theme['colors'], Theme['breakpoints']>;
}

export type SpacingProps<Theme extends BaseTheme> = {
[Key in keyof typeof spacingProperties]?: ResponsiveValue<
keyof Theme['spacing'],
Theme
Theme['breakpoints']
>;
};

export type SpacingShorthandProps<Theme extends BaseTheme> = {
[Key in keyof typeof spacingPropertiesShorthand]?: ResponsiveValue<
keyof Theme['spacing'],
Theme
Theme['breakpoints']
>;
};

export type TypographyProps<Theme extends BaseTheme> = {
[Key in keyof typeof typographyProperties]?: ResponsiveValue<
TextStyle[Key],
Theme
Theme['breakpoints']
>;
};

export type LayoutProps<Theme extends BaseTheme> = {
[Key in keyof typeof layoutProperties]?: ResponsiveValue<
FlexStyle[Key],
Theme
Theme['breakpoints']
>;
};

export type PositionProps<Theme extends BaseTheme> = {
[Key in keyof typeof positionProperties]?: ResponsiveValue<
FlexStyle[Key],
Theme
Theme['breakpoints']
>;
} & {
zIndex?: ResponsiveValue<
Theme['zIndices'] extends object ? keyof Theme['zIndices'] : number,
Theme
Theme['breakpoints']
>;
};

export type BorderProps<Theme extends BaseTheme> = {
[Key in keyof typeof borderProperties]?: ResponsiveValue<
ViewStyle[Key],
Theme
Theme['breakpoints']
>;
} & {
[Key in keyof typeof borderColorProperties]?: ResponsiveValue<
keyof Theme['colors'],
Theme
Theme['breakpoints']
>;
} & {
[Key in keyof typeof borderRadiusProperties]?: ResponsiveValue<
Theme['borderRadii'] extends object ? keyof Theme['borderRadii'] : number,
Theme
Theme['breakpoints']
>;
};

export type ShadowProps<Theme extends BaseTheme> = {
[Key in keyof typeof shadowProperties]?: ResponsiveValue<
ViewStyle[Key],
Theme
Theme['breakpoints']
>;
} & {
shadowColor?: ResponsiveValue<keyof Theme['colors'], Theme>;
shadowColor?: ResponsiveValue<keyof Theme['colors'], Theme['breakpoints']>;
};

export type TextShadowProps<Theme extends BaseTheme> = {
[Key in keyof typeof textShadowProperties]?: ResponsiveValue<
TextStyle[Key],
Theme
Theme['breakpoints']
>;
} & {
textShadowColor?: ResponsiveValue<keyof Theme['colors'], Theme>;
textShadowColor?: ResponsiveValue<
keyof Theme['colors'],
Theme['breakpoints']
>;
};

export type AllProps<Theme extends BaseTheme> = BackgroundColorProps<Theme> &
Expand Down
7 changes: 3 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ import {ImageStyle, TextStyle, ViewStyle, StyleProp} from 'react-native';

export type AtLeastOneResponsiveValue<
Value,
Theme extends BaseTheme,
B = Theme['breakpoints'],
B extends BaseTheme['breakpoints'],
R = {[Key in keyof B]: {[key in Key]: Value}},
> = Partial<{
[K in keyof B]: Value;
}> &
R[keyof R];

export type ResponsiveValue<Value, Theme extends BaseTheme> =
export type ResponsiveValue<Value, B extends BaseTheme['breakpoints']> =
| Value
| AtLeastOneResponsiveValue<Value, Theme>;
| AtLeastOneResponsiveValue<Value, B>;

export type SafeVariants<T> = Omit<T, keyof KnownBaseTheme>;

Expand Down

0 comments on commit 11a9812

Please sign in to comment.