Card
Group related information in a bordered container with a shadow, consisting of a header, body, and footer. Cards can have a cover image, collapse control, and close button.
Import
import { HuiCard } from '@nscode/handy-ui'
Internal State and Controlled Updates
When collapsed is omitted, internal state starts at false. Prop presence enables controlled mode, with undefined displayed as false. Collapsing preserves content state and emits only update:collapsed. Without the prop, or with only an update listener, the component maintains its state across unrelated parent renders. With the prop present, including undefined, rejected updates leave the parent's value displayed. Mode changes produce a development diagnostic; there is no additional defaultValue prop.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCard, HuiButton, HuiSpace, HuiInput } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('No requests yet')
const q1Model = ref(false)
</script>
<template>
<div class="q1-state-demo card-q1-demo">
<HuiButton class="q1-redraw" @click="q1Redraw++">Parent redraw {{ q1Redraw }}</HuiButton>
<HuiSpace direction="vertical" fill>
<div>
<p>Prop omitted: internal state</p>
<div class="q1-local">
<HuiCard title="Content" collapsible
><HuiInput placeholder="Input persists when collapsed"
/></HuiCard>
</div>
</div>
<div>
<p>Listener only: still internal state</p>
<div class="q1-listener">
<HuiCard title="Content" collapsible @update:collapsed="q1Last = String($event)"
><HuiInput placeholder="Input persists when collapsed"
/></HuiCard>
</div>
</div>
<div>
<p>Explicit undefined: parent value wins</p>
<div class="q1-empty">
<HuiCard title="Content" collapsible :collapsed="undefined"
><HuiInput placeholder="Input persists when collapsed"
/></HuiCard>
</div>
</div>
<div>
<p>Fixed controlled value: parent rejects updates</p>
<div class="q1-fixed">
<HuiCard
title="Content"
collapsible
:collapsed="false"
@update:collapsed="q1Last = String($event)"
><HuiInput placeholder="Input persists when collapsed"
/></HuiCard>
</div>
</div>
<div>
<p>Two-way binding</p>
<div class="q1-bound">
<HuiCard title="Content" collapsible v-model:collapsed="q1Model"
><HuiInput placeholder="Input persists when collapsed"
/></HuiCard>
</div>
</div>
<p class="q1-notification">Bound value:{{ q1Model }}; latest request:{{ q1Last }}</p>
</HuiSpace>
</div>
</template>
Basic Usage
title sets the heading; the default slot provides the body content.
<script setup lang="ts">
import { HuiCard } from '@nscode/handy-ui'
</script>
<template>
<HuiCard title="Card title">
<p>Card content</p>
</HuiCard>
</template>
Titles and Subtitles
subtitle appears below the title. Customize either through the title and subtitle slots. Content in the header slot, or legacy header prop, appears below the title and subtitle and can provide custom header content.
<script setup lang="ts">
import { HuiCard, HuiSpace, HuiTag } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCard title="Card title" subtitle="This is the subtitle">
<p>Card content</p>
</HuiCard>
<HuiCard>
<template #title>Custom title</template>
<p>Card content</p>
</HuiCard>
<HuiCard title="Order #20261118">
<template #subtitle>
<HuiSpace size="small">
<HuiTag type="success" size="small">Paid</HuiTag>
<HuiTag type="info" size="small">Awaiting shipment</HuiTag>
</HuiSpace>
</template>
<p>Use the subtitle slot for tags below the title</p>
</HuiCard>
<HuiCard>
<template #header>
<div><strong>header slot</strong>: custom header content</div>
</template>
<p>Card content</p>
</HuiCard>
<HuiCard header="header prop (legacy)">
<p>Use title in new code</p>
</HuiCard>
</HuiSpace>
</template>
Header Actions
The header-extra slot appears on the right of the header, suitable for actions such as Edit or More. Clicking this area does not collapse a collapsible card; clicking elsewhere in the header does.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCard, HuiButton, HuiSpace } from '@nscode/handy-ui'
const editCount = ref(0)
const edit = () => {
editCount.value += 1
}
</script>
<template>
<HuiCard title="User information" collapsible>
<template #header-extra>
<HuiSpace size="small">
<HuiButton size="small" @click="edit">Edit</HuiButton>
<HuiButton size="small" variant="text" icon="more-horizontal" aria-label="More" />
</HuiSpace>
</template>
<p>Edit clicked {{ editCount }} times; the card did not collapse</p>
</HuiCard>
</template>
Footer
The footer slot uses secondary text color. The action slot displays a row of buttons below it, automatically spaced by 8px. Either slot can be used independently.
<script setup lang="ts">
import { HuiCard, HuiButton, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCard title="Card with a footer">
<p>Card content</p>
<template #footer>Updated on 2026-11-17</template>
<template #action>
<HuiButton size="small">Cancel</HuiButton>
<HuiButton size="small" type="primary">Confirm</HuiButton>
</template>
</HuiCard>
<HuiCard title="footer only">
<p>Card content</p>
<template #footer>12 records</template>
</HuiCard>
<HuiCard title="action only">
<p>Card content</p>
<template #action>
<HuiButton size="small" icon="edit">Edit</HuiButton>
<HuiButton size="small" icon="share">Share</HuiButton>
<HuiButton size="small" type="danger" icon="trash">Delete</HuiButton>
</template>
</HuiCard>
</HuiSpace>
</template>
Cover Images
Pass an image URL to cover to display it at the top. Its alt uses title, or an empty string when no title is provided, allowing screen readers to skip it.
<script setup lang="ts">
import { HuiButton, HuiCard, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap align="start">
<HuiCard cover="/images/cover.png" title="Landscape" style="width: 300px">
<p>Card content</p>
</HuiCard>
<HuiCard cover="/images/cover.png" hoverable style="width: 300px">
<p>Without a title, the image alt is empty</p>
<template #action>
<HuiButton size="small" type="primary">View</HuiButton>
</template>
</HuiCard>
</HuiSpace>
</template>
Shadows, Borders, and Hover Effects
shadow controls when the shadow appears: always (the default), hover, or never. :bordered="false" removes the border. hoverable raises the card by 2px on hover, deepens the shadow, and shows a pointer cursor, suitable for clickable cards. Hover over the cards below to compare:
<script setup lang="ts">
import { HuiCard } from '@nscode/handy-ui'
</script>
<template>
<div class="grid">
<HuiCard title="Always visible (always)">
<p>Default</p>
</HuiCard>
<HuiCard title="Visible on hover (hover)" shadow="hover">
<p>Shadow appears on hover</p>
</HuiCard>
<HuiCard title="Hidden (never)" shadow="never">
<p>Border only</p>
</HuiCard>
<HuiCard title="No border" :bordered="false">
<p>Shadow only</p>
</HuiCard>
<HuiCard title="Hover shadow, no border" shadow="hover" :bordered="false">
<p>Blends into the background when not hovered</p>
</HuiCard>
<HuiCard title="Hoverable card (hoverable)" hoverable>
<p>Rises on hover</p>
</HuiCard>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: var(--hui-spacing-4);
}
</style>
Sizes
size accepts small, default, or large. In that order, body padding is 12, 16, or 20px; body and footer text is 12, 14, or 16px; titles are 14, 16, or 20px; collapse and close icons are 14, 16, or 18px. When omitted, it follows HuiConfigProvider.size, falling back to default.
The legacy medium value is deprecated and behaves like default; existing code can continue to use it.
<script setup lang="ts">
import { HuiButton, HuiCard, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCard size="small" title="Small (small)" subtitle="Subtitle" collapsible closable>
<p>Card content</p>
<template #footer>Footer text</template>
<template #action>
<HuiButton size="small" type="primary">Confirm</HuiButton>
</template>
</HuiCard>
<HuiCard title="Default (default)" subtitle="Subtitle" collapsible closable>
<p>Card content</p>
<template #footer>Footer text</template>
<template #action>
<HuiButton type="primary">Confirm</HuiButton>
</template>
</HuiCard>
<HuiCard size="large" title="Large (large)" subtitle="Subtitle" collapsible closable>
<p>Card content</p>
<template #footer>Footer text</template>
<template #action>
<HuiButton size="large" type="primary">Confirm</HuiButton>
</template>
</HuiCard>
</HuiSpace>
</template>
Body Styles
body-style adds inline styles to the body, for example to remove padding for an edge-to-edge image or change its background.
<script setup lang="ts">
import { HuiCard, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap align="start">
<HuiCard :body-style="{ padding: '0' }" style="width: 300px">
<img src="/images/cover.png" alt="" style="width: 100%; display: block" />
</HuiCard>
<HuiCard
title="Custom background and padding"
:body-style="{ padding: '32px', backgroundColor: 'var(--hui-fill-color)' }"
style="width: 300px"
>
<p>The body background is --hui-fill-color</p>
</HuiCard>
</HuiSpace>
</template>
Collapsible Cards
collapsible enables toggling through the header, excluding its action area, or the collapse button on the right. When collapsed, only the header remains and the footer is hidden. Use v-model:collapsed to read and control the state. A bare collapsed attribute is also controlled, so clicking only requests an update. For an initially collapsed card that can toggle, bind v-model:collapsed to a value initialized to true.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCard, HuiButton, HuiSpace } from '@nscode/handy-ui'
const collapsed = ref(false)
</script>
<template>
<HuiSpace direction="vertical" fill>
<div>
<HuiButton @click="collapsed = !collapsed">{{ collapsed ? 'Expand' : 'Collapse' }}</HuiButton>
</div>
<HuiCard v-model:collapsed="collapsed" title="Collapsible card" collapsible>
<p>Card content</p>
<template #footer>Current state:{{ collapsed ? 'Collapsed' : 'Expanded' }}</template>
</HuiCard>
<HuiCard title="Fixed collapse; parent rejects updates" collapsible collapsed>
<p>Click the header to request expansion; the parent keeps it collapsed</p>
<template #footer>The footer appears only after expansion</template>
</HuiCard>
</HuiSpace>
</template>
Closable Cards
closable displays a close button on the right of the header, including when there is no title. Clicking only emits close; the parent must hide the card. With collapsible, the collapse button appears to the left of the close button.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCard, HuiSpace } from '@nscode/handy-ui'
const visible = ref(true)
const noTitleVisible = ref(true)
const reset = () => {
visible.value = true
noTitleVisible.value = true
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCard v-if="visible" title="Closable card" closable collapsible @close="visible = false">
<p>Card content</p>
</HuiCard>
<HuiCard v-if="noTitleVisible" closable @close="noTitleVisible = false">
<p>The close button also appears without a title</p>
</HuiCard>
<div v-if="!visible || !noTitleVisible">
<HuiButton icon="refresh" @click="reset">Show again</HuiButton>
</div>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | - | Title |
| subtitle | string | - | Subtitle below the title |
| header | string | - | Legacy header text; use title in new code |
| cover | string | - | Cover image URL |
| bodyStyle | Record<string, string | number> | - | Inline body styles |
| shadow | 'always' | 'hover' | 'never' | 'always' | When to display a shadow |
| bordered | boolean | true | Whether to show the border |
| hoverable | boolean | false | Whether to rise and deepen the shadow on hover |
| collapsible | boolean | false | Whether collapsible |
| collapsed | boolean | undefined | Whether collapsed, supporting v-model:collapsed when collapsible is enabled. Internal state starts at false if omitted; prop presence enables controlled mode, with undefined displayed as false |
| closable | boolean | false | Whether to show the close button |
| size | 'large' | 'default' | 'small' | 'medium' | - | Size. Follows HuiConfigProvider.size when omitted, otherwise falls back to 'default'. Deprecated 'medium' is equivalent to 'default' |
Events
| Event | Callback arguments | Description |
|---|---|---|
| update:collapsed | (collapsed: boolean) | Emitted when the user requests a collapse-state change through the header or collapse button; used by v-model:collapsed |
| close | - | Emitted when the close button is clicked; does not hide the card automatically |
Slots
| Slot | Scope arguments | Description |
|---|---|---|
| default | - | Body area |
| title | - | Custom title, replacing the title prop |
| subtitle | - | Custom subtitle, replacing the subtitle prop |
| header | - | Custom header content below the title and subtitle, replacing the header prop |
| header-extra | - | Actions on the header's right; clicking here does not collapse the card |
| footer | - | Footer area |
| action | - | Row of action buttons below footer content |
Types
import type { CardProps, CardEmits, CardShadow, CardSize } from '@nscode/handy-ui'
type CardShadow = 'always' | 'hover' | 'never'
/** @deprecated Use 'default'. 'medium' remains only for compatibility and behaves like 'default' */
type CardSizeMedium = 'medium'
type CardSize = 'large' | 'default' | 'small' | CardSizeMedium
interface CardProps {
header?: string
title?: string
subtitle?: string
bodyStyle?: Record<string, string | number>
shadow?: CardShadow
bordered?: boolean
hoverable?: boolean
collapsible?: boolean
collapsed?: boolean
closable?: boolean
cover?: string
size?: CardSize
}
interface CardEmits {
(e: 'update:collapsed', value: boolean): void
(e: 'close'): void
}