Skip to content

Commit dd3ef07

Browse files
committed
feat: make spacing property as optional
1 parent 9610aac commit dd3ef07

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## next
9+
10+
- Added: defining spacing in theme is now optional [#257](https://github.com/Shopify/restyle/pull/257) by [mlecoq](https://github.com/mlecoq)
11+
812
## 2.4.2 - 2023-03-30
913

1014
- Fixed: Variant with breakpoints memoizing separate values into the same hash key by deconstructing breakpoint objects into strings [#241](https://github.com/Shopify/restyle/pull/241) by [mattfrances](https://github.com/mattfrances)

src/context.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {BaseTheme} from 'types';
33

44
export const ThemeContext = React.createContext({
55
colors: {},
6-
spacing: {},
76
});
87

98
export const ThemeProvider = ({

src/restyleFunctions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,14 @@ export interface BackgroundColorShorthandProps<Theme extends BaseTheme> {
310310

311311
export type SpacingProps<Theme extends BaseTheme> = {
312312
[Key in keyof typeof spacingProperties]?: ResponsiveValue<
313-
keyof Theme['spacing'],
313+
Theme['spacing'] extends object ? keyof Theme['spacing'] : number | string,
314314
Theme['breakpoints']
315315
>;
316316
};
317317

318318
export type SpacingShorthandProps<Theme extends BaseTheme> = {
319319
[Key in keyof typeof spacingPropertiesShorthand]?: ResponsiveValue<
320-
keyof Theme['spacing'],
320+
Theme['spacing'] extends object ? keyof Theme['spacing'] : number | string,
321321
Theme['breakpoints']
322322
>;
323323
};

src/test/createRestyleComponent.test.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ const themeWithVariant = {
4848
},
4949
};
5050

51+
const themeWithoutSpacing = {
52+
...theme,
53+
spacing: undefined,
54+
};
55+
5156
const {breakpoints, ...themeWithoutBreakpoints} = theme;
5257

5358
type Theme = typeof theme;
5459
type ThemeWithVariant = typeof themeWithVariant;
60+
type ThemeWithoutSpacing = typeof themeWithoutSpacing;
5561

5662
const mockUseWindowDimensions = jest.fn();
5763

@@ -67,6 +73,14 @@ const Component = createRestyleComponent<
6773
ViewProps,
6874
Theme
6975
>([backgroundColor, spacing, spacingShorthand, opacity]);
76+
const ComponentWithoutSpacing = createRestyleComponent<
77+
BackgroundColorProps<ThemeWithoutSpacing> &
78+
SpacingProps<ThemeWithoutSpacing> &
79+
SpacingShorthandProps<ThemeWithoutSpacing> &
80+
OpacityProps<ThemeWithoutSpacing> &
81+
ViewProps,
82+
ThemeWithoutSpacing
83+
>([backgroundColor, spacing, spacingShorthand, opacity]);
7084
const cardVariant = createVariant<ThemeWithVariant, 'cardVariants'>({
7185
themeKey: 'cardVariants',
7286
});
@@ -230,5 +244,32 @@ describe('createRestyleComponent', () => {
230244
style: [{gap: 8, columnGap: 8, rowGap: 8}],
231245
});
232246
});
247+
248+
it('passes gap values from the theme when no spacing prop is defined', () => {
249+
const {root} = render(
250+
<ThemeProvider theme={themeWithoutSpacing}>
251+
<ComponentWithoutSpacing
252+
gap={10}
253+
columnGap={20}
254+
rowGap={22}
255+
marginTop={2}
256+
marginLeft="auto"
257+
paddingBottom={3}
258+
/>
259+
</ThemeProvider>,
260+
);
261+
expect(root.findByType(View).props).toStrictEqual({
262+
style: [
263+
{
264+
gap: 10,
265+
columnGap: 20,
266+
rowGap: 22,
267+
marginTop: 2,
268+
marginLeft: 'auto',
269+
paddingBottom: 3,
270+
},
271+
],
272+
});
273+
});
233274
});
234275
});

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export interface KnownBaseTheme {
3535
colors: {
3636
[key: string]: string;
3737
};
38-
spacing: {
38+
spacing?: {
3939
[key: string]: number | string;
4040
};
4141
breakpoints?: {

0 commit comments

Comments
 (0)