---
title: "Tab View Navigator"
description: "Open any number of documents as tabs, with context menus and window transfers, using the tab view navigator."
---

# Tab View Navigator

`createTabViewNavigator` renders its routes as the pages of an `AdwTabView`, with an `AdwTabBar` under the header and an optional `AdwTabOverview`. Unlike the [tab navigator](/guide/navigation#tab-navigator), it opens any number of tabs from one screen, so a document or browser window declares the screen once and opens it repeatedly:

```tsx [src/documents.tsx]
import * as Gtk from "@gtkx/gi/gtk";
import { GtkBox, GtkButton, GtkLabel } from "@gtkx/jsx/gtk";
import {
    createTabViewNavigator,
    type TabViewScreenProps,
} from "@gtkx/navigation";

type DocumentParams = { Document: { title: string } };

const Tabs = createTabViewNavigator<DocumentParams>();

const Document = ({
    route,
    navigation,
}: TabViewScreenProps<DocumentParams, "Document">) => (
    <GtkBox
        orientation={Gtk.Orientation.VERTICAL}
        spacing={12}
        halign={Gtk.Align.CENTER}
        valign={Gtk.Align.CENTER}
    >
        <GtkLabel label={route.params.title} />
        <GtkButton
            label="Open related"
            onClicked={() =>
                navigation.openTab(
                    "Document",
                    { title: "Related" },
                    { parent: route.key },
                )
            }
        />
    </GtkBox>
);

export const Documents = () => (
    <Tabs.Navigator
        overview={{
            enableNewTab: true,
            onCreateTab: () => ({
                name: "Document",
                params: { title: "Untitled" },
            }),
        }}
        screenOptions={({ route }) => ({ title: route.params.title })}
    >
        <Tabs.Screen
            name="Document"
            component={Document}
            initialParams={{ title: "Untitled" }}
        />
    </Tabs.Navigator>
);
```

<figure class="result">
    <img class="result-light" src="/figures/guide-navigation-tab-view-light.png" width="440" height="280" loading="lazy" alt="A window titled Documents with a tab bar holding an Untitled tab and a selected Related tab, above a page showing Related and an Open related button." />
    <img class="result-dark" src="/figures/guide-navigation-tab-view-dark.png" width="440" height="280" loading="lazy" alt="A window titled Documents with a tab bar holding an Untitled tab and a selected Related tab, above a page showing Related and an Open related button." />
    <figcaption>Captured from the sample above after clicking <strong>Open related</strong> in the first tab.</figcaption>
</figure>

The navigator comes from `@gtkx/navigation` and renders inside a [`NavigationContainer`](/guide/navigation#navigationcontainer).

## Opening and closing tabs

Tabs are addressed by route key. The navigation object adds [tab actions](/reference/@gtkx/navigation/type-aliases/TabViewActionHelpers) such as `openTab`, `closeTab`, `selectTab` and `pinTab`, also available as `TabViewActions`. Opening with `parent` places the tab after its parent and the parent's descendants, and closing it selects that parent again. Pinned tabs stay ahead of the others; the state's `pinnedKeys` lists them.

[Screen options](/reference/@gtkx/navigation/type-aliases/TabViewNavigationOptions) set the native tab page's properties, such as `title`, `icon`, `loading` and `needsAttention`. The header shows the window title unless `headerTitle` is set. Screens mount when first selected unless `lazy: false`.

`navigate` selects the last open tab of a screen and updates its params, or opens one when none is open. Give the screen a `getId` to identify tabs by their params: `navigate` and `openTab` then select the tab with the same id and open a new tab for a new id.

Native interaction flows back into the state: selecting a tab emits a cancellable `tabPress`, dragging reorders the route, and the close button dispatches `closeTab`, which `usePreventRemove` can refuse.

Closing the last tab is left unhandled, so a parent navigator or the container's `onUnhandledAction` decides whether the window closes. A tab view inside a section of the app has no window to close; set `replaceLastTab` there, and closing the last tab replaces it with a new tab of the initial route.

The overview's `open` follows the same rule as other [open-state props](/guide/modals-and-portals#closing): when given, update it from `onOpenChange`, or leave it out to let the overview open and close on its own. The [tab view configuration reference](/reference/@gtkx/navigation/type-aliases/TabViewNavigationConfig) lists the tab bar, overview, menu and drop target settings.

## Tab context menu

`renderMenu` declares the menu that right-clicking a tab or pressing Shift+F10 opens. It receives a ref to attach to a `GMenu` anywhere in what it returns, directly or through a component of yours that passes the ref on; the navigator renders the result outside the widget tree and uses that menu. `onSetupMenu` reports the tab the menu opens for, and `null` once it closes after the chosen action has run:

```tsx
import { GMenu, GSimpleAction } from "@gtkx/jsx/gio";
import { AdwApplicationWindow } from "@gtkx/jsx/adw";
import {
    NavigationContainer,
    TabViewActions,
    useNavigationContainerRef,
} from "@gtkx/navigation";
import { useRef } from "react";

export const DocumentWindow = () => {
    const navigation = useNavigationContainerRef<DocumentParams>();
    const menuTab = useRef<string | null>(null);

    return (
        <AdwApplicationWindow
            actions={
                <GSimpleAction
                    name="close-others"
                    onActivate={() =>
                        navigation.dispatch(
                            TabViewActions.closeOtherTabs(
                                menuTab.current ?? undefined,
                            ),
                        )
                    }
                />
            }
        >
            <NavigationContainer ref={navigation}>
                <Tabs.Navigator
                    renderMenu={(ref) => (
                        <GMenu
                            ref={ref}
                            items={[
                                {
                                    label: "Close Other Tabs",
                                    action: "win.close-others",
                                },
                            ]}
                        />
                    )}
                    onSetupMenu={(route) => {
                        menuTab.current = route?.key ?? null;
                    }}
                >
                    <Tabs.Screen name="Document" component={Document} />
                </Tabs.Navigator>
            </NavigationContainer>
        </AdwApplicationWindow>
    );
};
```

To reuse a menu model that already exists, such as one shared with another widget, pass it as `menuModel` instead. A navigator given both `renderMenu` and `menuModel` throws.

Without a tab from `onSetupMenu`, as when the same action runs from a shortcut, the actions act on the selected tab.

## Windows and transfers

Pass `onCreateWindow` to open a window for a tab dragged out: create an `Adw.TabView`, store it, and return it. Render the new window with its own `NavigationContainer` and a navigator that receives that view through the `view` prop; the transferred page becomes its initial route. A page dragged into another window is adopted by that navigator with its params, and its screen mounts again there, so keep tab state in params or an external store. `useTabView()` gives screens and header content the native view, `getPage(key)` and `transferTab(key, target)`.

The [Adwaita demo](https://github.com/gtkx-org/gtkx/tree/v2.1.0/examples/adwaita-demo) shows the complete window lifecycle with menus, shortcuts and the overview.

## Tabs without the navigator

Ordinary `AdwTabView` children work for tabs that share their parent's React lifetime. For tabs the user opens, closes, reorders and drags between windows, use the tab view navigator, which keeps the open tabs in navigation state and follows native transfers.

A JSX tab stays open while React renders it. To let the user close tabs with their close buttons or the tab overview, handle `onClosePage`: remove the page from the state that renders it and confirm the close. Without a handler, the close does nothing and GTKX warns in development; see [Closing](/guide/modals-and-portals#closing).

```tsx [src/documents.tsx]
import type * as Adw from "@gtkx/gi/adw";
import * as Gtk from "@gtkx/gi/gtk";
import { AdwTabBar, AdwTabPage, AdwTabView } from "@gtkx/jsx/adw";
import { GtkBox, GtkLabel } from "@gtkx/jsx/gtk";
import { useState } from "react";

export const Documents = () => {
    const [titles, setTitles] = useState(["Notes", "Ideas"]);
    const [view, setView] = useState<Adw.TabView | null>(null);

    return (
        <GtkBox orientation={Gtk.Orientation.VERTICAL}>
            <AdwTabBar view={view} />
            <AdwTabView
                ref={setView}
                vexpand
                onClosePage={(page, tabView) => {
                    setTitles((current) =>
                        current.filter((title) => title !== page.getTitle()),
                    );
                    tabView.closePageFinish(page, true);
                    return true;
                }}
            >
                {titles.map((title) => (
                    <AdwTabPage key={title} title={title}>
                        <GtkLabel>{title}</GtkLabel>
                    </AdwTabPage>
                ))}
            </AdwTabView>
        </GtkBox>
    );
};
```

<figure class="result">
    <img class="result-light" src="/figures/guide-components-tabs-light.png" width="400" height="260" loading="lazy" alt="A window titled AdwTabView with a tab bar holding a selected Notes tab with a close button and an Ideas tab, above the Notes page." />
    <img class="result-dark" src="/figures/guide-components-tabs-dark.png" width="400" height="260" loading="lazy" alt="A window titled AdwTabView with a tab bar holding a selected Notes tab with a close button and an Ideas tab, above the Notes page." />
    <figcaption>Captured from the sample above. The tab bar shows a tab for each page the component renders.</figcaption>
</figure>

`onNotifySelectedPage` reports the selection libadwaita makes: the first page once the view mounts, and the neighbour it selects after your state drops the selected page, so a window subtitle or per-tab action mirrored from that handler stays current.

`AdwTabPage` accepts `pinned` in ordinary JSX, where keyed pages keep their requested order within each pin group and pinning follows libadwaita's insertion order; clear a nullable object prop such as its `icon` with an explicit `null`. `AdwTabBar` and `AdwTabOverview` accept `extraDropTarget`, whose `actions` and `types` (GType values or generated classes) can follow React state, and report drops to `onExtraDragDrop`.
