Skip to content

Instantly share code, notes, and snippets.

@mg901
Created November 8, 2023 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mg901/f7a5247ef6fb7afd5e196579abf884ce to your computer and use it in GitHub Desktop.
Save mg901/f7a5247ef6fb7afd5e196579abf884ce to your computer and use it in GitHub Desktop.
exports.createBreakpointsApi = ({ breakpoints }) => {
const breakpointsObject = Object(breakpoints);
const keys = Object.keys(breakpointsObject);
const indexMap = {};
keys.forEach((key, index) => {
indexMap[key] = index;
});
const up = (min) => breakpointsObject[min];
const down = (max) => calcMaxWidth(breakpointsObject[max]);
const getNextKey = (key) => {
const currentIndex = indexMap[key];
return currentIndex < keys.length - 1 ? keys[currentIndex + 1] : undefined;
};
const between = (min, max) => ({
min: up(min),
max: down(max),
});
const only = (key) =>
key === keys[keys.length - 1] ? up(key) : between(key, getNextKey(key));
return {
keys,
getNextKey,
up,
down,
between,
only,
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment