All files / json-viewer/src Row.tsx

80.64% Statements 25/31
89.28% Branches 75/84
72.72% Functions 8/11
89.28% Lines 25/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219                      1x                                                           2x           1x   1x           1144x   148x     162x   1x     2x         4x         826x   1x       1x                               1146x 1146x   1146x                                                                   901x                     4x 4x                                                                 5x 5x                             1x 1x                     1x 1x                    
"use client";
 
/**
 * One rendered row (SPEC.md §3.3).
 *
 * Memoised, because a scroll re-renders the whole window and the row itself is
 * the only thing in this package that is rendered dozens of times per frame.
 */
 
import { memo } from "react";
import { cx } from "./cx";
import styles from "./styles/JsonViewer.module.scss";
import type { JsonKind, JsonPath, JsonRow } from "./types";
 
export interface RowProps {
  /** DOM id, so the tree can point `aria-activedescendant` at it. */
  id: string;
  row: JsonRow;
  active: boolean;
  matched: boolean;
  currentMatch: boolean;
  showDataTypes: boolean;
  quoteKeys: boolean;
  onToggle: (path: JsonPath) => void;
  onActivate: (index: number) => void;
  onCopyPath: (row: JsonRow) => void;
  onCopyValue: (row: JsonRow) => void;
  onShowFull: (row: JsonRow, anchor: HTMLElement) => void;
  onRowClick?: (row: JsonRow, event: React.MouseEvent) => void;
  renderValue?: (row: JsonRow) => React.ReactNode;
}
 
/**
 * Whether a type badge would tell the reader anything new.
 *
 * `showDataTypes` earns its keep on `"42"` versus `42`. It does not on a row
 * that already reads `undefined`, `function retry()` or `{ 3 keys }` — there it
 * just prints the word twice ("draft: undefined undefined"), which is noise
 * dressed up as information.
 */
function typeIsInformative(kind: JsonKind): boolean {
  switch (kind) {
    case "string":
    case "number":
    case "boolean":
    case "bigint":
    case "date":
      return true;
    default:
      return false;
  }
}
 
/** Which colour token a value gets. Kept out of the render path as a lookup. */
function valueClass(kind: JsonKind): string {
  switch (kind) {
    case "string":
      return styles.string!;
    case "number":
    case "bigint":
      return styles.number!;
    case "boolean":
      return styles.boolean!;
    case "null":
    case "undefined":
      return styles.null!;
    case "circular":
    case "unknown":
    case "symbol":
    case "function":
      return styles.special!;
    case "object":
    case "array":
    case "map":
    case "set":
      return styles.container!;
    default:
      return styles.value!;
  }
}
 
export const Row = memo(function Row({
  id,
  row,
  active,
  matched,
  currentMatch,
  showDataTypes,
  quoteKeys,
  onToggle,
  onActivate,
  onCopyPath,
  onCopyValue,
  onShowFull,
  onRowClick,
  renderValue,
}: RowProps) {
  const hasKey = row.key !== undefined && !row.closing;
  const label = quoteKeys && typeof row.key === "string" ? `"${row.label}"` : row.label;
 
  return (
    <div
      id={id}
      // A closing bracket is the same tree item as its opening row, so it is
      // presentational — otherwise every container would be announced twice
      // and cost two arrow presses to walk past.
      role={row.closing ? "none" : "treeitem"}
      // A bare `}` between tree items is noise in a screen reader's browse
      // mode; the item it closes has already been announced with its state.
      aria-hidden={row.closing ? "true" : undefined}
      aria-level={row.closing ? undefined : row.depth + 1}
      aria-posinset={row.closing ? undefined : row.posInSet}
      aria-setsize={row.closing ? undefined : row.setSize}
      aria-expanded={row.expandable && !row.closing ? row.expanded : undefined}
      aria-selected={row.closing ? undefined : active || undefined}
      data-row-index={row.index}
      data-testid={`json-row-${row.index}`}
      className={cx(
        styles.row,
        active && styles.rowActive,
        currentMatch ? styles.rowMatchCurrent : matched && styles.rowMatch,
      )}
      onMouseDown={() => {
        if (!row.closing) onActivate(row.index);
      }}
      onClick={(event) => {
        if (!row.closing) onRowClick?.(row, event);
      }}
      onDoubleClick={() => {
        if (row.expandable) onToggle(row.path);
      }}
    >
      <span className={styles.guides} aria-hidden="true">
        {Array.from({ length: row.depth }, (_, level) => (
          <span key={level} className={styles.guide} />
        ))}
      </span>
 
      {row.expandable && !row.closing ? (
        <button
          type="button"
          tabIndex={-1}
          aria-hidden="true"
          className={cx(styles.twisty, row.expanded && styles.twistyOpen)}
          onClick={(event) => {
            event.stopPropagation();
            onToggle(row.path);
          }}
        >
          <span className={styles.twistyGlyph}>{"▸"}</span>
        </button>
      ) : (
        <span className={styles.twistySpacer} aria-hidden="true" />
      )}
 
      {hasKey ? (
        <>
          <span className={styles.key}>{label}</span>
          <span className={styles.punct}>{": "}</span>
        </>
      ) : null}
 
      {renderValue && !row.closing ? (
        renderValue(row)
      ) : (
        <span className={cx(styles.value, valueClass(row.kind))}>{row.display}</span>
      )}
 
      {showDataTypes && !row.closing && typeIsInformative(row.kind) ? (
        <span className={styles.type}>{row.kind}</span>
      ) : null}
 
      {row.truncated ? (
        <button
          type="button"
          tabIndex={-1}
          className={styles.more}
          aria-label={`Show the full value of ${label || "this node"}`}
          onClick={(event) => {
            event.stopPropagation();
            onShowFull(row, event.currentTarget);
          }}
        >
          {"…"}
        </button>
      ) : null}
 
      {row.closing ? null : (
        <span className={styles.actions}>
          <button
            type="button"
            tabIndex={-1}
            className={styles.action}
            aria-label={`Copy the path to ${label || "the root"}`}
            onClick={(event) => {
              event.stopPropagation();
              onCopyPath(row);
            }}
          >
            path
          </button>
          <button
            type="button"
            tabIndex={-1}
            className={styles.action}
            aria-label={`Copy the value of ${label || "the root"}`}
            onClick={(event) => {
              event.stopPropagation();
              onCopyValue(row);
            }}
          >
            value
          </button>
        </span>
      )}
    </div>
  );
});