A CardView displays a group of related objects, with support for selection and bulk actions.
Content
CardView follows the Collection Components API, accepting both static and dynamic collections. This example shows a dynamic collection, passing a list of objects to the items prop, and a recursive function to render the children.
import {CardView, AssetCard, CardPreview, Image, Content, Text} from '@react-spectrum/s2/CardView';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
for (let i = 0; images.length < 200; i++) {
images.push({...images[i % 30], id: String(i)});
}
<CardView
aria-label="Nature photos"
styles={style({width: 'full', height: 500})}
layout="waterfall"
size="S"
items={images}>
{image => (
<AssetCard>
<CardPreview>
<Image src={image.image} width={image.width} height={image.height} />
</CardPreview>
<Content>
<Text slot="title">{image.title}</Text>
<Text slot="description">{image.user}</Text>
</Content>
</AssetCard>
)}
</CardView>
Asynchronous loading
Use the loadingState and onLoadMore props to enable async loading and infinite scrolling. When loading, render a <SkeletonCollection> to generate placeholder content to display as skeleton cards.
Links
Use the href prop on a Card to create a link. See the getting started guide to learn how to integrate with your framework. Link interactions vary depending on the selection behavior. See the selection guide for more details.
Empty state
Use renderEmptyState to render placeholder content when the CardView is empty.
Create your first asset.
import {CardView, Content} from '@react-spectrum/s2/CardView';
import {IllustratedMessage, Heading} from '@react-spectrum/s2/IllustratedMessage';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
import Image from '@react-spectrum/s2/illustrations/gradient/generic1/Image';
<CardView
aria-label="Assets"
styles={style({width: 'full', height: 300})}
renderEmptyState={() => (
<IllustratedMessage size="L">
<Image />
<Heading>Create your first asset.</Heading>
<Content>Get started by uploading or importing some assets.</Content>
</IllustratedMessage>
)}>
{[]}
</CardView>
Selection and actions
Use selectionMode to enable single or multiple selection, and selectedKeys (matching each card's id) to control the selected cards. Return an ActionBar from renderActionBar to handle bulk actions, and use onAction for row navigation. Disable cards with isDisabled. See the selection guide for details.
Current selection:
API
<CardView>
<Card />
<SkeletonCollection />
</CardView>
CardView
| Name | Type | Default |
|---|---|---|
layout | 'grid' | 'waterfall' | Default: 'grid'
|
The layout of the cards. | ||
size | 'XS'
| 'S'
| 'M'
| 'L'
| 'XL' | Default: 'M'
|
The size of the cards. | ||
density | 'compact'
| 'regular'
| 'spacious' | Default: 'regular'
|
The amount of space between the cards. | ||
variant | 'primary'
| 'secondary'
| 'tertiary'
| 'quiet' | Default: 'primary'
|
The visual style of the cards. | ||
selectionStyle | 'checkbox' | 'highlight' | Default: 'checkbox'
|
How selection should be displayed. | ||
styles | StylesPropWithHeight | Default: — |
Spectrum-defined styles, returned by the | ||
renderActionBar | | Default: — |
Provides the ActionBar to render when cards are selected in the CardView. | ||
disallowTypeAhead | boolean | Default: false
|
Whether typeahead navigation is disabled. | ||
dragAndDropHooks | DragAndDropHooks | Default: — |
The drag and drop hooks returned by | ||
orientation | Orientation | Default: 'vertical'
|
The primary orientation of the items. Usually this is the direction that the collection scrolls. | ||
children | ReactNode | | Default: — |
The contents of the collection. | ||
items | Iterable | Default: — |
Item objects in the collection. | ||
loadingState | LoadingState | Default: — |
The loading state of the CardView. | ||
onLoadMore | | Default: — |
Handler that is called when more items should be loaded, e.g. while scrolling near the bottom. | ||
renderEmptyState | | Default: — |
Provides content to display when there are no items in the list. | ||
dependencies | ReadonlyArray | Default: — |
Values that should invalidate the item cache when using dynamic collections. | ||
selectionMode | SelectionMode | Default: — |
The type of selection that is allowed in the collection. | ||
selectedKeys | 'all' | Iterable | Default: — |
The currently selected keys in the collection (controlled). | ||
defaultSelectedKeys | 'all' | Iterable | Default: — |
The initial selected keys in the collection (uncontrolled). | ||
onSelectionChange | | Default: — |
Handler that is called when the selection changes. | ||
disabledKeys | Iterable | Default: — |
The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. | ||
disabledBehavior | DisabledBehavior | Default: 'all'
|
Whether | ||
disallowEmptySelection | boolean | Default: — |
Whether the collection allows empty selection. | ||
shouldSelectOnPressUp | boolean | Default: — |
Whether selection should occur on press up instead of press down. | ||
escapeKeyBehavior | 'clearSelection' | 'none' | Default: 'clearSelection'
|
Whether pressing the escape key should clear selection in the grid list or not. Most experiences should not modify this option as it eliminates a keyboard user's ability to easily clear selection. Only use if the escape key is being handled externally or should not trigger selection clearing contextually. | ||
onAction | | Default: — |
Handler that is called when a user performs an action on an item. The exact user event depends
on the collection's | ||
autoFocus | boolean | FocusStrategy | Default: — |
Whether to auto focus the gridlist or an option. | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
UNSAFE_className | UnsafeClassName | Default: — |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: — |
Sets inline style for the
element. Only use as a last resort. Use the | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||