-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add breakpoints to theme
- Loading branch information
Showing
6 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
export enum BreakpointKey { | ||
zero = 'zero', | ||
s = 's', | ||
m = 'm', | ||
l = 'l', | ||
xl = 'xl', | ||
} | ||
|
||
export type BreakpointFnParam = BreakpointKey | keyof typeof BreakpointKey; | ||
|
||
export type CanvasBreakpoints = { | ||
zero: number; | ||
s: number; | ||
m: number; | ||
l: number; | ||
xl: number; | ||
[key: string]: number; | ||
}; | ||
|
||
export const breakpointKeys = ['zero', 's', 'm', 'l', 'xl']; | ||
|
||
export const breakpoints: CanvasBreakpoints = { | ||
zero: 0, | ||
s: 600, | ||
m: 960, | ||
l: 1280, | ||
xl: 1920, | ||
}; | ||
|
||
const step = 0.5; | ||
|
||
export function up(key: BreakpointFnParam) { | ||
const value = typeof breakpoints[key as BreakpointKey] === 'number' ? breakpoints[key] : key; | ||
return `@media (min-width:${value}px)`; | ||
} | ||
|
||
export function down(key: BreakpointFnParam) { | ||
const endIndex = breakpointKeys.indexOf(key as BreakpointKey) + 1; | ||
const upperbound = breakpoints[breakpointKeys[endIndex]]; | ||
|
||
if (endIndex === breakpointKeys.length) { | ||
// xl down applies to all sizes | ||
return up(BreakpointKey.zero); | ||
} | ||
|
||
const value = typeof upperbound === 'number' && endIndex > 0 ? upperbound : 0; | ||
return `@media (max-width:${value - step}px)`; | ||
} | ||
|
||
export function between(start: BreakpointFnParam, end: BreakpointFnParam) { | ||
const endIndex = breakpointKeys.indexOf(end) + 1; | ||
|
||
if (endIndex === breakpointKeys.length) { | ||
return up(start); | ||
} | ||
|
||
return ( | ||
`@media (min-width:${breakpoints[start]}px) and ` + | ||
`(max-width:${breakpoints[breakpointKeys[endIndex]] - step}px)` | ||
); | ||
} | ||
|
||
export function only(key: BreakpointFnParam) { | ||
return between(key, key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {BreakpointKey, up, down, between, only} from '../lib/theming/breakpoints'; | ||
|
||
describe('Breakpoints', () => { | ||
test('up function works with enum', () => { | ||
const mediaQuery = up(BreakpointKey.m); | ||
|
||
expect(mediaQuery).toBe('@media (min-width:960px)'); | ||
}); | ||
|
||
test('up function works with string', () => { | ||
const mediaQuery = up('m'); | ||
|
||
expect(mediaQuery).toBe('@media (min-width:960px)'); | ||
}); | ||
|
||
test('down function works with enum', () => { | ||
const mediaQuery = down(BreakpointKey.m); | ||
|
||
expect(mediaQuery).toBe('@media (max-width:1279.5px)'); | ||
}); | ||
|
||
test('down function works with string', () => { | ||
const mediaQuery = down('m'); | ||
|
||
expect(mediaQuery).toBe('@media (max-width:1279.5px)'); | ||
}); | ||
|
||
test('between function works with enums', () => { | ||
const mediaQuery = between(BreakpointKey.m, BreakpointKey.l); | ||
|
||
expect(mediaQuery).toBe('@media (min-width:960px) and (max-width:1919.5px)'); | ||
}); | ||
|
||
test('between function works with string', () => { | ||
const mediaQuery = between('m', 'l'); | ||
|
||
expect(mediaQuery).toBe('@media (min-width:960px) and (max-width:1919.5px)'); | ||
}); | ||
|
||
test('only function works with enums', () => { | ||
const mediaQuery = only(BreakpointKey.m); | ||
|
||
expect(mediaQuery).toBe('@media (min-width:960px) and (max-width:1279.5px)'); | ||
}); | ||
|
||
test('between function works with string', () => { | ||
const mediaQuery = only('m'); | ||
|
||
expect(mediaQuery).toBe('@media (min-width:960px) and (max-width:1279.5px)'); | ||
}); | ||
}); |