Spaces:
Running

MiniSearch / client /components /ClearFieldButton.tsx
system's picture
system HF Staff
Sync from felladrin/MiniSearch@c4a67e2
41eb01d verified
Raw History Blame Contribute Delete
2.03 kB
import { ActionIcon, Tooltip } from "@mantine/core";
import { IconX } from "@tabler/icons-react";
/**
* The button floats over the right end of the field it fills, so the field
* has to reserve this much room for it. Exported rather than repeated, so a
* size change here cannot leave a caller's text running underneath it.
*/
export const clearButtonWidth = 34;
/**
* Mantine's input border. The button is inset by it so its hover fill stays
* inside the field instead of painting over its border.
*/
const fieldBorderWidth = 1;
interface ClearFieldButtonProps {
/**
* Names the field in the tooltip and the accessible name, as the object of
* "Clear": "the follow-up question", "the search query". One string feeds
* both, so an icon-only control never says one thing on screen and another
* to a screen reader.
*/
label: string;
/** Empties the field. */
onClear: () => void;
/** Distance from the field's right edge to the positioned ancestor's, in pixels. */
rightOffset?: number;
disabled?: boolean;
}
export default function ClearFieldButton({
label,
onClear,
rightOffset = 0,
disabled = false,
}: ClearFieldButtonProps) {
return (
<Tooltip label={label} withArrow position="top">
<ActionIcon
size="lg"
variant="subtle"
color="gray"
onClick={onClear}
// Same as the dictation button: a press must not take the focus off
// the field, so clearing leaves the caret where the user left it.
onMouseDown={(event) => event.preventDefault()}
disabled={disabled}
aria-label={label}
style={{
position: "absolute",
right: rightOffset + fieldBorderWidth,
top: fieldBorderWidth,
bottom: fieldBorderWidth,
height: `calc(100% - ${2 * fieldBorderWidth}px)`,
width: clearButtonWidth,
...(disabled && { backgroundColor: "transparent" }),
}}
>
<IconX size={18} />
</ActionIcon>
</Tooltip>
);
}