TopAppBar
The Top App Bar provides context and actions for the current screen, supporting branding, navigation, search, and other actions.
Usage
Import the component:
import 'mdui/components/top-app-bar.js';
import 'mdui/components/top-app-bar-title.js';
Import the TypeScript type:
import type { TopAppBar } from 'mdui/components/top-app-bar.js';
import type { TopAppBarTitle } from 'mdui/components/top-app-bar-title.js';
Example: (Note: The style="position: relative" in the example is for demonstration purposes. Don't include it in production.)
<mdui-top-app-bar style="position: relative;">
<mdui-button-icon icon="menu"></mdui-button-icon>
<mdui-top-app-bar-title>Title</mdui-top-app-bar-title>
<div style="flex-grow: 1"></div>
<mdui-button-icon icon="more_vert"></mdui-button-icon>
</mdui-top-app-bar>Notes:
By default, the top app bar uses position: fixed and automatically adds padding-top to the body to prevent the page content from being obscured.
However, it uses position: absolute in the following cases:
- When the
scroll-targetattribute is specified. In this case,padding-topis added to the element specified byscroll-target. - When the component is used inside the
<mdui-layout></mdui-layout>component. In this case,padding-topis not added.
Examples
In Container
By default, the top app bar is fixed to the top of the viewport.
To place the top app bar inside a container, set the scroll-target attribute on the <mdui-top-app-bar> component to the CSS selector or DOM element of the scrollable container. In this case, the top app bar is positioned relative to its parent element. Be sure to add the styles position: relative; overflow: hidden to the parent element yourself.
Shape
The variant attribute on the <mdui-top-app-bar> component controls the top app bar's shape.
Scroll Behavior
The scroll-behavior attribute on the <mdui-top-app-bar> component defines how the top app bar behaves when the page is scrolled. You can use multiple scroll behaviors at once by separating them with spaces.
Scroll behaviors include:
hide: Hides the top app bar on scroll down and shows it on scroll up.shrink: Effective whenvariantismediumorlarge. Expands the top app bar on scroll down and shrinks it on scroll up.elevate: Adds a shadow to the top app bar on scroll down and removes the shadow when scrolling back to the top.
The scroll-threshold attribute sets how many pixels must be scrolled before the top app bar starts reacting. Do not set scroll-threshold when using elevate, so it responds immediately.
Example: Hide on Scroll
Example: Add Shadow on Scroll
Example: Shrink on Scroll
Example: Shrink and Add Shadow on Scroll
Expanded State Text
For a top app bar with variant set to medium or large, and scroll-behavior set to shrink, you can use the label-large slot within the <mdui-top-app-bar-title> component to specify the text displayed in the expanded state.
mdui-top-app-bar-title API
Slots
| Name |
|---|
| (default) |
The title text of the top app bar. |
label-large |
The title text when the top app bar is in the expanded state. |
CSS Parts
| Name |
|---|
label |
The title text. |
label-large |
The title text when the top app bar is in the expanded state. |
mdui-top-app-bar API
Properties
| Attribute | Property | Reflect | Type | Default |
|---|---|---|---|---|
variant | variant | 'center-aligned' | 'small' | 'medium' | 'large' | 'small' | |
Defines the top app bar variant. Default is
| ||||
hide | hide | boolean | false | |
Whether the top app bar is hidden. | ||||
shrink | shrink | boolean | false | |
Shrinks the app bar to the | ||||
scroll-behavior | scrollBehavior | 'hide' | 'shrink' | 'elevate' | - | |
Defines the scroll behavior. Multiple space-separated values are accepted. Possible values:
| ||||
scroll-target | scrollTarget | string | HTMLElement | JQ<HTMLElement> | - | |
The element to watch for scroll events. Accepts a CSS selector, a DOM element, or a JQ object. Defaults to | ||||
scroll-threshold | scrollThreshold | number | - | |
The scroll distance (in pixels) required to trigger the scroll behavior. | ||||
order | order | number | - | |
Specifies the layout order within the | ||||
Events
| Name |
|---|
show |
Emitted when the top app bar starts to show. Can be prevented with |
shown |
Emitted after the top app bar has shown and animations are complete. |
hide |
Emitted when the top app bar starts to hide. Can be prevented with |
Emitted after the top app bar has hidden and animations are complete. |
CSS Custom Properties
| Name |
|---|
--shape-corner |
The corner radius of the component. You can use a specific pixel value, but it is recommended to reference design tokens. |
--z-index |
The CSS |