ListItem
ListItem 提供丰富的列表项布局。默认情况下它可交互,并渲染为按钮;设置 href 时则渲染为锚点。对于纯信息行或由内部控件负责交互的行,可设置 interactive="false"。此时列表项会渲染为 div,不具备按钮、链接、焦点、状态层或按压行为。列表项处于被动状态时,任何 href 都会被忽略。
ListItem 会自动参与 List Grouping CSS 工具类的分组;该工具类也可用于分组其他元素和组件。
尺寸
size 独立于 appearance,用于选择行几何尺寸。它继承自 listItemDefaults.size 或 provideListItemDefaults({ size }),库默认值为 medium(56px)。Helper 会响应式地合并字段;设为 undefined 可恢复继承。Button 尺寸默认值不会控制列表行。
| 尺寸 | 最小高度 | 块方向内边距 | 行内方向内边距 | 图标间距 | 图标尺寸 |
|---|---|---|---|---|---|
extra-small | 36px | 6px | 12px | 8px | 20px |
small | 48px | 6px | 16px | 12px | 24px |
medium | 56px | 6px | 16px | 20px | 24px |
large | 64px | 8px | 22px | 20px | 24px |
extra-large | 80px | 10px | 24px | 24px | 28px |
公开 Token 会覆盖预设值:--md-item-min-height、--md-item-padding-block、--md-item-padding-inline、--md-item-gap、--md-item-content-gap 和 --md-item-icon-size。可在祖先元素或组件根元素上设置;根元素上的值优先于继承值。组件本身不会设置这些公开几何 Token。
表格显示密度 0 的尺寸。继承的 --md-density token 会降低最小高度和垂直内边距,不改变文字、图标或水平间距。公开尺寸 token 优先级更高。预设定义的是最小高度,因此描述和多行标题可使行增高。详见密度。
示例
默认
组合用法
Vue Router
当 ListItem 用作导航时,请使用 RouterLink custom,并将其 href 和 navigate 传给该组件。
外观
appearance="standard" 是默认值:活动指示器固定为整行宽度、转角紧凑,并且不会展开高亮或执行按压形状变换。appearance="expressive" 保留展开式指示器和按压形状变换。外观不会改变按钮/链接/展示语义、插槽或布局 Token。应用级外观可通过 listItemDefaults.appearance 设置,子树可在 setup 中调用 provideListItemDefaults();实例的 appearance 属性优先级更高。
standard
expressive
现有条目如需保留之前的外观,可设置 appearance="expressive"。shape 属性控制 expressive 外观的形状;standard 使用 --md-shape-compact。--md-item-indicator-shape 可定制两种外观中的指示器。
菜单命令请使用 MenuItem。ListItem 仍用于一般列表项。外观与密度建议请参阅样式指南。
列表项中的选择控件
CheckBox、Radio 和 Switch 可以出现在列表项中,作为状态指示器。在这种布局中,整行通常是更大、更易点击的目标,而选择控件只负责传达当前状态。
此时请使用控件的 presentation 属性。展示模式保留 Material 3 视觉状态,但会移除嵌套的原生输入框、对辅助技术隐藏视觉控件,并禁用控件自身的 state layer 效果。父级行必须提供可访问名称、角色、选中状态、禁用状态和交互反馈。
模式
无障碍规则
- 每行只保留一个交互目标。整行是控件;视觉上的
CheckBox、Radio或Switch不应成为另一个可聚焦元素。 - 将可访问名称放在行上,通常使用可见的
title文本。 - 在行上设置
aria-checked,并使其与视觉控件的checked状态保持同步。 - 使用匹配的角色:独立选择使用
checkbox,一组单选项使用radio,即时开关设置使用switch。 - 将相关单选行放入带有可访问名称的
radiogroup中。 - 对需要原生单选键盘行为的单选组,请保留原生 radio 输入框,或在父级组中添加方向键导航。
- 禁用某行时,同时禁用该行并向展示控件传入
disabled,使视觉状态与交互状态一致。
不应使用展示模式的情况
独立控件不要使用 presentation。独立的 CheckBox、Radio 或 Switch 应保留其原生输入框,以便表单、键盘交互、焦点指示和辅助技术无需额外代码即可正常工作。
也应避免在可用行内显示禁用状态的视觉控件。这样会让人误以为可见控件不可用,但整行仍会改变状态,造成困惑。
参考
结构
| 元素 | 部件 | 选择器 |
|---|---|---|
<component> | root | [data-scope="item"][data-part="root"]鼠标左键单击复制 |
<Icon> | leading条件显示 | [data-scope="item"][data-part="leading"]鼠标左键单击复制 |
<span> | main | [data-scope="item"][data-part="main"]鼠标左键单击复制 |
<span> | header条件显示 | [data-scope="item"][data-part="header"]鼠标左键单击复制 |
<span> | title条件显示 | [data-scope="item"][data-part="title"]鼠标左键单击复制 |
<span> | description条件显示 | [data-scope="item"][data-part="description"]鼠标左键单击复制 |
<span> | footer条件显示 | [data-scope="item"][data-part="footer"] |
<Icon> | trailing条件显示 | [data-scope="item"][data-part="trailing"]鼠标左键单击复制 |
属性
| 名称 | 说明 | 默认值 | 控件 |
|---|---|---|---|
shape可选 | The shape of the button. M3Shape | undefined | undefined | - |
href可选 | The href attribute. If provided, the button will be rendered as an anchor element. string | undefined | undefined | |
leadingIcon可选 | Name of the leading icon. string | undefined | undefined | |
trailingIcon可选 | Name of the trailing icon. string | undefined | undefined | |
disabled可选 | Whether the button is disabled. boolean | undefined | undefined | |
size可选 | Row geometry preset. Public item tokens override individual dimensions. ListItemSize | undefined | undefined | - |
appearance可选 | Standard fixed highlight or expressive expanding indicator and press morph. standardexpressive | undefined | |
interactive可选 | Whether the whole item is an interactive action or link. boolean | undefined | true | |
active可选 | Highlight the item as active/selected. boolean | undefined | false | |
title可选 | Fallback title when default slot is not provided. string | undefined | undefined | |
description可选 | Fallback description when description slot is not provided. string | undefined | undefined |
插槽6
| 名称 | 参数 | 说明 |
|---|---|---|
header | - | Optional content above the main row. |
leading | - | Optional leading content. |
default | - | Main title content. |
description | - | Optional description below the title. |
trailing | - | Optional trailing content. |
footer | - | Optional content below the main row. |
样式20
| Token | 说明 |
|---|---|
--md-item-active-icon-colorColor回退值 var(--md-sys-color-on-secondary-container) | Controls active icon color. |
--md-item-active-indicator-colorColor回退值 var(--md-sys-color-secondary-container) | Controls active indicator color. |
--md-item-active-label-colorColor回退值 var(--md-sys-color-on-secondary-container) | Controls active label color. |
--md-item-active-state-layer-colorState回退值 var(--md-sys-color-on-secondary-container) | Controls active state layer color. |
--md-item-content-gapLayout回退值 --dp(2) | Controls content gap. |
--md-item-footer-colorColor回退值 var(--md-sys-color-on-surface-variant) | Controls footer color. |
--md-item-gapLayout回退值 --dp(12) | Controls gap. |
--md-item-header-colorColor回退值 var(--md-sys-color-on-surface-variant) | Controls header color. |
--md-item-icon-sizeLayout回退值 Size, typography, density and icon scale (default medium reference: 24dp) | Controls icon size. |
--md-item-inactive-icon-colorColor回退值 var(--md-color-on-container) | Controls inactive icon color. |
--md-item-inactive-label-colorColor回退值 var(--md-color-on-container) | Controls inactive label color. |
--md-item-inactive-state-layer-colorState回退值 var(--md-color-on-container) | Controls inactive state layer color. |
--md-item-indicator-collapsed-visual-widthLayout回退值 --dp(12) | Controls indicator collapsed visual width. |
--md-item-indicator-shapeShape回退值 var(--md-shape-indicator) | Controls indicator shape. |
--md-item-main-padding-blockLayout回退值 --dp(8) | Controls main padding block. |
--md-item-main-padding-inlineLayout回退值 --dp(12) | Controls main padding inline. |
--md-item-min-heightLayout回退值 --dp(56) | Controls min height. |
--md-item-motionMotion回退值 var(--md-easing-spatial) | Controls motion. |
--md-item-padding-blockLayout回退值 --dp(8) | Controls padding block. |
--md-item-padding-inlineLayout回退值 --dp(8) | Controls padding inline. |