mirror of
https://github.com/Athou/commafeed.git
synced 2026-10-17 09:50:37 +00:00
65 lines
2.2 KiB
TypeScript
65 lines
2.2 KiB
TypeScript
import type { MessageDescriptor } from "@lingui/core"
|
|
import { useLingui } from "@lingui/react"
|
|
import { ActionIcon, type ActionIconVariant, Box, Button, type ButtonVariant, Tooltip, useMantineTheme } from "@mantine/core"
|
|
import { forwardRef, type MouseEventHandler, type ReactNode } from "react"
|
|
import { Constants } from "@/app/constants"
|
|
import { useActionButton } from "@/hooks/useActionButton"
|
|
|
|
export type Mode = "auto" | "mobile" | "desktop"
|
|
|
|
interface ActionButtonProps {
|
|
icon: ReactNode
|
|
className?: string
|
|
label?: string | MessageDescriptor
|
|
onClick?: MouseEventHandler
|
|
variant?: ActionIconVariant & ButtonVariant
|
|
mode?: Mode
|
|
}
|
|
|
|
/**
|
|
* Switches between Button with label (desktop) and ActionIcon (mobile)
|
|
*/
|
|
export const ActionButton = forwardRef<HTMLDivElement, ActionButtonProps>((props: ActionButtonProps, ref) => {
|
|
const { mobile } = useActionButton()
|
|
const theme = useMantineTheme()
|
|
const { _ } = useLingui()
|
|
|
|
const label = typeof props.label === "string" ? props.label : props.label && _(props.label)
|
|
const variant = props.variant ?? "subtle"
|
|
|
|
const mode: Mode = props.mode ?? "auto"
|
|
const iconOnly = mode === "mobile" || (mode === "auto" && mobile)
|
|
|
|
return (
|
|
<Box ref={ref} className="cf-action-button">
|
|
{iconOnly && (
|
|
<Tooltip label={label} openDelay={Constants.tooltip.delay}>
|
|
<ActionIcon
|
|
color={theme.primaryColor}
|
|
variant={variant}
|
|
className={props.className}
|
|
onClick={props.onClick}
|
|
aria-label={label}
|
|
>
|
|
{props.icon}
|
|
</ActionIcon>
|
|
</Tooltip>
|
|
)}
|
|
{!iconOnly && (
|
|
<Button
|
|
variant={variant}
|
|
size="xs"
|
|
className={props.className}
|
|
leftSection={props.icon}
|
|
onClick={props.onClick}
|
|
aria-label={label}
|
|
>
|
|
{label}
|
|
</Button>
|
|
)}
|
|
</Box>
|
|
)
|
|
})
|
|
|
|
ActionButton.displayName = "HeaderButton"
|