197 lines
6.1 KiB
JavaScript
197 lines
6.1 KiB
JavaScript
|
|
"use strict";
|
||
|
|
var __create = Object.create;
|
||
|
|
var __defProp = Object.defineProperty;
|
||
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||
|
|
var __getProtoOf = Object.getPrototypeOf;
|
||
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||
|
|
var __export = (target, all) => {
|
||
|
|
for (var name in all)
|
||
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
||
|
|
};
|
||
|
|
var __copyProps = (to, from, except, desc) => {
|
||
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
||
|
|
for (let key of __getOwnPropNames(from))
|
||
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
||
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||
|
|
}
|
||
|
|
return to;
|
||
|
|
};
|
||
|
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
||
|
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
||
|
|
mod
|
||
|
|
));
|
||
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||
|
|
|
||
|
|
// src/breakpoint.ts
|
||
|
|
var breakpoint_exports = {};
|
||
|
|
__export(breakpoint_exports, {
|
||
|
|
analyzeBreakpoints: () => analyzeBreakpoints,
|
||
|
|
px: () => px,
|
||
|
|
toMediaQueryString: () => toMediaQueryString
|
||
|
|
});
|
||
|
|
module.exports = __toCommonJS(breakpoint_exports);
|
||
|
|
|
||
|
|
// src/array.ts
|
||
|
|
function getLastItem(array) {
|
||
|
|
const length = array == null ? 0 : array.length;
|
||
|
|
return length ? array[length - 1] : void 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
// src/assertion.ts
|
||
|
|
function isNumber(value) {
|
||
|
|
return typeof value === "number";
|
||
|
|
}
|
||
|
|
function isArray(value) {
|
||
|
|
return Array.isArray(value);
|
||
|
|
}
|
||
|
|
function isObject(value) {
|
||
|
|
const type = typeof value;
|
||
|
|
return value != null && (type === "object" || type === "function") && !isArray(value);
|
||
|
|
}
|
||
|
|
var __DEV__ = process.env.NODE_ENV !== "production";
|
||
|
|
var __TEST__ = process.env.NODE_ENV === "test";
|
||
|
|
|
||
|
|
// src/object.ts
|
||
|
|
var import_lodash = __toESM(require("lodash.mergewith"));
|
||
|
|
function get(obj, path, fallback, index) {
|
||
|
|
const key = typeof path === "string" ? path.split(".") : [path];
|
||
|
|
for (index = 0; index < key.length; index += 1) {
|
||
|
|
if (!obj)
|
||
|
|
break;
|
||
|
|
obj = obj[key[index]];
|
||
|
|
}
|
||
|
|
return obj === void 0 ? fallback : obj;
|
||
|
|
}
|
||
|
|
var memoize = (fn) => {
|
||
|
|
const cache = /* @__PURE__ */ new WeakMap();
|
||
|
|
const memoizedFn = (obj, path, fallback, index) => {
|
||
|
|
if (typeof obj === "undefined") {
|
||
|
|
return fn(obj, path, fallback);
|
||
|
|
}
|
||
|
|
if (!cache.has(obj)) {
|
||
|
|
cache.set(obj, /* @__PURE__ */ new Map());
|
||
|
|
}
|
||
|
|
const map = cache.get(obj);
|
||
|
|
if (map.has(path)) {
|
||
|
|
return map.get(path);
|
||
|
|
}
|
||
|
|
const value = fn(obj, path, fallback, index);
|
||
|
|
map.set(path, value);
|
||
|
|
return value;
|
||
|
|
};
|
||
|
|
return memoizedFn;
|
||
|
|
};
|
||
|
|
var memoizedGet = memoize(get);
|
||
|
|
var fromEntries = (entries) => entries.reduce((carry, [key, value]) => {
|
||
|
|
carry[key] = value;
|
||
|
|
return carry;
|
||
|
|
}, {});
|
||
|
|
|
||
|
|
// src/breakpoint.ts
|
||
|
|
function analyzeCSSValue(value) {
|
||
|
|
const num = parseFloat(value.toString());
|
||
|
|
const unit = value.toString().replace(String(num), "");
|
||
|
|
return { unitless: !unit, value: num, unit };
|
||
|
|
}
|
||
|
|
function px(value) {
|
||
|
|
if (value == null)
|
||
|
|
return value;
|
||
|
|
const { unitless } = analyzeCSSValue(value);
|
||
|
|
return unitless || isNumber(value) ? `${value}px` : value;
|
||
|
|
}
|
||
|
|
var sortByBreakpointValue = (a, b) => parseInt(a[1], 10) > parseInt(b[1], 10) ? 1 : -1;
|
||
|
|
var sortBps = (breakpoints) => fromEntries(Object.entries(breakpoints).sort(sortByBreakpointValue));
|
||
|
|
function normalize(breakpoints) {
|
||
|
|
const sorted = sortBps(breakpoints);
|
||
|
|
return Object.assign(Object.values(sorted), sorted);
|
||
|
|
}
|
||
|
|
function keys(breakpoints) {
|
||
|
|
const value = Object.keys(sortBps(breakpoints));
|
||
|
|
return new Set(value);
|
||
|
|
}
|
||
|
|
function subtract(value) {
|
||
|
|
var _a;
|
||
|
|
if (!value)
|
||
|
|
return value;
|
||
|
|
value = (_a = px(value)) != null ? _a : value;
|
||
|
|
const factor = value.endsWith("px") ? -0.02 : -0.01;
|
||
|
|
return isNumber(value) ? `${value + factor}` : value.replace(/(\d+\.?\d*)/u, (m) => `${parseFloat(m) + factor}`);
|
||
|
|
}
|
||
|
|
function toMediaQueryString(min, max) {
|
||
|
|
const query = ["@media screen"];
|
||
|
|
if (min)
|
||
|
|
query.push("and", `(min-width: ${px(min)})`);
|
||
|
|
if (max)
|
||
|
|
query.push("and", `(max-width: ${px(max)})`);
|
||
|
|
return query.join(" ");
|
||
|
|
}
|
||
|
|
function analyzeBreakpoints(breakpoints) {
|
||
|
|
var _a;
|
||
|
|
if (!breakpoints)
|
||
|
|
return null;
|
||
|
|
breakpoints.base = (_a = breakpoints.base) != null ? _a : "0px";
|
||
|
|
const normalized = normalize(breakpoints);
|
||
|
|
const queries = Object.entries(breakpoints).sort(sortByBreakpointValue).map(([breakpoint, minW], index, entry) => {
|
||
|
|
var _a2;
|
||
|
|
let [, maxW] = (_a2 = entry[index + 1]) != null ? _a2 : [];
|
||
|
|
maxW = parseFloat(maxW) > 0 ? subtract(maxW) : void 0;
|
||
|
|
return {
|
||
|
|
_minW: subtract(minW),
|
||
|
|
breakpoint,
|
||
|
|
minW,
|
||
|
|
maxW,
|
||
|
|
maxWQuery: toMediaQueryString(null, maxW),
|
||
|
|
minWQuery: toMediaQueryString(minW),
|
||
|
|
minMaxQuery: toMediaQueryString(minW, maxW)
|
||
|
|
};
|
||
|
|
});
|
||
|
|
const _keys = keys(breakpoints);
|
||
|
|
const _keysArr = Array.from(_keys.values());
|
||
|
|
return {
|
||
|
|
keys: _keys,
|
||
|
|
normalized,
|
||
|
|
isResponsive(test) {
|
||
|
|
const keys2 = Object.keys(test);
|
||
|
|
return keys2.length > 0 && keys2.every((key) => _keys.has(key));
|
||
|
|
},
|
||
|
|
asObject: sortBps(breakpoints),
|
||
|
|
asArray: normalize(breakpoints),
|
||
|
|
details: queries,
|
||
|
|
media: [
|
||
|
|
null,
|
||
|
|
...normalized.map((minW) => toMediaQueryString(minW)).slice(1)
|
||
|
|
],
|
||
|
|
toArrayValue(test) {
|
||
|
|
if (!isObject(test)) {
|
||
|
|
throw new Error("toArrayValue: value must be an object");
|
||
|
|
}
|
||
|
|
const result = _keysArr.map((bp) => {
|
||
|
|
var _a2;
|
||
|
|
return (_a2 = test[bp]) != null ? _a2 : null;
|
||
|
|
});
|
||
|
|
while (getLastItem(result) === null) {
|
||
|
|
result.pop();
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
},
|
||
|
|
toObjectValue(test) {
|
||
|
|
if (!Array.isArray(test)) {
|
||
|
|
throw new Error("toObjectValue: value must be an array");
|
||
|
|
}
|
||
|
|
return test.reduce((acc, value, index) => {
|
||
|
|
const key = _keysArr[index];
|
||
|
|
if (key != null && value != null)
|
||
|
|
acc[key] = value;
|
||
|
|
return acc;
|
||
|
|
}, {});
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}
|
||
|
|
// Annotate the CommonJS export names for ESM import in node:
|
||
|
|
0 && (module.exports = {
|
||
|
|
analyzeBreakpoints,
|
||
|
|
px,
|
||
|
|
toMediaQueryString
|
||
|
|
});
|