Skip to content

Commit

Permalink
extend width prop
Browse files Browse the repository at this point in the history
  • Loading branch information
8845musign committed Jul 12, 2024
1 parent f36063c commit 09ef38b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/components/Center/Center.module.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.center {
width: var(--width);
min-width: var(--min-width);
max-width: var(--max-width);
padding: var(--padding-top) var(--padding-right) var(--padding-bottom) var(--padding-left);
margin-inline: auto;
Expand Down
33 changes: 33 additions & 0 deletions src/components/Center/Center.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,37 @@ describe('<Center>', () => {
expect(div).toHaveStyle('--padding-bottom: var(--size-spacing-lg)');
expect(div).toHaveStyle('--padding-left: var(--size-spacing-xl)');
});

it('receives width', () => {
render(
<Center width="100px" data-testid="flex-item">
Test
</Center>,
);
const div = screen.getByTestId('flex-item');

expect(div).toHaveStyle('--width: 100px');
});

it('receives max-width', () => {
render(
<Center maxWidth="100px" data-testid="flex-item">
Test
</Center>,
);
const div = screen.getByTestId('flex-item');

expect(div).toHaveStyle('--max-width: 100px');
});

it('receives min-width', () => {
render(
<Center minWidth="100px" data-testid="flex-item">
Test
</Center>,
);
const div = screen.getByTestId('flex-item');

expect(div).toHaveStyle('--min-width: 100px');
});
});
19 changes: 11 additions & 8 deletions src/components/Center/Center.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
} from 'react';
import styles from './Center.module.css';
import { CustomDataAttributeProps } from '../../types/attributes';
import { marginVariables, paddingVariables } from '../../utils/style';
import { marginVariables, paddingVariables, widthVariables } from '../../utils/style';
import { HTMLTagname } from '../../utils/types';
import { Box } from '../Box/Box';
import type { MarginYProps, PaddingProps } from '../../types/style';
import type { MarginYProps, PaddingProps, WidthProps } from '../../types/style';

type Props = {
/**
Expand All @@ -34,17 +34,13 @@ type Props = {
* @default false
*/
childrenCenter?: boolean;
/**
* 横幅の最大値
* @default none
*/
maxWidth?: string;
/**
* HTMLのID属性の値
*/
id?: string;
} & MarginYProps &
PaddingProps &
WidthProps &
CustomDataAttributeProps;

export const Center: FC<PropsWithChildren<Props>> = ({
Expand All @@ -63,7 +59,9 @@ export const Center: FC<PropsWithChildren<Props>> = ({
textCenter,
childrenCenter,
id,
maxWidth = 'none',
width,
minWidth,
maxWidth,
...props
}) => {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
Expand Down Expand Up @@ -95,6 +93,11 @@ export const Center: FC<PropsWithChildren<Props>> = ({
mt,
mb,
}),
...widthVariables({
width,
minWidth,
maxWidth,
}),
} as CSSProperties,
...props,
},
Expand Down
22 changes: 22 additions & 0 deletions src/stories/Center.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,25 @@ export const CustomDataAttribute: Story = {
</Center>
),
};

export const Width: Story = {
render: () => (
<>
<Center maxWidth="100%" textCenter>
<p>width: 100%</p>
</Center>

<br />

<Center maxWidth="500px" textCenter>
<p>max-width: 500px</p>
</Center>

<br />

<Center minWidth="500px" textCenter>
<p>min-width: 500px</p>
</Center>
</>
),
};

0 comments on commit 09ef38b

Please sign in to comment.