ShowMore 展开更多
内容超过指定高度时先显示一部分,点击按钮展开全文。没有溢出时直接显示内容,不出现展开按钮。
引入
ts
import { HuiShowMore } from '@nscode/handy-ui'
import type {
ShowMoreProps,
ShowMoreEmits,
ShowMoreExpose,
ShowMoreTriggerSlotProps
} from '@nscode/handy-ui'
示例
长段落
默认最多显示 120px,收起时底部渐隐。展开后内容变高时会重新测量,避免新内容被裁掉。
vue
<script setup lang="ts">
import { HuiShowMore } from '@nscode/handy-ui'
const paragraphs = Array.from(
{ length: 6 },
(_, index) =>
`第 ${index + 1} 段:组件文档、使用说明和公告通常有多段内容。先保留摘要,读者需要时再展开全文。`
)
</script>
<template>
<HuiShowMore
><p v-for="paragraph in paragraphs" :key="paragraph">{{ paragraph }}</p></HuiShowMore
>
</template>
数字和 CSS 长度
数字及数字字符串按 px,模板中 max-height="80" 和 :max-height="80" 效果相同;'6em' 跟随内容区域的字号,也支持可由浏览器换算成绝对高度的 rem、calc(...) 等长度。
vue
<script setup lang="ts">
import { HuiShowMore, HuiSpace } from '@nscode/handy-ui'
const limits = [80, '80', '6em']
</script>
<template>
<HuiSpace direction="vertical" fill>
<div v-for="(limit, limitIndex) in limits" :key="limitIndex">
<strong>maxHeight: {{ JSON.stringify(limit) }}</strong>
<HuiShowMore :max-height="limit"
><p v-for="index in 8" :key="index">
第 {{ index }} 行内容,展开后可以阅读完整说明。
</p></HuiShowMore
>
</div>
</HuiSpace>
</template>
渐隐开关
渐隐使用遮罩,放在不同背景的容器里也能自然显示。fade=false 保留直接裁剪的边缘。
vue
<script setup lang="ts">
import { HuiCard, HuiShowMore, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCard v-for="fade in [true, false]" :key="String(fade)" :title="`fade: ${fade}`">
<HuiShowMore :max-height="96" :fade="fade"
><p v-for="index in 7" :key="index">
第 {{ index }} 段内容:收起时可比较下边缘的效果。
</p></HuiShowMore
>
</HuiCard>
</HuiSpace>
</template>
受控展开和文字
使用 v-model:expanded 同步状态;单独传入 expanded 时,组件请求变化后会等待父组件回填。外部状态变化不重复发送 change。
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiShowMore, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
const expanded = ref(false)
const last = ref('尚未操作')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace align="center"
><HuiSwitch v-model="expanded" /><span
>外部控制:{{ expanded ? '展开' : '收起' }}</span
></HuiSpace
>
<HuiShowMore
v-model:expanded="expanded"
:max-height="100"
expand-text="阅读全文"
collapse-text="收起正文"
@change="last = $event ? '用户请求展开' : '用户请求收起'"
>
<p v-for="index in 8" :key="index">第 {{ index }} 段:可以通过外部开关或组件按钮切换。</p>
</HuiShowMore>
<span>最近事件:{{ last }}</span>
</HuiSpace>
</template>
自定义触发器
trigger 插槽获得 expanded、toggle、contentId。自定义按钮保留 aria-expanded 和 aria-controls,键盘和读屏用户也能理解当前状态。
vue
<script setup lang="ts">
import { HuiButton, HuiShowMore } from '@nscode/handy-ui'
const items = Array.from({ length: 12 }, (_, index) => `第 ${index + 1} 条项目记录`)
</script>
<template>
<HuiShowMore :max-height="100">
<ul>
<li v-for="item in items" :key="item">{{ item }}</li>
</ul>
<template #trigger="{ expanded, toggle, contentId }">
<HuiButton
variant="link"
:aria-expanded="expanded"
:aria-controls="contentId"
@click="toggle"
>{{ expanded ? '收起列表' : `查看全部(${items.length} 条)` }}</HuiButton
>
</template>
</HuiShowMore>
</template>
内容不够高时
只有实际内容超过上限时,组件才会限制高度并显示触发器。
vue
<script setup lang="ts">
import { HuiShowMore, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<div>
<strong>短内容:没有按钮</strong
><HuiShowMore :max-height="120"><p>这一小段无需折叠。</p></HuiShowMore>
</div>
<div>
<strong>长内容:可以展开</strong
><HuiShowMore :max-height="120"
><p v-for="index in 10" :key="index">第 {{ index }} 行,内容超过最大高度。</p></HuiShowMore
>
</div>
</HuiSpace>
</template>
动画时长
默认 200ms;0 立即切换。系统开启“减少动态效果”时不播放动画。
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiShowMore, HuiSpace } from '@nscode/handy-ui'
const expanded = ref(false)
const durations = [0, 200, 600]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace><HuiButton @click="expanded = !expanded">同时切换</HuiButton></HuiSpace>
<div v-for="duration in durations" :key="duration">
<strong>{{ duration }}ms</strong
><HuiShowMore v-model:expanded="expanded" :max-height="80" :duration="duration"
><p v-for="index in 6" :key="index">第 {{ index }} 行内容。</p></HuiShowMore
>
</div>
</HuiSpace>
</template>
方法和动态内容
toggle() 请求切换状态,remeasure() 在下次 DOM 更新后重新测量。组件会自动观察内容尺寸;主动测量适合业务改变了外部样式的场景。
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiShowMore, HuiSpace } from '@nscode/handy-ui'
const more = ref<InstanceType<typeof HuiShowMore>>()
const count = ref(2)
const append = () => {
count.value += 3
more.value?.remeasure()
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap
><HuiButton @click="append">追加三段并测量</HuiButton
><HuiButton @click="more?.toggle()">toggle()</HuiButton
><HuiButton @click="more?.remeasure()">remeasure()</HuiButton></HuiSpace
>
<HuiShowMore ref="more" :max-height="100"
><p v-for="index in count" :key="index">
第 {{ index }} 段,动态内容会重新计算是否溢出。
</p></HuiShowMore
>
</HuiSpace>
</template>
自持状态与受控回填
未传 expanded 或仅监听更新时,组件从 false 保存状态。传入属性(包括 undefined)即受控;undefined 按收起显示。父组件拒绝更新不会污染内部状态,动态移除属性后恢复原内部值,切换模式时会在开发环境警告。默认按钮、自定义触发器、焦点请求和 toggle() 使用同一状态。
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiShowMore, HuiSpace } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('尚无请求')
const q1Model = ref(false)
</script>
<template>
<HuiSpace class="show-more-q1-demo" direction="vertical" fill>
<HuiButton class="q1-redraw" @click="q1Redraw++">父重绘 {{ q1Redraw }}</HuiButton>
<div>
<p>未传属性,自行保存</p>
<HuiShowMore class="q1-local" :max-height="48" :duration="0">
<p v-for="index in 4" :key="index">第 {{ index }} 段内容</p>
</HuiShowMore>
</div>
<div>
<p>仅监听更新,仍自行保存</p>
<HuiShowMore
class="q1-listener"
:max-height="48"
:duration="0"
@update:expanded="q1Last = String($event)"
>
<p v-for="index in 4" :key="index">第 {{ index }} 段内容</p>
</HuiShowMore>
</div>
<div>
<p>显式undefined,保持收起</p>
<HuiShowMore class="q1-empty" :max-height="48" :duration="0" :expanded="undefined">
<p v-for="index in 4" :key="index">第 {{ index }} 段内容</p>
</HuiShowMore>
</div>
<div>
<p>固定受控,父拒绝更新</p>
<HuiShowMore
class="q1-fixed"
:max-height="48"
:duration="0"
:expanded="false"
@update:expanded="q1Last = String($event)"
>
<p v-for="index in 4" :key="index">第 {{ index }} 段内容</p>
</HuiShowMore>
</div>
<div>
<p>双向绑定</p>
<HuiShowMore v-model:expanded="q1Model" class="q1-bound" :max-height="48" :duration="0">
<p v-for="index in 4" :key="index">第 {{ index }} 段内容</p>
</HuiShowMore>
</div>
<p class="q1-notification">绑定值:{{ q1Model }};最近请求:{{ q1Last }}</p>
</HuiSpace>
</template>
Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| maxHeight | 折叠时最大高度,数字及数字字符串按 px,也可传 CSS 长度 | number | string | 120 |
| expanded | 传入即受控(含undefined,按false显示),未传从false保存;支持 v-model:expanded | boolean | undefined |
| expandText | 展开文字;空字符串有效 | string | 语言包“展开” |
| collapseText | 收起文字;空字符串有效 | string | 语言包“收起” |
| fade | 收起时底部渐隐 | boolean | true |
| duration | 动画时长,单位 ms;0 不播放动画 | number | 200 |
Events
| 事件 | 说明 | 参数 |
|---|---|---|
| update:expanded | 用户请求切换状态,可用于 v-model | expanded: boolean |
| change | 用户请求展开或收起;外部回填不重复触发 | expanded: boolean |
Slots
| 插槽 | 说明 | 参数 |
|---|---|---|
| default | 完整内容 | — |
| trigger | 替换默认展开/收起按钮;不溢出时不渲染 | { expanded, toggle, contentId } |
方法
| 方法 | 说明 | 参数 |
|---|---|---|
| toggle | 请求切换展开状态;不溢出时不操作 | — |
| remeasure | 下次 DOM 更新后重新测量 | — |
类型
ts
interface ShowMoreProps {
maxHeight?: number | string
expanded?: boolean
expandText?: string
collapseText?: string
fade?: boolean
duration?: number
}
interface ShowMoreExpose {
toggle: () => void
remeasure: () => void
}
interface ShowMoreTriggerSlotProps {
expanded: boolean
toggle: () => void
contentId: string
}