Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

feat(core): add padding props to Center (#4054)

Jiun Shinca77e27440a6
diff --git a/.changeset/center-add-padding-paddinginline-paddingblock-sp-pz1v.md b/.changeset/center-add-padding-paddinginline-paddingblock-sp-pz1v.md
new file mode 100644
index 0000000000..8672a07ad5
--- /dev/null
+++ b/.changeset/center-add-padding-paddinginline-paddingblock-sp-pz1v.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[feat] Center: add `padding`, `paddingInline`, `paddingBlock` (spacing-scale inner padding) props. These match the existing `padding` props on `Stack`, `Card`, `LayoutContent`, and `LayoutPanel`, so centered page content no longer needs inline `style={{}}` or `xstyle` wrappers for basic padding.
+@jiunshinn
diff --git a/apps/storybook/stories/Center.stories.tsx b/apps/storybook/stories/Center.stories.tsx
index 11cdfe06ed..a64d42d384 100644
--- a/apps/storybook/stories/Center.stories.tsx
+++ b/apps/storybook/stories/Center.stories.tsx
@@ -37,6 +37,16 @@ const styles = stylex.create({
padding: spacingVars['--spacing-2'],
borderRadius: radiusVars['--radius-element'],
},
+ paddingOutline: {
+ borderWidth: 1,
+ borderStyle: 'dashed',
+ borderColor: colorVars['--color-border-gray'],
+ borderRadius: radiusVars['--radius-element'],
+ },
+ fillArea: {
+ width: '100%',
+ height: '100%',
+ },
});
// Demo box component for visibility
@@ -68,6 +78,22 @@ const meta: Meta<typeof Center> = {
control: 'boolean',
description: 'Whether to render as inline-flex',
},
+ padding: {
+ control: 'select',
+ options: [0, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 8, 10],
+ description: 'Inner padding on all sides (spacing step)',
+ },
+ paddingInline: {
+ control: 'select',
+ options: [0, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 8, 10],
+ description:
+ 'Inline (horizontal) padding; overrides padding on that axis',
+ },
+ paddingBlock: {
+ control: 'select',
+ options: [0, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 8, 10],
+ description: 'Block (vertical) padding; overrides padding on that axis',
+ },
},
};
@@ -191,6 +217,29 @@ export const InsideACard: Story = {
),
};
+// ============================================================================
+// Padding — inner padding via the spacing scale (no inline styles needed)
+// ============================================================================
+
+export const Padding: Story = {
+ args: {
+ axis: 'both',
+ width: '100%',
+ height: 200,
+ padding: 4,
+ children: null,
+ },
+ render: args => (
+ <Section variant="muted" width="100%">
+ <Center {...args} xstyle={styles.paddingOutline}>
+ <div {...stylex.props(styles.fillArea)}>
+ <Box>Inset by padding on the spacing scale</Box>
+ </div>
+ </Center>
+ </Section>
+ ),
+};
+
export const AllAxisModes: Story = {
args: {
children: null,
diff --git a/packages/core/src/Center/Center.doc.mjs b/packages/core/src/Center/Center.doc.mjs
index 7b13e3a394..6584078086 100644
--- a/packages/core/src/Center/Center.doc.mjs
+++ b/packages/core/src/Center/Center.doc.mjs
@@ -36,6 +36,24 @@ export const docs = {
type: 'SizeValue',
description: 'Minimum container height (px or CSS value).',
},
+ {
+ name: 'padding',
+ type: 'SpacingStep',
+ description:
+ 'Inner padding on all sides, using the spacing scale (0, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 8, 10). Matches the padding prop on Stack, Card, LayoutContent, and LayoutPanel. Pass as a JSX number expression e.g. padding={3}.',
+ },
+ {
+ name: 'paddingInline',
+ type: 'SpacingStep',
+ description:
+ 'Inline (horizontal) padding, using the spacing scale. Overrides padding on the inline axis when both are set.',
+ },
+ {
+ name: 'paddingBlock',
+ type: 'SpacingStep',
+ description:
+ 'Block (vertical) padding, using the spacing scale. Overrides padding on the block axis when both are set.',
+ },
{
name: 'isInline',
type: 'boolean',
@@ -95,6 +113,22 @@ export const docsZh = {
{name: 'axis', type: "'both' | 'horizontal' | 'vertical'", description: '居中的方向。', default: "'both'"},
{name: 'width', type: 'number | string', description: '容器宽度(px 或 CSS 值)。'},
{name: 'height', type: 'number | string', description: '容器高度(px 或 CSS 值)。'},
+ {
+ name: 'padding',
+ type: 'SpacingStep',
+ description:
+ '所有方向的内边距,使用间距刻度(0、0.5、1、1.5、2、3、4、5、6、8、10)。与 Stack、Card、LayoutContent 和 LayoutPanel 的 padding 属性一致。在 JSX 中使用数字表达式 e.g. padding={3}。',
+ },
+ {
+ name: 'paddingInline',
+ type: 'SpacingStep',
+ description: '行内(水平)内边距,使用间距刻度。两者同时设置时在行内轴上覆盖 padding。',
+ },
+ {
+ name: 'paddingBlock',
+ type: 'SpacingStep',
+ description: '块(垂直)内边距,使用间距刻度。两者同时设置时在块轴上覆盖 padding。',
+ },
{name: 'isInline', type: 'boolean', description: '使用 inline-flex(适用于文本/图标)。', default: 'false'},
{name: 'children', type: 'ReactNode', description: '要居中的内容。'},
{
@@ -134,6 +168,10 @@ export const docsDense = {
axis: 'centering direction(s)',
width: 'container width (px or CSS)',
height: 'container height (px or CSS)',
+ padding:
+ 'inner padding on all sides (spacing step: 0, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 8, 10)',
+ paddingInline: 'inline (horizontal) padding; overrides padding on that axis',
+ paddingBlock: 'block (vertical) padding; overrides padding on that axis',
isInline: 'use inline-flex for text/icons',
children: 'content to center',
xstyle: 'StyleX styles for layout (margins, positioning, sizing); must be stylex.create() value',
diff --git a/packages/core/src/Center/Center.test.tsx b/packages/core/src/Center/Center.test.tsx
index 4317e91aa7..b95d84cbf1 100644
--- a/packages/core/src/Center/Center.test.tsx
+++ b/packages/core/src/Center/Center.test.tsx
@@ -82,6 +82,81 @@ describe('Center', () => {
expect(element).toBeInTheDocument();
});
+ it('applies a class when padding is set', () => {
+ const {rerender} = render(
+ <Center data-testid="center">
+ <div>Content</div>
+ </Center>,
+ );
+ const baseline = screen.getByTestId('center').className;
+ rerender(
+ <Center padding={3} data-testid="center">
+ <div>Content</div>
+ </Center>,
+ );
+ const withPadding = screen.getByTestId('center').className;
+ expect(withPadding).not.toBe('');
+ expect(withPadding).not.toBe(baseline);
+ });
+
+ it('accepts paddingInline and paddingBlock without error', () => {
+ render(
+ <Center paddingInline={4} paddingBlock={2} data-testid="center">
+ <div>Content</div>
+ </Center>,
+ );
+ expect(screen.getByTestId('center')).toBeInTheDocument();
+ });
+
+ it('lets paddingInline/paddingBlock override padding on their axis', () => {
+ // padding sets both axes; paddingInline overrides the inline axis. The
+ // component should render without conflict and carry a class.
+ render(
+ <Center padding={2} paddingInline={5} data-testid="center">
+ <div>Content</div>
+ </Center>,
+ );
+ expect(screen.getByTestId('center').className).not.toBe('');
+ });
+
+ it('applies a class for explicit padding={0} (zero is a valid spacing step)', () => {
+ const {rerender} = render(
+ <Center data-testid="center">
+ <div>Content</div>
+ </Center>,
+ );
+ const baseline = screen.getByTestId('center').className;
+ rerender(
+ <Center padding={0} data-testid="center">
+ <div>Content</div>
+ </Center>,
+ );
+ expect(screen.getByTestId('center').className).not.toBe(baseline);
+ });
+
+ it('leaves the default className unchanged when no padding props are set', () => {
+ const {rerender} = render(
+ <Center data-testid="center">
+ <div>Content</div>
+ </Center>,
+ );
+ const baseline = screen.getByTestId('center').className;
+ // Opting in changes the className...
+ rerender(
+ <Center padding={3} data-testid="center">
+ <div>Content</div>
+ </Center>,
+ );
+ expect(screen.getByTestId('center').className).not.toBe(baseline);
+ // ...and dropping the prop restores the exact default output.
+ rerender(
+ <Center data-testid="center">
+ <div>Content</div>
+ </Center>,
+ );
+ expect(screen.getByTestId('center').className).toBe(baseline);
+ });
+
it('renders as inline-flex when isInline is true', () => {
render(
<Center isInline data-testid="center">
diff --git a/packages/core/src/Center/Center.tsx b/packages/core/src/Center/Center.tsx
index 626419470b..bf2c5e7edc 100644
--- a/packages/core/src/Center/Center.tsx
+++ b/packages/core/src/Center/Center.tsx
@@ -4,7 +4,7 @@
/**
* @file Center.tsx
- * @input Uses React, StyleX for centering styles
+ * @input Uses React, StyleX for centering styles, Layout padding.stylex for spacing-scale padding
* @output Exports Center component and CenterProps
* @position Center component for centering children horizontally/vertically
*
@@ -18,7 +18,11 @@
import type {ReactNode} from 'react';
import type {BaseProps} from '../BaseProps';
import * as stylex from '@stylexjs/stylex';
-import type {SizeValue} from '../utils/types';
+import type {SizeValue, SpacingStep} from '../utils/types';
+import {
+ paddingInlineStyles,
+ paddingBlockStyles,
+} from '../Layout/padding.stylex';
import {mergeProps} from '../utils';
import {themeProps} from '../utils/themeProps';
@@ -90,6 +94,26 @@ export interface CenterProps extends BaseProps<HTMLDivElement> {
*/
minHeight?: SizeValue;
+ /**
+ * Inner padding on all sides, using the spacing scale.
+ * Accepts numeric spacing steps: 0, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 8, 10.
+ *
+ * Matches the `padding` prop on `Stack`, `Card`, `LayoutContent`, and `LayoutPanel`.
+ */
+ padding?: SpacingStep;
+
+ /**
+ * Inline (horizontal) padding, using the spacing scale.
+ * Overrides `padding` on the inline axis when both are set.
+ */
+ paddingInline?: SpacingStep;
+
+ /**
+ * Block (vertical) padding, using the spacing scale.
+ * Overrides `padding` on the block axis when both are set.
+ */
+ paddingBlock?: SpacingStep;
+
/**
* Whether to make the container inline-flex (useful for text/icons).
* @default false
@@ -121,6 +145,9 @@ export function Center({
height,
maxWidth,
minHeight,
+ padding,
+ paddingInline,
+ paddingBlock,
isInline = false,
children,
xstyle,
@@ -129,6 +156,11 @@ export function Center({
ref,
...props
}: CenterProps) {
+ // Resolve padding to per-axis values: `padding` sets both axes; `paddingInline`
+ // / `paddingBlock` take precedence on their own axis when provided.
+ const resolvedPaddingInline = paddingInline ?? padding;
+ const resolvedPaddingBlock = paddingBlock ?? padding;
+
const stylexProps = mergeProps(
themeProps('center', {axis}),
stylex.props(
@@ -141,6 +173,9 @@ export function Center({
maxWidth ?? null,
minHeight ?? null,
),
+ resolvedPaddingInline != null &&
+ paddingInlineStyles[resolvedPaddingInline],
+ resolvedPaddingBlock != null && paddingBlockStyles[resolvedPaddingBlock],
xstyle,
),
className,