mango-v4-ui/components/shared/Button.tsx

139 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-06-06 22:00:59 -07:00
import mangoStore from '@store/mangoStore'
2022-12-05 02:59:10 -08:00
import { useTheme } from 'next-themes'
import { forwardRef, FunctionComponent, ReactNode, Ref } from 'react'
2022-05-03 21:20:14 -07:00
2022-07-20 21:50:56 -07:00
interface AllButtonProps {
2022-05-03 21:20:14 -07:00
onClick?: (e?: React.MouseEvent) => void
disabled?: boolean
className?: string
2022-07-14 22:20:20 -07:00
secondary?: boolean
2022-05-03 21:20:14 -07:00
children?: ReactNode
}
2022-07-20 21:50:56 -07:00
interface ButtonProps {
size?: 'large' | 'medium' | 'small'
type?: 'button' | 'submit'
2022-07-20 21:50:56 -07:00
}
type ButtonCombinedProps = AllButtonProps & ButtonProps
const Button: FunctionComponent<ButtonCombinedProps> = ({
2022-05-03 21:20:14 -07:00
children,
onClick,
disabled = false,
className,
2022-07-14 22:20:20 -07:00
secondary,
2022-07-20 21:50:56 -07:00
size = 'medium',
type = 'button',
2022-05-03 21:20:14 -07:00
...props
}) => {
2022-12-05 02:59:10 -08:00
const { theme } = useTheme()
2023-06-06 22:00:59 -07:00
const themeData = mangoStore((s) => s.themeData)
2022-05-03 21:20:14 -07:00
return (
<button
onClick={onClick}
disabled={disabled}
2023-01-06 02:55:51 -08:00
className={`rounded-md ${
2023-06-06 22:00:59 -07:00
themeData.buttonStyle === 'raised'
2023-10-05 16:49:46 -07:00
? 'raised-button group relative top-0 after:rounded-md'
2023-06-01 23:05:08 -07:00
: secondary
? 'border border-th-button focus-visible:border-th-fgd-4 md:hover:border-th-button-hover'
: 'bg-th-button focus-visible:border focus-visible:border-th-fgd-4 md:hover:bg-th-button-hover'
2022-07-20 21:50:56 -07:00
} ${
size === 'medium'
? 'h-10 px-4'
: size === 'large'
? 'h-12 px-6'
: 'h-8 px-3'
2023-04-19 18:12:45 -07:00
} font-display ${
2022-12-05 02:59:10 -08:00
theme === 'High Contrast' && !secondary
? 'text-th-bkg-1'
: 'text-th-fgd-1'
2023-04-05 20:24:29 -07:00
} disabled:cursor-not-allowed disabled:opacity-60 ${className}`}
type={type}
2022-07-05 20:37:49 -07:00
{...props}
>
2023-10-05 16:49:46 -07:00
<span
2023-10-05 19:21:35 -07:00
className={`flex items-center justify-center ${
2023-10-05 16:49:46 -07:00
themeData.buttonStyle === 'raised'
? 'group-hover:mt-1 group-active:mt-2'
: ''
}`}
>
{children}
</span>
2022-07-05 20:37:49 -07:00
</button>
)
}
2022-07-18 03:02:43 -07:00
interface IconButtonProps {
hideBg?: boolean
2022-08-14 04:56:19 -07:00
size?: 'small' | 'medium' | 'large'
ref?: Ref<HTMLButtonElement>
2022-07-18 03:02:43 -07:00
}
2022-07-20 21:50:56 -07:00
type IconButtonCombinedProps = AllButtonProps & IconButtonProps
2022-07-18 03:02:43 -07:00
export const IconButton = forwardRef<
HTMLButtonElement,
IconButtonCombinedProps
>((props, ref) => {
const { children, onClick, disabled = false, className, hideBg, size } = props
2022-07-05 20:37:49 -07:00
return (
<button
onClick={onClick}
disabled={disabled}
2022-11-22 15:43:25 -08:00
className={`flex flex-shrink-0 ${
2022-08-14 04:56:19 -07:00
size === 'large'
? 'h-12 w-12'
: size === 'small'
2022-10-29 04:38:40 -07:00
? 'h-8 w-8'
: size === 'medium'
? 'h-10 w-10'
: ''
2023-04-19 18:12:45 -07:00
} items-center justify-center rounded-full ${
2022-12-02 03:28:46 -08:00
hideBg
? 'md:hover:text-th-active'
: 'border border-th-button focus-visible:border-th-fgd-3 md:hover:border-th-button-hover'
2022-07-18 03:02:43 -07:00
} text-th-fgd-1 focus:outline-none disabled:cursor-not-allowed disabled:bg-th-bkg-4
disabled:text-th-fgd-4 md:disabled:hover:text-th-fgd-4 ${className} focus-visible:text-th-active`}
ref={ref}
2022-05-03 21:20:14 -07:00
>
{children}
</button>
)
})
IconButton.displayName = 'IconButton'
2022-05-03 21:20:14 -07:00
2022-08-01 19:05:38 -07:00
interface LinkButtonProps {
icon?: ReactNode
}
type LinkButtonCombinedProps = AllButtonProps & LinkButtonProps
export const LinkButton: FunctionComponent<LinkButtonCombinedProps> = ({
2022-07-14 16:36:31 -07:00
children,
onClick,
disabled = false,
className,
2022-07-14 22:20:20 -07:00
secondary,
2022-07-14 16:36:31 -07:00
...props
}) => {
return (
<button
onClick={onClick}
disabled={disabled}
2023-04-19 18:12:45 -07:00
className={`flex items-center border-0 font-bold ${
2022-11-30 19:32:32 -08:00
secondary ? 'text-th-active' : 'text-th-fgd-2'
2023-04-20 19:32:20 -07:00
} rounded-sm focus-visible:text-th-active focus-visible:underline disabled:cursor-not-allowed disabled:opacity-50 ${className} md:hover:text-th-fgd-3`}
2022-07-14 16:36:31 -07:00
{...props}
type="button"
2022-07-14 16:36:31 -07:00
>
2022-08-02 12:20:27 -07:00
{children}
2022-07-14 16:36:31 -07:00
</button>
)
}
2022-05-03 21:20:14 -07:00
export default Button