-
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
4 changed files
with
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
export enum BreakpointKey { | ||
zero = 'zero', | ||
s = 's', | ||
m = 'm', | ||
l = 'l', | ||
xl = 'xl', | ||
} | ||
|
||
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: BreakpointKey | Number) { | ||
const value = | ||
typeof breakpoints[key as BreakpointKey] === 'number' ? breakpoints[key as BreakpointKey] : key; | ||
return `@media (min-width:${value}px)`; | ||
} | ||
|
||
export function down(key: BreakpointKey | Number) { | ||
if (typeof key === 'number') { | ||
return `@media (max-width:${key - step}px)`; | ||
} | ||
|
||
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: BreakpointKey | Number, end: BreakpointKey | Number) { | ||
const startValue = typeof start === 'number' ? start : breakpoints[start as BreakpointKey]; | ||
let endValue; | ||
|
||
if (typeof end === 'number') { | ||
endValue = end; | ||
} else { | ||
const endIndex = breakpointKeys.indexOf(end as BreakpointKey) + 1; | ||
|
||
if (endIndex === breakpointKeys.length) { | ||
return up(start); | ||
} | ||
endValue = breakpoints[breakpointKeys[endIndex]]; | ||
} | ||
|
||
return `@media (min-width:${startValue}px) and ` + `(max-width:${endValue - step}px)`; | ||
} | ||
|
||
export function only(key: BreakpointKey | Number) { | ||
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