Skip to content

Commit

Permalink
Merge pull request #143 from 8845musign/add-spacing-alias-to-flex
Browse files Browse the repository at this point in the history
Add gap alias prop to Stack Component
  • Loading branch information
takanorip authored Nov 18, 2024
2 parents adf3a7b + 4de6e56 commit 8751851
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
43 changes: 35 additions & 8 deletions src/components/Flex/Flex.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import clsx from 'clsx';
import { cloneElement, isValidElement } from 'react';
import { isValidElement, cloneElement, useMemo } from 'react';
import styles from './Flex.module.css';
import { CustomDataAttributeProps } from '../../types/attributes'; // 追加したインポート
import { AlignItems, CSSWitdh, FlexDirection, JustifyContent, Spacing, WidthProps } from '../../types/style';
Expand All @@ -17,11 +17,6 @@ type Props = {
* @default div
*/
as?: HTMLTagname | ReactElement<ComponentType<typeof Box>>;
/**
* 子要素同士の間隔。指定しない場合は0
* xxs=4px, xs=8px, sm=12px, md=16px, lg=24px, xl=40px, xxl=64px
*/
spacing?: Spacing;
/**
* direction 重ねる向き
* @default row
Expand Down Expand Up @@ -54,7 +49,29 @@ type Props = {
* inline-flexとして扱う
*/
inline?: boolean;
} & MarginProps &
} & (
| {
/**
* 子要素の間隔。指定しない場合は0
* xxs=4px, xs=8px, sm=12px, md=16px, lg=24px, xl=40px, xxl=64px
*/
spacing: Spacing;
gap?: never;
}
| {
/**
* spacingのエイリアス(どちらかしか指定できません)
* xxs=4px, xs=8px, sm=12px, md=16px, lg=24px, xl=40px, xxl=64px
*/
gap: Spacing;
spacing?: never;
}
| {
gap?: never;
spacing?: never;
}
) &
MarginProps &
PaddingProps &
Omit<WidthProps, 'width'> &
CustomDataAttributeProps;
Expand All @@ -67,6 +84,7 @@ export const Flex: FC<PropsWithChildren<Props>> = ({
justifyContent = 'flex-start',
wrap,
spacing,
gap,
height,
inline,
p,
Expand Down Expand Up @@ -98,6 +116,15 @@ export const Flex: FC<PropsWithChildren<Props>> = ({
};

const width = _width === 'full' ? '100%' : _width;
const _spacing = useMemo(() => {
if (gap != null) {
return gap;
} else if (spacing != null) {
return spacing;
} else {
return undefined;
}
}, [gap, spacing]);

return createElement(
{
Expand All @@ -107,7 +134,7 @@ export const Flex: FC<PropsWithChildren<Props>> = ({
'--align-items': alignItems,
'--justify-content': justifyContent,
'--flex-wrap': wrap ? 'wrap' : 'nowrap',
...gapVariables(spacing),
...gapVariables(_spacing),
...paddingVariables({
p,
px,
Expand Down
10 changes: 10 additions & 0 deletions src/stories/Flex.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ export const Spacing: Story = {
),
};

export const Gap: Story = {
render: () => (
<Flex gap="xl">
<div>xxl</div>
<div>xxl</div>
<div>xxl</div>
</Flex>
),
};

export const Vertical: Story = {
render: () => (
<div>
Expand Down
2 changes: 1 addition & 1 deletion src/stories/Stack.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const Width: Story = {

export const Gap: Story = {
render: () => (
<Stack spacing="md">
<Stack gap="md">
<p style={{ margin: 0 }}>Text</p>
<p style={{ margin: 0 }}>Text</p>
<p style={{ margin: 0 }}>Text</p>
Expand Down

0 comments on commit 8751851

Please sign in to comment.