SpreadsheetGrid
Guides

Use it inside a dialog

Where popups go (popupContainer) and Escape when you put the grid in a modal or popover

You can put the grid in a modal (the Modal / Dialog of shadcn/ui, Radix, Headless UI, HeroUI, Mantine or UI SDK, or a plain <dialog>) or a popover. The column menu, column filters, context menu, select editor options, tool panel, fill handle menu, tooltips and drag ghosts appear inside the dialog around the grid (since 1.1.0).

Demo (a dialog to choose a customer)

popupContainer
Pick a customer
Rows 40 of 40Columns 3 of 3Sort: —
#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Rows 40 of 40Columns 3 of 3
Cell —Range —0 cells / 0 rows0 columns

The column header ⋮, a double-click on a Region cell and a right-click all open their popups inside the dialog.

  • "Open in a dialog" opens a plain <dialog> with showModal(). The column menu from the header's ⋮, the options from double-clicking "Region", and the context menu appear inside the dialog and can be pressed.
  • With popupContainer="body", popups go directly under document.body and are hidden behind the dialog (top layer) (the behavior up to 1.0.2).

Where popups go (popupContainer)

ValueWhere they are drawn
'auto' (default)Inside the nearest dialog around the grid (<dialog> / an element with the popover attribute / aria-modal="true" / role="dialog" / role="alertdialog"). Without one, a grid inside a Shadow root uses the element wrapping the grid directly under that Shadow root (since 1.3.0; Grids inside Shadow DOM); otherwise directly under document.body. When the dialog is a fixed containing block that clips its content (the content of Mantine's Modal), inside the outer position: fixed ancestor ("Things to watch" below)
'body'Always directly under document.body (the same as up to 1.0)
An elementInside that element
A functionReceives the grid's element and returns where to draw (null / undefined means document.body). Called on each render while a popup is open; not called while closed

Directly under document.body, popups may not work inside a modal. A plain <dialog>'s showModal() and popover go to the top layer, so elements directly under body are hidden behind them. Modals of Radix (shadcn/ui), React Aria (HeroUI) and Headless UI block pointers outside (pointer-events: none on body), trap focus inside and close on outside presses, so popups directly under body can't be pressed or typed into. Radix's Dialog and Popover don't set aria-modal, so they're found by role="dialog".

// shadcn/ui's Dialog: without writing anything, popups appear inside DialogContent
<Dialog>
  <DialogContent>
    <SpreadsheetGrid rows={rows} columns={columns} />
  </DialogContent>
</Dialog>

// Decide where to draw yourself
<SpreadsheetGrid rows={rows} columns={columns} popupContainer={(grid) => grid.closest<HTMLElement>('.sheet-frame')} />

The place is decided every time a popup opens. Even if the <dialog> holding the grid is opened later with showModal(), popups appear inside it. Popups are added as the dialog's last child and removed when closed.

Things to watch

  • Cancelling offsets: when an ancestor of the place has transform / translate and the like (shadcn/ui's DialogContent is centered with translate), that element becomes the containing block for position: fixed. The grid measures the offset and cancels it with the popup's translate (when opened and every time its position is recomputed). Scaling (scale) isn't cancelled.
  • Clipped by the dialog's frame: when that element clips its content with overflow and also becomes the containing block for position: fixed through transform and the like (a dialog with max-height, overflow-y: auto and transform), popups are clipped by the dialog's frame too. 'auto' uses an outer position: fixed ancestor if there is one (Mantine's Modal goes to .mantine-Modal-inner outside the content, so nothing is clipped). Without one (a DialogContent that is itself position: fixed with overflow-y: auto, for example), popups are clipped, so pass a place that doesn't clip as an element or function ('body' is fine too if the dialog doesn't block pointers and focus outside it).
  • Tokens: CSS tokens (--ssg-*) set on the table are copied to popups when they open, as before. Put the dark class of theme="class" on <html> or <body>.
  • Popups you draw yourself: popups your code draws inside a custom editor or renderCell (a list of options, for example) aren't covered by popupContainer. Portaling them to document.body hides them behind a modal <dialog>, where they can't be pressed. Like the grid, draw them inside the dialog around them (createPortal(list, anchor.closest('dialog') ?? document.body); in Solid, <Portal mount={anchor.closest('dialog') ?? document.body}>, where anchor is the editor's input or similar. For Radix and other modals, pass the same element to that library's Portal container).
  • Escape: the Escape the grid uses (cancelling an edit, closing a menu and so on) doesn't close the dialog. Details are in "A grid in a dialog" in Keyboard and touch.
  • Closing drops the grid's records: many dialogs unmount their content when closed, so the grid's change tracking (trackChanges) records, undo history and selection go too (row values stay in rows). In dialogs that may be closed before saving, use a setting that keeps the content mounted (Radix / UI SDK forceMount / keepMounted and the like), or save with getChanges() before closing. When you use AI agent tools, how to keep AI away from the table while it's closed is in Let AI agents operate the table.

On this page