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.replace(
'组件文档、使用说明和公告通常有多段内容。先保留摘要,读者需要时再展开全文。',
'元件文件、使用說明與公告通常有多段內容。先保留摘要,讀者需要時再展開全文。'
)
}}
</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.replace('条项目记录', '筆專案記錄') }}</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
}