Files
wdi-dashboard/node_modules/@chakra-ui/breakpoint-utils/dist/index.js
2024-08-16 15:06:52 +05:30

206 lines
6.2 KiB
JavaScript

"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
analyzeBreakpoints: () => analyzeBreakpoints,
arrayToObjectNotation: () => arrayToObjectNotation,
breakpoints: () => breakpoints,
isCustomBreakpoint: () => isCustomBreakpoint,
isResponsiveObjectLike: () => isResponsiveObjectLike,
mapResponsive: () => mapResponsive,
objectToArrayNotation: () => objectToArrayNotation,
px: () => px,
toMediaQueryString: () => toMediaQueryString
});
module.exports = __toCommonJS(src_exports);
// src/responsive.ts
var import_shared_utils = require("@chakra-ui/shared-utils");
var breakpoints = Object.freeze([
"base",
"sm",
"md",
"lg",
"xl",
"2xl"
]);
function mapResponsive(prop, mapper) {
if (Array.isArray(prop)) {
return prop.map((item) => item === null ? null : mapper(item));
}
if ((0, import_shared_utils.isObject)(prop)) {
return Object.keys(prop).reduce((result, key) => {
result[key] = mapper(prop[key]);
return result;
}, {});
}
if (prop != null) {
return mapper(prop);
}
return null;
}
function objectToArrayNotation(obj, bps = breakpoints) {
const result = bps.map((br) => {
var _a;
return (_a = obj[br]) != null ? _a : null;
});
const lastItem = result[result.length - 1];
while (lastItem === null)
result.pop();
return result;
}
function arrayToObjectNotation(values, bps = breakpoints) {
const result = {};
values.forEach((value, index) => {
const key = bps[index];
if (value == null)
return;
result[key] = value;
});
return result;
}
function isResponsiveObjectLike(obj, bps = breakpoints) {
const keys2 = Object.keys(obj);
return keys2.length > 0 && keys2.every((key) => bps.includes(key));
}
var isCustomBreakpoint = (v) => Number.isNaN(Number(v));
// src/breakpoint.ts
var import_shared_utils2 = require("@chakra-ui/shared-utils");
function getLastItem(array) {
const length = array == null ? 0 : array.length;
return length ? array[length - 1] : void 0;
}
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 || typeof value === "number" ? `${value}px` : value;
}
var sortByBreakpointValue = (a, b) => parseInt(a[1], 10) > parseInt(b[1], 10) ? 1 : -1;
var sortBps = (breakpoints2) => Object.fromEntries(Object.entries(breakpoints2).sort(sortByBreakpointValue));
function normalize(breakpoints2) {
const sorted = sortBps(breakpoints2);
return Object.assign(Object.values(sorted), sorted);
}
function keys(breakpoints2) {
const value = Object.keys(sortBps(breakpoints2));
return new Set(value);
}
function subtract(value) {
var _a;
if (!value)
return value;
value = (_a = px(value)) != null ? _a : value;
const OFFSET = -0.02;
return typeof value === "number" ? `${value + OFFSET}` : value.replace(/(\d+\.?\d*)/u, (m) => `${parseFloat(m) + OFFSET}`);
}
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(breakpoints2) {
var _a;
if (!breakpoints2)
return null;
breakpoints2.base = (_a = breakpoints2.base) != null ? _a : "0px";
const normalized = normalize(breakpoints2);
const queries = Object.entries(breakpoints2).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(breakpoints2);
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(breakpoints2),
asArray: normalize(breakpoints2),
details: queries,
get(key) {
return queries.find((q) => q.breakpoint === key);
},
media: [
null,
...normalized.map((minW) => toMediaQueryString(minW)).slice(1)
],
toArrayValue(test) {
if (!(0, import_shared_utils2.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,
arrayToObjectNotation,
breakpoints,
isCustomBreakpoint,
isResponsiveObjectLike,
mapResponsive,
objectToArrayNotation,
px,
toMediaQueryString
});