块编辑器开发文档

💡 云策文档标注

概述

Card 是 WordPress 组件库中一个灵活且可扩展的内容容器组件,用于构建界面布局。它提供了一系列子组件,如 CardHeader、CardBody 等,并支持多种属性配置,如阴影、边框和逻辑内边距。

关键要点

  • Card 是一个核心容器组件,可组合使用 CardHeader、CardBody、CardFooter、CardDivider 和 CardMedia 等子组件。
  • 支持 props 如 elevation(阴影大小)、isBorderless(无边框)、isRounded(圆角)和 size(内边距控制),其中 size 可接受单个尺寸令牌或对象形式。
  • size prop 支持逻辑属性(blockStart、blockEnd、inlineStart、inlineEnd),以适应不同书写方向。
  • 子组件通过 Context 继承 Card 的 props(如 size),但可被覆盖以提供灵活性。
  • Card 继承了 Surface 组件的所有 props。

代码示例

import {
    Card,
    CardHeader,
    CardBody,
    CardFooter,
    __experimentalText as Text,
    __experimentalHeading as Heading,
} from '@wordpress/components';

function Example() {
    return (
        <Card>
            <CardHeader>
                <Heading level={ 4 }>Card Title</Heading>
            </CardHeader>
            <CardBody>
                <Text>Card Content</Text>
            </CardBody>
            <CardFooter>
                <Text>Card Footer</Text>
            </CardFooter>
        </Card>
    );
}

注意事项

  • 使用子组件时,确保从 '@wordpress/components' 正确导入。
  • size prop 的对象形式允许精细控制内边距,但需注意逻辑属性在不同书写模式下的映射。
  • Context 机制使得子组件默认继承 Card 的 props,但可通过显式设置覆盖,以实现定制化布局。

📄 原文内容

Card provides a flexible and extensible content container.

Usage

Card also provides a convenient set of sub-components such as CardBody, CardHeader, CardFooter, and more (see below).

import {
    Card,
    CardHeader,
    CardBody,
    CardFooter,
    __experimentalText as Text,
    __experimentalHeading as Heading,
} from '@wordpress/components';

function Example() {
    return (
        <Card>
            <CardHeader>
                <Heading level={ 4 }>Card Title</Heading>
            </CardHeader>
            <CardBody>
                <Text>Card Content</Text>
            </CardBody>
            <CardFooter>
                <Text>Card Footer</Text>
            </CardFooter>
        </Card>
    );
}

Props

elevation: number

Size of the elevation shadow, based on the Style system’s elevation system. This may be helpful in highlighting certain content. For more information, check out Elevation.

  • Required: No
  • Default: 0

isBorderless: boolean

Renders without a border.

  • Required: No
  • Default: false

isRounded: boolean

Renders with rounded corners.

  • Required: No
  • Default: true

size: string | object

Determines the amount of padding within the component. Can be specified either as a single size token or as an object.

  • Required: No
  • Default: medium
  • Allowed values:
    • Single size token: none, xSmall, small, medium, large
    • Object:
    {
      blockStart: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
      blockEnd: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
      inlineStart: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
      inlineEnd: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
    }
    

Inherited props

Card also inherits all of the Surface props.

Sub-Components

This component provides a collection of sub-component that can be used to compose various interfaces.

Sub-Components Example

import {
    Card,
    CardBody,
    CardDivider,
    CardFooter,
    CardHeader,
    CardMedia,
} from '@wordpress/components';

const Example = () => (
    <Card>
        <CardHeader>...</CardHeader>
        <CardBody>...</CardBody>
        <CardDivider />
        <CardBody>...</CardBody>
        <CardMedia>
            <img src="..." />
        </CardMedia>
        <CardFooter>...</CardFooter>
    </Card>
);

Logical Padding Properties

The size prop supports logical properties that adapt to different writing directions:

  • blockStart – Maps to top in horizontal writing modes
  • blockEnd – Maps to bottom in horizontal writing modes
  • inlineStart – Maps to left in horizontal left-to-right writing modes
  • inlineEnd – Maps to right in horizontal left-to-right writing modes
import { Card, CardBody } from '@wordpress/components';

const Example = () => (
    <Card
        size={ {
            blockStart: 'large',
            blockEnd: 'small',
            inlineStart: 'medium',
            inlineEnd: 'medium',
        } }
    >
        <CardBody
            size={ {
                blockStart: 'small',
                inlineStart: 'large',
                inlineEnd: 'large',
            } }
        >
            Content with logical padding properties
        </CardBody>
    </Card>
);

Context

<Card />‘s sub-components are connected to <Card /> using Context. Certain props like size and isBorderless are passed through to some of the sub-components.

In the following example, the <CardBody /> will render with a size of small:

import { Card, CardBody } from '@wordpress/components';

const Example = () => (
    <Card size="small">
        <CardBody>...</CardBody>
    </Card>
);

These sub-components are designed to be flexible. The Context props can be overridden by the sub-component(s) as required. In the following example, the last <CardBody /> will render it’s specified size:

import { Card, CardBody } from '@wordpress/components';

const Example = () => (
    <Card size="small">
        <CardBody>...</CardBody>
        <CardBody>...</CardBody>
        <CardBody size="large">...</CardBody>
    </Card>
);