Skip to content
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

Enabling loading does not change the size of the buttons #118

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/components/Button/Button.module.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.button {
--border-width: 1px;

position: relative;
box-sizing: border-box;
display: inline-grid;
grid-template-columns: 1fr auto 1fr;
Expand Down Expand Up @@ -34,8 +35,12 @@
outline: solid var(--color-accent) 2px;
}

.button.loading {
cursor: wait;
}

@media (hover: hover) {
.button:hover {
.button:hover:not(.loading) {
color: var(--text-hover);
cursor: pointer;
background-color: var(--bg-hover);
Expand All @@ -60,9 +65,16 @@
border-width: 0;
}

.loading {
pointer-events: none;
cursor: wait;
.loadingLabel {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
}

.children.loading {
opacity: 0;
}

/* Variant */
Expand Down Expand Up @@ -224,6 +236,10 @@
vertical-align: middle;
}

.icon.loading {
opacity: 0;
}

.icon > * {
width: var(--icon-size);
height: var(--icon-size);
Expand Down
9 changes: 5 additions & 4 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { clsx } from 'clsx';
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';
Expand All @@ -21,6 +20,7 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
type = 'button',
disabled = false,
loading = false,
loadingLabel = '通信中',
onClick,
m,
mx,
Expand All @@ -33,7 +33,7 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
},
ref,
) => {
const icon = loading ? <CircularProgress /> : _icon === 'default' ? <VariantIcon variant={variant} /> : _icon;
const icon = _icon === 'default' ? <VariantIcon variant={variant} /> : _icon;
const fixedIcon = _fixedIcon === 'default' ? <VariantIcon variant={variant} /> : _fixedIcon;
const suffixIcon = _suffixIcon === 'default' ? <VariantIcon variant={variant} /> : _suffixIcon;
const cls = clsx({
Expand Down Expand Up @@ -75,10 +75,11 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
onClick={handleClick}
{...props}
>
{loading && <span className={styles.loadingLabel}>{loadingLabel}</span>}
{fixedIcon && <span className={styles.fixedIcon}>{fixedIcon}</span>}
<span className={styles.label}>
{icon && <span className={styles.icon}>{icon}</span>}
{children}
{icon && <span className={clsx(styles.icon, loading && styles.loading)}>{icon}</span>}
<span className={clsx(styles.children, loading && styles.loading)}>{children}</span>
{suffixIcon && <span className={styles.suffixIcon}>{suffixIcon}</span>}
</span>
</button>
Expand Down
4 changes: 4 additions & 0 deletions src/components/Button/ButtonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export type OnlyButtonProps = {
* ローディング状態を示す
*/
loading?: boolean;
/**
* ローディング中に表示する文言
*/
loadingLabel?: string;
};

export type OnlyLinkButtonProps = {
Expand Down
38 changes: 0 additions & 38 deletions src/components/Button/CircularProgress.module.css

This file was deleted.

26 changes: 0 additions & 26 deletions src/components/Button/CircularProgress.tsx

This file was deleted.

58 changes: 45 additions & 13 deletions src/stories/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ export const WithIcon: Story = {
};

export const Block: Story = {
render: () => (
render: (args) => (
<div style={{ display: 'flex', flexDirection: 'column', gap: '32px' }}>
<div>
<Button {...defaultArgs} size="small" variant="secondary" />
<Button {...args} size="small" variant="secondary" />
</div>
<div>
<Button {...defaultArgs} size="medium" variant="secondary" />
<Button {...args} size="medium" variant="secondary" />
</div>
<div>
<Button {...defaultArgs} size="large" variant="secondary" />
<Button {...args} size="large" variant="secondary" />
</div>
</div>
),
Expand All @@ -132,15 +132,47 @@ export const Disabled: Story = {
};

export const Loading: Story = {
render: () => (
<div style={{ display: 'flex', flexDirection: 'row', alignItems: 'flex-start', gap: '32px' }}>
<Button {...defaultArgs} loading />
<Button {...defaultArgs} variant="secondary" loading />
<Button {...defaultArgs} variant="accent" loading />
<Button {...defaultArgs} variant="alert" loading />
<Button {...defaultArgs} variant="text" loading />
<Button {...defaultArgs} variant="textAlert" loading />
</div>
args: {
...defaultArgs,
children: 'OK',
loading: true,
},
render: (args) => (
<Stack spacing="md">
<div style={{ display: 'flex', flexDirection: 'row', alignItems: 'flex-start', gap: '32px' }}>
<Button {...args} />
<Button {...args} variant="secondary" />
<Button {...args} variant="accent" />
<Button {...args} variant="alert" />
<Button {...args} variant="text" />
<Button {...args} variant="textAlert" />
</div>

<div style={{ display: 'flex', flexDirection: 'row', alignItems: 'flex-start', gap: '32px' }}>
<Button {...args} size="medium" />
<Button {...args} variant="secondary" size="medium" />
<Button {...args} variant="accent" size="medium" />
<Button {...args} variant="alert" size="medium" />
<Button {...args} variant="text" size="medium" />
<Button {...args} variant="textAlert" size="medium" />
</div>

<div style={{ display: 'flex', flexDirection: 'row', alignItems: 'flex-start', gap: '32px' }}>
<Button {...args} size="small" />
<Button {...args} variant="secondary" size="small" />
<Button {...args} variant="accent" size="small" />
<Button {...args} variant="alert" size="small" />
<Button {...args} variant="text" size="small" />
<Button {...args} variant="textAlert" size="small" />
</div>

<div style={{ display: 'flex', flexDirection: 'row', alignItems: 'flex-start', gap: '32px' }}>
<Button {...args} icon={<Icon icon="UbieIcon" />} size="medium" loadingLabel="ラベル変更">
次のページへ
</Button>
<Button {...args} variant="secondary" size="medium" disabled />
</div>
</Stack>
),
};

Expand Down