Skip to content

Commit 2679eab

Browse files
author
eunchankim
committed
feat: implement Spacer component
1 parent 4c7c0b3 commit 2679eab

File tree

11 files changed

+132
-5
lines changed

11 files changed

+132
-5
lines changed

example/src/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22

33
import { StyleSheet, View, Text } from 'react-native';
4-
import { multiply, Hello } from 'good-ui';
4+
import { multiply, Hello, Spacer } from 'react-native-good-ui';
55

66
export default function App() {
77
const [result, setResult] = React.useState<number | undefined>();
@@ -14,6 +14,8 @@ export default function App() {
1414
<View style={styles.container}>
1515
<Hello />
1616

17+
<Spacer direction={'both'} preset={'huge'} />
18+
1719
<Text>Result: {result}</Text>
1820
</View>
1921
);

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@
5151
"registry": "https://registry.npmjs.org/"
5252
},
5353
"devDependencies": {
54-
"@evilmartians/lefthook": "^1.2.2",
5554
"@commitlint/config-conventional": "^17.0.2",
55+
"@evilmartians/lefthook": "^1.2.2",
5656
"@react-native-community/eslint-config": "^3.0.2",
5757
"@release-it/conventional-changelog": "^5.0.0",
5858
"@types/jest": "^28.1.2",
59+
"@types/ramda": "^0.28.20",
5960
"@types/react": "~17.0.21",
6061
"@types/react-native": "0.70.0",
6162
"commitlint": "^17.0.2",
@@ -155,5 +156,8 @@
155156
}
156157
]
157158
]
159+
},
160+
"dependencies": {
161+
"ramda": "^0.28.0"
158162
}
159163
}

src/components/Spacer/index.tsx

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* The Spacer component is responsible for handling the space UI.
3+
* This component can create 3 types of spaces that is flex, preset and pixel type.
4+
* You should avoid using "size" prop that allow input in units of pixels for coherent spatial processing.
5+
*/
6+
7+
import React from 'react';
8+
import { View, ViewStyle } from 'react-native';
9+
import { Spacing, spacing } from '../../theme';
10+
import { isNilOrEmpty } from '../../utils';
11+
12+
interface SpacerDirection {
13+
direction?: 'both' | 'vertical' | 'horizontal';
14+
}
15+
16+
interface FlexSpacerProps extends SpacerDirection {
17+
/**
18+
* flex type is joint 1st place priority. Strictly speaking, this is First.
19+
* If you don't know flex layout, Reference https://reactnative.dev/docs/flexbox.
20+
*/
21+
flex: number;
22+
/**
23+
* When using the above props, you should never use any other props.
24+
*/
25+
preset?: never;
26+
size?: never;
27+
}
28+
29+
interface PresetSpacerProps extends SpacerDirection {
30+
/**
31+
* preset Type is joint 1st place priority.
32+
*/
33+
preset: Spacing;
34+
/**
35+
* When using the above props, you should never use any other props.
36+
*/
37+
flex?: never;
38+
size?: never;
39+
}
40+
41+
interface SizeSpacerProps extends SpacerDirection {
42+
/**
43+
* size has the lowest priority.
44+
* because The use of size is not recommended because UI & UX quality is often poor.
45+
*/
46+
size?: number;
47+
/**
48+
* When using the above props, you should never use any other props.
49+
*/
50+
preset?: never;
51+
flex?: never;
52+
}
53+
54+
type SpacerProps = PresetSpacerProps | FlexSpacerProps | SizeSpacerProps;
55+
56+
export function Spacer(props: SpacerProps) {
57+
const { direction = 'both', flex, preset, size: pixelSize, ...rest } = props;
58+
59+
// @ts-ignore
60+
const presetSize = spacing[preset];
61+
const value = presetSize ? presetSize : pixelSize;
62+
63+
const style: ViewStyle = {
64+
flex: flex ? flex : undefined,
65+
width:
66+
direction === 'both' || direction === 'horizontal' ? value : undefined,
67+
height:
68+
direction === 'both' || direction === 'vertical' ? value : undefined,
69+
};
70+
71+
if (__DEV__ && isNilOrEmpty(value)) {
72+
console.warn(`Spacer component's value is nil or empty!`);
73+
}
74+
75+
return <View style={style} {...rest} />;
76+
}

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { Spacer } from './Spacer';

src/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import React from 'react';
2-
import { View } from 'react-native';
2+
import { View, Text } from 'react-native';
3+
4+
export { Spacer } from './components';
35

46
export function multiply(a: number, b: number): Promise<number> {
57
return Promise.resolve(a * b);
68
}
79

810
export function Hello() {
9-
return <View>Hello World</View>;
11+
return (
12+
<View>
13+
<Text>Hello World</Text>
14+
</View>
15+
);
1016
}

src/theme/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './spacing';

src/theme/spacing.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
Use these spacings for margins/paddings and other whitespace throughout your app.
3+
*/
4+
export const spacing = {
5+
micro: 2,
6+
tiny: 4,
7+
extraSmall: 8,
8+
small: 12,
9+
medium: 16,
10+
large: 24,
11+
extraLarge: 32,
12+
huge: 48,
13+
massive: 64,
14+
} as const;
15+
16+
export type Spacing = keyof typeof spacing;

src/utils/functions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { isNil, isEmpty } from 'ramda';
2+
3+
export const isNilOrEmpty = (value: any) => isNil(value) || isEmpty(value);

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './functions';

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"baseUrl": "./",
44
"paths": {
5-
"good-ui": ["./src/index"]
5+
"react-native-good-ui": ["./src/index"]
66
},
77
"allowUnreachableCode": false,
88
"allowUnusedLabels": false,

0 commit comments

Comments
 (0)