Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .changeset/34604-modal-form-maximize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@object-ui/plugin-form': patch
---

feat(plugin-form): the form dialog's header has a maximize / restore button, like the record drawer's, and remembers the choice per object

Clause-②: no
Metadata-protocol: none

A create or edit dialog could not be made bigger: a long form, or a form with line items, stayed in a dialog capped at 90% of the window height. The dialog header now has the record drawer's maximize button (the same icon, size and labels, "Maximize" / "Restore size"), just before the close (X) button.

- **Maximize** widens the dialog to the `full` size (95% of the window width) and makes it fill 95% of the window height. The form body scrolls inside the dialog.
- **Restore size** puts the dialog back to its declared or inferred size.
- The form stays open the whole time. Typed values, the unsaved-changes prompt on close, and line items added in a form with `subforms` are all kept.
- Both dialog kinds have the button: a plain form and a form with `subforms` (line items).
- A maximized dialog with no declared `columns` uses the `full` column count, so a form with sixteen or more fields shows four per row.
- The choice is remembered per object in the browser's local storage. The next dialog for that object opens the same way. Other objects are not affected.
- On a phone the dialog already fills the screen, so the button is shown from the `sm` breakpoint up only.

No metadata changes. An authored `modalSize` is still the size the dialog opens with until the user maximizes it.
58 changes: 52 additions & 6 deletions ui/packages/plugin-form/src/ModalForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ import {
AlertDialogDescription,
AlertDialogAction,
AlertDialogCancel,
RecordTopBarButton,
toast,
FormSectionNavContext,
} from '@object-ui/components';
import { Loader2 } from 'lucide-react';
import { Loader2, Maximize2, Minimize2 } from 'lucide-react';
import { MasterDetailForm } from './MasterDetailForm';
import { SchemaRenderer, useSafeFieldLabel, usePreviewMode } from '@object-ui/react';
import { createSafeTranslation, useLocalization } from '@object-ui/i18n';
Expand Down Expand Up @@ -88,6 +89,7 @@ import { useUploadGate, UploadGateProvider, UploadInFlightNotice } from './uploa
import { useInitialFieldFocus } from './initialFieldFocus';
import { FormSectionNav, useFormSectionNav } from './FormSectionNav';
import { FormDirtyReportContext } from './formDirtyReport';
import { useModalFormMaximized } from './modalMaximize';

// Localized strings for the unsaved-changes guard and, since objectui#11039,
// the form's own chrome (the default submit and cancel labels and the
Expand Down Expand Up @@ -118,6 +120,10 @@ const useDiscardTranslation = createSafeTranslation(
// The heading of the fields no declared field group claims
// (`deriveFieldGroupSections`' trailing bucket).
'common.ungroupedSection': 'Other',
// The header's maximize / restore switch: the record drawer's own pair of
// keys and labels (objectstack-ai/objectstack#34604).
'detail.maximize': 'Maximize',
'detail.restoreSize': 'Restore size',
},
'form.discardTitle',
);
Expand Down Expand Up @@ -511,6 +517,12 @@ const ModalFormBody: React.FC<ModalFormProps> = ({
? effectiveSections.reduce((n, s) => n + (Array.isArray(s.fields) ? s.fields.length : 0), 0)
: layoutPool.length;

// The header's maximize switch (objectstack-ai/objectstack#34604):
// maximized, the dialog is `full` whatever its authored or inferred size,
// and every reader of the size below — the width class and the undeclared
// column count — reads that effective size.
const { maximized, toggleMaximized } = useModalFormMaximized(schema.objectName);

// The dialog's size when the author declared none (objectui#12521): bounded
// — `lg` or `xl`, never `full` — and chosen from the declared grid and the
// field count, so a short form is not stretched across a wide dialog. An
Expand All @@ -519,12 +531,13 @@ const ModalFormBody: React.FC<ModalFormProps> = ({
// size, so the size reads that floor and never the count it then bounds
// (objectstack-ai/objectstack#34607).
const effectiveModalSize = useMemo(() => {
if (maximized) return 'full';
if (schema.modalSize) return schema.modalSize;
const columns = effectiveSections?.length
? sectionedFormColumns(schema.columns, effectiveSections.map((s) => s.columns), DIALOG_DEFAULT_COLUMNS)
: (clampColumns(schema.columns) ?? DIALOG_DEFAULT_COLUMNS);
return inferModalSize(Math.min(columns, inferColumns(layoutFieldCount)));
}, [schema.modalSize, schema.columns, effectiveSections, layoutFieldCount]);
}, [maximized, schema.modalSize, schema.columns, effectiveSections, layoutFieldCount]);

// The count an undeclared form or section draws in THIS dialog
// (objectstack-ai/objectstack#34607): from the field count, bounded by the
Expand All @@ -542,6 +555,13 @@ const ModalFormBody: React.FC<ModalFormProps> = ({
}, [formFields, objectSchema, flatColumns, schema.mode, effectiveSections]);

const sizeClass = modalSizeClasses[effectiveModalSize] || modalSizeClasses.default;
// The content's height: a phone sheet is always the whole screen; from `sm`
// up the dialog grows with its body to a `90vh` cap, and maximized it fills
// `95dvh` — the height counterpart of the `full` size's `95vw` — so its body
// scroller, not the page, scrolls a long form.
const heightClass = maximized
? 'h-[100dvh] sm:h-[95dvh] sm:max-h-[95dvh]'
: 'h-[100dvh] sm:h-auto sm:max-h-[90vh]';

// Fetch object schema
useEffect(() => {
Expand Down Expand Up @@ -1226,6 +1246,28 @@ const ModalFormBody: React.FC<ModalFormProps> = ({
attemptClose(false);
};

// The header's maximize / restore switch (objectstack-ai/objectstack#34604):
// the record drawer's control — its `RecordTopBarButton`, its lucide pair at
// its size, its labels — drawn just before the dialog's close (X), in both
// arms. The last child of the content, so it sits after the form in the tab
// order and right before the X, as the drawer orders them. From `sm` up
// only: the phone sheet already fills the screen. Toggling changes the
// content's classes and nothing else, so the form below stays mounted.
const maximizeButton = (
<RecordTopBarButton
label={maximized ? t('detail.restoreSize') : t('detail.maximize')}
onClick={toggleMaximized}
data-testid="modal-form-maximize"
className={cn('absolute top-2.5 hidden sm:inline-flex', showCloseButton ? 'right-9' : 'right-3')}
>
{maximized ? <Minimize2 className="h-3.5 w-3.5" /> : <Maximize2 className="h-3.5 w-3.5" />}
</RecordTopBarButton>
);
// The header keeps its title clear of the switch and the X from `sm` up.
// Its bottom rule is each arm's: the flat arm hands it to the section nav
// strip when one is drawn.
const headerClass = 'shrink-0 px-4 pt-4 sm:px-6 sm:pt-6 sm:pr-20 pb-2';

// Unsaved-changes guard — rendered INSIDE DialogContent so its portal
// inherits the Dialog's Radix layer context (focus scope + dismissable
// layer) through React. As a sibling of <Dialog> it had no such context, so
Expand Down Expand Up @@ -1265,10 +1307,11 @@ const ModalFormBody: React.FC<ModalFormProps> = ({
showCloseButton={showCloseButton}
onOpenAutoFocus={initialFieldFocus.onOpenAutoFocus}
onCloseAutoFocus={initialFieldFocus.onCloseAutoFocus}
className={cn(sizeClass, 'flex flex-col h-[100dvh] sm:h-auto sm:max-h-[90vh] overflow-hidden p-0', className, schema.className)}
className={cn(sizeClass, 'flex flex-col overflow-hidden p-0', heightClass, className, schema.className)}
data-maximized={maximized ? '' : undefined}
>
{(schema.title || schema.description) && (
<DialogHeader className="shrink-0 px-4 pt-4 sm:px-6 sm:pt-6 pb-2 border-b">
<DialogHeader className={cn(headerClass, 'border-b')}>
{schema.title && <DialogTitle>{schema.title}</DialogTitle>}
{schema.description ? (
<DialogDescription>{schema.description}</DialogDescription>
Expand Down Expand Up @@ -1319,6 +1362,7 @@ const ModalFormBody: React.FC<ModalFormProps> = ({
</Suspense>
</div>
{discardDialog}
{maximizeButton}
</MobileDialogContent>
</Dialog>
);
Expand All @@ -1332,10 +1376,11 @@ const ModalFormBody: React.FC<ModalFormProps> = ({
showCloseButton={showCloseButton}
onOpenAutoFocus={initialFieldFocus.onOpenAutoFocus}
onCloseAutoFocus={initialFieldFocus.onCloseAutoFocus}
className={cn(sizeClass, 'flex flex-col h-[100dvh] sm:h-auto sm:max-h-[90vh] overflow-hidden p-0', className, schema.className)}
className={cn(sizeClass, 'flex flex-col overflow-hidden p-0', heightClass, className, schema.className)}
data-maximized={maximized ? '' : undefined}
>
{(schema.title || schema.description) && (
<DialogHeader className={cn('shrink-0 px-4 pt-4 sm:px-6 sm:pt-6 pb-2', !drawsSectionNav && 'border-b')}>
<DialogHeader className={cn(headerClass, !drawsSectionNav && 'border-b')}>
{schema.title && <DialogTitle>{schema.title}</DialogTitle>}
{schema.description ? (
<DialogDescription>{schema.description}</DialogDescription>
Expand Down Expand Up @@ -1416,6 +1461,7 @@ const ModalFormBody: React.FC<ModalFormProps> = ({
the Dialog content the conflict alert stacks as the topmost Radix
layer, so its buttons receive clicks while the modal stays open. */}
{conflictDialog}
{maximizeButton}
</MobileDialogContent>
</Dialog>
);
Expand Down
Loading
Loading