1
- import { CSSProperties , ReactNode } from 'react' ;
1
+ import { ReactNode } from 'react' ;
2
2
3
3
import styled from '@emotion/styled' ;
4
4
import { css } from '@emotion/react' ;
5
5
import isPropValid from '@emotion/is-prop-valid' ;
6
6
7
- import { config , BREAKPOINTS } from '../../config' ;
8
- import { media } from '../../utils' ;
7
+ import { config } from '../../config' ;
8
+ import { responsive } from '../../utils' ;
9
9
10
- import { Breakpoints , StyleProps } from '../../types/emotion' ;
10
+ import { DefaultTheme , StyleProps } from '../../types/emotion' ;
11
11
12
12
export interface ColProps {
13
+ /**
14
+ * Trigger column styles
15
+ * based on breakpoint
16
+ */
13
17
xs ?: number ;
18
+
14
19
sm ?: number ;
20
+
15
21
md ?: number ;
22
+
16
23
lg ?: number ;
24
+
17
25
xl ?: number ;
18
- offset ?: Record < Breakpoints , number > ;
19
- style ?: CSSProperties ;
20
- className ?: string ;
26
+
27
+ /**
28
+ * The column `span`
29
+ */
30
+ offset ?: DefaultTheme [ 'grid' ] [ 'breakpoints' ] ;
31
+
32
+ /**
33
+ * The children nodes
34
+ */
21
35
children : ReactNode ;
22
36
}
23
37
@@ -32,52 +46,48 @@ const baseStyle = ({ theme }: StyleProps) => css`
32
46
33
47
box-sizing : border-box;
34
48
35
- ${ BREAKPOINTS . map (
36
- ( breakpoint ) =>
37
- css `
38
- ${ media ( breakpoint ) } {
39
- padding-left : ${ config ( theme ) . grid . gutter [ breakpoint ] / 2 } rem;
40
- padding-right : ${ config ( theme ) . grid . gutter [ breakpoint ] / 2 } rem;
41
- }
42
- `
43
- ) }
49
+ ${ responsive (
50
+ config ( theme ) . grid . breakpoints ,
51
+ ( breakpoint : keyof DefaultTheme [ 'grid' ] [ 'breakpoints' ] ) => `
52
+ padding-left: ${ config ( theme ) . grid . gutter [ breakpoint ] / 2 } rem;
53
+ padding-right: ${ config ( theme ) . grid . gutter [ breakpoint ] / 2 } rem;
54
+ `
55
+ ) } ;
44
56
` ;
45
57
46
- const sizeStyle = ( props : ColProps & StyleProps ) => {
47
- const { theme } = props ;
48
-
49
- return css `
50
- ${ BREAKPOINTS . map (
51
- ( breakpoint ) =>
52
- props [ breakpoint ] &&
53
- css `
54
- ${ media ( breakpoint ) } {
55
- flex : 0 0
56
- ${ ( props [ breakpoint ] / config ( theme ) . grid . columns [ breakpoint ] ) *
57
- 100 } %;
58
- max-width : ${ ( props [ breakpoint ] /
59
- config ( theme ) . grid . columns [ breakpoint ] ) *
60
- 100 } %;
61
- }
62
- `
63
- ) }
64
- ` ;
65
- } ;
58
+ const sizeStyle = ( props : ColProps & StyleProps ) =>
59
+ responsive (
60
+ config ( props . theme ) . grid . breakpoints ,
61
+ ( breakpoint : keyof DefaultTheme [ 'grid' ] [ 'breakpoints' ] ) =>
62
+ props [ breakpoint ] &&
63
+ `
64
+ label: col--size;
65
+ flex: 0 0 ${
66
+ ( props [ breakpoint ] / config ( props . theme ) . grid . columns [ breakpoint ] ) *
67
+ 100
68
+ } %;
69
+ max-width: ${
70
+ ( props [ breakpoint ] / config ( props . theme ) . grid . columns [ breakpoint ] ) *
71
+ 100
72
+ } %;
73
+ `
74
+ ) ;
66
75
67
76
const offsetStyle = ( { theme, offset } : ColProps & StyleProps ) =>
68
77
offset &&
69
- typeof offset === 'object' &&
70
- BREAKPOINTS . filter ( ( breakpoint ) => offset [ breakpoint ] ) . map (
71
- ( breakpoint ) =>
78
+ responsive (
79
+ config ( theme ) . grid . breakpoints ,
80
+ ( breakpoint : keyof DefaultTheme [ 'grid' ] [ 'breakpoints' ] ) =>
72
81
offset [ breakpoint ] &&
73
- css `
74
- ${ media ( breakpoint ) } {
75
- margin-left: ${ offset [ breakpoint ] >= 0
82
+ `
83
+ label: col--offset;
84
+ margin-left: ${
85
+ offset [ breakpoint ] >= 0
76
86
? ( offset [ breakpoint ] / config ( theme ) . grid . columns [ breakpoint ] ) *
77
87
100
78
- : 0 } %;
79
- }
80
- `
88
+ : 0
89
+ } %;
90
+ `
81
91
) ;
82
92
83
93
const Col = styled ( 'div' , {
0 commit comments