Files
Athou_commafeed/commafeed-client/src/components/ActionButtton.tsx

35 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-05-08 14:54:16 +02:00
import { ActionIcon, Button, useMantineTheme } from "@mantine/core"
import { ActionIconProps } from "@mantine/core/lib/ActionIcon/ActionIcon"
import { ButtonProps } from "@mantine/core/lib/Button/Button"
import { useMediaQuery } from "@mantine/hooks"
2023-05-04 21:27:02 +02:00
import { forwardRef, MouseEventHandler, ReactNode } from "react"
interface ActionButtonProps {
className?: string
2023-05-04 21:27:02 +02:00
icon?: ReactNode
label?: ReactNode
2023-05-04 21:27:02 +02:00
onClick?: MouseEventHandler
2023-05-08 14:54:16 +02:00
variant?: ActionIconProps["variant"] & ButtonProps["variant"]
2023-05-04 21:27:02 +02:00
showLabelOnMobile?: boolean
}
/**
* Switches between Button with label (desktop) and ActionIcon (mobile)
*/
export const ActionButton = forwardRef<HTMLButtonElement, ActionButtonProps>((props: ActionButtonProps, ref) => {
const theme = useMantineTheme()
2023-05-04 21:27:02 +02:00
const variant = props.variant ?? "subtle"
2023-05-08 14:54:16 +02:00
const mobile = !useMediaQuery(`(min-width: ${theme.breakpoints.lg})`)
2023-05-04 21:27:02 +02:00
const iconOnly = !props.showLabelOnMobile && (mobile || !props.label)
return iconOnly ? (
<ActionIcon ref={ref} color={theme.primaryColor} variant={variant} className={props.className} onClick={props.onClick}>
{props.icon}
</ActionIcon>
) : (
2023-05-04 21:27:02 +02:00
<Button ref={ref} variant={variant} size="xs" className={props.className} leftIcon={props.icon} onClick={props.onClick}>
{props.label}
</Button>
)
})
ActionButton.displayName = "HeaderButton"