Tab Outlet
The router outlet whose routes are an app's tabs, which it keeps once they've been shown. import { LynxTabOutlet, LynxTabPage } from '@blotch/angular-lynx';
Usage
See Keep each tab alive for what it does.
API
LynxTabOutlet
A RouterOutlet whose routes are an app's tabs, which it keeps once they've been shown, as UITabBarController and Material's navigation bar do. Use it in place of <router-outlet> beside a tab bar or a dock: it has the same inputs, outputs and API.
For example, [keep]="['home', 'library']" or [prebuild]="['library', 'profile']". A tab built ahead makes the first tap on it as quick as a return.
Which routes are tabs
- Each route is one tab, whatever its params.
/item/1and/item/2share the tab ofitem/:id, as they share its component. - A route without a component whose children (
childrenorloadChildren) start with the tab's component on''is the tab, under its own path.keep,prebuild, and itskeepAliveandprebuilddata go by it.
Hidden tabs
- A hidden tab is
visibility: hiddenand moved out of the outlet, which clips it. - Why not
display: none: on Lynx, it resizes everything in the tab to 0×0 and loses its scroll positions. - A route's
data: { keepAlive: false }destroys its tab when it's hidden.keepAlive: truekeeps it whateverkeepsays. - A navigation to a hidden tab's own URL goes to the URL the tab was at when it was hidden. A guard the outlet adds to the router's copy of each tab's route does this.
- These go where they say: Back and Forward, a navigation with a query or a deeper path, and one whose
infois{ stack: 'root' }. - A
LynxStackOutletin a tab keeps its screens while the tab is hidden. When the tab comes back, the stack puts its screens' entries back in the history before the current one, so Back goes through them. - With a
RouteReuseStrategythat detaches a tab, the outlet hands it over, as<router-outlet>does, and its native views go. - With
withErrorBoundaries(), a tab that throws shows its route's error component in its frame, andisErrorComponentActiveistruewhile the tab shown does, as on<router-outlet>. A tab showing its error component isn't kept: switching back to it builds it again. A hidden tab that fails gets onlyerror, as the router binds route inputs only for the tab shown, so make the error component's route inputs optional. A tab built ahead that fails in its first check is left out and its error goes to theErrorHandler: Angular builds no route before the router goes to it, and the boundary'sRedirectCommandwould navigate away from where you are. See When a tab fails.
Switching tabs
- The tab left stays on screen until the next one is drawn, then the two swap in a frame.
- A tab built on its first visit shows once the app's next change detection has drawn its bindings,
@ifand@for. Until then it held nothing, and the screen would flash white between the two. - On iOS, a Core Animation transition that ends while a tab is being built draws the screen partway through. It shows the tab left rather than nothing.
Tabs built ahead
A tab is built ahead when it's in prebuild, or its route has data: { prebuild: true }. The route's data wins over the list either way.
- When: once the first tab is drawn, one at a time, each between frames. Then it's hidden.
- What: its component and the screens its URL leads to, with their guards and resolvers run (those of the routes above it too).
- Its
LynxTabPage.active()isfalse. - The router doesn't move: its URL, its history and the tab bar's selection stay put. The router's navigation to the tab shows it as a tab coming back.
- Which tabs it can build: the routes beside the one shown that have a path without a parameter, and a component or children that start with one on
''. - Which it can't: a tab the outlet doesn't keep, or one whose path has a parameter. In development, it warns of a tab it's asked for and can't build.
- Its elements don't get
binduiappearuntil it's shown.
LynxTabPage
The tab of a LynxTabOutlet that a component is in. Any component in the tab can inject it, the screens of a stack in the tab included: inject(LynxTabPage, { optional: true }), which is null outside a tab.
active()istruewhile the tab is the one shown.- A hidden tab's components keep running, so use
active()to pause what only matters while it shows.
Related
- Keep each tab alive: what the tab outlet does, with examples.
- Stack Outlet: a stack of screens inside a tab.
- Routing:
provideRouter()and the rest of the router API. - Tab Bar and Dock
