Skip to content

Commit

Permalink
Merge pull request #80 from 8845musign/add-spacing-props
Browse files Browse the repository at this point in the history
Add margin and padding props to layout components and button components
  • Loading branch information
takanorip authored May 13, 2024
2 parents 49b4409 + 3c41fef commit 80776d5
Show file tree
Hide file tree
Showing 18 changed files with 161 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/components/Box/Box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import clsx from 'clsx';
import styles from './Box.module.css';
import {} from '../../types/style';
import {
paddingVariables,
marginVariables,
Expand Down
1 change: 1 addition & 0 deletions src/components/Button/Button.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
transition: 0.3s cubic-bezier(0.22, 1, 0.36, 1);
cursor: pointer;
overflow-wrap: anywhere;
margin: var(--margin-top) var(--margin-right) var(--margin-bottom) var(--margin-left);
}

.button:focus-visible {
Expand Down
13 changes: 13 additions & 0 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { forwardRef } from 'react';
import styles from './Button.module.css';
import { CircularProgress } from './CircularProgress';
import { VariantIcon } from './VariantIcon';
import { marginVariables } from '../../utils/style';
import type { ButtonProps } from './ButtonTypes';

export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
Expand All @@ -21,6 +22,10 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
disabled = false,
loading = false,
onClick,
mt,
mr,
mb,
ml,
...props
},
ref,
Expand Down Expand Up @@ -50,6 +55,14 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
<button
type={type}
className={cls}
style={{
...marginVariables({
mt,
mr,
mb,
ml,
}),
}}
ref={ref}
disabled={disabled}
aria-disabled={loading}
Expand Down
3 changes: 2 additions & 1 deletion src/components/Button/ButtonTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MarginProps } from '../../types/style';
import type { ButtonHTMLAttributes, ReactNode, AnchorHTMLAttributes, ReactElement } from 'react';

export type BaseProps = {
Expand Down Expand Up @@ -37,7 +38,7 @@ export type BaseProps = {
* 後方配置のアイコン
*/
suffixIcon?: 'default' | ReactNode;
};
} & MarginProps;

export type OnlyButtonProps = {
/**
Expand Down
13 changes: 13 additions & 0 deletions src/components/Button/LinkButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import clsx from 'clsx';
import { cloneElement, forwardRef } from 'react';
import styles from './Button.module.css';
import { VariantIcon } from './VariantIcon';
import { marginVariables } from '../../utils/style';
import type { LinkButtonProps } from './ButtonTypes';
import type { ReactNode } from 'react';

Expand All @@ -18,6 +19,10 @@ export const LinkButton = forwardRef<HTMLAnchorElement, LinkButtonProps>(
icon: _icon,
fixedIcon: _fixedIcon,
suffixIcon: _suffixIcon,
mt,
mr,
mb,
ml,
...props
},
forwardedRef,
Expand All @@ -40,6 +45,14 @@ export const LinkButton = forwardRef<HTMLAnchorElement, LinkButtonProps>(
return createElement(
{
className: cls,
style: {
...marginVariables({
mt,
mr,
mb,
ml,
}),
},
...props,
ref: forwardedRef,
},
Expand Down
2 changes: 2 additions & 0 deletions src/components/Center/Center.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
max-width: var(--max-width);
margin-inline: auto;
padding: var(--padding-top) var(--padding-right) var(--padding-bottom) var(--padding-left);
margin-top: var(--margin-top);
margin-bottom: var(--margin-bottom);
}

.center.textCenter {
Expand Down
13 changes: 10 additions & 3 deletions src/components/Center/Center.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import clsx from 'clsx';
import styles from './Center.module.css';
import { paddingVariables } from '../../utils/style';
import { createSpacingVariableFromKey, paddingVariables } from '../../utils/style';
import { HTMLTagname } from '../../utils/types';
import type { PaddingProps } from '../../types/style';
import type { MarginYProps, PaddingProps } from '../../types/style';
import type { FC, PropsWithChildren, CSSProperties } from 'react';

type Props = {
Expand Down Expand Up @@ -32,7 +32,8 @@ type Props = {
* HTMLのID属性の値
*/
id?: string;
} & PaddingProps;
} & MarginYProps &
PaddingProps;

export const Center: FC<PropsWithChildren<Props>> = ({
as: CenterCopmonent = 'div',
Expand All @@ -41,6 +42,8 @@ export const Center: FC<PropsWithChildren<Props>> = ({
pr,
pb,
pl,
mt,
mb,
textCenter,
childrenCenter,
id,
Expand All @@ -59,6 +62,10 @@ export const Center: FC<PropsWithChildren<Props>> = ({
pb,
pl,
}),
...{
'--margin-top': mt ? createSpacingVariableFromKey(mt) : 0,
'--margin-bottom': mb ? createSpacingVariableFromKey(mb) : 0,
},
} as CSSProperties
}
>
Expand Down
2 changes: 2 additions & 0 deletions src/components/Flex/Flex.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
align-items: var(--align-items);
justify-content: var(--justify-content);
flex-flow: var(--flex-direction) var(--flex-wrap);
padding: var(--padding-top) var(--padding-right) var(--padding-bottom) var(--padding-left);
margin: var(--margin-top) var(--margin-right) var(--margin-bottom) var(--margin-left);
}

.flex.heightFull {
Expand Down
25 changes: 24 additions & 1 deletion src/components/Flex/Flex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import clsx from 'clsx';
import styles from './Flex.module.css';
import { Spacing, AlignItems, JustifyContent, FlexDirection } from '../../types/style';
import { paddingVariables, marginVariables } from '../../utils/style';
import { HTMLTagname } from '../../utils/types';
import type { PaddingProps, MarginProps } from '../../types/style';
import type { FC, PropsWithChildren } from 'react';

type Props = {
Expand Down Expand Up @@ -44,7 +46,8 @@ type Props = {
* デフォルトで<Flex>は横幅いっぱいを専有する。しかし例えば、フレックスコンテナの子要素として配置した場合、横幅が自身の子に合わせて小さくなる。これが不都合の場合に100%とする事が可能
*/
width?: 'full';
};
} & MarginProps &
PaddingProps;

export const Flex: FC<PropsWithChildren<Props>> = ({
as: FlexCopmonent = 'div',
Expand All @@ -56,6 +59,14 @@ export const Flex: FC<PropsWithChildren<Props>> = ({
spacing,
height,
width,
pt,
pr,
pb,
pl,
mt,
mr,
mb,
ml,
}) => {
// Directly specifying the markuplint will result in a markuplint error.
const gapStyle = spacing ? `var(--size-spacing-${spacing})` : '0';
Expand All @@ -70,6 +81,18 @@ export const Flex: FC<PropsWithChildren<Props>> = ({
'--align-items': alignItems,
'--justify-content': justifyContent,
'--flex-wrap': wrap ? 'wrap' : 'nowrap',
...paddingVariables({
pt,
pr,
pb,
pl,
}),
...marginVariables({
mt,
mr,
mb,
ml,
}),
} as React.CSSProperties
}
>
Expand Down
2 changes: 2 additions & 0 deletions src/components/Stack/Stack.module.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.stack {
display: flex;
flex-direction: column;
padding: var(--padding-top) var(--padding-right) var(--padding-bottom) var(--padding-left);
margin: var(--margin-top) var(--margin-right) var(--margin-bottom) var(--margin-left);
}
25 changes: 24 additions & 1 deletion src/components/Stack/Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { clsx } from 'clsx';
import { FC, ReactNode } from 'react';
import styles from './Stack.module.css';
import { Spacing, AlignItems, JustifyContent } from '../../types/style';
import { paddingVariables, marginVariables } from '../../utils/style';
import { HTMLTagname } from '../../utils/types';
import type { PaddingProps, MarginProps } from '../../types/style';

type Props = {
/**
Expand Down Expand Up @@ -35,7 +37,8 @@ type Props = {
* 子要素
*/
children: ReactNode;
};
} & MarginProps &
PaddingProps;

/**
* Stackコンポーネント
Expand All @@ -48,13 +51,33 @@ export const Stack: FC<Props> = ({
spacing,
alignItems = 'flex-start',
justifyContent = 'flex-start',
pt,
pr,
pb,
pl,
mt,
mr,
mb,
ml,
}) => {
return (
<StackComponent
style={{
alignItems: `${alignItems}`,
justifyContent: `${justifyContent}`,
gap: `var(--size-spacing-${spacing})`,
...paddingVariables({
pt,
pr,
pb,
pl,
}),
...marginVariables({
mt,
mr,
mb,
ml,
}),
}}
className={clsx(className, styles.stack)}
>
Expand Down
8 changes: 8 additions & 0 deletions src/stories/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,11 @@ export const Loading: Story = {
</div>
),
};

export const Margin: Story = {
render: () => (
<div style={{ backgroundColor: 'var(--color-background-gray)', width: 'fit-content', overflow: 'hidden' }}>
<Button {...defaultArgs} mt="lg" mr="lg" mb="lg" ml="lg" />
</div>
),
};
17 changes: 17 additions & 0 deletions src/stories/Center.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,20 @@ export const AsSection: Story = {
</Center>
),
};

export const MarginAndPadding: Story = {
render: () => (
<div style={{ backgroundColor: 'var(--color-background-primary)', overflow: 'hidden' }}>
<div>prev element</div>

<Center mt="lg" mb="lg" pt="xxl" pr="xxl" pb="xxl" pl="xxl" maxWidth="400px">
<div style={{ width: '100%', backgroundColor: 'var(--color-background-accent-darken)', overflow: 'hidden' }}>
<h2>Heading</h2>
<p>body</p>
</div>
</Center>

<div>next element</div>
</div>
),
};
11 changes: 11 additions & 0 deletions src/stories/Flex.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ export const Wrap: Story = {
export const AsSection: Story = {
render: () => (
<Flex as="section" spacing="md" alignItems="center">
<h1>Heading</h1>
<p>text</p>
<p>text</p>
<p>text</p>
</Flex>
),
};

export const MarginAndPadding: Story = {
render: () => (
<Flex spacing="md" alignItems="center" mt="lg" mr="lg" mb="lg" ml="lg" pt="xxl" pr="xxl" pb="xxl" pl="xxl">
<h1>Heading</h1>
<p>Section</p>
<p>Section</p>
Expand Down
8 changes: 8 additions & 0 deletions src/stories/LinkButton.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,11 @@ export const Block: Story = {
</div>
),
};

export const Margin: Story = {
render: () => (
<div style={{ backgroundColor: 'var(--color-background-gray)', width: 'fit-content', overflow: 'hidden' }}>
<LinkButton {...defaultArgs} mt="lg" mr="lg" mb="lg" ml="lg" />
</div>
),
};
12 changes: 12 additions & 0 deletions src/stories/Stack.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,15 @@ export const BlockLevelElementsToFullWidth: Story = {
),
args: defaultArgs,
};

export const MarginAndPadding: Story = {
render: (args) => (
<Stack {...args} mt="lg" mr="lg" mb="lg" ml="lg" pt="xxl" pr="xxl" pb="xxl" pl="xxl">
<p style={{ margin: 0 }}>Text</p>
<p style={{ margin: 0 }}>Text</p>
<p style={{ margin: 0 }}>Text</p>
<p style={{ margin: 0 }}>Text</p>
</Stack>
),
args: defaultArgs,
};
11 changes: 11 additions & 0 deletions src/types/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ export type MarginProps = {
ml?: Spacing;
};

export type MarginYProps = {
/**
* margin-topの値。Spacing Tokenのキーを指定
*/
mt?: Spacing;
/**
* margin-bottomの値。Spacing Tokenのキーを指定
*/
mb?: Spacing;
};

export type FlexDirection = 'row' | 'column';

export type AlignItems = 'normal' | 'flex-start' | 'center' | 'flex-end' | 'stretch' | 'baseline';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const opacityToClassName = (opacity: Opacity) => {
}
};

const createSpacingVariableFromKey = (key: Spacing) => {
export const createSpacingVariableFromKey = (key: Spacing) => {
return `var(--${DesignTokens.size[`spacing-${key}`].path.join('-')})`;
};

Expand Down

0 comments on commit 80776d5

Please sign in to comment.