Skip to content

Commit f5ab437

Browse files
committed
Better breakpoint utility
1 parent cc8e823 commit f5ab437

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/lib/breakpoints.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,35 @@ import {
44
breakpointValuesUnit as unit,
55
breakpointKeys as keys
66
} from "./generatedFromCss/breakpoints";
7+
import { assert } from "tsafe/assert";
78

89
export type { BreakpointKeys };
910

1011
const epsilon = 0.003125;
1112

1213
export const breakpoints = {
13-
"up": (key: BreakpointKeys) => `@media (min-width:${values[key]}${unit})` as const,
14-
"down": (key: BreakpointKeys) => `@media (max-width:${values[key] - epsilon}${unit})` as const,
14+
"up": (key: Exclude<BreakpointKeys, "xs">) =>
15+
`@media (min-width:${values[key]}${unit})` as const,
16+
"down": (key: Exclude<BreakpointKeys, "xs">) =>
17+
`@media (max-width:${values[key] - epsilon}${unit})` as const,
1518
"between": (start: BreakpointKeys, end: BreakpointKeys) =>
1619
`@media (min-width:${values[start]}${unit}) and (max-width:${
1720
values[end] - epsilon
1821
}${unit})` as const,
1922
"only": (key: BreakpointKeys) =>
2023
keys.indexOf(key) + 1 < keys.length
2124
? breakpoints.between(key, keys[keys.indexOf(key) + 1])
22-
: breakpoints.up(key),
25+
: breakpoints.up((assert(key !== "xs"), key)),
2326
"not": (key: BreakpointKeys) => {
2427
// handle first and last key separately, for better readability
2528
const keyIndex = keys.indexOf(key);
2629
if (keyIndex === 0) {
2730
return breakpoints.up(keys[1]);
2831
}
2932
if (keyIndex === keys.length - 1) {
30-
return breakpoints.down(keys[keyIndex]);
33+
const key = keys[keyIndex];
34+
assert(key !== "xs");
35+
return breakpoints.down(key);
3136
}
3237

3338
return breakpoints

0 commit comments

Comments
 (0)