Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(variants): add defaults to theme #51

Merged
merged 10 commits into from
Sep 22, 2020
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ const theme = createTheme({
tablet: 768,
},
cardVariants: {
defaults: {
// We can define defaults for the variant here.
// This will be applied after the defaults passed to createVariant and before the variant defined below.
},
regular: {
// We can refer to other values in the theme here, and use responsive props
padding: {
Expand Down
13 changes: 10 additions & 3 deletions src/createVariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ function createVariant<
const expandedProps = styleFunction.func(props, {theme, dimensions})[
property
];
if (!expandedProps && !defaults) return {};

const variantDefaults = theme[themeKey].defaults as Partial<
AllProps<Theme>
>;

if (!expandedProps && !defaults && !variantDefaults) return {};
return allRestyleFunctions.buildStyle(
{...defaults, ...expandedProps},
{...defaults, ...variantDefaults, ...expandedProps},
{
theme,
dimensions,
Expand All @@ -71,6 +76,8 @@ export type VariantProps<
Theme extends BaseTheme,
K extends keyof Theme,
Property extends keyof any = 'variant'
> = {[key in Property]?: ResponsiveValue<keyof Theme[K], Theme>};
> = {
[key in Property]?: ResponsiveValue<keyof Omit<Theme[K], 'defaults'>, Theme>;
};

export default createVariant;
75 changes: 75 additions & 0 deletions src/test/createVariant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ const theme = {
color: 'white',
},
},
boxVariants: {
defaults: {
fontSize: {
phone: 12,
tablet: 24,
},
backgroundColor: {
phone: 'black',
tablet: 'white',
},
},
primary: {
width: 50,
height: 50,
},
},
};
const dimensions = {
width: 375,
Expand Down Expand Up @@ -62,6 +78,65 @@ describe('createVariant', () => {
});
});

it('accepts defaults from the theme', () => {
const variant = createVariant({
themeKey: 'boxVariants',
});
expect(variant.func({}, {theme, dimensions})).toStrictEqual({
fontSize: 12,
backgroundColor: '#111111',
});
});

it('accepts defaults from the theme and correctly overrides variant defaults', () => {
const variant = createVariant({
themeKey: 'boxVariants',
defaults: {
fontSize: 10,
opacity: 0.5,
},
});

expect(variant.func({}, {theme, dimensions})).toStrictEqual({
backgroundColor: '#111111',
fontSize: 12,
opacity: 0.5,
});
});

it('corerctly uses the breakpoints for defaults within the theme', () => {
Johan-dutoit marked this conversation as resolved.
Show resolved Hide resolved
const variant = createVariant({
themeKey: 'boxVariants',
defaults: {
fontSize: 10,
opacity: 0.5,
},
});

expect(variant.func({}, {theme, dimensions})).toStrictEqual({
backgroundColor: '#111111',
fontSize: 12,
opacity: 0.5,
});

expect(
variant.func(
{},
{
theme,
dimensions: {
width: 376,
height: 667,
},
},
),
).toStrictEqual({
backgroundColor: '#EEEEEE',
fontSize: 24,
opacity: 0.5,
});
});

it('correctly overrides default values', () => {
const variant = createVariant({
themeKey: 'textVariants',
Expand Down