55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
'use client';
|
|
|
|
import { useId, useState } from 'react';
|
|
|
|
import { FlexBox } from '../../flex-box';
|
|
import { Typography } from '../../typography';
|
|
import { buttonLayout } from '../constants';
|
|
import type { FrozenButtonProps } from '../types';
|
|
import { frozenTipVariants } from '../variants';
|
|
|
|
/**
|
|
* A frozen button with its reason. Hover and keyboard focus show the reason through CSS; a tap
|
|
* toggles it, because touch has no hover. Only frozen buttons with a reason load this code.
|
|
*/
|
|
export function FrozenButton({
|
|
reason,
|
|
block,
|
|
children,
|
|
...props
|
|
}: FrozenButtonProps) {
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
const tipId = useId();
|
|
|
|
return (
|
|
<FlexBox
|
|
tag="span"
|
|
display={block ? 'flex' : 'inline-flex'}
|
|
className="group relative"
|
|
onMouseLeave={() => setIsOpen(false)}
|
|
>
|
|
<FlexBox
|
|
tag="button"
|
|
{...buttonLayout(block)}
|
|
type="button"
|
|
aria-disabled
|
|
aria-describedby={tipId}
|
|
onClick={() => setIsOpen((open) => !open)}
|
|
{...props}
|
|
>
|
|
{children}
|
|
</FlexBox>
|
|
<Typography
|
|
tag="span"
|
|
id={tipId}
|
|
role="tooltip"
|
|
variant="caption"
|
|
color="inverse"
|
|
className={frozenTipVariants({ open: isOpen })}
|
|
>
|
|
{reason}
|
|
</Typography>
|
|
</FlexBox>
|
|
);
|
|
}
|