Skip to content
This repository was archived by the owner on Jun 12, 2025. It is now read-only.

Commit 46f2b5d

Browse files
authored
Merge pull request #191 from mverissimo/perf/responsive
Perf/responsive
2 parents 7ef24e3 + 67f5e51 commit 46f2b5d

File tree

13 files changed

+322
-339
lines changed

13 files changed

+322
-339
lines changed

src/components/Col/Col.test.tsx

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Col } from '.';
44
import { Breakpoints } from '../../types/emotion';
55

66
describe('Col', () => {
7-
it('should render with default styles on media %s', () => {
7+
it('should render with default styles', () => {
88
const {
99
container: { firstChild },
1010
} = render(<Col>Col</Col>);
@@ -21,7 +21,7 @@ describe('Col', () => {
2121
xl: 5,
2222
},
2323
] as Record<Breakpoints, number>[])(
24-
'should render with width styles on media %s',
24+
'should render with size styles on breakpoint %s',
2525
(size) => {
2626
const {
2727
container: { firstChild },
@@ -40,34 +40,11 @@ describe('Col', () => {
4040
xl: 5,
4141
},
4242
] as Record<Breakpoints, number>[])(
43-
'should render with offset styles on media %s',
44-
(size) => {
45-
const {
46-
container: { firstChild },
47-
} = render(<Col offset={size}>Col</Col>);
48-
49-
expect(firstChild).toMatchSnapshot();
50-
}
51-
);
52-
53-
it.each([
54-
{
55-
xs: 1,
56-
sm: 2,
57-
md: 3,
58-
lg: 4,
59-
xl: 5,
60-
},
61-
] as Record<Breakpoints, number>[])(
62-
'should handle with offset styles on media %s',
43+
'should render with offset styles on breakpoint %s',
6344
(offset) => {
6445
const {
6546
container: { firstChild },
66-
} = render(
67-
<Col md={8} offset={offset}>
68-
Col
69-
</Col>
70-
);
47+
} = render(<Col {...offset}>Col</Col>);
7148

7249
expect(firstChild).toMatchSnapshot();
7350
}

src/components/Col/Col.tsx

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
1-
import { CSSProperties, ReactNode } from 'react';
1+
import { ReactNode } from 'react';
22

33
import styled from '@emotion/styled';
44
import { css } from '@emotion/react';
55
import isPropValid from '@emotion/is-prop-valid';
66

7-
import { config, BREAKPOINTS } from '../../config';
8-
import { media } from '../../utils';
7+
import { config } from '../../config';
8+
import { responsive } from '../../utils';
99

10-
import { Breakpoints, StyleProps } from '../../types/emotion';
10+
import { DefaultTheme, StyleProps } from '../../types/emotion';
1111

1212
export interface ColProps {
13+
/**
14+
* Trigger column styles
15+
* based on breakpoint
16+
*/
1317
xs?: number;
18+
1419
sm?: number;
20+
1521
md?: number;
22+
1623
lg?: number;
24+
1725
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+
*/
2135
children: ReactNode;
2236
}
2337

@@ -32,52 +46,48 @@ const baseStyle = ({ theme }: StyleProps) => css`
3246
3347
box-sizing: border-box;
3448
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+
)};
4456
`;
4557

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+
);
6675

6776
const offsetStyle = ({ theme, offset }: ColProps & StyleProps) =>
6877
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']) =>
7281
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
7686
? (offset[breakpoint] / config(theme).grid.columns[breakpoint]) *
7787
100
78-
: 0}%;
79-
}
80-
`
88+
: 0
89+
}%;
90+
`
8191
);
8292

8393
const Col = styled('div', {

0 commit comments

Comments
 (0)