feat: refine widget interactions
This commit is contained in:
125 files changed
+3281
-1459
No files matched your search
@@ -1,8 +1,8 @@
|
||||
import type { CSSProperties, KeyboardEvent, MutableRefObject, PointerEvent } from "react";
|
||||
import type { CSSProperties, KeyboardEvent, MutableRefObject } from "react";
|
||||
import { useEffect, useRef } from "react";
|
||||
import { ActionIcon, Badge, Box, Card, FocusTrap, Text } from "@mantine/core";
|
||||
import { ActionIcon, Badge, Box, Card } from "@mantine/core";
|
||||
import { useElementSize, useViewportSize } from "@mantine/hooks";
|
||||
import { IconMaximize, IconPin, IconX } from "@tabler/icons-react";
|
||||
import { IconMaximize, IconX } from "@tabler/icons-react";
|
||||
import { QueryErrorResetBoundary, useQueryClient } from "@tanstack/react-query";
|
||||
import combineClasses from "clsx";
|
||||
import { NoIntegrationSelectedError } from "@homarr/widgets/errors/classes";
|
||||
@@ -18,7 +18,7 @@ import { WidgetError } from "@homarr/widgets/errors";
|
||||
import type { SectionItem } from "~/app/[locale]/boards/_types";
|
||||
import advancedFocusClasses from "../advanced-focus/advanced-focus.module.css";
|
||||
import { useAdvancedFocus } from "../advanced-focus/context";
|
||||
import { getAdvancedFocusRect } from "../advanced-focus/geometry";
|
||||
import { getAdvancedFocusClosePosition, getAdvancedFocusRect } from "../advanced-focus/geometry";
|
||||
import classes from "../sections/item.module.css";
|
||||
import { useItemActions } from "./item-actions";
|
||||
import itemContentClasses from "./item-content.module.css";
|
||||
@@ -43,42 +43,43 @@ export const BoardItemContent = ({ item }: BoardItemContentProps) => {
|
||||
const board = useRequiredBoard();
|
||||
const t = useI18n();
|
||||
const widgetName = t(`widget.${item.kind}.name`);
|
||||
const advancedDialogLabel = t("item.advancedFocus.dialog", { widget: widgetName });
|
||||
const advancedViewLabel = t("item.advancedFocus.label", { widget: widgetName });
|
||||
const [isEditMode] = useEditMode();
|
||||
const widgetStateRef = useRef<Record<string, unknown> | null>(null);
|
||||
const { active, open, close, dismiss, pin, hover, leave } = useAdvancedFocus();
|
||||
const activeFocus = active?.itemId === item.id ? active : null;
|
||||
const closeButtonRef = useRef<HTMLButtonElement>(null);
|
||||
const { active, open, close, dismiss, hover, leave } = useAdvancedFocus();
|
||||
const supportsAdvancedFocus = item.kind !== "app";
|
||||
const advancedViewId = `advanced-focus-${item.id}`;
|
||||
const activeFocus = supportsAdvancedFocus && active?.itemId === item.id ? active : null;
|
||||
const isAdvanced = activeFocus !== null;
|
||||
const advancedRect = activeFocus
|
||||
? getAdvancedFocusRect(activeFocus.sourceRect, { width: viewportWidth, height: viewportHeight })
|
||||
: null;
|
||||
const closePosition = advancedRect ? getAdvancedFocusClosePosition(advancedRect, viewportWidth) : null;
|
||||
|
||||
// Responsive layout changes remount grid items. Never leave their focus backdrop orphaned.
|
||||
useEffect(() => () => dismiss(item.id), [dismiss, item.id]);
|
||||
|
||||
const openPinned = () => {
|
||||
if (sourceRef.current) open(item.id, sourceRef.current, true);
|
||||
useEffect(() => {
|
||||
if (!activeFocus?.autofocusClose || activeFocus.phase !== "visible") return;
|
||||
const frame = requestAnimationFrame(() => closeButtonRef.current?.focus());
|
||||
return () => cancelAnimationFrame(frame);
|
||||
}, [activeFocus?.autofocusClose, activeFocus?.phase]);
|
||||
|
||||
const openAdvanced = (autofocusClose = false) => {
|
||||
if (sourceRef.current) open(item.id, sourceRef.current, { activation: "manual", autofocusClose });
|
||||
};
|
||||
const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
|
||||
if (event.currentTarget !== event.target || event.key !== "Enter" || !event.shiftKey || isEditMode) return;
|
||||
if (
|
||||
event.currentTarget !== event.target ||
|
||||
event.key !== "Enter" ||
|
||||
!event.shiftKey ||
|
||||
isEditMode ||
|
||||
!supportsAdvancedFocus
|
||||
)
|
||||
return;
|
||||
event.preventDefault();
|
||||
openPinned();
|
||||
};
|
||||
const handleSurfacePointerDown = (event: PointerEvent<HTMLDivElement>) => {
|
||||
if (!activeFocus || activeFocus.pinned) return;
|
||||
|
||||
const target =
|
||||
event.target instanceof Element
|
||||
? event.target.closest<HTMLElement>(
|
||||
"a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), iframe, [tabindex]:not([tabindex='-1']), [contenteditable='true']",
|
||||
)
|
||||
: null;
|
||||
const alreadyAutofocused = target?.hasAttribute("data-autofocus") ?? false;
|
||||
if (target && !alreadyAutofocused) target.setAttribute("data-autofocus", "true");
|
||||
pin(false);
|
||||
if (target && !alreadyAutofocused) {
|
||||
window.setTimeout(() => target.removeAttribute("data-autofocus"), 250);
|
||||
}
|
||||
openAdvanced(true);
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -86,15 +87,21 @@ export const BoardItemContent = ({ item }: BoardItemContentProps) => {
|
||||
<WidgetContextMenu item={item} widgetStateRef={widgetStateRef} sourceRef={sourceRef}>
|
||||
<Box
|
||||
ref={sourceRef}
|
||||
tabIndex={isEditMode ? undefined : 0}
|
||||
role={isEditMode ? undefined : "group"}
|
||||
aria-label={isEditMode ? undefined : `${widgetName}: ${t("item.advancedFocus.open")}`}
|
||||
aria-haspopup="dialog"
|
||||
aria-expanded={isAdvanced}
|
||||
aria-keyshortcuts="Shift+Enter"
|
||||
onKeyDown={handleKeyDown}
|
||||
onPointerEnter={() => sourceRef.current && hover(item.id, sourceRef.current)}
|
||||
onPointerLeave={() => leave(item.id)}
|
||||
tabIndex={!isEditMode && supportsAdvancedFocus ? 0 : undefined}
|
||||
role={!isEditMode && supportsAdvancedFocus ? "group" : undefined}
|
||||
aria-label={
|
||||
!isEditMode && supportsAdvancedFocus ? `${widgetName}: ${t("item.advancedFocus.open")}` : undefined
|
||||
}
|
||||
aria-expanded={supportsAdvancedFocus ? isAdvanced : undefined}
|
||||
aria-controls={isAdvanced ? advancedViewId : undefined}
|
||||
aria-keyshortcuts={supportsAdvancedFocus ? "Shift+Enter" : undefined}
|
||||
onKeyDown={supportsAdvancedFocus ? handleKeyDown : undefined}
|
||||
onPointerEnter={() => {
|
||||
if (supportsAdvancedFocus && sourceRef.current) hover(item.id, sourceRef.current);
|
||||
}}
|
||||
onPointerLeave={() => {
|
||||
if (supportsAdvancedFocus) leave(item.id);
|
||||
}}
|
||||
className={combineClasses("grid-stack-item-content", isAdvanced && advancedFocusClasses.sourcePlaceholder)}
|
||||
>
|
||||
<div
|
||||
@@ -103,105 +110,82 @@ export const BoardItemContent = ({ item }: BoardItemContentProps) => {
|
||||
isAdvanced && advancedFocusClasses.surfaceHostAdvanced,
|
||||
)}
|
||||
>
|
||||
<FocusTrap active={Boolean(activeFocus?.pinned)}>
|
||||
<Card
|
||||
role={isAdvanced ? "dialog" : undefined}
|
||||
aria-modal={activeFocus?.pinned || undefined}
|
||||
aria-label={isAdvanced ? advancedDialogLabel : undefined}
|
||||
radius={board.itemRadius}
|
||||
p={isAdvanced ? undefined : 0}
|
||||
h={isAdvanced ? undefined : "100%"}
|
||||
w={isAdvanced ? undefined : "100%"}
|
||||
className={combineClasses(
|
||||
classes.itemCard,
|
||||
`${item.kind}-wrapper`,
|
||||
isAdvanced && advancedFocusClasses.surface,
|
||||
activeFocus?.phase === "closing" && advancedFocusClasses.surfaceClosing,
|
||||
item.advancedOptions.customCssClasses.join(" "),
|
||||
)}
|
||||
onPointerDownCapture={isAdvanced ? handleSurfacePointerDown : undefined}
|
||||
styles={{
|
||||
root: {
|
||||
"--opacity": isAdvanced ? 0.98 : board.opacity / 100,
|
||||
containerType: "size",
|
||||
overflow: getOverflowFromKind(item.kind),
|
||||
"--border-color":
|
||||
item.advancedOptions.borderColor !== "" ? item.advancedOptions.borderColor : undefined,
|
||||
},
|
||||
}}
|
||||
style={
|
||||
activeFocus && advancedRect
|
||||
? ({
|
||||
left: advancedRect.left,
|
||||
top: advancedRect.top,
|
||||
width: advancedRect.width,
|
||||
height: advancedRect.height,
|
||||
"--focus-translate-x": `${activeFocus.sourceRect.left - advancedRect.left}px`,
|
||||
"--focus-translate-y": `${activeFocus.sourceRect.top - advancedRect.top}px`,
|
||||
"--focus-scale-x": activeFocus.sourceRect.width / advancedRect.width,
|
||||
"--focus-scale-y": activeFocus.sourceRect.height / advancedRect.height,
|
||||
} as CSSProperties)
|
||||
: undefined
|
||||
}
|
||||
{activeFocus?.activation === "manual" && closePosition && (
|
||||
<ActionIcon
|
||||
ref={closeButtonRef}
|
||||
className={advancedFocusClasses.closeButton}
|
||||
variant="default"
|
||||
size={44}
|
||||
aria-label={t("item.advancedFocus.close")}
|
||||
aria-controls={advancedViewId}
|
||||
style={closePosition}
|
||||
onClick={() => close()}
|
||||
>
|
||||
<div
|
||||
className={combineClasses(
|
||||
advancedFocusClasses.surfaceControls,
|
||||
isAdvanced && advancedFocusClasses.surfaceControlsAdvanced,
|
||||
)}
|
||||
>
|
||||
{isAdvanced && (
|
||||
<Text className={advancedFocusClasses.surfaceTitle} size="sm" fw={600} truncate>
|
||||
{advancedDialogLabel}
|
||||
</Text>
|
||||
)}
|
||||
{isAdvanced && (
|
||||
<ActionIcon
|
||||
data-autofocus={activeFocus.pinned && activeFocus.autofocusClose ? true : undefined}
|
||||
className={advancedFocusClasses.closeButton}
|
||||
variant="default"
|
||||
size={44}
|
||||
aria-label={t("item.advancedFocus.close")}
|
||||
onClick={() => close()}
|
||||
>
|
||||
<IconX size={18} />
|
||||
</ActionIcon>
|
||||
)}
|
||||
{isAdvanced && (
|
||||
<ActionIcon
|
||||
className={advancedFocusClasses.pinButton}
|
||||
variant={activeFocus.pinned ? "filled" : "default"}
|
||||
size={44}
|
||||
aria-label={activeFocus.pinned ? t("item.advancedFocus.pinned") : t("item.advancedFocus.pin")}
|
||||
aria-pressed={activeFocus.pinned}
|
||||
onClick={(event) => pin(event.detail === 0)}
|
||||
>
|
||||
<IconPin size={18} />
|
||||
</ActionIcon>
|
||||
)}
|
||||
{!isEditMode && !isAdvanced && (
|
||||
<ActionIcon
|
||||
className={advancedFocusClasses.touchButton}
|
||||
variant="default"
|
||||
size={44}
|
||||
aria-label={t("item.advancedFocus.open")}
|
||||
onClick={openPinned}
|
||||
>
|
||||
<IconMaximize size={18} />
|
||||
</ActionIcon>
|
||||
)}
|
||||
</div>
|
||||
<Box ref={contentRef} w="100%" h="100%" mih={0}>
|
||||
<InnerContent
|
||||
item={item}
|
||||
width={width}
|
||||
height={height}
|
||||
widgetStateRef={widgetStateRef}
|
||||
displayMode={isAdvanced ? "advanced" : "compact"}
|
||||
/>
|
||||
</Box>
|
||||
</Card>
|
||||
</FocusTrap>
|
||||
<IconX size={18} />
|
||||
</ActionIcon>
|
||||
)}
|
||||
<Card
|
||||
id={isAdvanced ? advancedViewId : undefined}
|
||||
role={isAdvanced ? "region" : undefined}
|
||||
aria-label={isAdvanced ? advancedViewLabel : undefined}
|
||||
radius={board.itemRadius}
|
||||
p={isAdvanced ? undefined : 0}
|
||||
h={isAdvanced ? undefined : "100%"}
|
||||
w={isAdvanced ? undefined : "100%"}
|
||||
className={combineClasses(
|
||||
classes.itemCard,
|
||||
`${item.kind}-wrapper`,
|
||||
isAdvanced && advancedFocusClasses.surface,
|
||||
activeFocus?.phase === "closing" && advancedFocusClasses.surfaceClosing,
|
||||
item.advancedOptions.customCssClasses.join(" "),
|
||||
)}
|
||||
styles={{
|
||||
root: {
|
||||
"--opacity": isAdvanced ? 0.98 : board.opacity / 100,
|
||||
containerType: "size",
|
||||
overflow: getOverflowFromKind(item.kind),
|
||||
"--border-color":
|
||||
item.advancedOptions.borderColor !== "" ? item.advancedOptions.borderColor : undefined,
|
||||
},
|
||||
}}
|
||||
style={
|
||||
activeFocus && advancedRect
|
||||
? ({
|
||||
left: advancedRect.left,
|
||||
top: advancedRect.top,
|
||||
width: advancedRect.width,
|
||||
height: advancedRect.height,
|
||||
"--focus-translate-x": `${activeFocus.sourceRect.left - advancedRect.left}px`,
|
||||
"--focus-translate-y": `${activeFocus.sourceRect.top - advancedRect.top}px`,
|
||||
"--focus-scale-x": activeFocus.sourceRect.width / advancedRect.width,
|
||||
"--focus-scale-y": activeFocus.sourceRect.height / advancedRect.height,
|
||||
} as CSSProperties)
|
||||
: undefined
|
||||
}
|
||||
>
|
||||
<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}
|
||||
width={width}
|
||||
height={height}
|
||||
widgetStateRef={widgetStateRef}
|
||||
displayMode={isAdvanced ? "advanced" : "compact"}
|
||||
/>
|
||||
</Box>
|
||||
</Card>
|
||||
</div>
|
||||
</Box>
|
||||
</WidgetContextMenu>
|
||||
|
||||
@@ -283,11 +283,6 @@ const WidgetItem = ({
|
||||
{item.description}
|
||||
</Text>
|
||||
</Tooltip>
|
||||
{hasMatchingIntegration && (
|
||||
<Text size="10px" c="blue.6" fw={600}>
|
||||
{t("item.create.integrationAvailable")}
|
||||
</Text>
|
||||
)}
|
||||
<SupportedIntegrations integrations={item.supportedIntegrations} />
|
||||
</Stack>
|
||||
<Box
|
||||
|
||||
@@ -250,17 +250,21 @@ export const WidgetContextMenu = ({ item, widgetStateRef, sourceRef, children }:
|
||||
>
|
||||
<Menu.ContextMenu>{children}</Menu.ContextMenu>
|
||||
<Menu.Dropdown>
|
||||
<Menu.Item
|
||||
closeMenuOnClick
|
||||
leftSection={<IconMaximize size={16} />}
|
||||
onClick={() => {
|
||||
if (sourceRef.current) openAdvancedFocus(item.id, sourceRef.current, true);
|
||||
}}
|
||||
disabled={isEditMode}
|
||||
>
|
||||
{t("item.advancedFocus.open")}
|
||||
</Menu.Item>
|
||||
<Menu.Divider />
|
||||
{item.kind !== "app" && (
|
||||
<>
|
||||
<Menu.Item
|
||||
closeMenuOnClick
|
||||
leftSection={<IconMaximize size={16} />}
|
||||
onClick={() => {
|
||||
if (sourceRef.current) openAdvancedFocus(item.id, sourceRef.current);
|
||||
}}
|
||||
disabled={isEditMode}
|
||||
>
|
||||
{t("item.advancedFocus.open")}
|
||||
</Menu.Item>
|
||||
<Menu.Divider />
|
||||
</>
|
||||
)}
|
||||
{toggleOptions.length > 0 && (
|
||||
<>
|
||||
<Menu.Label>{tMenu("options")}</Menu.Label>
|
||||
|
||||
Reference in new issue
Block a user