Spaces:
Running

File size: 2,027 Bytes
41eb01d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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>
  );
}