Files
maoyingjie 145a208687 docs: close documentation facts, structure and related-component mapping
HD-04 scope, against heroui ce90ea3 cn sources:

- stale/fictional content: input.md no longer claims TextField is
  unmigrated; color-field.md no longer claims ColorPicker is private;
  header.md drops the fictional Header.Root subcomponent and the stale
  'menus not migrated' wording (MenuSection label registration is now
  documented from HeaderRoot source); radio-group.md drops three copied
  CheckboxGroup wordings (variant override is Radio's, scalar emits,
  native single-value submit); input-otp.md drops the fictional
  data-complete state and lists the real missing upstream props
- toggle-button disabled row now matches the real CSS hooks
  (:disabled / [aria-disabled], with data-disabled as a marker only)
- part API / CSS classes / interactive states restored per pinned
  sources: button (full class lists + real variable-based global CSS),
  accordion (per-part tables + variant/state lists), card (per-part
  tables + variant list), switch (SwitchGroup snippet + Switch/
  SwitchGroup class lists + states), color-field Group (class list +
  states), input-otp (variant notes, global CSS, class/state lists);
  input-group gains the two upstream usage chapters
- RelatedComponents restored from the pinned componentRelationships
  (first-3, filtered against componentsMap): 64 pages rewritten,
  list-box/slider sections added, 7 empty-mapping pages' fabricated
  entries removed; RelatedShowcases left unmigrated with its real data
  inventory recorded (2 cases, showcase-site cards)
- defaults unified on implementation evidence: granularity (inferred
  day/minute) and visibleYears (full min-max span, no "or 20" fallback);
  HD-03 hand-offs resolved (trigger focus-visible noted for HD-06,
  width guards kept)
- new demos: header-collection-grouping (MenuSection/ListBoxSection
  label wiring), empty-state-collection (collection empty slot);
  empty-state page deduplicated with a boundary compatibility note
- ComponentPreview minHeight/isBgSolid restored per the 30 upstream
  marks; tabs-overflow max-w-full kept after measuring internal scroll
- tests: +3 header registration/demo cases, +1 empty-state collection
  case; full suite 110 files / 1630 tests, typecheck and build:docs
  pass
2026-10-28 22:32:10 +08:00

163 lines
8.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: ScrollShadow 滚动阴影
description: 通过阴影提示可滚动溢出内容,并根据滚动位置自动检测显示或隐藏。
component: scroll-shadow
group: 组件
---
> 固定 HeroUI 3.2.6 / ce90ea3;使用 Vue 具名部件、原生 attrs 与同源 SFC 演示。
## 用法
```ts
import { ScrollShadow } from "@heroui-vue/components";
```
<ComponentPreview name="scroll-shadow-default" />
## 示例
### 方向
<ComponentPreview name="scroll-shadow-orientation" />
### 阴影尺寸
<ComponentPreview name="scroll-shadow-size" />
### 与 Card 组合
<ComponentPreview name="scroll-shadow-with-card" />
### 隐藏滚动条
<ComponentPreview name="scroll-shadow-hide-scroll-bar" />
### 可见性变化
<ComponentPreview name="scroll-shadow-visibility-change" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="scroll-shadow-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
```css
@layer components {
.hv-scroll-shadow {
@apply rounded-xl border border-border;
}
.hv-scroll-shadow--vertical {
@apply pr-2; /* Add padding for custom scrollbar styling */
}
.hv-scroll-shadow--horizontal {
@apply pb-2;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
ScrollShadow 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/scroll-shadow.css)):
#### 基础类
- `.hv-scroll-shadow` - 根容器元素
#### 方向变体
- `.hv-scroll-shadow--vertical` - 纵向滚动(默认)
- `.hv-scroll-shadow--horizontal` - 横向滚动
#### 状态修饰符
- `.hv-scroll-shadow--hide-scrollbar` - 隐藏原生滚动条
### CSS 变量
ScrollShadow 组件使用 CSS 变量设置渐变遮罩尺寸,并为可见的原生滚动条保留空间:
| 变量 | 默认值 | 描述 |
| -------------------------------- | ------------------------------------ | -------------------------------------------------------------------------------------------- |
| `--scroll-shadow-size` | `40px` | 控制渐变阴影尺寸。该值由 `size` prop 设置。 |
| `--scroll-shadow-offset` | `0px` | 开始显示渐变之前需要滚动的距离。该值由 `offset` prop 设置。 |
| `--scroll-shadow-scrollbar-size` | `10px`(`hideScrollBar` 时为 `0px`) | 为原生滚动条保留一段实色遮罩区域,避免渐变覆盖滚动条。使用更宽的自定义滚动条时可以覆盖该值。 |
### Data 属性
组件使用 data 属性控制阴影可见性:
- **滚动状态**:`[data-top-scroll]`、`[data-bottom-scroll]`、`[data-left-scroll]`、`[data-right-scroll]` — 当内容可向对应方向滚动时应用
- **组合状态**:`[data-top-bottom-scroll]`、`[data-left-right-scroll]` — 当内容可向两个方向滚动时应用
- **方向**:`[data-orientation="vertical"]` 或 `[data-orientation="horizontal"]` — 表示滚动方向
- **尺寸**:`[data-scroll-shadow-size]` — 阴影渐变尺寸数值
- **阴影模式**:`[data-scroll-shadow-mode]` — 渐变由滚动位置推导时为 `"auto"`;`visibility` 受控或 `isEnabled` 为 `false` 时为 `"manual"`
### 滚动驱动的渐变
在 `auto` 模式下,支持[滚动驱动动画](https://developer.mozilla.org/zh-CN/docs/Web/CSS/animation-timeline)的浏览器会直接在 CSS
中根据滚动位置推导渐变。因此遮罩在首次绘制时就是正确的,无需测量,也不会在 hydration 期间出现未渐变内容的闪烁。
不支持的浏览器会回退到上面的 `[data-*-scroll]` 属性,这些属性在 hydration 之后才写入。自定义样式时需要注意两点:
- 在 `auto` 模式下,即使没有可滚动内容,根元素也始终会解析出 `mask-image`。这会使其成为层叠上下文,
并成为 `position: fixed` 后代元素的包含块。如需退出该行为,请显式设置 `visibility`。
- 滚动驱动的渐变依赖根元素上的 `animation` 属性。在同一元素上使用 `animate-*` 工具类会覆盖它,导致渐变消失。
请改为对外层容器应用动画。
## API 参考
### ScrollShadow
| Prop | 类型 | 默认值 | 描述 |
| -------------------- | ---------------------------------------------------------------------------------- | ------------ | ---------------------------------- |
| `orientation` | `"vertical"` \| `"horizontal"` | `"vertical"` | 滚动方向 |
| `variant` | `"fade"` | `"fade"` | 阴影视觉效果样式 |
| `size` | `number` | `40` | 阴影渐变尺寸(像素) |
| `offset` | `number` | `0` | 开始显示阴影前的滚动偏移量(像素) |
| `hideScrollBar` | `boolean` | `false` | 是否隐藏原生滚动条 |
| `isEnabled` | `boolean` | `true` | 是否启用滚动阴影检测 |
| `visibility` | `"auto"` \| `"both"` \| `"top"` \| `"bottom"` \| `"left"` \| `"right"` \| `"none"` | `"auto"` | 受控的阴影可见性 |
| `@visibility-change` | `(visibility: ScrollShadowVisibility) => void` | - | 阴影可见性变化时调用的回调 |
| `class` | `string` | - | 应用到根元素上的额外 CSS 类 |
| `默认插槽` | `VNode / 插槽` | - | 可滚动的子内容 |
## Vue 事件与测量边界
`@visibility-change` 仅在自动检测状态变化时通知;显式 visibility 控制显示,不产生自动检测事件。isEnabled=false 停止自动检测并清除旧阴影,显式 visibility 仍优先。水平 left/right 与基线一样表示逻辑起始/结束,RTL 由 CSS 交换物理边缘。偏移边界与末端 1px 容差保留。
通过 VueUse 监听尺寸、子树文本/样式/节点与资源 load,滚动测量合并到 requestAnimationFrame,卸载清理;暴露 element、update() 和 scrollTo(options)。遮罩布局/滚动由真实浏览器验证,单元测试的模拟尺寸只验证边界计算。CSS scroll timeline、首次渲染和堆叠上下文限制保留上面的源说明。默认不添加 tabIndex,需键盘滚动的独立区域请提供 tabindex="0" 和可访问名称。
## useScrollShadow
公开组合式函数复用组件的同一份测量内核,接受元素 ref/getter 和响应式 orientation / offset / visibility / isEnabled。返回只读 visibility、attributes 与 update();Vue 通过 v-bind 明确绑定状态属性,不像 React hook 隐式修改 dataset。样式与尺寸由调用者设置。
```vue
<script setup lang="ts">
import { ref } from "vue";
import { useScrollShadow } from "@heroui-vue/components";
const element = ref<HTMLElement>();
const { attributes } = useScrollShadow({ containerRef: element });
</script>
<template>
<div
ref="element"
class="hv-scroll-shadow hv-scroll-shadow--fade hv-scroll-shadow--vertical h-40"
v-bind="attributes"
data-scroll-shadow-mode="auto"
>
<!-- 可滚动内容 -->
</div>
</template>
```