42 lines
1.8 KiB
JavaScript
42 lines
1.8 KiB
JavaScript
|
export const filterBreakpointKeys = (breakpointsKeys, responsiveKeys) => breakpointsKeys.filter(key => responsiveKeys.includes(key));
|
||
|
export const traverseBreakpoints = (breakpoints, responsive, iterator) => {
|
||
|
const smallestBreakpoint = breakpoints.keys[0]; // the keys is sorted from smallest to largest by `createBreakpoints`.
|
||
|
|
||
|
if (Array.isArray(responsive)) {
|
||
|
responsive.forEach((breakpointValue, index) => {
|
||
|
iterator((responsiveStyles, style) => {
|
||
|
if (index <= breakpoints.keys.length - 1) {
|
||
|
if (index === 0) {
|
||
|
Object.assign(responsiveStyles, style);
|
||
|
} else {
|
||
|
responsiveStyles[breakpoints.up(breakpoints.keys[index])] = style;
|
||
|
}
|
||
|
}
|
||
|
}, breakpointValue);
|
||
|
});
|
||
|
} else if (responsive && typeof responsive === 'object') {
|
||
|
// prevent null
|
||
|
// responsive could be a very big object, pick the smallest responsive values
|
||
|
|
||
|
const keys = Object.keys(responsive).length > breakpoints.keys.length ? breakpoints.keys : filterBreakpointKeys(breakpoints.keys, Object.keys(responsive));
|
||
|
keys.forEach(key => {
|
||
|
if (breakpoints.keys.indexOf(key) !== -1) {
|
||
|
// @ts-ignore already checked that responsive is an object
|
||
|
const breakpointValue = responsive[key];
|
||
|
if (breakpointValue !== undefined) {
|
||
|
iterator((responsiveStyles, style) => {
|
||
|
if (smallestBreakpoint === key) {
|
||
|
Object.assign(responsiveStyles, style);
|
||
|
} else {
|
||
|
responsiveStyles[breakpoints.up(key)] = style;
|
||
|
}
|
||
|
}, breakpointValue);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
} else if (typeof responsive === 'number' || typeof responsive === 'string') {
|
||
|
iterator((responsiveStyles, style) => {
|
||
|
Object.assign(responsiveStyles, style);
|
||
|
}, responsive);
|
||
|
}
|
||
|
};
|