Skip to content

Modals and Portals ​

Windows and Adwaita dialogs are JSX elements: rendering one opens it, and the app closes it by no longer rendering it. GTKX places them natively, outside the widget tree they are declared in. Use createPortal for other objects that belong outside their surrounding container.

Windows ​

Window elements present themselves on mount and destroy the native window on unmount. A hidden Activity only hides a window, and its reveal shows the same window, though GTK closes the popovers and menus inside it and the reveal reopens only those given visible or active under future.v3ControlledRestore. Render a secondary window conditionally, and handle onCloseRequest by clearing the state that rendered it:

tsx
import {
    AdwApplicationWindow,
    AdwHeaderBar,
    AdwToolbarView,
} from "@gtkx/jsx/adw";

const MirrorWindow = ({
    open,
    onClose,
}: {
    open: boolean;
    onClose: () => void;
}) =>
    open ? (
        <AdwApplicationWindow
            title="Mirror"
            defaultWidth={400}
            defaultHeight={300}
            onCloseRequest={() => {
                onClose();
                return true;
            }}
        >
            <AdwToolbarView top={<AdwHeaderBar />} />
        </AdwApplicationWindow>
    ) : null;

Returning true keeps the window until React unmounts it.

A window that should keep its state while hidden can stay mounted and pass visible instead, setting it to false from onCloseRequest. It also needs hideOnClose, or GTK destroys it when the user closes it; with future.v3ControlledRestore, GTKX refuses the close instead, as described in Controlled Props. Mounted with visible={false}, it stays hidden until visible becomes true. defaultVisible={false} mounts a window hidden without controlling it.

AdwApplicationWindow registers with its nearest application element, normally AdwApplication, and throws without one. To associate it with another window, pass transientFor explicitly.

Plain AdwWindow and GtkWindow elements default transientFor to their nearest window ancestor. An explicit value selects another parent; null keeps the window independent.

Dialogs ​

Adwaita dialog elements present on mount over their nearest window and close on unmount. Handle onClosed by clearing the state that rendered the dialog:

tsx
import { AdwDialog } from "@gtkx/jsx/adw";
import { GtkLabel } from "@gtkx/jsx/gtk";

const Notice = ({ onClose }: { onClose: () => void }) => (
    <AdwDialog onClosed={onClose} title="Notice">
        <GtkLabel>Nothing to report.</GtkLabel>
    </AdwDialog>
);

Specialized dialogs keep the same lifecycle. Place their content in the supported JSX slots; for example, AdwPreferencesDialog takes AdwPreferencesPage children.

Without onClosed, the close button and Escape do nothing and the dialog stays open, as described in Closing. To confirm before closing, for example when there are unsaved changes, set canClose={false} and handle onCloseAttempt, then stop rendering the dialog once the user confirms. Unmounting always closes the dialog.

AdwAlertDialog takes plain-text heading and body values and a responses array. Its onResponse prop receives the selected response ID and also answers the close request. Children appear between the body and the response buttons:

src/dialogs/rename-dialog.tsx
tsx
import * as Adw from "@gtkx/gi/adw";
import { AdwAlertDialog } from "@gtkx/jsx/adw";
import { GtkEntry } from "@gtkx/jsx/gtk";

export function RenameDialog({
    onResponse,
}: {
    onResponse: (id: string) => void;
}) {
    return (
        <AdwAlertDialog
            heading="Rename"
            body="Pick a new name for this list."
            defaultResponse="rename"
            closeResponse="cancel"
            responses={[
                { id: "cancel", label: "Cancel" },
                {
                    id: "rename",
                    label: "Rename",
                    appearance: Adw.ResponseAppearance.SUGGESTED,
                },
            ]}
            onResponse={onResponse}
        >
            <GtkEntry placeholderText="List name" activatesDefault />
        </AdwAlertDialog>
    );
}
A window titled Lists, dimmed behind an alert dialog with the heading Rename, the body Pick a new name for this list., an empty List name entry, and Cancel and Rename buttons, Rename highlighted.A window titled Lists, dimmed behind an alert dialog with the heading Rename, the body Pick a new name for this list., an empty List name entry, and Cancel and Rename buttons, Rename highlighted.
Captured from the sample above. SUGGESTED colors the Rename response.

About dialog metadata ​

AdwAboutDialog accepts links, creditSections, acknowledgementSections, legalSections, and otherApps as arrays:

src/dialogs/about.tsx
tsx
import * as Gtk from "@gtkx/gi/gtk";
import { AdwAboutDialog } from "@gtkx/jsx/adw";

export function About({ onClose }: { onClose: () => void }) {
    return (
        <AdwAboutDialog
            onClosed={onClose}
            applicationName="Notes"
            links={[{ title: "User Guide", url: "https://example.org/guide" }]}
            creditSections={[{ name: "Design", people: ["Ada Lovelace"] }]}
            acknowledgementSections={[
                { name: "Thanks", people: ["Our translators"] },
            ]}
            legalSections={[
                {
                    title: "Sample data",
                    copyright: "© Example",
                    licenseType: Gtk.License.MIT_X11,
                },
            ]}
            otherApps={[
                {
                    appid: "org.example.Calendar",
                    name: "Calendar",
                    summary: "Plan your day",
                },
            ]}
        />
    );
}
A window titled Notes behind an about dialog headed Notes, with a Details row, a group of Credits, Legal and Acknowledgements rows, and a Calendar row reading Plan your day with an external link icon.A window titled Notes behind an about dialog headed Notes, with a Details row, a group of Credits, Legal and Acknowledgements rows, and a Calendar row reading Plan your day with an external link icon.
Captured from the sample above. Libadwaita puts links under Details, gives each section array its own page, and lists otherApps at the bottom.

Libadwaita cannot remove these entries. Each array becomes fixed when its first nonempty value is applied; equivalent arrays on later renders are accepted, and a different one throws. To change or remove a populated list, give the dialog a new React key. Legal-section copyright and custom license text accept Pango markup, so escape externally supplied plain text with GLib.markupEscapeText.

Popovers ​

A GtkPopover rendered as a child of a widget is anchored to that widget. Open it with visible, or with defaultVisible when it only starts open. A GtkMenuButton opens its popover with active or defaultActive:

tsx
<GtkButton label="Details" onClicked={() => setOpen(true)}>
    <GtkPopover visible={open} onClosed={() => setOpen(false)}>
        <GtkLabel>Button details</GtkLabel>
    </GtkPopover>
</GtkButton>

GTKX applies these props once the window has painted a frame, so a popover can be open from the first render. A direct call to popup() or setVisible(true) on the popover does not wait. GTK blocks until the compositor answers a popup, and some compositors, sway among them, never answer one for a window they have not shown yet: the popup does not appear, timers stop firing and the window stops repainting. Call popup() only from something that happens once the window is on screen, such as a click.

Closing ​

The app decides what is open by what it renders. A close button, Escape, a gesture or the window manager only asks to close, and the app answers by rendering something else. Until it answers, GTKX keeps the native state matching React as far as the toolkit allows:

ElementAnswered byWhen nothing answers
Adwaita dialogsonClosed or onCloseAttempt; onResponse on AdwAlertDialogThe dialog stays open.
AdwTabPage in a JSX AdwTabViewA close-page handler on the view: onClosePage or one connected with useSignalThe tab stays.
AdwBottomSheet given openonNotifyOpen or onCloseAttemptThe sheet stays open.
Open-state propsRendering the new value, usually from the matching onNotify… handlerGTKX restores the rendered value before the next frame.
WindowsonCloseRequestThe window closes, so the user can always close it. Under future.v3ControlledRestore, a window given visible stays open.
visible on GtkPopover, active on GtkMenuButtononClosed or onNotifyVisible, onNotifyActiveThe popover closes. Under future.v3ControlledRestore, GTKX opens it again before the next frame.

Returning false from onClosePage lets libadwaita close the tab without the app answering.

The open-state props are showSidebar on AdwOverlaySplitView, showContent on AdwNavigationSplitView, open on AdwBottomSheet and AdwTabOverview, and searchModeEnabled on GtkSearchBar. Like selectedIndex on GtkListBox, each one is controlled when given; omitting it leaves it to the widget.

Controlled Props describes how the v3ControlledRestore future flag treats visible on windows and popovers and active on GtkMenuButton.

In development, GTKX warns once per element type when nothing answers, naming the handler to add. It also warns when a window other than the last one, a dialog or a popover closed while the app still renders it open, as when an onClosed handler only logs.

Finding the parent window ​

useParentWindow() from @gtkx/react returns the nearest window once its native instance exists. It returns null while the instance is unavailable or when there is no window ancestor. The lookup follows component ancestry through portals, so portaled dialogs can still use their originating window.

createPortal ​

createPortal from @gtkx/react renders into a native object whose child-placement rules accept the content. Capture that target with a state callback ref:

tsx
import type * as Gtk from "@gtkx/gi/gtk";
import { GtkBox, GtkLabel } from "@gtkx/jsx/gtk";
import { createPortal } from "@gtkx/react";
import { useState } from "react";

const StatusArea = () => {
    const [tray, setTray] = useState<Gtk.Box | null>(null);
    return (
        <>
            <GtkBox ref={setTray} />
            {tray && createPortal(<GtkLabel>Synced</GtkLabel>, tray)}
        </>
    );
};

Pass rootElement from @gtkx/react as the target when the object needs React ownership without a native parent. The async operations example uses this for a file dialog.

See the createPortal reference for GTKX's arguments, and React's portal documentation for context and component lifetime.

Synchronous native callbacks ​

Some native signals need an object returned before their handler finishes. For example, AdwTabOverview.onCreateTab returns the new Adw.TabPage, and AdwTabView.onCreateWindow returns the destination Adw.TabView.

React commits asynchronously, so JSX rendered in response to the signal is not ready in time. Create the native object in the handler, keep it in state, and render into it with createPortal once React commits. For a new tab, append an Adw.Bin to the view and return its page; for a new window, construct the Adw.TabView and mount the window around it. The tab view navigator packages this pattern for tabs, and the list components use it for their cells.

createRoot describes independent roots, which keep a tab's widget and state when libadwaita transfers the tab to another window.

The exported API is in the @gtkx/react reference.