TabView
The floating glass tab bar. Press and drag across it and a lens follows the finger. On a phone it can minimize while content scrolls and carry an accessory; on a tablet it becomes a top bar, with a sidebar when there is room.
import { Tab, TabSection, TabView } from "@adamexu/glass";Phone
The TabView fills its box, and the selected tab's content fills it under the bar. Scroll a page down and the bar shrinks into the selected tab's circle, with the accessory beside it and the search tab in a circle of its own; scroll back to the top and it grows again.
const [selection, setSelection] = useState("home");
<div style={{ position: "relative", width: 402, height: 640, overflow: "hidden" }}>
<TabView
selection={selection}
onSelectionChange={setSelection}
idiom="phone"
minimizeBehavior="onScrollDown"
bottomAccessory={<NowPlaying />}
>
<Tab value="home" title="Home" icon={<House size={24} />}><Page title="Home" /></Tab>
<Tab value="inbox" title="Inbox" icon={<Inbox size={24} />} badge={3}><Page title="Inbox" /></Tab>
<Tab value="shared" title="Shared" icon={<Users size={24} />}><Page title="Shared" /></Tab>
<Tab value="files" title="Files" icon={<FolderOpen size={24} />}><Page title="Files" /></Tab>
<Tab value="search" role="search" title="Search" icon={<Search size={24} />}><Page title="Search" /></Tab>
</TabView>
</div>A page that scrolls should be its own scroller filling the tab (for example position: absolute; inset: 0; overflow-y: auto) with an opaque background. Keep each tab's content element stable (a constant or a memo) so hidden tabs don't re-render when the selection changes. More than five tabs put the fifth and later under More, a list with its own back button. A box wider than tall lays the items inline.
Tablet
With the pad idiom the bar floats at the top from 668 px wide. With tabViewStyle="sidebarAdaptable", a landscape box over 1024 px wide shows a sidebar too, toggled from the bar; this box is narrower, so it shows the top bar alone. Content under the pad bars is inset by --tab-view-inset-top|left|bottom|right, set on the content.
<TabView selection={selection} onSelectionChange={setSelection} idiom="pad" tabViewStyle="sidebarAdaptable">
<Tab value="home" title="Home" icon={<House size={24} />}><Page title="Home" /></Tab>
<Tab value="starred" title="Starred" icon={<Star size={24} />}><Page title="Starred" /></Tab>
<TabSection title="Folders">
<Tab value="files" title="Files" icon={<FolderOpen size={24} />}><Page title="Files" /></Tab>
<Tab value="shared" title="Shared" icon={<Users size={24} />}><Page title="Shared" /></Tab>
</TabSection>
<Tab value="settings" title="Settings" icon={<Settings size={24} />}><Page title="Settings" /></Tab>
<Tab value="search" role="search" title="Search" icon={<Search size={24} />}><Page title="Search" /></Tab>
</TabView>Props
TabView
| Prop | Type | Default | Description |
|---|---|---|---|
| selection | V extends string | number | - | Required. The selected tab's value; the TabView is always controlled. |
| onSelectionChange | (value: V) => void | - | A tap, a drag's lift-off, a sidebar or More row. |
| tabViewStyle | "automatic" | "sidebarAdaptable" | "tabBarOnly" | "automatic" | sidebarAdaptable: wide pad windows show a sidebar, and the top bar a sidebar toggle. |
| minimizeBehavior | "automatic" | "onScrollDown" | "onScrollUp" | "never" | "automatic" | Phone: the bar shrinks into the selected tab's circle while the content scrolls (automatic does not minimize). |
| bottomAccessory | ReactNode | - | Phone: glass above the bar (a now-playing row), beside the minimized bar. |
| idiom | "auto" | "phone" | "pad" | "duo" | "auto" | auto lays out as an iPad window of the box's size (a bottom bar under 668 px). Pass phone for an iPhone; duo for the foldable's layouts. |
| defaultTabBarPlacement | "automatic" | "sidebar" | "tabBar" | "automatic" | Duo with tabViewStyle="sidebarAdaptable": "sidebar" floats a sidebar on the inner screen. |
| customization | boolean | false | Pad: the sidebar's Edit, and holding a top-bar item to customize the bar. |
| sidebarFocus | boolean | true | Pad: the selected sidebar row as with keyboard focus (blue) or without (gray). |
| safeAreaInsets | Partial<{ top; left; bottom; right }> | an iPad's full screen | Pad: the window's safe-area insets in px (full screen 32 top and 20 bottom; a window that does not fill the screen 10 and 10). |
| debugCellWidths, debugEditing | number[], boolean | - | For comparisons with iOS only: UIKit's top-bar cell widths; start the sidebar in Edit mode. |
| className, style | string, CSSProperties | - | On the TabView's box. It fills its parent (100% by 100%). |
| children | ReactNode | - | Tab and TabSection elements. |
Tab
| Prop | Type | Default | Description |
|---|---|---|---|
| value | V | - | Required. The tab's value. |
| title | string | - | Required. Its title. |
| icon | ReactNode | - | Required. 24 px, currentColor (Lucide icons fit). |
| badge | number | string | - | A count or short text; 0 and "" hide it. |
| role | "search" | - | The search tab: its own circle beside the minimized bar on phones, the bar's trailing button on pads. |
| customizationId | string | - | Identifies it for customization. |
| customizationBehavior | "automatic" | "reorderable" | "disabled" | - | disabled: a bare sidebar row and a dimmed bar item. reorderable: no checkmark, it cannot be hidden. |
| tabPlacement | "automatic" | "pinned" | "sidebarOnly" | "optional" | - | sidebarOnly never goes to the bar; optional starts off the bar and keeps its capsule in a sidebar-only section. |
| defaultVisibility | "automatic" | "hidden" | - | hidden: starts off the bar, in the sidebar, and can be dragged onto it. |
| sidebarItem | TabSidebarItem | - | Pad sidebar row options, below. |
| children | ReactNode | - | The tab's content. |
TabSidebarItem
| Prop | Type | Default | Description |
|---|---|---|---|
| onDelete | () => void | - | Shows a delete control in Edit mode; called once the row has gone. |
| thumbnail | string | - | An image URL drawn as is, in place of icon. |
| wraps | boolean | - | Long titles wrap to two lines and the row grows. |
| locked | boolean | - | Draws a lock. |
| fixedInSection | boolean | - | No reorder control. |
TabSection
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | - | Required. The section's title. |
| customizationId | string | - | Identifies it for customization. |
| tabPlacement | "automatic" | "sidebarOnly" | - | sidebarOnly: the section is not one item in the bar; its tabs can go there alone. |
| children | ReactNode | - | Tab elements. |
Behaviour
- Phone: the floating bottom bar, items stacked (inline when the box is wider than tall); more than five tabs go under More.
- Pad: a floating top bar from 668 px wide, and a sidebar with sidebarAdaptable in a landscape box over 1024 px wide; a narrower box keeps a bottom bar with the pad's spacing.
- Duo: a vertical strip at the trailing edge, or a floating sidebar.
A tab once shown keeps its state and scroll; one not shown yet is built while the page is idle, so its first show is as quick as the rest. Hidden tabs are inert under content-visibility: hidden. Bar items are buttons named by their titles.
SwiftUI
| SwiftUI | @adamexu/glass |
|---|---|
| TabView(selection:) | <TabView selection onSelectionChange> |
| Tab("Home", systemImage: "house", value: 0) | <Tab value={0} title="Home" icon={<House />}> |
| Tab(role: .search) | <Tab role="search"> |
| .badge(3) | badge={3} |
| TabSection("Albums") | <TabSection title="Albums"> |
| .tabViewStyle(.sidebarAdaptable) | tabViewStyle="sidebarAdaptable" |
| .tabBarMinimizeBehavior(.onScrollDown) | minimizeBehavior="onScrollDown" |
| .tabViewBottomAccessory { } | bottomAccessory={...} |
| .tabViewCustomization($c) | customization |
| .customizationID(_:) / .customizationBehavior(_:for:) | customizationId / customizationBehavior |
| .tabPlacement(.sidebarOnly) | tabPlacement="sidebarOnly" |
| .defaultVisibility(.hidden, for: .tabBar) | defaultVisibility="hidden" |
| .defaultTabBarPlacement(.sidebar) | defaultTabBarPlacement="sidebar" |