Repository navigation
Card Component Padding System Enhancement #72511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
35212ec
32ca2e4
9351230
36836b2
3f29e38
5928de8
0979b78
47ab490
832e1b1
f5d8c47
c4af856
9768cee
b06b2a5
4a6144d
a398629
5860263
ff9fc33
0bfd81e
c59cbc5
b7b554b
3c4960a
7bca388
767da77
5c873be
ba07d71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 ); | ||
| case 'large': | ||
| return space( 8 ); | ||
| case 'none': | ||
| return '0'; | ||
| default: | ||
| return space( 6 ); | ||
| } | ||
|
Comment on lines
+47
to
+49
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
No strong option here — 2 is probably fine? I'll also note that there's already a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
| ), | ||
| }; | ||
There was a problem hiding this comment.
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 getspace( 6 )for the top padding, but if I specifysize="medium", I getspace( 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, and8.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.