TopAppBar
The Top App Bar provides information and actions related to the current screen, serving as a tool for branding, navigation, search, and 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. Remove it in actual use.)
<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-target
attribute is specified. In this case,padding-top
is added to the element specified byscroll-target
. - When it is within the
<mdui-layout></mdui-layout>
component. In this case,padding-top
is not added.
Examples
In Container
By default, the top app bar is positioned relative to the current window and appears at the top of the page.
To place the top app bar inside a container, specify the scroll-target
attribute on the <mdui-top-app-bar>
component. Set its value to the CSS selector or DOM element of the container with scrollable content. In this case, the top app bar will be positioned relative to the parent element. Ensure to add the styles position: relative; overflow: hidden
to the parent element.
Shape
The variant
attribute on the <mdui-top-app-bar>
component sets the shape of the top app bar.
Scroll Behavior
The scroll-behavior
attribute on the <mdui-top-app-bar>
component defines the top app bar's behavior when the page is scrolled. You can use multiple scroll behaviors simultaneously by separating them with spaces.
Scroll behaviors include:
hide
: Hides the top app bar when scrolling down and shows it when scrolling up.shrink
: Effective whenvariant
ismedium
orlarge
. Expands the top app bar when scrolling down and shrinks it when scrolling up.elevate
: Adds a shadow to the top app bar when scrolling down and removes the shadow when scrolling back to the top.
The scroll-threshold
attribute sets the number of pixels to start the scroll behavior of the top app bar. (Do not set the scroll-threshold
attribute when using the elevate
scroll behavior to respond promptly)
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 style. Default is
| ||||
hide | hide | boolean | false | |
Hide the top app bar. | ||||
shrink | shrink | boolean | false | |
Shrinks the app bar to | ||||
scroll-behavior | scrollBehavior | 'hide' | 'shrink' | 'elevate' | - | |
Defines the scroll behavior. Accepts multiple space-separated values. Possible values:
| ||||
scroll-target | scrollTarget | string | HTMLElement | JQ<HTMLElement> | - | |
The element that listens for scroll events. Accepts a CSS selector, DOM element, or JQ object. Defaults to | ||||
scroll-threshold | scrollThreshold | number | - | |
The scroll distance (in pixels) that triggers 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 size of the component corner. You can use a specific pixel value, but it's recommended to reference design tokens. |
--z-index |
The CSS |