fix: refine widget interactions

This commit is contained in:
Thomas Camlong committed 2026-08-03 18:45:16 +02:00
1 parent fbe381cbb5
commit 7847eea1a3
27 files changed
+177 -122

No files matched your search

@@ -1,9 +1,8 @@
import type { CSSProperties, KeyboardEvent } from "react";
import { useEffect, useRef, useState } from "react";
import { createPortal } from "react-dom";
import { ActionIcon, Badge, Box, Card, Portal } from "@mantine/core";
import { Badge, Box, Card, Portal } from "@mantine/core";
import { useElementSize, useIsomorphicEffect, useViewportSize } from "@mantine/hooks";
import { IconMaximize } from "@tabler/icons-react";
import { QueryErrorResetBoundary, useQueryClient } from "@tanstack/react-query";
import combineClasses from "clsx";
import { NoIntegrationSelectedError } from "@homarr/widgets/errors/classes";
@@ -74,11 +73,12 @@ export const BoardItemContent = ({ item }: BoardItemContentProps) => {
// Keep one widget subtree mounted while moving its stable portal target between the grid and viewport layers.
// This preserves iframe/editor state and escapes Gridstack's transformed containing block.
useIsomorphicEffect(() => {
if (!supportsAdvancedFocus) return;
const target = document.createElement("div");
target.className = advancedFocusClasses.surfacePortalTarget ?? "";
setSurfacePortalTarget(target);
return () => target.remove();
}, []);
}, [supportsAdvancedFocus]);
// Responsive layout changes remount grid items. Never leave their focus backdrop orphaned.
useEffect(() => () => dismiss(item.id), [dismiss, item.id]);
@@ -166,19 +166,6 @@ export const BoardItemContent = ({ item }: BoardItemContentProps) => {
}}
style={previewStyle}
>
<div className={advancedFocusClasses.surfaceControls}>
{!isEditMode && supportsAdvancedFocus && !isAdvanced && (
<ActionIcon
className={advancedFocusClasses.touchButton}
variant="default"
size={44}
aria-label={t("item.advancedFocus.open")}
onClick={() => openAdvanced()}
>
<IconMaximize size={18} />
</ActionIcon>
)}
</div>
<Box ref={contentRef} w="100%" h="100%" mih={0}>
<InnerContent
item={item}
@@ -214,7 +201,7 @@ export const BoardItemContent = ({ item }: BoardItemContentProps) => {
className={combineClasses("grid-stack-item-content", isAdvanced && advancedFocusClasses.sourcePlaceholder)}
>
<div ref={(node) => mountPortalTarget(node, "compact")} className={advancedFocusClasses.surfaceHost}>
{!surfacePortalTarget && widgetCard}
{!supportsAdvancedFocus && widgetCard}
</div>
{surfacePortalTarget && createPortal(widgetCard, surfacePortalTarget)}
{isPreview && (
@@ -289,7 +289,7 @@ export const WidgetContextMenu = ({ item, widgetRuntimeRef, sourceRef, children
disabled={action.disabled}
color={action.color}
>
{t(action.label as never)}
{translateIfNecessary(t, action.label)}
</Menu.Item>
);
})}
@@ -27,11 +27,16 @@ const matches = (kind: WidgetKind, path: readonly string[], input: unknown, scop
describe("matchesWidgetItemQuery", () => {
test("matches only the current Custom API definition", () => {
const scope = createScope({ options: { definitionId: "definition-1" } });
const input = { boardId: "board-1", itemId: "item-1", definitionId: "definition-1" };
expect(matches("customApi", ["widget", "customApi", "getData"], { definitionId: "definition-1" }, scope)).toBe(
true,
expect(matches("customApi", ["widget", "customApi", "getData"], input, scope)).toBe(true);
expect(
matches("customApi", ["widget", "customApi", "getData"], { ...input, definitionId: "definition-2" }, scope),
).toBe(false);
expect(matches("customApi", ["widget", "customApi", "getData"], { ...input, itemId: "item-2" }, scope)).toBe(
false,
);
expect(matches("customApi", ["widget", "customApi", "getData"], { definitionId: "definition-2" }, scope)).toBe(
expect(matches("customApi", ["widget", "customApi", "getData"], { ...input, boardId: "board-2" }, scope)).toBe(
false,
);
});