应用栏
提供当前页面的顶层导航与操作。
本指南覆盖默认 Emotion 环境的 Material UI Core;扩展包与额外示例按清单单独标注。
使用场景与业务边界
导航目标与响应式菜单状态由宿主管理;演示搜索框不提供搜索服务。
原生组成与范围
AppBar, Toolbar, Menu
保持 Material UI 原生组合与类型,不替换为 JGuang/Base UI/Radix 同名 API。示例条目不是额外组件。
原生安装与初始化
npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1
采用原生 npm 包与 Emotion,不提供虚构的 JGuang UI Registry 地址。将示例放在原生 ThemeProvider 内,按项目选择 CssBaseline 或 ScopedCssBaseline。先检查 React、现有 Provider、锁文件与 SSR 配置;不要覆盖用户文件或忽略 peer 冲突。本批实际验收 React 19.2.8;React 18 及以下须匹配 react-is。完整初始化规则见随附的双语 setup 文档。
关键原生参数
仅解释本指南选出的关键参数,不是平铺的通用 API。原生继承来源、slot 结构、可空值与泛型仍按固定源码保留。
| 所属组件.属性 | 用途说明 | 声明来源 |
|---|---|---|
AppBar.position | 原生 AppBar CSS 定位策略。 | AppBar |
AppBar.color | 所属组件的原生语义/自定义颜色,不是其他系统的颜色枚举。 | AppBar |
Toolbar.variant | 所属组件的原生视觉枚举,不可替换为通用 variant 属性。 | Toolbar |
可运行原生示例
下列机器键只选择示例入口,不会作为通用 variant 属性传入每个组件。原生示例 JSX 与数据保持上游原文;JGuang 编写的验收示例明确标注。
- 基础应用栏 ·
ButtonAppBar· 上游原生示例 - 带菜单的应用栏 ·
MenuAppBar· 上游原生示例 - 带响应式菜单的应用栏 ·
ResponsiveAppBar· 上游原生示例 - 带搜索框的应用栏 ·
SearchAppBar· 上游原生示例 - 带抽屉的响应式应用栏 ·
DrawerAppBar· 上游原生示例 - 带主搜索框的应用栏 ·
PrimarySearchAppBar· 上游原生示例 - 紧凑样式,仅桌面 ·
DenseAppBar· 上游原生示例 - 突出应用栏 ·
ProminentAppBar· 上游原生示例 - 底部应用栏 ·
BottomAppBar· 上游原生示例 - 滚动时隐藏应用栏 ·
HideAppBar· 上游原生示例 - 滚动时增加应用栏阴影 ·
ElevateAppBar· 上游原生示例 - 返回顶部 ·
BackToTop· 上游原生示例 - 深色模式启用颜色 ·
EnableColorOnDarkAppBar· 上游原生示例
真实使用示例
固定版本的未修改原生示例;复制时同时保留所需的局部辅助文件。
import AppBar from '@mui/material/AppBar';
import Box from '@mui/material/Box';
import Toolbar from '@mui/material/Toolbar';
import Typography from '@mui/material/Typography';
import Button from '@mui/material/Button';
import IconButton from '@mui/material/IconButton';
import MenuIcon from '@mui/icons-material/Menu';
export default function ButtonAppBar() {
return (
<Box sx={{ flexGrow: 1 }}>
<AppBar position="static">
<Toolbar>
<IconButton
size="large"
edge="start"
color="inherit"
aria-label="menu"
sx={{ mr: 2 }}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
</Box>
);
}
示例依赖边界
@mui/material/AppBar, @mui/material/Box, @mui/material/Toolbar, @mui/material/Typography, @mui/material/Button, @mui/material/IconButton, @mui/icons-material/Menu, react, @mui/icons-material/AccountCircle, @mui/material/Switch, @mui/material/FormControlLabel, @mui/material/FormGroup, @mui/material/MenuItem, @mui/material/Menu, @mui/material/Container, @mui/material/Avatar, @mui/material/Tooltip, @mui/icons-material/Adb, @mui/material/styles, @mui/material/InputBase, @mui/icons-material/Search, @mui/material/CssBaseline, @mui/material/Divider, @mui/material/Drawer, @mui/material/List, @mui/material/ListItem, @mui/material/ListItemButton, @mui/material/ListItemText, @mui/material/Badge, @mui/icons-material/Mail, @mui/icons-material/Notifications, @mui/icons-material/MoreVert, @mui/material/Paper, @mui/material/Fab, @mui/material/ListItemAvatar, @mui/material/ListSubheader, @mui/icons-material/Add, @mui/material/useScrollTrigger, @mui/material/Slide, @mui/icons-material/KeyboardArrowUp, @mui/material/Fade, @mui/material/Stack
直接导入 Icons/System/Utils 的示例需要匹配 9.4.0 包。局部数据与辅助文件属于示例闭包,不是额外组件。远程图片和模拟操作不提供后端。
固定上游 API 元数据参考
以下表格保留上游元数据,不是重新推断的通用类型体系。条件泛型、继承属性与回调细节以以下源码声明为准。主题默认配置可能覆盖元数据默认值。
AppBar
import AppBar from '@mui/material/AppBar';
import { AppBar } from '@mui/material';
原生继承: Paper. 父级与 DOM 属性未被伪造为本组件新属性。
| 属性 | 必需 | 上游元数据类型/约束 | 元数据默认值 |
|---|---|---|---|
children | 否 | node | — |
classes | 否 | object | — |
color | 否 | 'default' | 'inherit' | 'primary' | 'secondary' | 'transparent' | 'error' | 'info' | 'success' | 'warning' | string | 'primary' |
elevation | 否 | number | 4 |
enableColorOnDark | 否 | bool | false |
position | 否 | 'absolute' | 'fixed' | 'relative' | 'static' | 'sticky' | 'fixed' |
square | 否 | bool | true |
sx | 否 | Array<func | object | bool> | func | object | — |
Toolbar
import Toolbar from '@mui/material/Toolbar';
import { Toolbar } from '@mui/material';
| 属性 | 必需 | 上游元数据类型/约束 | 元数据默认值 |
|---|---|---|---|
children | 否 | node | — |
classes | 否 | object | — |
component | 否 | elementType | — |
disableGutters | 否 | bool | false |
sx | 否 | Array<func | object | bool> | func | object | — |
variant | 否 | 'dense' | 'regular' | string | 'regular' |
Menu
import Menu from '@mui/material/Menu';
import { Menu } from '@mui/material';
原生继承: Popover. 父级与 DOM 属性未被伪造为本组件新属性。
| 属性 | 必需 | 上游元数据类型/约束 | 元数据默认值 |
|---|---|---|---|
open | 是 | bool | — |
anchorEl | 否 | HTML element | func | — |
autoFocus | 否 | bool | true |
children | 否 | node | — |
classes | 否 | object | — |
disableAutoFocusItem | 否 | bool | false |
onClose | 否 | func | — |
PopoverClasses | 否 | object | — |
slotProps | 否 | { backdrop?: func | object, list?: func | object, paper?: func | object, root?: func | object, transition?: func | object } | {} |
slots | 否 | { backdrop?: elementType, list?: elementType, paper?: elementType, root?: elementType, transition?: elementType } | {} |
sx | 否 | Array<func | object | bool> | func | object | — |
transitionDuration | 否 | 'auto' | number | { appear?: number, enter?: number, exit?: number } | 'auto' |
variant | 否 | 'menu' | 'selectedMenu' | 'selectedMenu' |
原生源码声明
声明与 import 从固定源码提取,不平铺联合类型或外部继承。英文注释/类型属于原始源码参考,不代表上游正文已全文翻译。共享类型仍位于原生包中。
vendor/src/AppBar/AppBar.d.ts
import * as React from 'react';
import { SxProps } from '@mui/system';
import { OverridableStringUnion } from '@mui/types';
import { OverridableComponent, OverrideProps } from '../OverridableComponent';
import { PropTypes, Theme } from '../styles';
import { AppBarClasses } from './appBarClasses';
import { ExtendPaperTypeMap } from '../Paper/Paper';
export interface AppBarPropsColorOverrides {}
export interface AppBarOwnProps {
/**
* Override or extend the styles applied to the component.
*/
classes?: Partial<AppBarClasses> | undefined;
/**
* The color of the component.
* It supports both default and custom theme colors, which can be added as shown in the
* [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
* @default 'primary'
*/
color?:
| OverridableStringUnion<
PropTypes.Color | 'transparent' | 'error' | 'info' | 'success' | 'warning',
AppBarPropsColorOverrides
>
| undefined;
/**
* Shadow depth, corresponds to `dp` in the spec.
* It accepts values between 0 and 24 inclusive.
* @default 4
*/
elevation?: number | undefined;
/**
* If true, the `color` prop is applied in dark mode.
* @default false
*/
enableColorOnDark?: boolean | undefined;
/**
* The positioning type. The behavior of the different options is described
* [in the MDN web docs](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/position).
* Note: `sticky` is not universally supported and will fall back to `static` when unavailable.
* @default 'fixed'
*/
position?: 'fixed' | 'absolute' | 'sticky' | 'static' | 'relative' | undefined;
/**
* If `false`, rounded corners are enabled.
* @default true
*/
square?: boolean | undefined;
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx?: SxProps<Theme> | undefined;
}
export type AppBarTypeMap<
AdditionalProps = {},
RootComponent extends React.ElementType = 'header',
> = ExtendPaperTypeMap<
{
props: AdditionalProps & AppBarOwnProps;
defaultComponent: RootComponent;
},
'position' | 'color' | 'classes' | 'elevation' | 'square'
>;
declare const AppBar: OverridableComponent<AppBarTypeMap>;
export type AppBarProps<
RootComponent extends React.ElementType = AppBarTypeMap['defaultComponent'],
AdditionalProps = {},
> = OverrideProps<AppBarTypeMap<AdditionalProps, RootComponent>, RootComponent> & {
component?: React.ElementType | undefined;
};
vendor/src/Toolbar/Toolbar.d.ts
import * as React from 'react';
import { SxProps } from '@mui/system';
import { OverridableStringUnion } from '@mui/types';
import { Theme } from '../styles';
import { OverridableComponent, OverrideProps } from '../OverridableComponent';
import { ToolbarClasses } from './toolbarClasses';
export interface ToolbarPropsVariantOverrides {}
export interface ToolbarOwnProps {
/**
* The Toolbar children, usually a mixture of `IconButton`, `Button` and `Typography`.
* The Toolbar is a flex container, allowing flex item properties to be used to lay out the children.
*/
children?: React.ReactNode;
/**
* Override or extend the styles applied to the component.
*/
classes?: Partial<ToolbarClasses> | undefined;
/**
* If `true`, disables gutter padding.
* @default false
*/
disableGutters?: boolean | undefined;
/**
* The variant to use.
* @default 'regular'
*/
variant?: OverridableStringUnion<'regular' | 'dense', ToolbarPropsVariantOverrides> | undefined;
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx?: SxProps<Theme> | undefined;
}
export interface ToolbarTypeMap<
AdditionalProps = {},
RootComponent extends React.ElementType = 'div',
> {
props: AdditionalProps & ToolbarOwnProps;
defaultComponent: RootComponent;
}
declare const Toolbar: OverridableComponent<ToolbarTypeMap>;
export type ToolbarProps<
RootComponent extends React.ElementType = ToolbarTypeMap['defaultComponent'],
AdditionalProps = {},
> = OverrideProps<ToolbarTypeMap<AdditionalProps, RootComponent>, RootComponent> & {
component?: React.ElementType | undefined;
};
vendor/src/Menu/Menu.d.ts
import * as React from 'react';
import { SxProps } from '@mui/system';
import { Theme } from '../styles';
import { InternalStandardProps as StandardProps } from '../internal';
import { PaperProps } from '../Paper';
import { PopoverProps } from '../Popover';
import { MenuListProps } from '../MenuList';
import { ModalProps } from '../Modal';
import { BackdropProps } from '../Backdrop';
import { TransitionProps } from '../transitions/types';
import { MenuClasses } from './menuClasses';
import { CreateSlotsAndSlotProps, SlotComponentProps, SlotProps } from '../utils/types';
export interface MenuRootSlotPropsOverrides {}
export interface MenuPaperSlotPropsOverrides {}
export interface MenuTransitionSlotPropsOverrides {}
export interface MenuListSlotPropsOverrides {}
export interface MenuBackdropSlotPropsOverrides {}
export interface MenuSlots {
/**
* The component used for the popper.
* @default Modal
*/
root: React.ElementType;
/**
* The component used for the paper.
* @default PopoverPaper
*/
paper: React.ElementType;
/**
* The component used for the list.
* @default MenuList
*/
list: React.ElementType;
/**
* The component used for the transition slot.
* @default Grow
*/
transition: React.ElementType;
/**
* The component used for the backdrop slot.
* @default Backdrop
*/
backdrop: React.ElementType;
}
export type MenuSlotsAndSlotProps = CreateSlotsAndSlotProps<
MenuSlots,
{
/**
* Props forwarded to the root slot.
* By default, the available props are based on the [Popover](https://mui.com/material-ui/api/popover/#props) component.
*/
root: SlotProps<React.ElementType<ModalProps>, MenuRootSlotPropsOverrides, MenuOwnerState>;
/**
* Props forwarded to the paper slot.
* By default, the available props are based on the [Paper](https://mui.com/material-ui/api/paper/#props) component.
*/
paper: SlotProps<React.ElementType<PaperProps>, MenuPaperSlotPropsOverrides, MenuOwnerState>;
/**
* Props forwarded to the list slot.
* By default, the available props are based on the [MenuList](https://mui.com/material-ui/api/menu-list/#props) component.
*/
list: SlotProps<React.ElementType<MenuListProps>, MenuListSlotPropsOverrides, MenuOwnerState>;
/**
* Props forwarded to the transition slot.
* By default, the available props are based on the [Grow](https://mui.com/material-ui/api/grow/#props) component.
*/
transition: SlotComponentProps<
// use SlotComponentProps because transition slot does not support `component` and `sx` prop
React.ElementType<TransitionProps>,
TransitionProps & MenuTransitionSlotPropsOverrides,
MenuOwnerState
>;
/**
* Props forwarded to the backdrop slot.
* By default, the available props are based on the [Backdrop](https://mui.com/material-ui/api/backdrop/#props) component.
*/
backdrop: SlotProps<
React.ElementType<BackdropProps>,
MenuBackdropSlotPropsOverrides,
MenuOwnerState
>;
}
>;
export interface MenuProps
extends StandardProps<Omit<PopoverProps, 'slots' | 'slotProps'>>, MenuSlotsAndSlotProps {
/**
* An HTML element, or a function that returns one.
* It's used to set the position of the menu.
*/
anchorEl?: PopoverProps['anchorEl'] | undefined;
/**
* If `true` (Default) will focus the `[role="menu"]` if no focusable child is found. Disabled
* children are not focusable. If you set this prop to `false` focus will be placed
* on the parent modal container. This has severe accessibility implications
* and should only be considered if you manage focus otherwise.
* @default true
*/
autoFocus?: boolean | undefined;
/**
* Menu contents, normally `MenuItem`s.
*/
children?: React.ReactNode;
/**
* Override or extend the styles applied to the component.
*/
classes?: Partial<MenuClasses> | undefined;
/**
* When opening the menu will not focus the active item but the `[role="menu"]`
* unless `autoFocus` is also set to `false`. Not using the default means not
* following WAI-ARIA authoring practices. Please be considerate about possible
* accessibility implications.
* @default false
*/
disableAutoFocusItem?: boolean | undefined;
/**
* Callback fired when the component requests to be closed.
*
* @param {object} event The event source of the callback.
* @param {string} reason Can be: `"escapeKeyDown"`, `"backdropClick"`, `"tabKeyDown"`.
*/
onClose?: PopoverProps['onClose'] | undefined;
/**
* If `true`, the component is shown.
*/
open: boolean;
/**
* `classes` prop applied to the [`Popover`](https://mui.com/material-ui/api/popover/) element.
*/
PopoverClasses?: PopoverProps['classes'] | undefined;
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx?: SxProps<Theme> | undefined;
/**
* The length of the transition in `ms`, or 'auto'
* @default 'auto'
*/
transitionDuration?: TransitionProps['timeout'] | 'auto' | undefined;
/**
* The variant to use. Use `menu` to prevent selected items from impacting the initial focus.
* @default 'selectedMenu'
*/
variant?: 'menu' | 'selectedMenu' | undefined;
}
export interface MenuOwnerState extends Omit<MenuProps, 'slots' | 'slotProps'> {}
export declare const MenuPaper: React.FC<PaperProps>;
export default function Menu(props: MenuProps): React.JSX.Element;
安装后验收
执行严格类型检查、生产构建和实际交互。检查键盘标签/焦点、原生支持的禁用/错误/只读语义、深浅色、窄屏和减少动画。按真实宿主数据检查回调,不把复制提示词当作安装完成。初始化工具还需检查服务端 HTML 与无告警水合。
来源与许可
https://github.com/mui/material-ui/tree/809a7717b4c050ba3f69b75300689f07c050a16e
MIT · @mui/material@9.4.0. 保留上游原始内容与许可证。上游完整原文保留于 vendor/docs,不标为全文中文化。