File size: 1,307 Bytes
8a8c5bb
 
 
 
 
 
06e51c6
 
 
 
8a8c5bb
 
 
 
06e51c6
 
 
 
 
 
 
8a8c5bb
 
 
 
06e51c6
 
8a8c5bb
 
 
 
 
 
 
 
 
06e51c6
8a8c5bb
06e51c6
8a8c5bb
 
06e51c6
 
 
 
 
8a8c5bb
 
 
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
import React from "react";

interface FormatToggleButtonProps {
  label: string;
  active?: boolean;
  disabled?: boolean;
  /** Keyboard shortcut shown in the tooltip, e.g. "Ctrl+B". */
  shortcut?: string;
  /** Render the label next to the icon instead of icon-only. */
  showLabel?: boolean;
  onMouseDown: (event: any) => void;
  children: React.ReactNode;
}

/**
 * A toolbar toggle that names itself.
 *
 * Icon-only buttons with nothing but a native `title` left users guessing what
 * they were about to click, so every button carries a styled tooltip with its
 * name and shortcut, plus `title` as the no-JS/no-hover fallback.
 */
export default function FormatToggleButton({
  label,
  active = false,
  disabled = false,
  shortcut,
  showLabel = false,
  onMouseDown,
  children,
}: FormatToggleButtonProps) {
  return (
    <button
      type="button"
      aria-label={label}
      aria-pressed={active}
      disabled={disabled}
      title={shortcut ? `${label} (${shortcut})` : label}
      onMouseDown={onMouseDown}
      className="dep-tool"
    >
      {children}
      {showLabel && <span className="dep-tool__text">{label}</span>}
      <span className="dep-tool__tip" aria-hidden="true">
        {label}
        {shortcut && <kbd>{shortcut}</kbd>}
      </span>
    </button>
  );
}