@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
45 lines (44 loc) • 2.11 kB
JavaScript
// packages/widget-dashboard/src/utils/resolve-dashboard-column-count/resolve-dashboard-column-count.ts
/**
* Resolves the column count a host asked for. A finite number is floored,
* with a floor of one and no ceiling; a map keeps its finite positive
* entries, keyed by container width, each count floored the same way, and
* an `'auto'` step only when a floor is set, since the surface skips it
* otherwise. Anything else, or a map left with no entries, is unset. How
* many columns a surface can carry is the host's decision, not the
* package's.
*
* @param columns Host-provided `gridSettings.columns`, if any.
* @param minColumnWidth Host-provided `gridSettings.minColumnWidth`, if any.
* @return The count, the steps, or `undefined` when the host set none.
*/
function resolveDashboardColumns(columns, minColumnWidth) {
if (typeof columns === "number") return Number.isFinite(columns) ? Math.max(1, Math.floor(columns)) : void 0;
if (typeof columns !== "object" || columns === null) return;
const hasFloor = resolveDashboardMinColumnWidth(minColumnWidth) !== void 0;
const steps = {};
for (const [width, count] of Object.entries(columns)) {
const from = Number(width);
if (!Number.isFinite(from) || from < 0) continue;
if (count === "auto") {
if (hasFloor) steps[from] = "auto";
continue;
}
if (typeof count !== "number" || !Number.isFinite(count)) continue;
steps[from] = Math.max(1, Math.floor(count));
}
return Object.keys(steps).length > 0 ? steps : void 0;
}
/**
* Resolves the per-column floor a host asked for. A finite positive value is
* the floor in pixels; anything else leaves it unset.
*
* @param minColumnWidth Host-provided `gridSettings.minColumnWidth`, if any.
* @return The floor in pixels, or `undefined` when the host set none.
*/
function resolveDashboardMinColumnWidth(minColumnWidth) {
if (typeof minColumnWidth !== "number" || !Number.isFinite(minColumnWidth) || minColumnWidth <= 0) return;
return minColumnWidth;
}
export { resolveDashboardColumns, resolveDashboardMinColumnWidth };
//# sourceMappingURL=resolve-dashboard-column-count.mjs.map