Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
35212ec
Enhance Card Component: Introduce directional padding options and upd…
gigitux Oct 21, 2025
32ca2e4
Merge branch 'trunk' of github.com:WordPress/gutenberg into 72175-car…
gigitux Oct 21, 2025
9351230
add changelog
gigitux Oct 21, 2025
36836b2
add size to footer
gigitux Oct 21, 2025
3f29e38
fix changelog
gigitux Oct 21, 2025
5928de8
Merge branch 'trunk' of github.com:WordPress/gutenberg into 72175-car…
gigitux Oct 22, 2025
0979b78
Merge branch 'trunk' of github.com:WordPress/gutenberg into 72175-car…
gigitux Oct 22, 2025
47ab490
Merge branch 'trunk' of github.com:WordPress/gutenberg into 72175-car…
gigitux Oct 23, 2025
832e1b1
update types
gigitux Oct 24, 2025
f5d8c47
move padding style in get-padding-by-size file
gigitux Oct 24, 2025
c4af856
update story documentation
gigitux Oct 24, 2025
9768cee
update documentation
gigitux Oct 24, 2025
b06b2a5
Merge branch 'trunk' of github.com:WordPress/gutenberg into 72175-car…
gigitux Oct 27, 2025
4a6144d
address feedback
gigitux Oct 27, 2025
a398629
Merge branch 'trunk' of github.com:WordPress/gutenberg into 72175-car…
gigitux Oct 28, 2025
5860263
Update packages/components/src/card/card/README.md
gigitux Oct 30, 2025
ff9fc33
Merge branch 'trunk' of github.com:WordPress/gutenberg into 72175-car…
gigitux Oct 30, 2025
0bfd81e
improve documentation
gigitux Oct 30, 2025
c59cbc5
improve logic
gigitux Oct 30, 2025
b7b554b
Merge branch '72175-card-support-more-padding-options' of github.com:…
gigitux Oct 30, 2025
3c4960a
implement none
gigitux Oct 30, 2025
7bca388
Merge branch 'trunk' of github.com:WordPress/gutenberg into 72175-car…
gigitux Nov 4, 2025
767da77
clean up code
gigitux Nov 4, 2025
5c873be
remove cardPaddings object
gigitux Nov 4, 2025
ba07d71
fix changelog
gigitux Nov 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

- Reexport all of the `build-types` folder from the `components` package entry point ([#72809](https://github.com/WordPress/gutenberg/pull/72809)).
- `TextareaControl`: Add `min-height` to the textarea element ([#72867](https://github.com/WordPress/gutenberg/pull/72867)).
- `Card`, `CardHeader`, `CardBody` and `CardFooter`: Add support for directional padding through object-based size prop, allowing independent control of padding for each side ([#72511](https://github.com/WordPress/gutenberg/pull/72511)).

## 30.7.0 (2025-10-29)

Expand Down
17 changes: 14 additions & 3 deletions packages/components/src/card/card-body/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,21 @@ Renders with a light gray background color.
- Required: No
- Default: `false`

### `size`: `string`
### `size`: `string | object`

Determines the amount of padding within the component.
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: `xSmall`, `small`, `medium`, `large`
- Allowed values:
- Single size token: `none`, `xSmall`, `small`, `medium`, `large`
- Object:

```ts
{
blockStart: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
blockEnd: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
inlineStart: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
inlineEnd: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
}
```
3 changes: 2 additions & 1 deletion packages/components/src/card/card-body/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useContextSystem } from '../../context';
import * as styles from '../styles';
import { useCx } from '../../utils/hooks/use-cx';
import type { BodyProps } from '../types';
import { getPaddingBySize } from '../get-padding-by-size';

export function useCardBody(
props: WordPressComponentProps< BodyProps, 'div' >
Expand All @@ -30,7 +31,7 @@ export function useCardBody(
cx(
styles.Body,
styles.borderRadius,
styles.cardPaddings[ size ],
getPaddingBySize( size ),
isShady && styles.shady,
// This classname is added for legacy compatibility reasons.
'components-card__body',
Expand Down
17 changes: 14 additions & 3 deletions packages/components/src/card/card-footer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,21 @@ Renders with a light gray background color.

See the documentation for the `justify` prop for the [`Flex` component](/packages/components/src/flex/flex/README.md#justify)

### `size`: `string`
### `size`: `string | object`

Determines the amount of padding within the component.
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: `xSmall`, `small`, `medium`, `large`
- Allowed values:
- Single size token: `none`, `xSmall`, `small`, `medium`, `large`
- Object:

```ts
{
blockStart: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
blockEnd: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
inlineStart: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
inlineEnd: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
}
```
3 changes: 2 additions & 1 deletion packages/components/src/card/card-footer/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useContextSystem } from '../../context';
import * as styles from '../styles';
import { useCx } from '../../utils/hooks/use-cx';
import type { FooterProps } from '../types';
import { getPaddingBySize } from '../get-padding-by-size';

export function useCardFooter(
props: WordPressComponentProps< FooterProps, 'div' >
Expand All @@ -32,7 +33,7 @@ export function useCardFooter(
styles.Footer,
styles.borderRadius,
styles.borderColor,
styles.cardPaddings[ size ],
getPaddingBySize( size ),
isBorderless && styles.borderless,
isShady && styles.shady,
// This classname is added for legacy compatibility reasons.
Expand Down
17 changes: 14 additions & 3 deletions packages/components/src/card/card-header/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,21 @@ Renders with a light gray background color.
- Required: No
- Default: `false`

### `size`: `string`
### `size`: `string | object`

Determines the amount of padding within the component.
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: `xSmall`, `small`, `medium`, `large`
- Allowed values:
- Single size token: `none`, `xSmall`, `small`, `medium`, `large`
- Object:

```ts
{
blockStart: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
blockEnd: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
inlineStart: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
inlineEnd: 'none' | 'xSmall' | 'small' | 'medium' | 'large';
}
```
3 changes: 2 additions & 1 deletion packages/components/src/card/card-header/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useContextSystem } from '../../context';
import * as styles from '../styles';
import { useCx } from '../../utils/hooks/use-cx';
import type { HeaderProps } from '../types';
import { getPaddingBySize } from '../get-padding-by-size';

export function useCardHeader(
props: WordPressComponentProps< HeaderProps, 'div' >
Expand All @@ -31,7 +32,7 @@ export function useCardHeader(
styles.Header,
styles.borderRadius,
styles.borderColor,
styles.cardPaddings[ size ],
getPaddingBySize( size ),
isBorderless && styles.borderless,
isShady && styles.shady,
// This classname is added for legacy compatibility reasons.
Expand Down
51 changes: 48 additions & 3 deletions packages/components/src/card/card/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,24 @@ Renders with rounded corners.
- Required: No
- Default: `true`

### `size`: `string`
### `size`: `string | object`

Determines the amount of padding within the component.
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: `xSmall`, `small`, `medium`, `large`
- Allowed values:
- Single size token: `none`, `xSmall`, `small`, `medium`, `large`
- Object:

```ts
{
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

Expand Down Expand Up @@ -104,6 +115,40 @@ const Example = () => (
);
```

### 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

```jsx
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](https://react.dev/learn/passing-data-deeply-with-context). Certain props like `size` and `isBorderless` are passed through to some of the sub-components.
Expand Down
70 changes: 70 additions & 0 deletions packages/components/src/card/get-padding-by-size.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* External dependencies
*/
import { css, type SerializedStyles } from '@emotion/react';

/**
* Internal dependencies
*/
import type { Props, SizeToken } from './types';
import { space } from '../utils/space';

const xSmallCardPadding = css`
padding: ${ space( 2 ) };
`;

export const cardPaddings = {
none: css`
padding: 0;
`,
large: css`
padding: ${ space( 6 ) } ${ space( 8 ) };
`,
medium: css`
padding: ${ space( 4 ) } ${ space( 6 ) };
`,
small: css`
padding: ${ space( 4 ) };
`,
xSmall: xSmallCardPadding,
// The `extraSmall` size is not officially documented, but the following styles
// are kept for legacy reasons to support older values of the `size` prop.
extraSmall: xSmallCardPadding,
};

const getSinglePaddingValue = ( size: SizeToken ): string | undefined => {
switch ( size ) {
case 'xSmall':
return space( 2 );
case 'small':
return space( 4 );
case 'medium':
return space( 6 );
Comment on lines +41 to +42

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One quirk of this approach is that if I specify size={ { blockStart: 'medium' } }, I'd get space( 6 ) for the top padding, but if I specify size="medium", I get space( 4 ) for the top padding.

We could maybe have some convoluted logic for mapping sizes based on vertical or horizontal properties, but I do quite like keeping this simpler with each step mapping to 2, 4, 6, and 8.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, do you expect some changes here, or is it just a note?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily expecting any changes, just flagging a potential point of confusion or inconsistency of expectations. Unless you have concerns about it, I'm personally happy to try this as-is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it could cause some confusion, but I’m not sure how to fix it without introducing breaking changes.

The idea is that with size={{ blockStart: 'medium', blockEnd: 'medium', inlineStart: 'medium', inlineEnd: 'medium' }}, we can define consistent padding across all properties.

case 'large':
return space( 8 );
case 'none':
return '0';
default:
return space( 6 );
}
Comment on lines +47 to +49

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what I think about the "fall back to 0" behavior, given that 0 is not an explicitly allowed value and the object shape marks all properties as optional. I can see it becoming a weird loophole with consumers who want to set a 0 padding.

	| {
			blockStart?: SizeToken;
			blockEnd?: SizeToken;
			inlineStart?: SizeToken;
			inlineEnd?: SizeToken;
	  };

I can think of two ways to address this:

  1. Mark all sides as required in the object case, and fall back to medium for undefined or invalid sides.
  2. Officially support none as a size token.

No strong option here — 2 is probably fine? I'll also note that there's already a CardMedia subcomponent that can be used for full-bleed content.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mark all sides as required in the object case, and fall back to medium for undefined or invalid sides.

I took this approach given that it is simple.

};

export const getPaddingBySize = ( size: Props[ 'size' ] ): SerializedStyles => {
// Handle string-based sizes (both standard and deprecated)
if ( typeof size === 'string' ) {
return cardPaddings[ size as SizeToken ];
}

if ( size ) {
const { blockStart, blockEnd, inlineStart, inlineEnd } = size;
return css`
padding-block-start: ${ getSinglePaddingValue( blockStart ) };
padding-block-end: ${ getSinglePaddingValue( blockEnd ) };
padding-inline-start: ${ getSinglePaddingValue( inlineStart ) };
padding-inline-end: ${ getSinglePaddingValue( inlineEnd ) };
`;
Comment on lines +60 to +65

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The padding shorthand property is not a logical one, so it's not going to respect our logical values. We have to set each property separately (padding-block-start, etc).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry! I completely forgot to update it! I fixed it!

}

// Default to medium if no size is provided
return cardPaddings.medium;
};
59 changes: 59 additions & 0 deletions packages/components/src/card/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,62 @@ export const FullBleedContent: StoryObj< typeof Card > = {
),
},
};

/**
* The Card component supports three approaches to padding:
* 1. Default padding (medium) - no size prop needed
* 2. Token-based padding - using size tokens: xSmall, small, medium, large
* 3. Logical padding - customize each direction using logical properties
*
* Each component (Card, CardHeader, CardBody) can have its own padding configuration.
*/
Comment on lines +95 to +102

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we remove the exact pixel values from this story description and from the code comments? We don't want to commit to pixel values in documentation, since they could be changed at some point.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with c4af856

export const PaddingVariations: StoryObj< typeof Card > = {
render: () => (
<div
style={ { display: 'flex', flexDirection: 'column', gap: '32px' } }
>
{ /* 1. Default Padding */ }
<div>
<Card>
<CardHeader>
<Text>Header with default padding</Text>
</CardHeader>
<CardBody>
<Text>Body with default padding (medium)</Text>
</CardBody>
</Card>
</div>

<div>
<Card>
<CardHeader
size={ {
blockStart: 'large',
blockEnd: 'small',
inlineStart: 'xSmall',
inlineEnd: 'large',
} }
>
<Text>
Header with logical padding (large blockStart, small
blockEnd, xSmall inlineStart, large inlineEnd)
</Text>
</CardHeader>
<CardBody
size={ {
blockStart: 'medium',
blockEnd: 'xSmall',
inlineStart: 'large',
inlineEnd: 'xSmall',
} }
>
<Text>
Body with logical padding (medium blockStart, xSmall
blockEnd, large inlineStart, xSmall inlineEnd)
</Text>
</CardBody>
</Card>
</div>
</div>
),
};
20 changes: 0 additions & 20 deletions packages/components/src/card/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,6 @@ export const rounded = css`
border-radius: ${ adjustedBorderRadius };
`;

const xSmallCardPadding = css`
padding: ${ CONFIG.cardPaddingXSmall };
`;

export const cardPaddings = {
large: css`
padding: ${ CONFIG.cardPaddingLarge };
`,
medium: css`
padding: ${ CONFIG.cardPaddingMedium };
`,
small: css`
padding: ${ CONFIG.cardPaddingSmall };
`,
xSmall: xSmallCardPadding,
// The `extraSmall` size is not officially documented, but the following styles
// are kept for legacy reasons to support older values of the `size` prop.
extraSmall: xSmallCardPadding,
};

export const shady = css`
background-color: ${ COLORS.ui.backgroundDisabled };
`;
10 changes: 9 additions & 1 deletion packages/components/src/card/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import type { SurfaceProps } from '../surface/types';
import type { FlexProps } from '../flex/types';

type DeprecatedSizeOptions = 'extraSmall';
export type SizeOptions = 'xSmall' | 'small' | 'medium' | 'large';
export type SizeToken = 'xSmall' | 'small' | 'medium' | 'large' | 'none';

export type SizeOptions =
| SizeToken
| {
blockStart: SizeToken;
blockEnd: SizeToken;
inlineStart: SizeToken;
inlineEnd: SizeToken;
};
type SizeableProps = {
/**
* Determines the amount of padding within the component.
Expand Down
Loading