Skip to content

Commit 2a87618

Browse files
authored
Add gap property (#188)
1 parent 2abc2fa commit 2a87618

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
88
## Next
99

1010
- Upgrade React Native to 0.71 in [#207](https://github.com/Shopify/restyle/pull/207) by [mattfrances](https://github.com/mattfrances)
11+
- Add support of gap properties in [#188](https://github.com/Shopify/restyle/pull/188) by [mlecoq](https://github.com/mlecoq)
1112

1213
## 2.1.0 - 2022-04-11
1314

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ The Restyle library comes with a number of predefined Restyle functions for your
372372
| color | color | colors |
373373
| opacity | opacity | _none_ |
374374
| visible | display (maps `true` / `false` to `flex` / `none`) | _none_ |
375-
| spacing | margin [m], marginTop [mt], marginRight [mr], marginBottom [mb], marginLeft [ml], marginStart [ms], marginEnd[me], marginHorizontal [mx], marginVertical [my], padding [p], paddingTop [pt], paddingRight [pr], paddingBottom [pb], paddingLeft [pl], paddingStart [ps], paddingEnd [pe], paddingHorizontal [px], paddingVertical [py] | spacing |
375+
| spacing | margin [m], marginTop [mt], marginRight [mr], marginBottom [mb], marginLeft [ml], marginStart [ms], marginEnd[me], marginHorizontal [mx], marginVertical [my], padding [p], paddingTop [pt], paddingRight [pr], paddingBottom [pb], paddingLeft [pl], paddingStart [ps], paddingEnd [pe], paddingHorizontal [px], paddingVertical [py], gap [g], rowGap [rG], columnGap [cG] | spacing |
376376
| layout | width, height, minWidth, maxWidth, minHeight, maxHeight, overflow, aspectRatio, alignContent, alignItems, alignSelf, justifyContent, flex, flexBasis, flexDirection, flexGrow, flexShrink, flexWrap | _none_ |
377377
| position | position, top, right, bottom, left, start, end | _none_ |
378378
| position | zIndex | zIndices |
@@ -539,11 +539,10 @@ const Button = ({
539539
const bgColor = textColorProp === 'purple' ? 'lightPurple' : 'lightBlue';
540540

541541
return (
542-
<BaseButton flexDirection="row" backgroundColor={bgColor} {...props}>
542+
<BaseButton flexDirection="row" columnGap="s" backgroundColor={bgColor} {...props}>
543543
<Text
544544
variant="buttonLabel"
545545
color={color}
546-
marginRight={isLoading ? 's' : undefined}
547546
>
548547
{label}
549548
</Text>

src/restyleFunctions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const spacingProperties = {
2323
paddingVertical: true,
2424
paddingStart: true,
2525
paddingEnd: true,
26+
columnGap: true,
27+
rowGap: true,
28+
gap: true,
2629
};
2730

2831
const spacingPropertiesShorthand = {
@@ -44,6 +47,9 @@ const spacingPropertiesShorthand = {
4447
py: 'paddingVertical',
4548
ps: 'paddingStart',
4649
pe: 'paddingEnd',
50+
g: 'gap',
51+
rG: 'rowGap',
52+
cG: 'columnGap',
4753
};
4854

4955
const typographyProperties = {

src/test/createRestyleComponent.test.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
BackgroundColorProps,
99
SpacingProps,
1010
spacing,
11+
SpacingShorthandProps,
12+
spacingShorthand,
1113
OpacityProps,
1214
opacity,
1315
} from '../restyleFunctions';
@@ -57,21 +59,23 @@ jest.mock('react-native/Libraries/Utilities/useWindowDimensions', () => ({
5759
const Component = createRestyleComponent<
5860
BackgroundColorProps<Theme> &
5961
SpacingProps<Theme> &
62+
SpacingShorthandProps<Theme> &
6063
OpacityProps<Theme> &
6164
ViewProps,
6265
Theme
63-
>([backgroundColor, spacing, opacity]);
66+
>([backgroundColor, spacing, spacingShorthand, opacity]);
6467
const cardVariant = createVariant<ThemeWithVariant, 'cardVariants'>({
6568
themeKey: 'cardVariants',
6669
});
6770
const ComponentWithVariant = createRestyleComponent<
6871
BackgroundColorProps<ThemeWithVariant> &
6972
SpacingProps<ThemeWithVariant> &
73+
SpacingShorthandProps<Theme> &
7074
OpacityProps<ThemeWithVariant> &
7175
VariantProps<ThemeWithVariant, 'cardVariants'> &
7276
ViewProps,
7377
ThemeWithVariant
74-
>([backgroundColor, spacing, opacity, cardVariant]);
78+
>([backgroundColor, spacing, spacingShorthand, opacity, cardVariant]);
7579

7680
describe('createRestyleComponent', () => {
7781
describe('creates a component that', () => {
@@ -192,5 +196,27 @@ describe('createRestyleComponent', () => {
192196
},
193197
]);
194198
});
199+
200+
it('uses gap values from the theme', () => {
201+
const {root} = render(
202+
<ThemeProvider theme={theme}>
203+
<Component gap="s" columnGap="s" rowGap="s" />
204+
</ThemeProvider>,
205+
);
206+
expect(root.findByType(View).props).toStrictEqual({
207+
style: [{gap: 8, columnGap: 8, rowGap: 8}],
208+
});
209+
});
210+
211+
it('passes gap shorthands as gap values', () => {
212+
const {root} = render(
213+
<ThemeProvider theme={theme}>
214+
<Component g="s" cG="s" rG="s" />
215+
</ThemeProvider>,
216+
);
217+
expect(root.findByType(View).props).toStrictEqual({
218+
style: [{gap: 8, columnGap: 8, rowGap: 8}],
219+
});
220+
});
195221
});
196222
});

0 commit comments

Comments
 (0)