feat(twenty-front/Button): add loading state on Button (#10536)

This commit is contained in:
Antoine Moreaux
2025-03-03 11:04:32 +01:00
committed by GitHub
parent 2e4c596644
commit 51c34b77d9
19 changed files with 374 additions and 99 deletions

View File

@ -0,0 +1,483 @@
import isPropValid from '@emotion/is-prop-valid';
import { css } from '@emotion/react';
import styled from '@emotion/styled';
import { IconComponent } from '@ui/display/icon/types/IconComponent';
import { useIsMobile } from '@ui/utilities';
import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import { ButtonText } from './internal/ButtonText';
import { ButtonIcon } from '@ui/input/button/components/Button/internal/ButtonIcon';
import { ButtonSoon } from '@ui/input/button/components/Button/internal/ButtonSoon';
import { ButtonHotkeys } from '@ui/input/button/components/Button/internal/ButtonHotKeys';
export type ButtonSize = 'medium' | 'small';
export type ButtonPosition = 'standalone' | 'left' | 'middle' | 'right';
export type ButtonVariant = 'primary' | 'secondary' | 'tertiary';
export type ButtonAccent = 'default' | 'blue' | 'danger';
export type ButtonProps = {
className?: string;
Icon?: IconComponent;
title?: string;
fullWidth?: boolean;
variant?: ButtonVariant;
inverted?: boolean;
size?: ButtonSize;
position?: ButtonPosition;
accent?: ButtonAccent;
soon?: boolean;
justify?: 'center' | 'flex-start' | 'flex-end';
disabled?: boolean;
focus?: boolean;
onClick?: (event: React.MouseEvent<HTMLButtonElement>) => void;
to?: string;
target?: string;
dataTestId?: string;
hotkeys?: string[];
ariaLabel?: string;
loading?: boolean;
} & React.ComponentProps<'button'>;
const StyledButton = styled('button', {
shouldForwardProp: (prop) =>
!['fullWidth'].includes(prop) && isPropValid(prop),
})<
Pick<
ButtonProps,
| 'fullWidth'
| 'variant'
| 'inverted'
| 'size'
| 'position'
| 'accent'
| 'focus'
| 'justify'
| 'to'
| 'target'
| 'loading'
> & { hasIcon: boolean }
>`
align-items: center;
${({ theme, variant, inverted, accent, disabled, focus }) => {
switch (variant) {
case 'primary':
switch (accent) {
case 'default':
return css`
background: ${!inverted
? theme.background.secondary
: theme.background.primary};
border-color: ${!inverted
? !disabled && focus
? theme.color.blue
: theme.background.transparent.light
: theme.background.transparent.light};
border-width: 1px 1px 1px 1px !important;
box-shadow: ${!disabled && focus
? `0 0 0 3px ${
!inverted
? theme.accent.tertiary
: theme.background.transparent.medium
}`
: 'none'};
color: ${!inverted
? !disabled
? theme.font.color.secondary
: theme.font.color.extraLight
: theme.font.color.secondary};
&:hover {
background: ${!inverted
? theme.background.tertiary
: theme.background.secondary};
}
&:active {
background: ${!inverted
? theme.background.quaternary
: theme.background.tertiary};
}
`;
case 'blue':
return css`
background: ${!inverted
? theme.color.blue
: theme.background.primary};
border-color: ${!inverted
? focus
? theme.color.blue
: theme.background.transparent.light
: theme.background.transparent.light};
border-width: 1px 1px 1px 1px !important;
box-shadow: ${!disabled && focus
? `0 0 0 3px ${
!inverted
? theme.accent.tertiary
: theme.background.transparent.medium
}`
: 'none'};
color: ${!inverted ? theme.grayScale.gray0 : theme.color.blue};
${disabled
? ''
: css`
&:hover {
background: ${!inverted
? theme.color.blue50
: theme.background.secondary};
}
&:active {
background: ${!inverted
? theme.color.blue60
: theme.background.tertiary};
}
`}
`;
case 'danger':
return css`
background: ${!inverted
? theme.color.red
: theme.background.primary};
border-color: ${!inverted
? focus
? theme.color.red
: theme.background.transparent.light
: theme.background.transparent.light};
border-width: 1px 1px !important;
box-shadow: ${!disabled && focus
? `0 0 0 3px ${
!inverted
? theme.color.red10
: theme.background.transparent.medium
}`
: 'none'};
color: ${!inverted ? theme.background.primary : theme.color.red};
${disabled
? ''
: css`
&:hover {
background: ${!inverted
? theme.color.red40
: theme.background.secondary};
}
&:active {
background: ${!inverted
? theme.color.red50
: theme.background.tertiary};
}
`}
`;
}
break;
case 'secondary':
case 'tertiary':
switch (accent) {
case 'default':
return css`
background: transparent;
border-color: ${!inverted
? variant === 'secondary'
? !disabled && focus
? theme.color.blue
: theme.background.transparent.medium
: focus
? theme.color.blue
: 'transparent'
: variant === 'secondary'
? focus || disabled
? theme.grayScale.gray0
: theme.background.transparent.primary
: focus
? theme.grayScale.gray0
: 'transparent'};
border-width: 1px 1px 1px 1px !important;
box-shadow: ${!disabled && focus
? `0 0 0 3px ${
!inverted
? theme.accent.tertiary
: theme.background.transparent.medium
}`
: 'none'};
color: ${!inverted
? !disabled
? theme.font.color.secondary
: theme.font.color.extraLight
: theme.font.color.inverted};
&:hover {
background: ${!inverted
? !disabled
? theme.background.transparent.light
: 'transparent'
: theme.background.transparent.light};
}
&:active {
background: ${!inverted
? !disabled
? theme.background.transparent.light
: 'transparent'
: theme.background.transparent.medium};
}
`;
case 'blue':
return css`
background: transparent;
border-color: ${!inverted
? variant === 'secondary'
? focus
? theme.color.blue
: theme.accent.primary
: focus
? theme.color.blue
: 'transparent'
: variant === 'secondary'
? focus || disabled
? theme.grayScale.gray0
: theme.background.transparent.primary
: focus
? theme.grayScale.gray0
: 'transparent'};
border-width: 1px 1px 1px 1px !important;
box-shadow: ${!disabled && focus
? `0 0 0 3px ${
!inverted
? theme.accent.tertiary
: theme.background.transparent.medium
}`
: 'none'};
color: ${!inverted
? !disabled
? theme.color.blue
: theme.accent.accent4060
: theme.font.color.inverted};
&:hover {
background: ${!inverted
? !disabled
? theme.accent.tertiary
: 'transparent'
: theme.background.transparent.light};
}
&:active {
background: ${!inverted
? !disabled
? theme.accent.secondary
: 'transparent'
: theme.background.transparent.medium};
}
`;
case 'danger':
return css`
background: transparent;
border-color: ${!inverted
? variant === 'secondary'
? focus
? theme.color.red
: theme.border.color.danger
: focus
? theme.color.red
: 'transparent'
: variant === 'secondary'
? focus || disabled
? theme.grayScale.gray0
: theme.background.transparent.primary
: focus
? theme.grayScale.gray0
: 'transparent'};
border-width: 1px 1px 1px 1px !important;
box-shadow: ${!disabled && focus
? `0 0 0 3px ${
!inverted
? theme.color.red10
: theme.background.transparent.medium
}`
: 'none'};
color: ${!inverted
? theme.font.color.danger
: theme.font.color.inverted};
&:hover {
background: ${!inverted
? !disabled
? theme.background.danger
: 'transparent'
: theme.background.transparent.light};
}
&:active {
background: ${!inverted
? !disabled
? theme.background.danger
: 'transparent'
: theme.background.transparent.medium};
}
`;
}
}
}}
text-decoration: none;
border-radius: ${({ position, theme }) => {
switch (position) {
case 'left':
return `${theme.border.radius.sm} 0px 0px ${theme.border.radius.sm}`;
case 'right':
return `0px ${theme.border.radius.sm} ${theme.border.radius.sm} 0px`;
case 'middle':
return '0px';
case 'standalone':
return theme.border.radius.sm;
}
}};
border-style: solid;
border-width: ${({ variant, position }) => {
switch (variant) {
case 'primary':
case 'secondary':
return position === 'middle' ? '1px 0px' : '1px';
case 'tertiary':
return '0';
}
}};
cursor: ${({ disabled }) => (disabled ? 'not-allowed' : 'pointer')};
display: flex;
flex-direction: row;
font-family: ${({ theme }) => theme.font.family};
font-weight: 500;
font-size: ${({ theme }) => theme.font.size.md};
gap: ${({ theme }) => theme.spacing(1)};
height: ${({ size }) => (size === 'small' ? '24px' : '32px')};
justify-content: ${({ justify }) => justify};
padding: ${({ theme, hasIcon }) => {
return `0 ${theme.spacing(2)} 0 ${
hasIcon ? theme.spacing(7) : theme.spacing(2)
}`;
}};
transition: background 0.1s ease;
white-space: nowrap;
width: ${({ fullWidth }) => (fullWidth ? '100%' : 'auto')};
&:focus {
outline: none;
}
`;
const StyledButtonWrapper = styled.div<
Pick<ButtonProps, 'loading' | 'variant' | 'accent' | 'inverted' | 'disabled'>
>`
${({ theme, variant, accent, inverted, disabled }) => css`
--tw-button-color: ${(() => {
switch (variant) {
case 'primary':
switch (accent) {
case 'default':
return !inverted
? !disabled
? theme.font.color.secondary
: theme.font.color.extraLight
: theme.font.color.secondary;
case 'blue':
return !inverted ? theme.grayScale.gray0 : theme.color.blue;
case 'danger':
return !inverted ? theme.background.primary : theme.color.red;
}
break;
case 'secondary':
case 'tertiary':
switch (accent) {
case 'default':
return !inverted
? !disabled
? theme.font.color.secondary
: theme.font.color.extraLight
: theme.font.color.inverted;
case 'blue':
return !inverted
? !disabled
? theme.color.blue
: theme.accent.accent4060
: theme.font.color.inverted;
case 'danger':
return !inverted
? theme.font.color.danger
: theme.font.color.inverted;
}
break;
}
return theme.font.color.secondary; // Valeur par défaut
})()};
`}
height: 100%;
max-width: ${({ loading, theme }) =>
loading ? `calc(100% - ${theme.spacing(8)})` : 'none'};
position: relative;
`;
export const Button = ({
className,
Icon,
title,
fullWidth = false,
variant = 'primary',
inverted = false,
size = 'medium',
accent = 'default',
position = 'standalone',
soon = false,
disabled = false,
justify = 'flex-start',
focus: propFocus = false,
onClick,
to,
target,
dataTestId,
hotkeys,
ariaLabel,
type,
loading,
}: ButtonProps) => {
const isMobile = useIsMobile();
const [isFocused, setIsFocused] = useState(propFocus);
return (
<StyledButtonWrapper
loading={loading}
variant={variant}
inverted={inverted}
disabled={soon || disabled}
>
{(loading || Icon) && <ButtonIcon Icon={Icon} loading={loading} />}
<StyledButton
fullWidth={fullWidth}
variant={variant}
inverted={inverted}
position={position}
disabled={soon || disabled}
hasIcon={!!Icon}
focus={isFocused}
justify={justify}
accent={accent}
className={className}
onClick={onClick}
to={to}
as={to ? Link : 'button'}
target={target}
data-testid={dataTestId}
aria-label={ariaLabel}
type={type}
loading={loading}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
>
<ButtonText hasIcon={!!Icon} title={title} loading={loading} />
{hotkeys && !isMobile && (
<ButtonHotkeys
hotkeys={hotkeys}
variant={variant}
accent={accent}
size={size}
/>
)}
{soon && <ButtonSoon />}
</StyledButton>
</StyledButtonWrapper>
);
};

View File

@ -0,0 +1 @@
export const baseTransitionTiming = 300;

View File

@ -0,0 +1,61 @@
import { getOsShortcutSeparator } from '@ui/utilities';
import { ButtonAccent, ButtonSize, ButtonVariant } from '@ui/input';
import styled from '@emotion/styled';
const StyledSeparator = styled.div<{
buttonSize: ButtonSize;
accent: ButtonAccent;
}>`
background: ${({ theme, accent }) => {
switch (accent) {
case 'blue':
return theme.border.color.blue;
case 'danger':
return theme.border.color.danger;
default:
return theme.font.color.light;
}
}};
height: ${({ theme, buttonSize }) =>
theme.spacing(buttonSize === 'small' ? 2 : 4)};
margin: 0;
width: 1px;
`;
const StyledShortcutLabel = styled.div<{
variant: ButtonVariant;
accent: ButtonAccent;
}>`
color: ${({ theme, variant, accent }) => {
switch (accent) {
case 'blue':
return theme.border.color.blue;
case 'danger':
return variant === 'primary'
? theme.border.color.danger
: theme.color.red40;
default:
return theme.font.color.light;
}
}};
font-weight: ${({ theme }) => theme.font.weight.medium};
`;
export const ButtonHotkeys = ({
size,
accent,
variant,
hotkeys,
}: {
size: ButtonSize;
accent: ButtonAccent;
variant: ButtonVariant;
hotkeys: string[];
}) => (
<>
<StyledSeparator buttonSize={size} accent={accent} />
<StyledShortcutLabel variant={variant} accent={accent}>
{hotkeys.join(getOsShortcutSeparator())}
</StyledShortcutLabel>
</>
);

View File

@ -0,0 +1,69 @@
import { Loader } from '@ui/feedback';
import { baseTransitionTiming } from '@ui/input/button/components/Button/constant';
import { IconComponent } from '@ui/display';
import { useTheme } from '@emotion/react';
import styled from '@emotion/styled';
import { isDefined } from 'twenty-shared';
const StyledIcon = styled.div<{
loading?: boolean;
}>`
align-items: center;
display: flex;
height: 100%;
color: var(--tw-button-color);
padding: 8px;
opacity: ${({ loading }) => (loading ? 0 : 1)};
transition: opacity ${baseTransitionTiming / 2}ms ease;
transition-delay: ${({ loading }) =>
loading ? '0ms' : `${baseTransitionTiming / 2}ms`};
`;
const StyledIconWrapper = styled.div<{ loading?: boolean }>`
align-items: center;
display: flex;
height: 100%;
position: absolute;
top: 50%;
transform: translateY(-50%);
width: ${({ loading }) => (loading ? 0 : '100%')};
`;
const StyledLoader = styled.div<{ loading?: boolean }>`
left: ${({ theme }) => theme.spacing(2)};
opacity: ${({ loading }) => (loading ? 1 : 0)};
position: absolute;
transition: opacity ${baseTransitionTiming / 2}ms ease;
transition-delay: ${({ loading }) =>
loading ? `${baseTransitionTiming / 2}ms` : '0ms'};
width: ${({ theme }) => theme.spacing(6)};
`;
export const ButtonIcon = ({
Icon,
loading,
}: {
Icon?: IconComponent;
loading?: boolean;
}) => {
const theme = useTheme();
return (
<StyledIconWrapper loading={loading}>
{isDefined(loading) && (
<StyledLoader loading={loading}>
<Loader />
</StyledLoader>
)}
{Icon && (
<StyledIcon loading={loading}>
<Icon size={theme.icon.size.sm} />
</StyledIcon>
)}
</StyledIconWrapper>
);
};

View File

@ -0,0 +1,8 @@
import styled from '@emotion/styled';
import { Pill } from '@ui/components';
const StyledSoonPill = styled(Pill)`
margin-left: auto;
`;
export const ButtonSoon = () => <StyledSoonPill label="Soon" />;

View File

@ -0,0 +1,59 @@
import styled from '@emotion/styled';
import { baseTransitionTiming } from '@ui/input/button/components/Button/constant';
const StyledEllipsis = styled.div<{ loading?: boolean }>`
right: 0;
clip-path: ${({ theme, loading }) =>
loading ? `inset(0 0 0 0)` : `inset(0 0 0 ${theme.spacing(6)})`};
overflow: hidden;
position: absolute;
transition: clip-path ${baseTransitionTiming}ms ease;
`;
const StyledTextWrapper = styled.div`
align-items: center;
display: flex;
height: 100%;
justify-content: center;
position: relative;
`;
const StyledText = styled.div<{ loading?: boolean; hasIcon: boolean }>`
clip-path: ${({ loading, theme, hasIcon }) =>
loading
? ` inset(0 ${!hasIcon ? theme.spacing(12) : theme.spacing(6)} 0 0)`
: ' inset(0 0 0 0)'};
overflow: hidden;
transform: ${({ theme, loading, hasIcon }) =>
loading
? `translateX(${!hasIcon ? theme.spacing(7) : theme.spacing(3)})`
: 'none'};
transition:
transform ${baseTransitionTiming}ms ease,
clip-path ${baseTransitionTiming}ms ease;
transition-delay: ${({ loading }) =>
loading ? '0ms' : `${baseTransitionTiming / 4}ms`};
white-space: nowrap;
`;
export const ButtonText = ({
hasIcon = false,
loading,
title,
}: {
loading?: boolean;
hasIcon: boolean;
title?: string;
}) => (
<StyledTextWrapper>
<StyledText loading={loading} hasIcon={hasIcon}>
{title}
</StyledText>
<StyledEllipsis loading={loading}>...</StyledEllipsis>
</StyledTextWrapper>
);