# Layout and Scrolling Hooks {#布局与滚动-hooks}
These APIs operate on the template's default layout. Check [layout slots and instances](../../guide/layout) before replacing the content or sidebar.
## Interactive example {#交互示例}
::: details View example source
<<< @/../admin/src/views/demo/template-hooks/index.tsx
:::
## useLayout
Import `useLayout` from `@/layout/api` and call it in component setup. Each application instance owns a registry.
| Member | Behavior |
| -------------------------------- | --------------------------------------------------------------------------------- |
| `layouts` | Read-only list of registered layouts, including disabled choices. |
| `selectedLayoutId` | Saved layout ID used for restoration. |
| `activeLayout`, `activeLayoutId` | Resolved desktop selection. The compact host uses the full navigation drawer. |
| `registerLayout(definition)` | Explicit registration. Invalid definitions and duplicate IDs throw. |
| `updateLayout(id)` | Returns `true` for an available choice and `false` for an unknown or disabled ID. |
An optional `preview` describes the thumbnail in layout settings. For example, `{ rows: [['actions']], bottomRows: [['tabs']], sidebar: 'right', footer: true }` places actions above the content, tabs below it, and navigation on the right. The designer generates this metadata from the arrangement. A handwritten definition without a preview uses the thumbnail for its `fallbackMode`, which defaults to the sidebar layout.
See the [layout file conventions and playground](../../guide/layout#自定义布局与源码生成).
A layout component can use Vue’s `defineAsyncComponent`. With its default loading behavior, the previous page stays interactive. After the new layout mounts, the host checks that it has one content outlet. A loading error or an invalid outlet count restores the last working layout. If the user switches again while loading, the latest selection wins.
Built-in and designer-generated layouts share the main content container, preserving an iframe’s document and unsaved input when switching layouts. If a handwritten layout needs to move the page to a different DOM parent and the browser lacks state-preserving moves, the host keeps the embedded document open and indicates when it will apply the change.
## useMaximize
`useMaximize(defaults?)` returns `isLayoutContentMaximized`, its alias `isFullscreen`, `maximize(full, options?)`, and `toggleFullscreen()`. The page, tab menu, and exit button use the same Pinia state. For a single DOM element, use [useElementFullscreen](./browser#useelementfullscreen).
Per-call `options` override `defaults`. Options include `zIndex` (default 999), `backgroundColor`, `transition`, `scrollToOptions: { left?, top?, behavior? }`, `beforeEnter`, `enter`, `beforeExit`, and `exit`. The before callbacks run first. Completion callbacks run on the next Vue update and are independent of CSS animation completion. Entering can adjust scroll position. For rapid toggles, only the latest state's completion callback runs. Selecting the current state returns immediately.
```ts
import { useMaximize } from '@/hooks'
export function useReportLayout() {
const { isLayoutContentMaximized, maximize } = useMaximize()
const expand = () => maximize(true, { scrollToOptions: { top: 0 } })
return { expand, collapse: () => maximize(false), isLayoutContentMaximized }
}
```
## useSpinning
| Method | Purpose |
| ---------------------------- | ---------------------------------------------------------------------------- |
| `openSpin()` / `closeSpin()` | Toggle content-area loading independently of the app's global spinner. |
| `reload(wait?)` | Remount the current address and keep other tabs' caches. Defaults to 800 ms. |
| `reloadWorkspace(wait?)` | Remount the complete content area and release page instances for all tabs. |
Content loading uses a thin progress bar and text. `openSpin` only shows it after 120 ms to avoid flashes. `reload` displays it immediately. When indicating a pending request, call `openSpin` and pair it with `closeSpin` in `finally`. Reload loses unpersisted state on the current page. For repeated reloads, Runtime Store retains only the most recent restore timer.
```ts
import { useSpinning } from '@/hooks'
export function useLoadWithSpinner() {
const { openSpin, closeSpin } = useSpinning()
return async (load: () => Promise) => {
openSpin()
try {
return await load()
} finally {
closeSpin()
}
}
}
```
## useContentScroll
`useContentScroll()` returns a `scrollTo` function. Pass `(x, y?)` or `{ top, left, behavior }`, for example `scrollContent({ top: 0, behavior: 'smooth' })`. It resolves `LAYOUT_CONTENT_REF` at call time, so it can be created in `setup` and used in an event handler. It is a no-op while the layout is unmounted.
## useSiderScroll
`useSiderScroll()` returns a function with the same signature for `LAYOUT_SIDER_REF`. It is a no-op in top navigation mode or when the sidebar is unmounted. It scrolls the sidebar container and leaves menu selection alone.
```ts
import { useContentScroll, useSiderScroll } from '@/hooks'
export function useScrollShortcuts() {
const scrollContent = useContentScroll()
const scrollSider = useSiderScroll()
return {
toTop: () => scrollContent(0, 0),
toMenuTop: () => scrollSider(0, 0),
}
}
```
[useMaximize source](https://github.com/XiaoDaiGua-Ray/ray-template/blob/main/apps/admin/src/hooks/template/useMaximize.ts) · [useSpinning source](https://github.com/XiaoDaiGua-Ray/ray-template/blob/main/apps/admin/src/hooks/template/useSpinning.ts) · [useContentScroll source](https://github.com/XiaoDaiGua-Ray/ray-template/blob/main/apps/admin/src/hooks/template/useContentScroll.ts) · [useSiderScroll source](https://github.com/XiaoDaiGua-Ray/ray-template/blob/main/apps/admin/src/hooks/template/useSiderScroll.ts)