fix: harden widget settings inspector
This commit is contained in:
1 parent
7d742c9efa
commit
b3e24f0d34
5 files changed
+52
-27
No files matched your search
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { use, useCallback, useState } from "react";
|
||||
import { use, useCallback, useId, useState } from "react";
|
||||
import { ActionIcon, Affix, Card } from "@mantine/core";
|
||||
import { IconDimensions, IconPencil, IconToggleLeft, IconToggleRight } from "@tabler/icons-react";
|
||||
import { QueryErrorResetBoundary } from "@tanstack/react-query";
|
||||
@@ -32,6 +32,7 @@ export const WidgetPreviewPageContent = ({ kind }: WidgetPreviewPageContentProps
|
||||
const { openModal: openWidgetEditModal } = useModalAction(LazyWidgetEditModal);
|
||||
const { openModal: openPreviewDimensionsModal } = useModalAction(PreviewDimensionsModal);
|
||||
const { definition: currentDefinition, Component } = use(loadWidgetResources(kind));
|
||||
const previewId = useId();
|
||||
const [editMode, setEditMode] = useState(false);
|
||||
const [isEditorLoading, setIsEditorLoading] = useState(false);
|
||||
const [dimensions, setDimensions] = useState<Dimensions>({
|
||||
@@ -71,6 +72,7 @@ export const WidgetPreviewPageContent = ({ kind }: WidgetPreviewPageContentProps
|
||||
),
|
||||
integrationSupport: hasIntegrationSupport,
|
||||
settings,
|
||||
focusTargetId: previewId,
|
||||
},
|
||||
{
|
||||
title(translate) {
|
||||
@@ -86,7 +88,7 @@ export const WidgetPreviewPageContent = ({ kind }: WidgetPreviewPageContentProps
|
||||
} finally {
|
||||
setIsEditorLoading(false);
|
||||
}
|
||||
}, [currentDefinition, kind, openWidgetEditModal, settings, state, t, utils]);
|
||||
}, [currentDefinition, kind, openWidgetEditModal, previewId, settings, state, t, utils]);
|
||||
|
||||
const toggleEditMode = useCallback(() => {
|
||||
setEditMode((editMode) => !editMode);
|
||||
@@ -110,7 +112,7 @@ export const WidgetPreviewPageContent = ({ kind }: WidgetPreviewPageContentProps
|
||||
|
||||
return (
|
||||
<>
|
||||
<Card w={dimensions.width} h={dimensions.height} p={dimensions.height >= 96 ? undefined : 4}>
|
||||
<Card id={previewId} w={dimensions.width} h={dimensions.height} p={dimensions.height >= 96 ? undefined : 4}>
|
||||
<QueryErrorResetBoundary>
|
||||
{({ reset }) => (
|
||||
<ErrorBoundary
|
||||
|
||||
@@ -297,6 +297,7 @@ const LoadedBoardItemContent = ({
|
||||
role={isPreview ? "region" : undefined}
|
||||
aria-label={isPreview ? advancedViewLabel : undefined}
|
||||
data-advanced-focus-surface={isPreview || undefined}
|
||||
data-advanced-focus-item-id={isAdvanced ? item.id : undefined}
|
||||
data-grid-item-content
|
||||
radius={board.itemRadius}
|
||||
p={isAdvanced ? undefined : 0}
|
||||
|
||||
@@ -46,16 +46,18 @@ const focusRectsMatch = (current: FocusRect | null, next: FocusRect | null) => {
|
||||
);
|
||||
};
|
||||
|
||||
export const useWidgetEditFocus = (itemId: string | undefined) => {
|
||||
export const useWidgetEditFocus = (itemId: string | undefined, focusTargetId: string | undefined) => {
|
||||
const [focusRect, setFocusRect] = useState<FocusRect | null>(null);
|
||||
|
||||
useIsomorphicEffect(() => {
|
||||
if (!itemId) {
|
||||
setFocusRect(null);
|
||||
return;
|
||||
let element: HTMLElement | null = null;
|
||||
if (focusTargetId) {
|
||||
element = document.getElementById(focusTargetId);
|
||||
} else if (itemId) {
|
||||
element = document.querySelector<HTMLElement>(`[data-advanced-focus-item-id="${CSS.escape(itemId)}"]`);
|
||||
element ??= document.querySelector<HTMLElement>(`[data-item-id="${CSS.escape(itemId)}"]`);
|
||||
}
|
||||
|
||||
const element = document.querySelector<HTMLElement>(`[data-item-id="${CSS.escape(itemId)}"]`);
|
||||
if (!element) {
|
||||
setFocusRect(null);
|
||||
return;
|
||||
@@ -84,7 +86,7 @@ export const useWidgetEditFocus = (itemId: string | undefined) => {
|
||||
window.removeEventListener("scroll", scheduleUpdate, true);
|
||||
window.cancelAnimationFrame(animationFrame);
|
||||
};
|
||||
}, [itemId]);
|
||||
}, [focusTargetId, itemId]);
|
||||
|
||||
const inspectorSide: WidgetInspectorSide =
|
||||
focusRect && focusRect.left + focusRect.width / 2 > window.innerWidth / 2 ? "left" : "right";
|
||||
@@ -95,17 +97,20 @@ export const useWidgetEditFocus = (itemId: string | undefined) => {
|
||||
export const WidgetEditFocusOverlay = ({ focusRect }: { focusRect: FocusRect | null }) => (
|
||||
<Portal>
|
||||
{focusRect ? (
|
||||
<Box
|
||||
aria-hidden
|
||||
className={classes.focusFrame}
|
||||
style={{
|
||||
top: focusRect.top,
|
||||
left: focusRect.left,
|
||||
width: focusRect.width,
|
||||
height: focusRect.height,
|
||||
zIndex: getDefaultZIndex("modal"),
|
||||
}}
|
||||
/>
|
||||
<>
|
||||
<Overlay fixed backgroundOpacity={0} zIndex={getDefaultZIndex("modal")} />
|
||||
<Box
|
||||
aria-hidden
|
||||
className={classes.focusFrame}
|
||||
style={{
|
||||
top: focusRect.top,
|
||||
left: focusRect.left,
|
||||
width: focusRect.width,
|
||||
height: focusRect.height,
|
||||
zIndex: getDefaultZIndex("modal"),
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
) : (
|
||||
<Overlay fixed color="#000" backgroundOpacity={0.58} blur={1} zIndex={getDefaultZIndex("modal")} />
|
||||
)}
|
||||
|
||||
@@ -4,14 +4,19 @@
|
||||
|
||||
.settings {
|
||||
min-width: 0;
|
||||
container-type: inline-size;
|
||||
}
|
||||
|
||||
.switchGrid {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
align-items: stretch;
|
||||
gap: var(--mantine-spacing-sm);
|
||||
}
|
||||
|
||||
.switchOption {
|
||||
display: flex;
|
||||
height: 100%;
|
||||
min-height: 4.25rem;
|
||||
align-items: center;
|
||||
padding: 0.625rem 0.75rem;
|
||||
@@ -41,7 +46,7 @@
|
||||
.footer {
|
||||
position: sticky;
|
||||
z-index: 2;
|
||||
bottom: calc(var(--mantine-spacing-md) * -1);
|
||||
bottom: 0;
|
||||
margin-inline: calc(var(--mantine-spacing-md) * -1);
|
||||
margin-bottom: calc(var(--mantine-spacing-md) * -1);
|
||||
padding: var(--mantine-spacing-sm) var(--mantine-spacing-md);
|
||||
@@ -50,6 +55,17 @@
|
||||
backdrop-filter: blur(10px);
|
||||
}
|
||||
|
||||
.footerActions {
|
||||
margin-inline-start: auto;
|
||||
}
|
||||
|
||||
@container (min-width: 30rem) {
|
||||
.switchGrid {
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
grid-auto-rows: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
.focusFrame {
|
||||
position: fixed;
|
||||
pointer-events: none;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import { useEffect, useId, useRef, useState } from "react";
|
||||
import type { ComponentType, FormEvent } from "react";
|
||||
import { Box, Button, Group, SimpleGrid, Stack, Tabs, Text } from "@mantine/core";
|
||||
import { Box, Button, Group, Stack, Tabs, Text } from "@mantine/core";
|
||||
import { schemaResolver } from "@mantine/form";
|
||||
import { IconArrowLeft, IconPencil } from "@tabler/icons-react";
|
||||
import { z } from "zod/v4";
|
||||
@@ -57,6 +57,7 @@ export interface WidgetEditModalProps<TSort extends WidgetKind> {
|
||||
settings: SettingsContextProps;
|
||||
itemId?: string;
|
||||
boardId?: string;
|
||||
focusTargetId?: string;
|
||||
appId?: string;
|
||||
integrationEditForm?: ComponentType<EmbeddedIntegrationEditFormProps>;
|
||||
onIntegrationSaved?: () => void;
|
||||
@@ -84,7 +85,7 @@ export const WidgetEditModal = createModal<WidgetEditModalProps<WidgetKind>>(({
|
||||
const [activeIntegrationId, setActiveIntegrationId] = useState<string | null>(null);
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const widgetFormId = useId();
|
||||
const { focusRect, inspectorSide } = useWidgetEditFocus(innerProps.itemId);
|
||||
const { focusRect, inspectorSide } = useWidgetEditFocus(innerProps.itemId, innerProps.focusTargetId);
|
||||
|
||||
z.config({
|
||||
customError: zodErrorMap(t),
|
||||
@@ -260,7 +261,7 @@ export const WidgetEditModal = createModal<WidgetEditModalProps<WidgetKind>>(({
|
||||
/>
|
||||
)}
|
||||
{canConfigureWidget && switchOptions.length > 0 && (
|
||||
<SimpleGrid className={classes.switchGrid} cols={{ base: 1, xs: 2 }} spacing="sm" verticalSpacing="sm">
|
||||
<Box className={classes.switchGrid}>
|
||||
{switchOptions.map(([key, value]) => {
|
||||
const Input = getInputForType(value.type);
|
||||
if (!Input) return null;
|
||||
@@ -278,7 +279,7 @@ export const WidgetEditModal = createModal<WidgetEditModalProps<WidgetKind>>(({
|
||||
</Box>
|
||||
);
|
||||
})}
|
||||
</SimpleGrid>
|
||||
</Box>
|
||||
)}
|
||||
{canConfigureWidget &&
|
||||
otherOptions.map(([key, value]) => {
|
||||
@@ -395,7 +396,7 @@ export const WidgetEditModal = createModal<WidgetEditModalProps<WidgetKind>>(({
|
||||
widgetFormContent
|
||||
)}
|
||||
</Box>
|
||||
<Group className={classes.footer} justify="space-between" wrap="nowrap">
|
||||
<Group className={classes.footer} justify="space-between" wrap="wrap">
|
||||
<Button
|
||||
variant="subtle"
|
||||
type="button"
|
||||
@@ -408,7 +409,7 @@ export const WidgetEditModal = createModal<WidgetEditModalProps<WidgetKind>>(({
|
||||
>
|
||||
{tItem("advancedOptions.label")}
|
||||
</Button>
|
||||
<Group gap="xs" wrap="nowrap">
|
||||
<Group className={classes.footerActions} gap="xs" justify="flex-end" wrap="wrap">
|
||||
<Button type="button" onClick={actions.closeModal} variant="subtle" color="gray">
|
||||
{tCommon("action.cancel")}
|
||||
</Button>
|
||||
|
||||
Reference in new issue
Block a user