2022-12-19 15:59:14 -08:00
|
|
|
import type { Ref } from 'react';
|
|
|
|
|
import React, { useRef, useCallback, useState, forwardRef } from 'react';
|
|
|
|
|
import type { ButtonProps as MantineButtonProps, TooltipProps } from '@mantine/core';
|
|
|
|
|
import { Button as MantineButton, createPolymorphicComponent } from '@mantine/core';
|
|
|
|
|
import { useTimeout } from '@mantine/hooks';
|
|
|
|
|
import styled from 'styled-components';
|
|
|
|
|
import { Spinner } from '/@/renderer/components/spinner';
|
|
|
|
|
import { Tooltip } from '/@/renderer/components/tooltip';
|
|
|
|
|
|
|
|
|
|
export interface ButtonProps extends MantineButtonProps {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
loading?: boolean;
|
|
|
|
|
onClick?: (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
|
|
|
|
|
onMouseDown?: (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
|
|
|
|
|
tooltip?: Omit<TooltipProps, 'children'>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface StyledButtonProps extends ButtonProps {
|
|
|
|
|
ref: Ref<HTMLButtonElement>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const StyledButton = styled(MantineButton)<StyledButtonProps>`
|
2023-03-09 18:08:15 -08:00
|
|
|
color: ${(props) => `var(--btn-${props.variant}-fg)`};
|
|
|
|
|
background: ${(props) => `var(--btn-${props.variant}-bg)`};
|
|
|
|
|
border: ${(props) => `var(--btn-${props.variant}-border)`};
|
|
|
|
|
border-radius: ${(props) => `var(--btn-${props.variant}-radius)`};
|
2022-12-19 15:59:14 -08:00
|
|
|
transition: background 0.2s ease-in-out, color 0.2s ease-in-out;
|
|
|
|
|
|
|
|
|
|
svg {
|
|
|
|
|
transition: fill 0.2s ease-in-out;
|
2023-03-09 18:08:15 -08:00
|
|
|
fill: ${(props) => `var(--btn-${props.variant}-fg)`};
|
2022-12-19 15:59:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
&:disabled {
|
2023-03-09 18:08:15 -08:00
|
|
|
color: ${(props) => `var(--btn-${props.variant}-fg)`};
|
|
|
|
|
background: ${(props) => `var(--btn-${props.variant}-bg)`};
|
2022-12-19 15:59:14 -08:00
|
|
|
|
|
|
|
|
opacity: 0.6;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 20:46:07 -08:00
|
|
|
&:not([data-disabled])&:hover {
|
2023-03-09 18:08:15 -08:00
|
|
|
color: ${(props) => `var(--btn-${props.variant}-fg-hover) !important`};
|
|
|
|
|
background: ${(props) => `var(--btn-${props.variant}-bg-hover)`};
|
2022-12-19 15:59:14 -08:00
|
|
|
|
|
|
|
|
svg {
|
2023-03-09 18:08:15 -08:00
|
|
|
fill: ${(props) => `var(--btn-${props.variant}-fg-hover)`};
|
2022-12-19 15:59:14 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 20:46:07 -08:00
|
|
|
&:not([data-disabled])&:focus-visible {
|
2023-03-09 18:08:15 -08:00
|
|
|
color: ${(props) => `var(--btn-${props.variant}-fg-hover)`};
|
|
|
|
|
background: ${(props) => `var(--btn-${props.variant}-bg-hover)`};
|
2022-12-19 15:59:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
&:active {
|
2022-12-22 01:59:02 -08:00
|
|
|
transform: none;
|
2022-12-19 15:59:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
& .mantine-Button-centerLoader {
|
|
|
|
|
display: none;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
& .mantine-Button-leftIcon {
|
|
|
|
|
display: flex;
|
2023-05-21 07:31:58 -07:00
|
|
|
height: 100%;
|
2022-12-19 15:59:14 -08:00
|
|
|
margin-right: 0.5rem;
|
2023-05-21 07:31:58 -07:00
|
|
|
transform: translateY(-0.1rem);
|
2022-12-19 15:59:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.mantine-Button-rightIcon {
|
|
|
|
|
display: flex;
|
|
|
|
|
margin-left: 0.5rem;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const ButtonChildWrapper = styled.span<{ $loading?: boolean }>`
|
|
|
|
|
color: ${(props) => props.$loading && 'transparent !important'};
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const SpinnerWrapper = styled.div`
|
|
|
|
|
position: absolute;
|
|
|
|
|
top: 50%;
|
|
|
|
|
left: 50%;
|
|
|
|
|
transform: translate3d(-50%, -50%, 0);
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
export const _Button = forwardRef<HTMLButtonElement, ButtonProps>(
|
|
|
|
|
({ children, tooltip, ...props }: ButtonProps, ref) => {
|
|
|
|
|
if (tooltip) {
|
|
|
|
|
return (
|
|
|
|
|
<Tooltip
|
|
|
|
|
withinPortal
|
|
|
|
|
{...tooltip}
|
|
|
|
|
>
|
|
|
|
|
<StyledButton
|
|
|
|
|
ref={ref}
|
|
|
|
|
loaderPosition="center"
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
<ButtonChildWrapper $loading={props.loading}>{children}</ButtonChildWrapper>
|
|
|
|
|
{props.loading && (
|
|
|
|
|
<SpinnerWrapper>
|
|
|
|
|
<Spinner />
|
|
|
|
|
</SpinnerWrapper>
|
|
|
|
|
)}
|
|
|
|
|
</StyledButton>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<StyledButton
|
|
|
|
|
ref={ref}
|
|
|
|
|
loaderPosition="center"
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
<ButtonChildWrapper $loading={props.loading}>{children}</ButtonChildWrapper>
|
|
|
|
|
{props.loading && (
|
|
|
|
|
<SpinnerWrapper>
|
|
|
|
|
<Spinner />
|
|
|
|
|
</SpinnerWrapper>
|
|
|
|
|
)}
|
|
|
|
|
</StyledButton>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const Button = createPolymorphicComponent<'button', ButtonProps>(_Button);
|
|
|
|
|
|
|
|
|
|
_Button.defaultProps = {
|
|
|
|
|
loading: undefined,
|
|
|
|
|
onClick: undefined,
|
|
|
|
|
tooltip: undefined,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface HoldButtonProps extends ButtonProps {
|
|
|
|
|
timeoutProps: {
|
|
|
|
|
callback: () => void;
|
|
|
|
|
duration: number;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const TimeoutButton = ({ timeoutProps, ...props }: HoldButtonProps) => {
|
2022-12-29 18:50:57 -08:00
|
|
|
const [, setTimeoutRemaining] = useState(timeoutProps.duration);
|
2022-12-19 15:59:14 -08:00
|
|
|
const [isRunning, setIsRunning] = useState(false);
|
|
|
|
|
const intervalRef = useRef(0);
|
|
|
|
|
|
|
|
|
|
const callback = () => {
|
|
|
|
|
timeoutProps.callback();
|
|
|
|
|
setTimeoutRemaining(timeoutProps.duration);
|
|
|
|
|
clearInterval(intervalRef.current);
|
|
|
|
|
setIsRunning(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { start, clear } = useTimeout(callback, timeoutProps.duration);
|
|
|
|
|
|
|
|
|
|
const startTimeout = useCallback(() => {
|
|
|
|
|
if (isRunning) {
|
|
|
|
|
clearInterval(intervalRef.current);
|
|
|
|
|
setIsRunning(false);
|
|
|
|
|
clear();
|
|
|
|
|
} else {
|
|
|
|
|
setIsRunning(true);
|
|
|
|
|
start();
|
|
|
|
|
|
|
|
|
|
const intervalId = window.setInterval(() => {
|
|
|
|
|
setTimeoutRemaining((prev) => prev - 100);
|
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
|
|
intervalRef.current = intervalId;
|
|
|
|
|
}
|
|
|
|
|
}, [clear, isRunning, start]);
|
|
|
|
|
|
|
|
|
|
return (
|
2022-12-29 18:50:57 -08:00
|
|
|
<Button
|
2022-12-19 15:59:14 -08:00
|
|
|
sx={{ color: 'var(--danger-color)' }}
|
|
|
|
|
onClick={startTimeout}
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
{isRunning ? 'Cancel' : props.children}
|
2022-12-29 18:50:57 -08:00
|
|
|
</Button>
|
2022-12-19 15:59:14 -08:00
|
|
|
);
|
|
|
|
|
};
|