ShowMore
Show a preview when content exceeds a specified height, with a button to expand the full content. Short content is displayed without a trigger.
Import
import { HuiShowMore } from '@nscode/handy-ui'
import type {
ShowMoreProps,
ShowMoreEmits,
ShowMoreExpose,
ShowMoreTriggerSlotProps
} from '@nscode/handy-ui'
Examples
Long paragraphs
The default collapsed height is 120px, with a fade at the bottom. Changes to expanded content are measured again so new content remains visible.
<script setup lang="ts">
import { HuiShowMore } from '@nscode/handy-ui'
const paragraphs = Array.from(
{ length: 6 },
(_, index) =>
`Paragraph ${index + 1}: Documentation, instructions and announcements often contain several paragraphs. Show a preview first and let the reader expand the full content when needed.`
)
</script>
<template>
<HuiShowMore
><p v-for="paragraph in paragraphs" :key="paragraph">{{ paragraph }}</p></HuiShowMore
>
</template>
Numbers and CSS lengths
Numbers and numeric strings use px, so max-height="80" and :max-height="80" have the same effect. '6em' follows the content area's font size. Other lengths such as rem and calc(...) are supported when the browser can resolve them to an absolute height.
<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">
Line {{ index }}: expand to read the full instructions.
</p></HuiShowMore
>
</div>
</HuiSpace>
</template>
Fade
The fade uses a mask so it works on different container backgrounds. Set fade=false for a plain clipped edge.
<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">
Paragraph {{ index }}: compare the bottom edge when collapsed.
</p></HuiShowMore
>
</HuiCard>
</HuiSpace>
</template>
Controlled state and text
Use v-model:expanded to synchronize state. When expanded is supplied alone, requests wait for the parent to update the prop. External updates do not emit another change event.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiShowMore, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
const expanded = ref(false)
const last = ref('No action yet')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace align="center"
><HuiSwitch v-model="expanded" /><span
>External state: {{ expanded ? 'Expanded' : 'Collapsed' }}</span
></HuiSpace
>
<HuiShowMore
v-model:expanded="expanded"
:max-height="100"
expand-text="Read full text"
collapse-text="Collapse text"
@change="last = $event ? 'Expansion requested' : 'Collapse requested'"
>
<p v-for="index in 8" :key="index">
Paragraph {{ index }}: change state with the external switch or the component's button.
</p>
</HuiShowMore>
<span>Last event: {{ last }}</span>
</HuiSpace>
</template>
Custom trigger
The trigger slot receives expanded, toggle and contentId. Preserve aria-expanded and aria-controls on custom buttons for keyboard and screen-reader users.
<script setup lang="ts">
import { HuiButton, HuiShowMore } from '@nscode/handy-ui'
const items = Array.from({ length: 12 }, (_, index) => `Project record ${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 ? 'Collapse list' : `View all (${items.length} items)` }}</HuiButton
>
</template>
</HuiShowMore>
</template>
Short content
A height limit and trigger are applied only when content actually exceeds the limit.
<script setup lang="ts">
import { HuiShowMore, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<div>
<strong>Short content: no button</strong
><HuiShowMore :max-height="120"><p>This short paragraph needs no collapse.</p></HuiShowMore>
</div>
<div>
<strong>Long content: expandable</strong
><HuiShowMore :max-height="120"
><p v-for="index in 10" :key="index">
Line {{ index }} exceeds the available preview height.
</p></HuiShowMore
>
</div>
</HuiSpace>
</template>
Duration
The default is 200ms; 0 switches immediately. System reduced-motion preferences disable animation.
<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">Toggle all</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">Line {{ index }} of content.</p></HuiShowMore
>
</div>
</HuiSpace>
</template>
Methods and dynamic content
toggle() requests a state change. remeasure() measures after the next DOM update. Content size changes are observed automatically; manual measurement is useful after external style changes.
<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">Append three paragraphs</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">
Paragraph {{ index }}: dynamic content is checked again for overflow.
</p></HuiShowMore
>
</HuiSpace>
</template>
Internal state and controlled updates
When expanded is omitted, the component stores its state starting from false, including when only an update listener is provided. Supplying the prop, even as undefined, makes it controlled; undefined displays collapsed content. Rejected requests do not change the stored internal state. Removing the prop restores that state and mode changes warn in development. Default and custom triggers, focus requests and toggle() share this state.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiShowMore, HuiSpace } from '@nscode/handy-ui'
const redraw = ref(0)
const last = ref('No request')
const model = ref(false)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="redraw++">Parent render {{ redraw }}</HuiButton>
<div>
<p>Omitted prop</p>
<HuiShowMore :max-height="48" :duration="0">
<p v-for="index in 4" :key="index">Paragraph {{ index }}</p>
</HuiShowMore>
</div>
<div>
<p>Update listener only</p>
<HuiShowMore :max-height="48" :duration="0" @update:expanded="last = String($event)">
<p v-for="index in 4" :key="index">Paragraph {{ index }}</p>
</HuiShowMore>
</div>
<div>
<p>Explicit undefined</p>
<HuiShowMore :expanded="undefined" :max-height="48" :duration="0">
<p v-for="index in 4" :key="index">Paragraph {{ index }}</p>
</HuiShowMore>
</div>
<div>
<p>Parent rejects updates</p>
<HuiShowMore
:expanded="false"
:max-height="48"
:duration="0"
@update:expanded="last = String($event)"
>
<p v-for="index in 4" :key="index">Paragraph {{ index }}</p>
</HuiShowMore>
</div>
<div>
<p>Two-way binding</p>
<HuiShowMore v-model:expanded="model" :max-height="48" :duration="0">
<p v-for="index in 4" :key="index">Paragraph {{ index }}</p>
</HuiShowMore>
</div>
<p>Bound value: {{ model }}; latest request: {{ last }}</p>
</HuiSpace>
</template>
Props
| Prop | Description | Type | Default |
|---|---|---|---|
| maxHeight | Maximum collapsed height; numbers and numeric strings use px, CSS lengths are also supported | number | string | 120 |
| expanded | Controlled whenever supplied, including undefined (collapsed); omitted props store state from false; supports v-model:expanded | boolean | undefined |
| expandText | Expand text; explicit empty strings are respected | string | Locale “Expand” |
| collapseText | Collapse text; explicit empty strings are respected | string | Locale “Collapse” |
| fade | Fade the bottom edge while collapsed | boolean | true |
| duration | Animation duration in ms; 0 disables animation | number | 200 |
Events
| Event | Description | Parameters |
|---|---|---|
| update:expanded | User requests a state change; supports v-model | expanded: boolean |
| change | User requests expansion or collapse; parent updates do not repeat it | expanded: boolean |
Slots
| Slot | Description | Parameters |
|---|---|---|
| default | Complete content | — |
| trigger | Replace the expand/collapse button; omitted when nothing overflows | { expanded, toggle, contentId } |
Methods
| Method | Description | Parameters |
|---|---|---|
| toggle | Request toggling expansion; does nothing when content does not overflow | — |
| remeasure | Measure after the next DOM update | — |
Types
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
}