-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathSpaced.tsx
71 lines (65 loc) · 1.71 KB
/
Spaced.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import type { FC } from 'react';
import React from 'react';
import { styled, ignoreSsrWarning } from '@storybook/theming';
const toNumber = (input: any) => (typeof input === 'number' ? input : Number(input));
export interface ContainerProps {
col?: number;
row?: number;
outer?: number;
}
const Container = styled.div<ContainerProps>(
({ theme, col, row = 1 }) =>
col
? {
display: 'inline-block',
verticalAlign: 'inherit',
'& > *': {
marginLeft: col * theme.layoutMargin,
verticalAlign: 'inherit',
},
[`& > *:first-child${ignoreSsrWarning}`]: {
marginLeft: 0,
},
}
: {
'& > *': {
marginTop: row * theme.layoutMargin,
},
[`& > *:first-child${ignoreSsrWarning}`]: {
marginTop: 0,
},
},
({ theme, outer, col, row }) => {
switch (true) {
case !!(outer && col): {
return {
marginLeft: outer * theme.layoutMargin,
marginRight: outer * theme.layoutMargin,
};
}
case !!(outer && row): {
return {
marginTop: outer * theme.layoutMargin,
marginBottom: outer * theme.layoutMargin,
};
}
default: {
return {};
}
}
}
);
export interface SpacedProps {
children?: React.ReactNode;
col?: number;
row?: number;
outer?: number | boolean;
}
export const Spaced: FC<SpacedProps> = ({ col, row, outer, children, ...rest }) => {
const outerAmount = toNumber(typeof outer === 'number' || !outer ? outer : col || row);
return (
<Container col={col} row={row} outer={outerAmount} {...rest}>
{children}
</Container>
);
};