fix: preserve widget preview dimensions
This commit is contained in:
1 parent
5188ed89d6
commit
763e1324e8
6 files changed
+86
-60
No files matched your search
@@ -180,6 +180,7 @@ const LoadedBoardItemContent = ({
|
|||||||
const { width: viewportWidth, height: viewportHeight } = viewportSize;
|
const { width: viewportWidth, height: viewportHeight } = viewportSize;
|
||||||
const supportsAdvancedFocus = definitionSupportsAdvancedFocus(definition);
|
const supportsAdvancedFocus = definitionSupportsAdvancedFocus(definition);
|
||||||
const widgetName = getWidgetName(item.kind, t);
|
const widgetName = getWidgetName(item.kind, t);
|
||||||
|
const previewDimensions = useMemo(() => ({ width, height }), [height, width]);
|
||||||
const advancedViewLabel = t("item.advancedFocus.label", { widget: widgetName });
|
const advancedViewLabel = t("item.advancedFocus.label", { widget: widgetName });
|
||||||
const advancedViewId = `advanced-focus-${item.id}`;
|
const advancedViewId = `advanced-focus-${item.id}`;
|
||||||
const activeFocus = supportsAdvancedFocus && active?.itemId === item.id ? active : null;
|
const activeFocus = supportsAdvancedFocus && active?.itemId === item.id ? active : null;
|
||||||
@@ -350,10 +351,11 @@ const LoadedBoardItemContent = ({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{isEditMode && <BoardItemMenu item={menuItem} definition={definition} />}
|
{isEditMode && <BoardItemMenu item={menuItem} definition={definition} previewDimensions={previewDimensions} />}
|
||||||
<WidgetContextMenu
|
<WidgetContextMenu
|
||||||
item={item}
|
item={item}
|
||||||
definition={definition}
|
definition={definition}
|
||||||
|
previewDimensions={previewDimensions}
|
||||||
widgetStateRef={widgetStateRef}
|
widgetStateRef={widgetStateRef}
|
||||||
widgetRuntimeRef={widgetRuntimeRef}
|
widgetRuntimeRef={widgetRuntimeRef}
|
||||||
sourceRef={sourceRef}
|
sourceRef={sourceRef}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import { LazyWidgetEditModal, preloadWidgetEditModal } from "./lazy-widget-edit-
|
|||||||
interface BoardItemMenuProps {
|
interface BoardItemMenuProps {
|
||||||
item: SectionItem;
|
item: SectionItem;
|
||||||
definition: WidgetDefinition;
|
definition: WidgetDefinition;
|
||||||
|
previewDimensions: { width: number; height: number };
|
||||||
resetErrorBoundary?: () => void;
|
resetErrorBoundary?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ export const BoardItemMenu = (props: BoardItemMenuProps) => {
|
|||||||
|
|
||||||
return <BoardItemMenuInner {...props} />;
|
return <BoardItemMenuInner {...props} />;
|
||||||
};
|
};
|
||||||
const BoardItemMenuInner = ({ item, definition, resetErrorBoundary }: BoardItemMenuProps) => {
|
const BoardItemMenuInner = ({ item, definition, previewDimensions, resetErrorBoundary }: BoardItemMenuProps) => {
|
||||||
const { data: session } = useSession();
|
const { data: session } = useSession();
|
||||||
const canDuplicate = item.kind !== "customApi" || (session?.user.permissions.includes("admin") ?? false);
|
const canDuplicate = item.kind !== "customApi" || (session?.user.permissions.includes("admin") ?? false);
|
||||||
const refResetErrorBoundaryOnNextRender = useRef(false);
|
const refResetErrorBoundaryOnNextRender = useRef(false);
|
||||||
@@ -89,7 +90,7 @@ const BoardItemMenuInner = ({ item, definition, resetErrorBoundary }: BoardItemM
|
|||||||
integrationSupport: "supportedIntegrations" in definition,
|
integrationSupport: "supportedIntegrations" in definition,
|
||||||
settings,
|
settings,
|
||||||
itemId: item.id,
|
itemId: item.id,
|
||||||
previewDimensions: { width: item.width * 200, height: item.height * 200 },
|
previewDimensions,
|
||||||
appId: item.kind === "app" ? (item.options.appId as string | undefined) : undefined,
|
appId: item.kind === "app" ? (item.options.appId as string | undefined) : undefined,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import type { FallbackProps } from "react-error-boundary";
|
|||||||
|
|
||||||
import { clientApi } from "@homarr/api/client";
|
import { clientApi } from "@homarr/api/client";
|
||||||
import type { WidgetKind } from "@homarr/definitions";
|
import type { WidgetKind } from "@homarr/definitions";
|
||||||
import { createModal, modalSizeSelect, useModalAction } from "@homarr/modals";
|
import { createModal, useModalAction } from "@homarr/modals";
|
||||||
import { WidgetError } from "@homarr/widgets/errors";
|
import { WidgetError } from "@homarr/widgets/errors";
|
||||||
import type * as WidgetModalsModule from "@homarr/widgets/modals";
|
import type * as WidgetModalsModule from "@homarr/widgets/modals";
|
||||||
import type { WidgetEditModalProps } from "@homarr/widgets/modals";
|
import type { WidgetEditModalProps } from "@homarr/widgets/modals";
|
||||||
@@ -160,7 +160,7 @@ export const LazyWidgetEditModal = createModal<WidgetEditModalProps<WidgetKind>>
|
|||||||
defaultTitle(t) {
|
defaultTitle(t) {
|
||||||
return t("item.edit.title");
|
return t("item.edit.title");
|
||||||
},
|
},
|
||||||
size: modalSizeSelect,
|
size: "100rem",
|
||||||
transitionProps: {
|
transitionProps: {
|
||||||
transition: "pop",
|
transition: "pop",
|
||||||
duration: 180,
|
duration: 180,
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import { matchesWidgetItemQuery } from "./widget-query-scope";
|
|||||||
interface WidgetContextMenuProps {
|
interface WidgetContextMenuProps {
|
||||||
item: SectionItem;
|
item: SectionItem;
|
||||||
definition: WidgetDefinition;
|
definition: WidgetDefinition;
|
||||||
|
previewDimensions: { width: number; height: number };
|
||||||
widgetStateRef: MutableRefObject<Record<string, unknown> | null>;
|
widgetStateRef: MutableRefObject<Record<string, unknown> | null>;
|
||||||
widgetRuntimeRef: WidgetRuntimeRef;
|
widgetRuntimeRef: WidgetRuntimeRef;
|
||||||
sourceRef: RefObject<HTMLElement | null>;
|
sourceRef: RefObject<HTMLElement | null>;
|
||||||
@@ -44,6 +45,7 @@ interface WidgetContextMenuProps {
|
|||||||
export const WidgetContextMenu = ({
|
export const WidgetContextMenu = ({
|
||||||
item,
|
item,
|
||||||
definition,
|
definition,
|
||||||
|
previewDimensions,
|
||||||
widgetRuntimeRef,
|
widgetRuntimeRef,
|
||||||
sourceRef,
|
sourceRef,
|
||||||
children,
|
children,
|
||||||
@@ -191,7 +193,7 @@ export const WidgetContextMenu = ({
|
|||||||
settings,
|
settings,
|
||||||
itemId: item.id,
|
itemId: item.id,
|
||||||
boardId: board.id,
|
boardId: board.id,
|
||||||
previewDimensions: { width: item.width * 200, height: item.height * 200 },
|
previewDimensions,
|
||||||
appId: item.kind === "app" ? (item.options.appId as string | undefined) : undefined,
|
appId: item.kind === "app" ? (item.options.appId as string | undefined) : undefined,
|
||||||
},
|
},
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
@@ -207,6 +209,7 @@ export const WidgetContextMenu = ({
|
|||||||
handleRefetch,
|
handleRefetch,
|
||||||
openModal,
|
openModal,
|
||||||
persistBoard,
|
persistBoard,
|
||||||
|
previewDimensions,
|
||||||
settings,
|
settings,
|
||||||
updateItemAdvancedOptions,
|
updateItemAdvancedOptions,
|
||||||
updateItemIntegrations,
|
updateItemIntegrations,
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.workspace[data-with-preview] {
|
.workspace[data-with-preview] {
|
||||||
grid-template-columns: minmax(0, 0.95fr) minmax(24rem, 1.05fr);
|
grid-template-columns: minmax(0, 3fr) minmax(34rem, 2fr);
|
||||||
}
|
}
|
||||||
|
|
||||||
.previewPanel {
|
.previewPanel {
|
||||||
@@ -28,7 +28,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.previewCanvas {
|
.previewCanvas {
|
||||||
min-height: 23rem;
|
height: clamp(24rem, calc(100dvh - 17rem), 44rem);
|
||||||
padding: var(--mantine-spacing-lg);
|
padding: var(--mantine-spacing-lg);
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
border: 1px solid var(--mantine-color-default-border);
|
border: 1px solid var(--mantine-color-default-border);
|
||||||
@@ -40,12 +40,19 @@
|
|||||||
background-size: 2rem 2rem;
|
background-size: 2rem 2rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.previewWidget {
|
.previewViewport {
|
||||||
position: relative;
|
position: relative;
|
||||||
flex: 0 0 auto;
|
flex: 0 0 auto;
|
||||||
|
animation: dock-widget 260ms cubic-bezier(0.22, 1, 0.36, 1) both;
|
||||||
|
}
|
||||||
|
|
||||||
|
.previewWidget {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
box-shadow: var(--mantine-shadow-lg);
|
box-shadow: var(--mantine-shadow-lg);
|
||||||
animation: dock-widget 260ms cubic-bezier(0.22, 1, 0.36, 1) both;
|
transform-origin: top left;
|
||||||
}
|
}
|
||||||
|
|
||||||
.previewTitle {
|
.previewTitle {
|
||||||
@@ -134,7 +141,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 62em) {
|
@media (max-width: 75em) {
|
||||||
.workspace[data-with-preview] {
|
.workspace[data-with-preview] {
|
||||||
grid-template-columns: 1fr;
|
grid-template-columns: 1fr;
|
||||||
}
|
}
|
||||||
@@ -144,7 +151,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.previewCanvas {
|
.previewCanvas {
|
||||||
min-height: 18rem;
|
height: 24rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.settingsPanel {
|
.settingsPanel {
|
||||||
@@ -154,7 +161,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
@media (prefers-reduced-motion: reduce) {
|
@media (prefers-reduced-motion: reduce) {
|
||||||
.previewWidget {
|
.previewViewport {
|
||||||
animation: none;
|
animation: none;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@ import { schemaResolver } from "@mantine/form";
|
|||||||
import { useElementSize } from "@mantine/hooks";
|
import { useElementSize } from "@mantine/hooks";
|
||||||
import { IconArrowLeft, IconEye, IconPencil, IconSettings } from "@tabler/icons-react";
|
import { IconArrowLeft, IconEye, IconPencil, IconSettings } from "@tabler/icons-react";
|
||||||
import { QueryErrorResetBoundary } from "@tanstack/react-query";
|
import { QueryErrorResetBoundary } from "@tanstack/react-query";
|
||||||
|
import combineClasses from "clsx";
|
||||||
import { ErrorBoundary } from "react-error-boundary";
|
import { ErrorBoundary } from "react-error-boundary";
|
||||||
import { z } from "zod/v4";
|
import { z } from "zod/v4";
|
||||||
|
|
||||||
@@ -16,7 +17,7 @@ import { objectEntries } from "@homarr/common";
|
|||||||
import { useSession } from "@homarr/auth/client";
|
import { useSession } from "@homarr/auth/client";
|
||||||
import { useOptionalBoard } from "@homarr/boards/context";
|
import { useOptionalBoard } from "@homarr/boards/context";
|
||||||
import type { WidgetKind } from "@homarr/definitions";
|
import type { WidgetKind } from "@homarr/definitions";
|
||||||
import { createModal, modalSizeSelect, useModalAction } from "@homarr/modals";
|
import { createModal, useModalAction } from "@homarr/modals";
|
||||||
import type { SettingsContextProps } from "@homarr/settings/creator";
|
import type { SettingsContextProps } from "@homarr/settings/creator";
|
||||||
import { useI18n } from "@homarr/translation/client";
|
import { useI18n } from "@homarr/translation/client";
|
||||||
import { IntegrationAvatar } from "@homarr/ui";
|
import { IntegrationAvatar } from "@homarr/ui";
|
||||||
@@ -71,6 +72,7 @@ export interface WidgetEditModalProps<TSort extends WidgetKind> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface WidgetEditPreviewProps {
|
interface WidgetEditPreviewProps {
|
||||||
|
kind: WidgetKind;
|
||||||
Component: ComponentType<WidgetComponentProps<WidgetKind>>;
|
Component: ComponentType<WidgetComponentProps<WidgetKind>>;
|
||||||
definition: WidgetDefinition;
|
definition: WidgetDefinition;
|
||||||
state: WidgetEditModalState;
|
state: WidgetEditModalState;
|
||||||
@@ -81,6 +83,7 @@ interface WidgetEditPreviewProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const WidgetEditPreview = ({
|
const WidgetEditPreview = ({
|
||||||
|
kind,
|
||||||
Component,
|
Component,
|
||||||
definition,
|
definition,
|
||||||
state,
|
state,
|
||||||
@@ -90,18 +93,15 @@ const WidgetEditPreview = ({
|
|||||||
onChangeOptions,
|
onChangeOptions,
|
||||||
}: WidgetEditPreviewProps) => {
|
}: WidgetEditPreviewProps) => {
|
||||||
const tItem = useI18n("item.edit");
|
const tItem = useI18n("item.edit");
|
||||||
const { ref, width: availableWidth } = useElementSize<HTMLDivElement>();
|
const { ref, width: availableWidth, height: availableHeight } = useElementSize<HTMLDivElement>();
|
||||||
const sourceWidth = Math.max(dimensions.width, 1);
|
const sourceWidth = Math.max(dimensions.width, 1);
|
||||||
const sourceHeight = Math.max(dimensions.height, 1);
|
const sourceHeight = Math.max(dimensions.height, 1);
|
||||||
const aspectRatio = sourceWidth / sourceHeight;
|
let previewScale = 1;
|
||||||
const maximumWidth = Math.max(availableWidth, 280);
|
if (availableWidth > 0 && availableHeight > 0) {
|
||||||
let previewWidth = maximumWidth;
|
previewScale = Math.min(availableWidth / sourceWidth, availableHeight / sourceHeight, 1);
|
||||||
let previewHeight = previewWidth / aspectRatio;
|
|
||||||
|
|
||||||
if (previewHeight > 320) {
|
|
||||||
previewHeight = 320;
|
|
||||||
previewWidth = previewHeight * aspectRatio;
|
|
||||||
}
|
}
|
||||||
|
const previewWidth = sourceWidth * previewScale;
|
||||||
|
const previewHeight = sourceHeight * previewScale;
|
||||||
|
|
||||||
const hasIntegrationSupport = "supportedIntegrations" in definition;
|
const hasIntegrationSupport = "supportedIntegrations" in definition;
|
||||||
const integrationRequired = hasIntegrationSupport && definition.integrationsRequired !== false;
|
const integrationRequired = hasIntegrationSupport && definition.integrationsRequired !== false;
|
||||||
@@ -118,43 +118,55 @@ const WidgetEditPreview = ({
|
|||||||
{tItem("preview.integrationRequired")}
|
{tItem("preview.integrationRequired")}
|
||||||
</Alert>
|
</Alert>
|
||||||
) : (
|
) : (
|
||||||
<Card
|
<Box className={classes.previewViewport} w={previewWidth} h={previewHeight}>
|
||||||
className={classes.previewWidget}
|
<Card
|
||||||
w={previewWidth}
|
className={combineClasses(
|
||||||
h={previewHeight}
|
classes.previewWidget,
|
||||||
p={previewHeight >= 96 ? undefined : 4}
|
`${kind}-wrapper`,
|
||||||
style={{ borderColor: state.advancedOptions.borderColor || undefined }}
|
"board-grid-item-content",
|
||||||
>
|
state.advancedOptions.customCssClasses,
|
||||||
{state.advancedOptions.title && (
|
|
||||||
<Badge className={classes.previewTitle} size="sm" variant="default">
|
|
||||||
{state.advancedOptions.title}
|
|
||||||
</Badge>
|
|
||||||
)}
|
|
||||||
<QueryErrorResetBoundary>
|
|
||||||
{({ reset }) => (
|
|
||||||
<ErrorBoundary
|
|
||||||
onReset={reset}
|
|
||||||
resetKeys={[state.options, state.integrationIds]}
|
|
||||||
fallbackRender={({ resetErrorBoundary, error }) => (
|
|
||||||
<WidgetError definition={definition} error={error} resetErrorBoundary={resetErrorBoundary} />
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<div className={classes.previewContent} inert>
|
|
||||||
<Component
|
|
||||||
options={state.options as never}
|
|
||||||
integrationIds={state.integrationIds}
|
|
||||||
width={previewWidth}
|
|
||||||
height={previewHeight}
|
|
||||||
isEditMode={false}
|
|
||||||
boardId={boardId}
|
|
||||||
itemId={itemId}
|
|
||||||
setOptions={({ newOptions }) => onChangeOptions(newOptions as Record<string, unknown>)}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</ErrorBoundary>
|
|
||||||
)}
|
)}
|
||||||
</QueryErrorResetBoundary>
|
w={sourceWidth}
|
||||||
</Card>
|
h={sourceHeight}
|
||||||
|
p={0}
|
||||||
|
style={{
|
||||||
|
borderColor: state.advancedOptions.borderColor || undefined,
|
||||||
|
containerType: "size",
|
||||||
|
transform: `scale(${previewScale})`,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{state.advancedOptions.title && (
|
||||||
|
<Badge className={classes.previewTitle} size="sm" variant="default">
|
||||||
|
{state.advancedOptions.title}
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
|
<QueryErrorResetBoundary>
|
||||||
|
{({ reset }) => (
|
||||||
|
<ErrorBoundary
|
||||||
|
onReset={reset}
|
||||||
|
resetKeys={[state.options, state.integrationIds]}
|
||||||
|
fallbackRender={({ resetErrorBoundary, error }) => (
|
||||||
|
<WidgetError definition={definition} error={error} resetErrorBoundary={resetErrorBoundary} />
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div className={classes.previewContent} inert>
|
||||||
|
<Component
|
||||||
|
options={state.options as never}
|
||||||
|
integrationIds={state.integrationIds}
|
||||||
|
width={sourceWidth}
|
||||||
|
height={sourceHeight}
|
||||||
|
isEditMode={false}
|
||||||
|
displayMode="compact"
|
||||||
|
boardId={boardId}
|
||||||
|
itemId={itemId}
|
||||||
|
setOptions={({ newOptions }) => onChangeOptions(newOptions as Record<string, unknown>)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</ErrorBoundary>
|
||||||
|
)}
|
||||||
|
</QueryErrorResetBoundary>
|
||||||
|
</Card>
|
||||||
|
</Box>
|
||||||
)}
|
)}
|
||||||
</Center>
|
</Center>
|
||||||
</Stack>
|
</Stack>
|
||||||
@@ -356,6 +368,7 @@ export const WidgetEditModal = createModal<WidgetEditModalProps<WidgetKind>>(({
|
|||||||
<Box className={classes.workspace} data-with-preview={innerProps.previewComponent ? true : undefined}>
|
<Box className={classes.workspace} data-with-preview={innerProps.previewComponent ? true : undefined}>
|
||||||
{innerProps.previewComponent && (
|
{innerProps.previewComponent && (
|
||||||
<WidgetEditPreview
|
<WidgetEditPreview
|
||||||
|
kind={innerProps.kind}
|
||||||
Component={innerProps.previewComponent}
|
Component={innerProps.previewComponent}
|
||||||
definition={definition}
|
definition={definition}
|
||||||
state={{ ...form.values, advancedOptions }}
|
state={{ ...form.values, advancedOptions }}
|
||||||
@@ -562,7 +575,7 @@ export const WidgetEditModal = createModal<WidgetEditModalProps<WidgetKind>>(({
|
|||||||
defaultTitle(t) {
|
defaultTitle(t) {
|
||||||
return t("item.edit.title");
|
return t("item.edit.title");
|
||||||
},
|
},
|
||||||
size: modalSizeSelect,
|
size: "100rem",
|
||||||
transitionProps: {
|
transitionProps: {
|
||||||
transition: "pop",
|
transition: "pop",
|
||||||
duration: 180,
|
duration: 180,
|
||||||
|
|||||||
Reference in new issue
Block a user