A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.
fix(board): preserve square tiles inside categories (#7265)
Thomas9af90df8ee38
diff --git a/apps/nextjs/src/components/board/items/item-move-modal.tsx b/apps/nextjs/src/components/board/items/item-move-modal.tsxindex 3c42b44b5..dd0f1c2d7 100644--- a/apps/nextjs/src/components/board/items/item-move-modal.tsx+++ b/apps/nextjs/src/components/board/items/item-move-modal.tsx@@ -20,7 +20,8 @@ import { useGridLayoutActions } from "../sections/grid/use-grid-layout-actions";export type MovableEntry = Pick<SectionItem | ContainerSectionItem,"id" | "type" | "width" | "height" | "xOffset" | "yOffset"->;+> &+ Pick<SectionGridPlacement, "minW" | "minH">;export interface OpenItemMoveModalInput {entry: MovableEntry;@@ -31,7 +32,7 @@ interface InnerProps extends OpenItemMoveModalInput {board: Board;currentLayoutId: string;commitSectionGrids: (snapshots: readonly CommitSectionGridInput[]) => void;- registeredGridEditors: ReadonlyMap<string, Pick<RegisteredGridEditor, "placementMaxRowCount">>;+ registeredGridEditors: ReadonlyMap<string, RegisteredGridEditor>;}interface MoveTarget {@@ -125,9 +126,10 @@ export const ItemMoveModal = createModal<InnerProps>(({ actions, innerProps }) =w: width,h: height,};- const targetPlacements = getSectionGridPlacements(board, currentLayoutId, target.id).filter(- (placement) => placement.id !== entry.id,- );+ const targetPlacements = (+ registeredGridEditors.get(target.id)?.transactionPlacements ??+ getSectionGridPlacements(board, currentLayoutId, target.id)+ ).filter((placement) => placement.id !== entry.id);const resolvedTarget = resolvePinnedGridCollisions([...targetPlacements, nextPlacement],nextPlacement,@@ -145,9 +147,10 @@ export const ItemMoveModal = createModal<InnerProps>(({ actions, innerProps }) ={layoutId: currentLayoutId,sectionId: sourceSectionId,- placements: getSectionGridPlacements(board, currentLayoutId, sourceSectionId).filter(- (placement) => placement.id !== entry.id,- ),+ placements: (+ registeredGridEditors.get(sourceSectionId)?.transactionPlacements ??+ getSectionGridPlacements(board, currentLayoutId, sourceSectionId)+ ).filter((placement) => placement.id !== entry.id),},{ layoutId: currentLayoutId, sectionId: target.id, placements: resolvedTarget },];@@ -164,6 +167,7 @@ export const ItemMoveModal = createModal<InnerProps>(({ actions, innerProps }) =entry,form,minimumSize,+ registeredGridEditors,sourceSectionId,targetById,tMoveResize,@@ -242,8 +246,12 @@ export const useOpenItemMoveModal = () => {return useCallback((input: OpenItemMoveModalInput) => {+ const placement = registeredGridEditors+ .get(input.sourceSectionId)+ ?.transactionPlacements.find((candidate) => candidate.id === input.entry.id);openModal({...input,+ entry: { ...input.entry, minW: placement?.minW, minH: placement?.minH },board,currentLayoutId,commitSectionGrids,@@ -259,7 +267,7 @@ export const getMoveTargets = (layoutId: string,entry: MovableEntry,labels: MoveTargetLabels,- registeredGridEditors?: ReadonlyMap<string, { placementMaxRowCount: number | null }>,+ registeredGridEditors?: ReadonlyMap<string, RegisteredGridEditor>,): MoveTarget[] => {const minimum = getEntryMinimumSize(board, layoutId, entry);@@ -284,10 +292,11 @@ export const getMoveTargets = (const layout = section.layouts.find((candidate) => candidate.layoutId === layoutId);if (!layout) return [];+ const maxRowCount = registeredGridEditors?.get(section.id)?.placementMaxRowCount ?? layout.height;if ((entry.type === "section" && isSameOrDescendant(board, layoutId, section.id, entry.id)) ||minimum.width > layout.width ||- minimum.height > layout.height+ minimum.height > maxRowCount) {return [];}@@ -297,7 +306,7 @@ export const getMoveTargets = (id: section.id,name: section.options.title || labels.container,columnCount: layout.width,- maxRowCount: layout.height,+ maxRowCount,},];});@@ -332,8 +341,8 @@ const getEntryMinimumSize = (board: Board, layoutId: string, entry: MovableEntryif (entry.type === "item") return { width: 1, height: 1 };const children = getSectionGridPlacements(board, layoutId, entry.id);return {- width: Math.max(1, ...children.map((child) => child.x + child.w)),- height: Math.max(1, ...children.map((child) => child.y + child.h)),+ width: entry.minW ?? Math.max(1, ...children.map((child) => child.x + child.w)),+ height: entry.minH ?? Math.max(1, ...children.map((child) => child.y + child.h)),};};diff --git a/apps/nextjs/src/components/board/sections/grid/fixed-grid-item.tsx b/apps/nextjs/src/components/board/sections/grid/fixed-grid-item.tsxindex e4d6338a0..d79f8e7f4 100644--- a/apps/nextjs/src/components/board/sections/grid/fixed-grid-item.tsx+++ b/apps/nextjs/src/components/board/sections/grid/fixed-grid-item.tsx@@ -155,8 +155,8 @@ export const FixedGridItem = ({direction: "se",deltaColumns: delta.x,deltaRows: delta.y,- minWidth,- minHeight,+ minWidth: current.minW ?? minWidth,+ minHeight: current.minH ?? minHeight,}): previewGridMove(transaction, {targetGridId: section.id,diff --git a/apps/nextjs/src/components/board/sections/grid/section-grid.tsx b/apps/nextjs/src/components/board/sections/grid/section-grid.tsxindex 8d5ad8bb9..2577ec3b8 100644--- a/apps/nextjs/src/components/board/sections/grid/section-grid.tsx+++ b/apps/nextjs/src/components/board/sections/grid/section-grid.tsx@@ -7,6 +7,7 @@ import combineClasses from "clsx";import { useCurrentLayout, useRequiredBoard } from "@homarr/boards/context";import { useEditMode } from "@homarr/boards/edit-mode";+import { getBoardLaneColumnCount, getRootSectionLane } from "@homarr/definitions";import type { ContainerSectionItem, Section } from "~/app/[locale]/boards/_types";import {@@ -18,7 +19,6 @@ import {getLogicalGridSize,getReadonlyCanvasAttributes,LOGICAL_GRID_PITCH,- normalizeGridPlacement,} from "~/components/board/layout";import { calculateBoardUiScale, useBoardCanvasScale } from "~/components/board/layout/scaled-board-canvas";import { useGridEditorRuntimeStatus } from "./grid-editor-runtime";@@ -32,7 +32,6 @@ import { useBoardGridPortalHost } from "./grid-portal-host";import classes from "./section-grid.module.css";const GridContentScaleContext = createContext(1);-const GridRowScaleContext = createContext(1);const CONTAINER_CARD_INSET = 5;interface SectionGridProps {@@ -59,7 +58,6 @@ export const SectionGrid = ({const [isEditMode] = useEditMode();const canvasScale = useBoardCanvasScale();const parentContentScale = useContext(GridContentScaleContext);- const parentRowScale = useContext(GridRowScaleContext);const [collapsedRowsPerPixel, setCollapsedRowsPerPixel] = useState<number>();const editorRuntimeStatus = useGridEditorRuntimeStatus();const editorRegistry = useGridEditorRegistry();@@ -74,19 +72,20 @@ export const SectionGrid = ({const { announce, integrations } = useBoardGridPortalHost();const collapsedSectionIds = useCollapsedSectionIds();const expandSectionsForEditing = useExpandSectionsForEditing();- const minimumBySectionId = useMemo(() => {- const minimumSizes = getContainerMinimumSizes(board, currentLayoutId);- return new Map(innerSections.map((innerSection) => [innerSection.id, minimumSizes.get(innerSection.id)]));- }, [board, currentLayoutId, innerSections]);+ const minimumBySectionId = useMemo(+ () => getContainerMinimumSizes(board, currentLayoutId, canvasScale),+ [board, canvasScale, currentLayoutId],+ );+ // Resolve expanded footprints before rendering or editing so header growth cannot overlap neighbours.const placements = useMemo(() =>- [...items, ...innerSections].map((item): SectionGridPlacement => {- const minimum = item.type === "section" ? minimumBySectionId.get(item.id) : undefined;- const isNonScrollableContainer = item.type === "section" && !item.options.scrollable;- const height = isNonScrollableContainer && minimum ? Math.max(item.height, minimum.height) : item.height;- return normalizeGridPlacement(- {+ getCollapsedDisplayLayout(+ [...items, ...innerSections].map((item): SectionGridPlacement => {+ const minimum = item.type === "section" ? minimumBySectionId.get(item.id) : undefined;+ const isNonScrollableContainer = item.type === "section" && !item.options.scrollable;+ const height = isNonScrollableContainer && minimum ? Math.max(item.height, minimum.height) : item.height;+ return {id: item.id,type: item.type,x: item.xOffset,@@ -95,10 +94,10 @@ export const SectionGrid = ({h: height,minW: minimum?.width,minH: minimum?.height,- },- columnCount,- );- }),+ };+ }),+ { columnCount, collapsedItemIds: new Set() },+ ),[columnCount, innerSections, items, minimumBySectionId],);const collapsibleSectionIds = useMemo(@@ -123,7 +122,6 @@ export const SectionGrid = ({),[collapsedSectionIds, collapsibleSectionIds, placements],);- const hasCollapsedHeader = innerSections.some((child) => child.options.showLabel && directCollapsedIds.has(child.id));const displayPlacements = useMemo(() => {if (directCollapsedIds.size === 0) return placements;@@ -169,17 +167,9 @@ export const SectionGrid = ({if (isRail) {rowCount = Math.max(railViewportRowCount, contentRowCount);}- const maxRowCount = section.kind === "container" ? rowCount : null;- const placementMaxRowCount = maxRowCount;- // A scrollable container isn't forced to grow with its content - it scrolls internally instead- // of expanding to fit every widget, so its viewport height is capped independently of rowCount.const isScrollableContainer = section.kind === "container" && section.options.scrollable;- const viewportRowCount =- viewportRowCountOverride ?? (isScrollableContainer ? Math.max(requestedRowCount, 1) : rowCount);const parentScale = canvasScale * parentContentScale;const effectiveCanvasScale = Number.isFinite(parentScale) && parentScale > 0 ? parentScale : 1;- // Match the card's inset without changing its persisted grid footprint. Include ancestor- // container zoom so equally sized nested containers keep distinct borders at every depth.let outerCardInset = 0;let outerCardInlineInset = 0;if (section.kind === "container") {@@ -187,14 +177,6 @@ export const SectionGrid = ({outerCardInlineInset = (containerInlineInsetCount * CONTAINER_CARD_INSET) / effectiveCanvasScale;}const fullGridWidth = getLogicalGridSize(columnCount);- const fullGridHeight = getLogicalGridSize(rowCount);- const fullViewportHeight = getLogicalGridSize(viewportRowCount);- let allocatedViewportHeight = fullViewportHeight;- if (isRail && railLogicalHeight > 0) {- allocatedViewportHeight = railLogicalHeight;- } else if (section.kind === "container") {- allocatedViewportHeight *= parentRowScale;- }const logicalWidth = Math.max(1, fullGridWidth - outerCardInlineInset);const hasContainerHeader = section.kind === "container" && section.options.collapsible && section.options.showLabel;let headerInset = 0;@@ -215,19 +197,29 @@ export const SectionGrid = ({// Keep the extra gap whenever either adjacent header still floats over its border.nestedHeaderInset = (20 * calculateBoardUiScale(canvasScale)) / effectiveCanvasScale;}- nestedHeaderInset = Math.min(nestedHeaderInset, Math.max(0, allocatedViewportHeight - outerCardInset - 1));- const viewportHeight = Math.max(1, allocatedViewportHeight - outerCardInset - nestedHeaderInset - headerInset);- // Fit columns to the card width. Rows fit the available height independently- // without creating horizontal gutters or distorting text and icons.+ let viewportHeight = getLogicalGridSize(rowCount);let containerContentScale = 1;let rowScale = 1;- if (section.kind === "container" && fullGridWidth > 0 && fullViewportHeight > 0) {+ if (section.kind === "container") {containerContentScale = Math.max(0.01, Math.min(logicalWidth / fullGridWidth, 1));- rowScale = Math.max(0.01, viewportHeight / (fullViewportHeight * containerContentScale));- } else if (isRail && railLogicalHeight > 0 && rowCount <= railViewportRowCount && !hasCollapsedHeader) {- // Keep fixed-height collapsed headers out of the rail's fill compression.- rowScale = viewportHeight / fullViewportHeight;+ const allocatedHeight = getLogicalGridSize(viewportRowCountOverride ?? Math.max(requestedRowCount, 1));+ nestedHeaderInset = Math.min(nestedHeaderInset, Math.max(0, allocatedHeight - outerCardInset - 1));+ viewportHeight = Math.max(1, allocatedHeight - outerCardInset - nestedHeaderInset - headerInset);+ // The saved height is the outer footprint. Only complete square rows fit inside+ // its header and insets; content-derived minimum sizes grow the parent if needed.+ const availableRows = Math.floor(viewportHeight / (LOGICAL_GRID_PITCH * containerContentScale));+ rowCount = Math.max(contentRowCount, availableRows, 1);+ } else if (isRail && railLogicalHeight > 0) {+ viewportHeight = railLogicalHeight;+ // Container minimum heights reserve space for square content before rail sizing.+ // Compressing their parent rows would remove that space again; let the rail scroll.+ if (rowCount <= railViewportRowCount && innerSections.length === 0) {+ rowScale = viewportHeight / getLogicalGridSize(rowCount);+ }}+ const fullGridHeight = getLogicalGridSize(rowCount);+ const maxRowCount = section.kind === "container" ? rowCount : null;+ const placementMaxRowCount = maxRowCount;const contentScale = parentContentScale * containerContentScale;const effectiveContentScale = effectiveCanvasScale * containerContentScale;// Compute the combined value in JS rather than a CSS calc() referencing the existing@@ -367,9 +359,7 @@ export const SectionGrid = ({data-grid-editor-error={isEditMode && editorRuntimeStatus === "error" ? "true" : undefined}><GridContentScaleContext.Provider value={contentScale}>- <GridRowScaleContext.Provider value={rowScale}>- <SectionContent />- </GridRowScaleContext.Provider>+ <SectionContent /></GridContentScaleContext.Provider></Box><div ref={editorHostRef} className={classes.editorPortalHost} />@@ -421,76 +411,123 @@ const useMinimumViewportRowCount = (enabled: boolean, canvasScale: number) => {const containerMinimumSizeCache = new WeakMap<ReturnType<typeof useRequiredBoard>,- Map<string, ReadonlyMap<string, { width: number; height: number }>>+ Map<string, { canvasScale: number; sizes: ReadonlyMap<string, { width: number; height: number }> }>>();-const getContainerMinimumSizes = (board: ReturnType<typeof useRequiredBoard>, layoutId: string) => {+const getContainerMinimumSizes = (+ board: ReturnType<typeof useRequiredBoard>,+ layoutId: string,+ canvasScale: number,+) => {const cachedByLayout = containerMinimumSizeCache.get(board);const cached = cachedByLayout?.get(layoutId);- if (cached) return cached;+ if (cached?.canvasScale === canvasScale) return cached.sizes;+ const boardLayout = board.layouts.find((layout) => layout.id === layoutId);const directItemsBySectionId = new Map<string, PlacementBounds[]>();for (const item of board.items) {const layout = item.layouts.find((candidate) => candidate.layoutId === layoutId);if (!layout) continue;const entries = directItemsBySectionId.get(layout.sectionId) ?? [];- entries.push(layout);+ entries.push({ ...layout, id: item.id });directItemsBySectionId.set(layout.sectionId, entries);}- const directSectionsBySectionId = new Map<string, { id: string; placement: PlacementBounds }[]>();+ const directSectionsBySectionId = new Map<string, ContainerSectionItem[]>();for (const section of board.sections) {if (section.kind !== "container") continue;const layout = section.layouts.find((candidate) => candidate.layoutId === layoutId);if (!layout) continue;const entries = directSectionsBySectionId.get(layout.parentSectionId) ?? [];- entries.push({ id: section.id, placement: layout });+ entries.push({ ...section, ...layout, type: "section" });directSectionsBySectionId.set(layout.parentSectionId, entries);}const minimumBySectionId = new Map<string, { width: number; height: number }>();const visiting = new Set<string>();- const resolve = (sectionId: string): { width: number; height: number } => {- const existing = minimumBySectionId.get(sectionId);+ const resolve = (+ section: ContainerSectionItem,+ parentContentScale = 1,+ inlineInsetCount = 2,+ ): { width: number; height: number } => {+ const existing = minimumBySectionId.get(section.id);if (existing) return existing;- if (visiting.has(sectionId)) return { width: 1, height: 1 };- visiting.add(sectionId);-- const section = board.sections.find((candidate) => candidate.id === sectionId);+ if (visiting.has(section.id)) return { width: 1, height: 1 };+ visiting.add(section.id);++ const effectiveScale = canvasScale * parentContentScale;+ const gridWidth = getLogicalGridSize(section.width);+ const inset = (inlineInsetCount * CONTAINER_CARD_INSET) / effectiveScale;+ const contentScale = Math.max(0.01, Math.min((gridWidth - inset) / gridWidth, 1));+ const innerSections = directSectionsBySectionId.get(section.id) ?? [];// A scrollable container isn't forced to grow with its content - it scrolls internally instead,// so it shouldn't have a content-derived height floor imposed by its parent's grid.- const isScrollable = section?.kind === "container" && section.options.scrollable;+ const isScrollable = section.options.scrollable;- const itemBounds = directItemsBySectionId.get(sectionId) ?? [];- const sectionBounds = (directSectionsBySectionId.get(sectionId) ?? []).map(({ id, placement }) => {- const minimum = resolve(id);+ const itemBounds = directItemsBySectionId.get(section.id) ?? [];+ const sectionBounds = innerSections.map((child) => {+ const minimum = resolve(child, parentContentScale * contentScale);return {- ...placement,- width: Math.max(placement.width, minimum.width),- height: Math.max(placement.height, minimum.height),+ ...child,+ width: Math.max(child.width, minimum.width),+ height: Math.max(child.height, minimum.height),};});const children = [...itemBounds, ...sectionBounds];+ const width = Math.max(1, ...children.map((child) => child.xOffset + child.width));+ const contentPlacements = getCollapsedDisplayLayout(+ children.map((child) => ({ id: child.id, x: child.xOffset, y: child.yOffset, w: child.width, h: child.height })),+ { columnCount: Math.max(width, section.width), collapsedItemIds: new Set() },+ );+ const contentRows = Math.max(1, getLayoutRowCount(contentPlacements));+ const hasHeader = section.options.collapsible && section.options.showLabel;+ let overhead = (2 * CONTAINER_CARD_INSET) / effectiveScale;+ if (hasHeader) overhead += (CONTAINER_HEADER_HEIGHT * calculateBoardUiScale(canvasScale)) / parentContentScale;+ if (+ (section.options.collapsible || (section.options.showLabel && section.options.title)) &&+ innerSections.some(+ (child) =>+ child.yOffset === 0 &&+ (child.options.collapsible || (child.options.showLabel && child.options.title)) &&+ (!hasHeader || !child.options.collapsible || !child.options.showLabel),+ )+ ) {+ overhead += (20 * calculateBoardUiScale(canvasScale)) / effectiveScale;+ }const minimum = {- width: Math.max(1, ...children.map((child) => child.xOffset + child.width)),- height: isScrollable ? 1 : Math.max(1, ...children.map((child) => child.yOffset + child.height)),+ width,+ height: 1,};- visiting.delete(sectionId);- minimumBySectionId.set(sectionId, minimum);+ if (!isScrollable) {+ minimum.height = Math.ceil((getLogicalGridSize(contentRows) * contentScale + overhead) / LOGICAL_GRID_PITCH);+ }+ visiting.delete(section.id);+ minimumBySectionId.set(section.id, minimum);return minimum;};- for (const section of board.sections) {- if (section.kind === "container") resolve(section.id);+ // Carry scale down the same traversal that resolves child sizes.+ for (const root of board.sections) {+ if (root.kind !== "empty") continue;+ for (const section of directSectionsBySectionId.get(root.id) ?? []) {+ let inlineInsetCount = 2;+ const lane = getRootSectionLane(root.xOffset);+ if (lane !== "main" && boardLayout) {+ if (section.xOffset === 0) inlineInsetCount -= 1;+ if (section.xOffset + section.width === getBoardLaneColumnCount(boardLayout, lane)) inlineInsetCount -= 1;+ }+ resolve(section, 1, inlineInsetCount);+ }}const nextByLayout = cachedByLayout ?? new Map();- nextByLayout.set(layoutId, minimumBySectionId);+ nextByLayout.set(layoutId, { canvasScale, sizes: minimumBySectionId });containerMinimumSizeCache.set(board, nextByLayout);return minimumBySectionId;};interface PlacementBounds {+ id: string;xOffset: number;yOffset: number;width: number;diff --git a/apps/nextjs/src/components/board/sections/grid/test/fixed-grid-item.spec.ts b/apps/nextjs/src/components/board/sections/grid/test/fixed-grid-item.spec.tsindex b6f5588f9..a3e2a36a7 100644--- a/apps/nextjs/src/components/board/sections/grid/test/fixed-grid-item.spec.ts+++ b/apps/nextjs/src/components/board/sections/grid/test/fixed-grid-item.spec.ts@@ -257,7 +257,7 @@ describe("fixed grid item behavior", () => {const item = container.querySelector<HTMLElement>('[data-grid-id="weather"]');expect(item).not.toBeNull();expect(item?.style.width).toBe(`${getLogicalGridSize(1)}px`);- expect(item?.style.height).toBe(`${getLogicalGridSize(1)}px`);+ expect(item?.style.height).toBe(`calc(${getLogicalGridSize(1)}px * var(--board-grid-row-scale, 1))`);expect(item?.getAttribute("role")).toBeNull();act(() => item?.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true })));