@granite-js/style-utils 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/LICENSE +202 -0
- package/README.md +24 -0
- package/dist/box-spacing.d.ts +220 -0
- package/dist/children.d.ts +53 -0
- package/dist/flex.d.ts +166 -0
- package/dist/index.d.ts +4 -0
- package/dist/spacing.d.ts +44 -0
- package/dist/stack.d.ts +126 -0
- package/package.json +52 -0
- package/src/box-spacing.spec.tsx +220 -0
- package/src/box-spacing.tsx +306 -0
- package/src/children.spec.tsx +24 -0
- package/src/children.tsx +74 -0
- package/src/flex.spec.tsx +92 -0
- package/src/flex.tsx +214 -0
- package/src/index.ts +4 -0
- package/src/spacing.spec.tsx +24 -0
- package/src/spacing.tsx +57 -0
- package/src/stack.spec.tsx +39 -0
- package/src/stack.tsx +155 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import type { ViewStyle } from 'react-native';
|
|
2
|
+
|
|
3
|
+
interface BoxSpacingOptionObject {
|
|
4
|
+
x?: never;
|
|
5
|
+
y?: never;
|
|
6
|
+
top?: never;
|
|
7
|
+
bottom?: never;
|
|
8
|
+
left?: never;
|
|
9
|
+
right?: never;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type BoxSpacingOptionObjectCase<Option extends keyof BoxSpacingOptionObject> = {
|
|
13
|
+
[O in Option]?: number;
|
|
14
|
+
} & {
|
|
15
|
+
[P in Exclude<keyof BoxSpacingOptionObject, Option>]?: never;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type BoxSpacingOption =
|
|
19
|
+
| BoxSpacingOptionObjectCase<'x' | 'y'>
|
|
20
|
+
| BoxSpacingOptionObjectCase<'x' | 'top' | 'bottom'>
|
|
21
|
+
| BoxSpacingOptionObjectCase<'y' | 'left' | 'right'>
|
|
22
|
+
| BoxSpacingOptionObjectCase<'top' | 'right' | 'bottom' | 'left'>
|
|
23
|
+
| number;
|
|
24
|
+
|
|
25
|
+
export function spacing(styleProperty: string, option: BoxSpacingOption): ViewStyle {
|
|
26
|
+
if (typeof option === 'number') {
|
|
27
|
+
return {
|
|
28
|
+
[styleProperty]: option,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const box: {
|
|
33
|
+
top?: number;
|
|
34
|
+
right?: number;
|
|
35
|
+
bottom?: number;
|
|
36
|
+
left?: number;
|
|
37
|
+
} = {};
|
|
38
|
+
|
|
39
|
+
if (option.x !== undefined) {
|
|
40
|
+
box.left = box.right = option.x;
|
|
41
|
+
}
|
|
42
|
+
if (option.y !== undefined) {
|
|
43
|
+
box.top = box.bottom = option.y;
|
|
44
|
+
}
|
|
45
|
+
if (option.top !== undefined) {
|
|
46
|
+
box.top = option.top;
|
|
47
|
+
}
|
|
48
|
+
if (option.right !== undefined) {
|
|
49
|
+
box.right = option.right;
|
|
50
|
+
}
|
|
51
|
+
if (option.bottom !== undefined) {
|
|
52
|
+
box.bottom = option.bottom;
|
|
53
|
+
}
|
|
54
|
+
if (option.left !== undefined) {
|
|
55
|
+
box.left = option.left;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Changes margin left to marginLeft */
|
|
59
|
+
const styleObject = Object.entries(box).reduce((_styleObject, [dir, value]) => {
|
|
60
|
+
return {
|
|
61
|
+
..._styleObject,
|
|
62
|
+
[`${styleProperty}${capitalizeFirstLetter(dir)}`]: value,
|
|
63
|
+
};
|
|
64
|
+
}, {});
|
|
65
|
+
|
|
66
|
+
return styleObject;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
type Property = 'x' | 'y' | 'top' | 'right' | 'bottom' | 'left';
|
|
70
|
+
type Unit = '4' | '8' | '12' | '16' | '24' | '32';
|
|
71
|
+
|
|
72
|
+
export interface BoxSpacingPresets {
|
|
73
|
+
readonly x: (value: number) => ViewStyle;
|
|
74
|
+
readonly x4: ViewStyle;
|
|
75
|
+
readonly x8: ViewStyle;
|
|
76
|
+
readonly x12: ViewStyle;
|
|
77
|
+
readonly x16: ViewStyle;
|
|
78
|
+
readonly x24: ViewStyle;
|
|
79
|
+
readonly x32: ViewStyle;
|
|
80
|
+
|
|
81
|
+
readonly y: (value: number) => ViewStyle;
|
|
82
|
+
readonly y4: ViewStyle;
|
|
83
|
+
readonly y8: ViewStyle;
|
|
84
|
+
readonly y12: ViewStyle;
|
|
85
|
+
readonly y16: ViewStyle;
|
|
86
|
+
readonly y24: ViewStyle;
|
|
87
|
+
readonly y32: ViewStyle;
|
|
88
|
+
|
|
89
|
+
readonly top: (value: number) => ViewStyle;
|
|
90
|
+
readonly top4: ViewStyle;
|
|
91
|
+
readonly top8: ViewStyle;
|
|
92
|
+
readonly top12: ViewStyle;
|
|
93
|
+
readonly top16: ViewStyle;
|
|
94
|
+
readonly top24: ViewStyle;
|
|
95
|
+
readonly top32: ViewStyle;
|
|
96
|
+
|
|
97
|
+
readonly right: (value: number) => ViewStyle;
|
|
98
|
+
readonly right4: ViewStyle;
|
|
99
|
+
readonly right8: ViewStyle;
|
|
100
|
+
readonly right12: ViewStyle;
|
|
101
|
+
readonly right16: ViewStyle;
|
|
102
|
+
readonly right24: ViewStyle;
|
|
103
|
+
readonly right32: ViewStyle;
|
|
104
|
+
|
|
105
|
+
readonly bottom: (value: number) => ViewStyle;
|
|
106
|
+
readonly bottom4: ViewStyle;
|
|
107
|
+
readonly bottom8: ViewStyle;
|
|
108
|
+
readonly bottom12: ViewStyle;
|
|
109
|
+
readonly bottom16: ViewStyle;
|
|
110
|
+
readonly bottom24: ViewStyle;
|
|
111
|
+
readonly bottom32: ViewStyle;
|
|
112
|
+
|
|
113
|
+
readonly left: (value: number) => ViewStyle;
|
|
114
|
+
readonly left4: ViewStyle;
|
|
115
|
+
readonly left8: ViewStyle;
|
|
116
|
+
readonly left12: ViewStyle;
|
|
117
|
+
readonly left16: ViewStyle;
|
|
118
|
+
readonly left24: ViewStyle;
|
|
119
|
+
readonly left32: ViewStyle;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export interface BoxSpacing extends BoxSpacingPresets {
|
|
123
|
+
(option: BoxSpacingOption): ViewStyle;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const properties: Property[] = ['x', 'y', 'top', 'right', 'bottom', 'left'];
|
|
127
|
+
const units: Unit[] = ['4', '8', '12', '16', '24', '32'];
|
|
128
|
+
|
|
129
|
+
function createSpacingWithProperty(cssProperty: string) {
|
|
130
|
+
const _spacing = (option: BoxSpacingOption) => spacing(cssProperty, option);
|
|
131
|
+
|
|
132
|
+
for (const property of properties) {
|
|
133
|
+
(_spacing as any)[property] = (value: number) => spacing(cssProperty, { [property]: value });
|
|
134
|
+
for (const unit of units) {
|
|
135
|
+
const key = `${property}${unit}` as keyof BoxSpacingPresets;
|
|
136
|
+
|
|
137
|
+
(_spacing as any)[key] = _spacing({
|
|
138
|
+
[property]: Number(unit),
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return _spacing as BoxSpacing;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @public
|
|
148
|
+
* @category UI
|
|
149
|
+
* @name padding
|
|
150
|
+
* @description
|
|
151
|
+
* The `padding` function sets the inner spacing of a component to create appropriate spacing between content and boundaries. You can specify horizontal (x), vertical (y), and individual direction (top, right, bottom, left) spacing using numbers.
|
|
152
|
+
* You can apply the same value to all directions by entering a number, or set individual values for each direction. There are also presets for commonly used values for easy application.
|
|
153
|
+
*
|
|
154
|
+
* @param {BoxSpacingOption} option - The option value to specify inner spacing. If you enter a number, it applies the same value to all directions,
|
|
155
|
+
* or you can set individual values for each direction.
|
|
156
|
+
* @property {(value: number) => ViewStyle} x - Returns a style object that sets the inner spacing of the component's horizontal direction (left and right) by the input number. The returned object is passed to the component's `style` property to apply the spacing.
|
|
157
|
+
* @property {ViewStyle} x4 - A style object that applies 4px inner spacing in the horizontal direction
|
|
158
|
+
* @property {ViewStyle} x8 - A style object that applies 8px inner spacing in the horizontal direction
|
|
159
|
+
* @property {ViewStyle} x12 - A style object that applies 12px inner spacing in the horizontal direction
|
|
160
|
+
* @property {ViewStyle} x16 - A style object that applies 16px inner spacing in the horizontal direction
|
|
161
|
+
* @property {ViewStyle} x24 - A style object that applies 24px inner spacing in the horizontal direction
|
|
162
|
+
* @property {ViewStyle} x32 - A style object that applies 32px inner spacing in the horizontal direction
|
|
163
|
+
* @property {(value: number) => ViewStyle} y - Returns a style object that sets the inner spacing of the component's vertical direction (top and bottom) by the input number. The returned object is passed to the component's `style` property to apply the spacing.
|
|
164
|
+
* @property {ViewStyle} y4 - A style object that applies 4px inner spacing in the vertical direction
|
|
165
|
+
* @property {ViewStyle} y8 - A style object that applies 8px inner spacing in the vertical direction
|
|
166
|
+
* @property {ViewStyle} y12 - A style object that applies 12px inner spacing in the vertical direction
|
|
167
|
+
* @property {ViewStyle} y16 - A style object that applies 16px inner spacing in the vertical direction
|
|
168
|
+
* @property {ViewStyle} y24 - A style object that applies 24px inner spacing in the vertical direction
|
|
169
|
+
* @property {ViewStyle} y32 - A style object that applies 32px inner spacing in the vertical direction
|
|
170
|
+
* @property {(value: number) => ViewStyle} top - Returns a style object that sets the inner spacing of the component's top direction by the input number. The returned object is passed to the component's `style` property to apply the spacing.
|
|
171
|
+
* @property {ViewStyle} top4 - A style object that applies 4px inner spacing to the top
|
|
172
|
+
* @property {ViewStyle} top8 - A style object that applies 8px inner spacing to the top
|
|
173
|
+
* @property {ViewStyle} top12 - A style object that applies 12px inner spacing to the top
|
|
174
|
+
* @property {ViewStyle} top16 - A style object that applies 16px inner spacing to the top
|
|
175
|
+
* @property {ViewStyle} top24 - A style object that applies 24px inner spacing to the top
|
|
176
|
+
* @property {ViewStyle} top32 - A style object that applies 32px inner spacing to the top
|
|
177
|
+
* @property {(value: number) => ViewStyle} right - Returns a style object that sets the inner spacing of the component's right direction by the input number. The returned object is passed to the component's `style` property to apply the spacing.
|
|
178
|
+
* @property {ViewStyle} right4 - A style object that applies 4px inner spacing to the right
|
|
179
|
+
* @property {ViewStyle} right8 - A style object that applies 8px inner spacing to the right
|
|
180
|
+
* @property {ViewStyle} right12 - A style object that applies 12px inner spacing to the right
|
|
181
|
+
* @property {ViewStyle} right16 - A style object that applies 16px inner spacing to the right
|
|
182
|
+
* @property {ViewStyle} right24 - A style object that applies 24px inner spacing to the right
|
|
183
|
+
* @property {ViewStyle} right32 - A style object that applies 32px inner spacing to the right
|
|
184
|
+
* @property {(value: number) => ViewStyle} bottom - Returns a style object that sets the inner spacing of the component's bottom direction by the input number. The returned object is passed to the component's `style` property to apply the spacing.
|
|
185
|
+
* @property {ViewStyle} bottom4 - A style object that applies 4px inner spacing to the bottom
|
|
186
|
+
* @property {ViewStyle} bottom8 - A style object that applies 8px inner spacing to the bottom
|
|
187
|
+
* @property {ViewStyle} bottom12 - A style object that applies 12px inner spacing to the bottom
|
|
188
|
+
* @property {ViewStyle} bottom16 - A style object that applies 16px inner spacing to the bottom
|
|
189
|
+
* @property {ViewStyle} bottom24 - A style object that applies 24px inner spacing to the bottom
|
|
190
|
+
* @property {ViewStyle} bottom32 - A style object that applies 32px inner spacing to the bottom
|
|
191
|
+
* @property {(value: number) => ViewStyle} left - Returns a style object that sets the inner spacing of the component's left direction by the input number. The returned object is passed to the component's `style` property to apply the spacing.
|
|
192
|
+
* @property {ViewStyle} left4 - A style object that applies 4px inner spacing to the left
|
|
193
|
+
* @property {ViewStyle} left8 - A style object that applies 8px inner spacing to the left
|
|
194
|
+
* @property {ViewStyle} left12 - A style object that applies 12px inner spacing to the left
|
|
195
|
+
* @property {ViewStyle} left16 - A style object that applies 16px inner spacing to the left
|
|
196
|
+
* @property {ViewStyle} left24 - A style object that applies 24px inner spacing to the left
|
|
197
|
+
* @property {ViewStyle} left32 - A style object that applies 32px inner spacing to the left
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ## Example of applying 8px inner spacing in horizontal and vertical directions, and 100px spacing in the bottom direction
|
|
201
|
+
*
|
|
202
|
+
* ```tsx
|
|
203
|
+
* import { padding } from '@granite-js/react-native';
|
|
204
|
+
* import { View } from 'react-native';
|
|
205
|
+
*
|
|
206
|
+
* function Component() {
|
|
207
|
+
* return (
|
|
208
|
+
* <View>
|
|
209
|
+
* <View style={padding.x8}>
|
|
210
|
+
* <Text>Has horizontal spacing</Text>
|
|
211
|
+
* </View>
|
|
212
|
+
* <View style={padding.y8}>
|
|
213
|
+
* <Text>Has vertical spacing</Text>
|
|
214
|
+
* </View>
|
|
215
|
+
* <View style={padding.bottom(100)}>
|
|
216
|
+
* <Text>Has 100px spacing at the bottom</Text>
|
|
217
|
+
* </View>
|
|
218
|
+
* </View>
|
|
219
|
+
* );
|
|
220
|
+
* }
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
export const padding = createSpacingWithProperty('padding');
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @public
|
|
227
|
+
* @category UI
|
|
228
|
+
* @name margin
|
|
229
|
+
* @description
|
|
230
|
+
* The `margin` function sets the outer spacing of a component to create appropriate spacing between components. You can specify horizontal (x), vertical (y), and individual direction (top, right, bottom, left) spacing using numbers.
|
|
231
|
+
* You can apply the same value to all directions by entering a number, or set individual values for each direction. There are also presets for commonly used values for easy application.
|
|
232
|
+
*
|
|
233
|
+
* @param {BoxSpacingOption} option - The option value to specify outer spacing. If you enter a number, it applies the same value to all directions,
|
|
234
|
+
* or you can set individual values for each direction.
|
|
235
|
+
* @property {(value: number) => ViewStyle} x - Returns a style object that sets the outer spacing of the component's horizontal direction (left and right) by the input number. The returned object is passed to the component's `style` property to apply the spacing.
|
|
236
|
+
* @property {ViewStyle} x4 - A style object that applies 4px outer spacing in the horizontal direction
|
|
237
|
+
* @property {ViewStyle} x8 - A style object that applies 8px outer spacing in the horizontal direction
|
|
238
|
+
* @property {ViewStyle} x12 - A style object that applies 12px outer spacing in the horizontal direction
|
|
239
|
+
* @property {ViewStyle} x16 - A style object that applies 16px outer spacing in the horizontal direction
|
|
240
|
+
* @property {ViewStyle} x24 - A style object that applies 24px outer spacing in the horizontal direction
|
|
241
|
+
* @property {ViewStyle} x32 - A style object that applies 32px outer spacing in the horizontal direction
|
|
242
|
+
* @property {(value: number) => ViewStyle} y - Returns a style object that sets the outer spacing of the component's vertical direction (top and bottom) by the input number. The returned object is passed to the component's `style` property to apply the spacing.
|
|
243
|
+
* @property {ViewStyle} y4 - A style object that applies 4px outer spacing in the vertical direction
|
|
244
|
+
* @property {ViewStyle} y8 - A style object that applies 8px outer spacing in the vertical direction
|
|
245
|
+
* @property {ViewStyle} y12 - A style object that applies 12px outer spacing in the vertical direction
|
|
246
|
+
* @property {ViewStyle} y16 - A style object that applies 16px outer spacing in the vertical direction
|
|
247
|
+
* @property {ViewStyle} y24 - A style object that applies 24px outer spacing in the vertical direction
|
|
248
|
+
* @property {ViewStyle} y32 - A style object that applies 32px outer spacing in the vertical direction
|
|
249
|
+
* @property {(value: number) => ViewStyle} top - Returns a style object that sets the outer spacing of the component's top direction by the input number. The returned object is passed to the component's `style` property to apply the spacing.
|
|
250
|
+
* @property {ViewStyle} top4 - A style object that applies 4px outer spacing to the top
|
|
251
|
+
* @property {ViewStyle} top8 - A style object that applies 8px outer spacing to the top
|
|
252
|
+
* @property {ViewStyle} top12 - A style object that applies 12px outer spacing to the top
|
|
253
|
+
* @property {ViewStyle} top16 - A style object that applies 16px outer spacing to the top
|
|
254
|
+
* @property {ViewStyle} top24 - A style object that applies 24px outer spacing to the top
|
|
255
|
+
* @property {ViewStyle} top32 - A style object that applies 32px outer spacing to the top
|
|
256
|
+
* @property {(value: number) => ViewStyle} right - Returns a style object that sets the outer spacing of the component's right direction by the input number. The returned object is passed to the component's `style` property to apply the spacing.
|
|
257
|
+
* @property {ViewStyle} right4 - A style object that applies 4px outer spacing to the right
|
|
258
|
+
* @property {ViewStyle} right8 - A style object that applies 8px outer spacing to the right
|
|
259
|
+
* @property {ViewStyle} right12 - A style object that applies 12px outer spacing to the right
|
|
260
|
+
* @property {ViewStyle} right16 - A style object that applies 16px outer spacing to the right
|
|
261
|
+
* @property {ViewStyle} right24 - A style object that applies 24px outer spacing to the right
|
|
262
|
+
* @property {ViewStyle} right32 - A style object that applies 32px outer spacing to the right
|
|
263
|
+
* @property {(value: number) => ViewStyle} bottom - Returns a style object that sets the outer spacing of the component's bottom direction by the input number. The returned object is passed to the component's `style` property to apply the spacing.
|
|
264
|
+
* @property {ViewStyle} bottom4 - A style object that applies 4px outer spacing to the bottom
|
|
265
|
+
* @property {ViewStyle} bottom8 - A style object that applies 8px outer spacing to the bottom
|
|
266
|
+
* @property {ViewStyle} bottom12 - A style object that applies 12px outer spacing to the bottom
|
|
267
|
+
* @property {ViewStyle} bottom16 - A style object that applies 16px outer spacing to the bottom
|
|
268
|
+
* @property {ViewStyle} bottom24 - A style object that applies 24px outer spacing to the bottom
|
|
269
|
+
* @property {ViewStyle} bottom32 - A style object that applies 32px outer spacing to the bottom
|
|
270
|
+
* @property {(value: number) => ViewStyle} left - Returns a style object that sets the outer spacing of the component's left direction by the input number. The returned object is passed to the component's `style` property to apply the spacing.
|
|
271
|
+
* @property {ViewStyle} left4 - A style object that applies 4px outer spacing to the left
|
|
272
|
+
* @property {ViewStyle} left8 - A style object that applies 8px outer spacing to the left
|
|
273
|
+
* @property {ViewStyle} left12 - A style object that applies 12px outer spacing to the left
|
|
274
|
+
* @property {ViewStyle} left16 - A style object that applies 16px outer spacing to the left
|
|
275
|
+
* @property {ViewStyle} left24 - A style object that applies 24px outer spacing to the left
|
|
276
|
+
* @property {ViewStyle} left32 - A style object that applies 32px outer spacing to the left
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ## Example of applying 8px outer spacing in horizontal and vertical directions, and 100px spacing in the bottom direction
|
|
280
|
+
*
|
|
281
|
+
* ```tsx
|
|
282
|
+
* import { margin } from '@granite-js/react-native';
|
|
283
|
+
* import { View } from 'react-native';
|
|
284
|
+
*
|
|
285
|
+
* function Component() {
|
|
286
|
+
* return (
|
|
287
|
+
* <View>
|
|
288
|
+
* <View style={margin.x8}>
|
|
289
|
+
* <Text>Has horizontal spacing</Text>
|
|
290
|
+
* </View>
|
|
291
|
+
* <View style={margin.y8}>
|
|
292
|
+
* <Text>Has vertical spacing</Text>
|
|
293
|
+
* </View>
|
|
294
|
+
* <View style={margin.bottom(100)}>
|
|
295
|
+
* <Text>Has 100px spacing at the bottom</Text>
|
|
296
|
+
* </View>
|
|
297
|
+
* </View>
|
|
298
|
+
* );
|
|
299
|
+
* }
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
302
|
+
export const margin = createSpacingWithProperty('margin');
|
|
303
|
+
|
|
304
|
+
function capitalizeFirstLetter(str: string) {
|
|
305
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
306
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { render } from '@testing-library/react-native';
|
|
2
|
+
import { View } from 'react-native';
|
|
3
|
+
import type { ReactTestRendererJSON } from 'react-test-renderer';
|
|
4
|
+
import { Children } from './children';
|
|
5
|
+
import { Spacing } from './spacing';
|
|
6
|
+
|
|
7
|
+
describe('Children', () => {
|
|
8
|
+
describe('Gap', () => {
|
|
9
|
+
function Spacing16() {
|
|
10
|
+
return <Spacing size={16} />;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
it(`Children.Gap renders with gap components added between child elements`, () => {
|
|
14
|
+
const childCount = 5;
|
|
15
|
+
const children = new Array(childCount).fill(null).map((_, index) => <View key={index} />);
|
|
16
|
+
const treeJSON = render(
|
|
17
|
+
<Children.Gap gap={<Spacing16 />}>{children}</Children.Gap>
|
|
18
|
+
).toJSON() as ReactTestRendererJSON[];
|
|
19
|
+
|
|
20
|
+
// Number of children + number of gaps between children
|
|
21
|
+
expect(treeJSON.length).toBe(childCount + childCount - 1);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
});
|
package/src/children.tsx
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import React, { Fragment, type ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @category Constants
|
|
5
|
+
* @name Children
|
|
6
|
+
* @description
|
|
7
|
+
* `Children` provides various components for controlling layouts related to child elements.
|
|
8
|
+
*
|
|
9
|
+
* @property {ChildrenGap} Gap - `Children.Gap` is a component used to add a specified component (gap) between multiple child elements when rendering.
|
|
10
|
+
*/
|
|
11
|
+
export const Children = {
|
|
12
|
+
/**
|
|
13
|
+
* @name Children.Gap
|
|
14
|
+
* @description Renders a gap component between ReactNode children.
|
|
15
|
+
*/
|
|
16
|
+
Gap: ChildrenGap,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
interface ChildrenGapProps {
|
|
20
|
+
children: ReactNode;
|
|
21
|
+
gap: ReactNode;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @category Components
|
|
26
|
+
* @name ChildrenGap
|
|
27
|
+
* @description
|
|
28
|
+
* `Children.Gap` is a useful component for maintaining consistent layouts by adding spacing (gap) between multiple child elements.
|
|
29
|
+
* Using this component allows you to apply consistent spacing between child elements, resulting in a cleaner and more consistent UI design.
|
|
30
|
+
*
|
|
31
|
+
* @param {object} props - The `props` object passed to the component.
|
|
32
|
+
* @param {ReactNode} props.children - The child components to be rendered.
|
|
33
|
+
* @param {ReactNode} props.gap - The component to be placed between child elements. `gap` must be a valid React component.
|
|
34
|
+
*
|
|
35
|
+
* @throws {Error} An error occurs when `gap` is not a valid React component. `gap` must use `React.Element`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ## When you want to add 16px spacing between child elements
|
|
39
|
+
*
|
|
40
|
+
* ```tsx
|
|
41
|
+
* import { Children, Spacing } from '@granite-js/react-native';
|
|
42
|
+
* import { Text } from 'react-native';
|
|
43
|
+
*
|
|
44
|
+
* function Component() {
|
|
45
|
+
* return (
|
|
46
|
+
* <Children.Gap gap={<Spacing size={16} />}>
|
|
47
|
+
* <Text>First</Text>
|
|
48
|
+
* <Text>Second</Text>
|
|
49
|
+
* <Text>Third</Text>
|
|
50
|
+
* </Children.Gap>
|
|
51
|
+
* );
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
function ChildrenGap({ children, gap }: ChildrenGapProps) {
|
|
56
|
+
if (!React.isValidElement(gap)) {
|
|
57
|
+
throw new Error('gap prop must be a component.');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<>
|
|
62
|
+
{React.Children.map(children, (child, index) => {
|
|
63
|
+
const isLastChild = index === React.Children.count(children) - 1;
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<Fragment key={index}>
|
|
67
|
+
{child}
|
|
68
|
+
{!isLastChild && child != null ? gap : undefined}
|
|
69
|
+
</Fragment>
|
|
70
|
+
);
|
|
71
|
+
})}
|
|
72
|
+
</>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { render, renderHook, screen } from '@testing-library/react-native';
|
|
2
|
+
import { useRef } from 'react';
|
|
3
|
+
import { View } from 'react-native';
|
|
4
|
+
import type { ReactTestRendererJSON } from 'react-test-renderer';
|
|
5
|
+
import { flex, Flex } from './flex';
|
|
6
|
+
|
|
7
|
+
describe('Flex', () => {
|
|
8
|
+
it(`Flex is rendered as <View />`, () => {
|
|
9
|
+
const treeJSON = render(<Flex />).toJSON() as ReactTestRendererJSON;
|
|
10
|
+
expect(treeJSON.type).toBe('View');
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('Flex.Center applies align="center" justify="center"', () => {
|
|
14
|
+
render(<Flex.Center testID="flex" />);
|
|
15
|
+
expect(screen.getByTestId('flex')).toHaveStyle({
|
|
16
|
+
display: 'flex',
|
|
17
|
+
justifyContent: 'center',
|
|
18
|
+
alignItems: 'center',
|
|
19
|
+
flexDirection: 'column',
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('Flex.CenterVertical applies justify="center"', () => {
|
|
24
|
+
render(<Flex.CenterVertical testID="flex" />);
|
|
25
|
+
expect(screen.getByTestId('flex')).toHaveStyle({
|
|
26
|
+
display: 'flex',
|
|
27
|
+
justifyContent: 'center',
|
|
28
|
+
alignItems: 'stretch',
|
|
29
|
+
flexDirection: 'column',
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('Flex.CenterHorizontal applies justyfyContent="center"', () => {
|
|
34
|
+
render(<Flex.CenterHorizontal testID="flex" />);
|
|
35
|
+
expect(screen.getByTestId('flex')).toHaveStyle({
|
|
36
|
+
display: 'flex',
|
|
37
|
+
justifyContent: 'flex-start',
|
|
38
|
+
alignItems: 'center',
|
|
39
|
+
flexDirection: 'column',
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('ref', () => {
|
|
44
|
+
it(`Flex should be able to receive ref`, () => {
|
|
45
|
+
const {
|
|
46
|
+
result: { current: ref },
|
|
47
|
+
} = renderHook(() => useRef<View>(null));
|
|
48
|
+
|
|
49
|
+
render(<Flex ref={ref} />);
|
|
50
|
+
|
|
51
|
+
expect(ref.current).not.toBeNull();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('flex', () => {
|
|
57
|
+
it(`when passing an empty object to flex, it returns defaultProps with display="flex" direction="column" justify="flex-start" align="stretch"`, () => {
|
|
58
|
+
const result = flex({});
|
|
59
|
+
expect(result).toMatchInlineSnapshot(`
|
|
60
|
+
{
|
|
61
|
+
"alignItems": "stretch",
|
|
62
|
+
"display": "flex",
|
|
63
|
+
"flexDirection": "column",
|
|
64
|
+
"justifyContent": "flex-start",
|
|
65
|
+
}
|
|
66
|
+
`);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it(`when passing string to flex, it returns with align, justify, direction applied`, () => {
|
|
70
|
+
const result = flex('flex-start', 'center', 'row');
|
|
71
|
+
expect(result).toMatchInlineSnapshot(`
|
|
72
|
+
{
|
|
73
|
+
"alignItems": "flex-start",
|
|
74
|
+
"display": "flex",
|
|
75
|
+
"flexDirection": "row",
|
|
76
|
+
"justifyContent": "center",
|
|
77
|
+
}
|
|
78
|
+
`);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('flex.center() returns with justyfy="center" align="center"', () => {
|
|
82
|
+
const result = flex.center();
|
|
83
|
+
expect(result).toMatchInlineSnapshot(`
|
|
84
|
+
{
|
|
85
|
+
"alignItems": "center",
|
|
86
|
+
"display": "flex",
|
|
87
|
+
"flexDirection": "column",
|
|
88
|
+
"justifyContent": "center",
|
|
89
|
+
}
|
|
90
|
+
`);
|
|
91
|
+
});
|
|
92
|
+
});
|