Skip to content

Commit 62b0442

Browse files
authored
fix: Themed breakpoint not being applied (#62)
* refactor: enable theme to export custom breakpoints from custom theme * refactor: media query to accept custom width * refactor: row to use new media query syntax * refactor: col to use new media query syntax * Revert "refactor: media query to accept custom width" This reverts commit 37f4378. * refactor: remove unused function * refactor: update media query to accept props * refactor: call media query with props * fix: add breakpoint * test: update snapshots * refactor: use key function within theme
1 parent 04616c5 commit 62b0442

File tree

8 files changed

+78
-25
lines changed

8 files changed

+78
-25
lines changed

src/components/Col/Col.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const Col = styled(props =>
1313
flex: 0 0 auto;
1414
display: block;
1515
16-
${props => themeProvider.breakpoints.map(breakpoint => mediaQuery[breakpoint]`
16+
${props => themeProvider.breakpointsKeys(props).map(breakpoint => mediaQuery(props)[breakpoint]`
1717
// Generate gutter
1818
${gutter.col(props, breakpoint)}
1919

src/components/Col/__snapshots__/Col.spec.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ exports[`style rendering renders corrects 1`] = `
88
>
99
<Component
1010
alignSelf="auto"
11-
className="sc-bdVaJa kXNDzH"
11+
className="sc-bdVaJa jURNTC"
1212
elementType="div"
1313
order={0}
1414
>
1515
<div
16-
className="sc-bdVaJa kXNDzH"
16+
className="sc-bdVaJa jURNTC"
1717
/>
1818
</Component>
1919
</Col>

src/components/Row/Row.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const Row = styled(props =>
1111
// Initial component property
1212
box-sizing: border-box;
1313
14-
${props => themeProvider.breakpoints.map(breakpoint => mediaQuery[breakpoint]`
14+
${props => themeProvider.breakpointsKeys(props).map(breakpoint => mediaQuery(props)[breakpoint]`
1515
// Generate gutter
1616
${gutter.row(props, breakpoint)}
1717

src/components/Row/__snapshots__/Row.spec.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ exports[`style rendering renders corrects 1`] = `
1313
<Component
1414
alignContent="flex-start"
1515
alignItems="flex-start"
16-
className="sc-bdVaJa jgdJrf"
16+
className="sc-bdVaJa hXfgcI"
1717
display="flex"
1818
elementType="div"
1919
flexDirection="row"
2020
flexWrap="wrap"
2121
justifyContent="flex-start"
2222
>
2323
<div
24-
className="sc-bdVaJa jgdJrf"
24+
className="sc-bdVaJa hXfgcI"
2525
/>
2626
</Component>
2727
</Row>

src/helpers/mediaQuery/mediaQuery.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ import { themeProvider } from '../../theme';
44

55
const { theme } = themeProvider;
66

7-
const mediaQuery = Object.keys(theme().breakpoints).reduce((accumulator, value) => {
8-
const breakpointSize = theme().breakpoints[value];
7+
const mediaQuery = props =>
8+
(themeProvider.breakpointsKeys(props).reduce((accumulator, value) => {
9+
const breakpointSize = theme(props).breakpoints[value];
910

10-
Object.assign(accumulator, { [value]: (...args) =>
11-
(breakpointSize === 0 ? css`${css(...args)}` :
12-
css`@media (min-width: ${breakpointSize}rem) {
13-
${css(...args)}
14-
}`
15-
),
16-
});
11+
Object.assign(accumulator, { [value]: (...args) =>
12+
(breakpointSize === 0 ? css`${css(...args)}` :
13+
css`@media (min-width: ${breakpointSize}rem) {
14+
${css(...args)}
15+
}`
16+
),
17+
});
1718

18-
return accumulator;
19-
}, {});
19+
return accumulator;
20+
}, {})
21+
);
2022

2123
export default mediaQuery;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import mediaQuery from './mediaQuery';
22

33
test('should generate a media query for lg breakpoint', () => {
4-
const query = (mediaQuery.lg`color: red`).join('');
4+
const query = (mediaQuery().lg`color: red`).join('');
55
expect(query).toContain('@media (min-width: 60rem)');
66
expect(query).toContain('color: red');
77
});
88

99
test('should generate a media query for md breakpoint', () => {
10-
const query = (mediaQuery.md`color: blue`).join('');
10+
const query = (mediaQuery().md`color: blue`).join('');
1111
expect(query).toContain('@media (min-width: 48rem)');
1212
expect(query).toContain('color: blue');
1313
});
1414

1515
test('should generate a media query for sm breakpoint', () => {
16-
const query = (mediaQuery.sm`color: green`).join('');
16+
const query = (mediaQuery().sm`color: green`).join('');
1717
expect(query).toContain('@media (min-width: 30rem)');
1818
expect(query).toContain('color: green');
1919
});
2020

2121
test('should not generate a media query for xs breakpoint', () => {
22-
const query = (mediaQuery.xs`color: yellow`).join('');
22+
const query = (mediaQuery().xs`color: yellow`).join('');
2323
expect(query).not.toContain('@media (min-width: 0rem)');
2424
expect(query).toContain('color: yellow');
2525
});

src/theme/theme.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ export const theme = (props) => {
2222
});
2323
};
2424

25-
// TODO generate these dynamically based on the object keys for breakpoints
26-
export const breakpoints = ['xs', 'sm', 'md', 'lg'];
25+
export const breakpoints = props => theme(props).breakpoints;
26+
export const breakpointsKeys = props => Object.keys(theme(props).breakpoints);

src/theme/theme.spec.js

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { themeProvider, defaultTheme } from '../theme';
22

3-
const { theme, breakpoints } = themeProvider;
3+
const {
4+
theme,
5+
breakpoints,
6+
breakpointsKeys,
7+
} = themeProvider;
48

59

610
test('should set the default theme if a custom theme is not provided', () => {
711
expect(theme()).toEqual(defaultTheme);
8-
expect(breakpoints).toEqual(Object.keys(defaultTheme.breakpoints));
912
});
1013

1114
describe('overriding the default theme', () => {
@@ -52,3 +55,51 @@ describe('overriding the default theme', () => {
5255
expect(theme(customTheme)).not.toEqual(defaultTheme);
5356
});
5457
});
58+
59+
describe('breakpoint method', () => {
60+
const customTheme = {
61+
theme: { flexa: { breakpoints: { lg: 65, md: 55, sm: 45, xs: 35 } } },
62+
};
63+
64+
test('should return breakpoints of custom theme', () => {
65+
expect(breakpoints(customTheme)).toEqual(
66+
{ lg: 65, md: 55, sm: 45, xs: 35 },
67+
);
68+
});
69+
70+
test('should return custom breakpoints of custom theme with additional breakpoints', () => {
71+
const customBreakpoints = {
72+
theme: { flexa: { breakpoints: { lg: 65, md: 55, sm: 45, xs: 35, xl: 90, xxl: 120 } } },
73+
};
74+
expect(breakpoints(customBreakpoints)).toEqual(
75+
{ lg: 65, md: 55, sm: 45, xl: 90, xs: 35, xxl: 120 },
76+
);
77+
});
78+
79+
test('should return default breakpoints if no theme is provided', () => {
80+
expect(breakpoints()).toEqual(defaultTheme.breakpoints);
81+
});
82+
});
83+
84+
describe('breakpointsKeys method', () => {
85+
const customTheme = {
86+
theme: { flexa: { breakpoints: { md: 55, lg: 65, xs: 35, sm: 45 } } },
87+
};
88+
89+
test('should return breakpoints keys of custom theme', () => {
90+
expect(breakpointsKeys(customTheme)).toEqual(['xs', 'sm', 'md', 'lg']);
91+
});
92+
93+
test('should return custom breakpoints of custom theme with additional breakpoints', () => {
94+
const customBreakpoints = {
95+
theme: { flexa: { breakpoints: { lg: 65, md: 55, sm: 45, xs: 35, xl: 90, xxl: 120 } } },
96+
};
97+
expect(breakpointsKeys(customBreakpoints)).toEqual(
98+
['xs', 'sm', 'md', 'lg', 'xl', 'xxl'],
99+
);
100+
});
101+
102+
test('should return default breakpoints if no theme is provided', () => {
103+
expect(breakpointsKeys()).toEqual(Object.keys(defaultTheme.breakpoints));
104+
});
105+
});

0 commit comments

Comments
 (0)