File size: 2,999 Bytes
05251fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React from 'react';

export type FilePathTone = 'primary' | 'secondary' | 'metadata';
export type FilePathSize = 'sm' | 'md' | 'lg';

interface FilePathProps {
  /** Full repository-relative path, e.g. `fastapi/routing.py`. */
  path: string;
  /** primary = the subject, secondary = a reference, metadata = telemetry. */
  tone?: FilePathTone;
  size?: FilePathSize;
  /** Renders a semantic button and fires on click/Enter/Space. */
  onActivate?: () => void;
  /** Currently selected — exposed via `aria-current`, not colour alone. */
  active?: boolean;
  /** A small directional glyph, for imports and relationship targets. */
  marker?: 'import' | 'target';
  /** Appended after the path, e.g. a line number or count. */
  trailing?: React.ReactNode;
  className?: string;
}

const TONE_CLASS: Record<FilePathTone, string> = {
  primary: 'fp--primary',
  secondary: 'fp--secondary',
  metadata: 'fp--metadata',
};

const SIZE_CLASS: Record<FilePathSize, string> = {
  sm: 'fp--sm',
  md: 'fp--md',
  lg: 'fp--lg',
};

/**
 * The single way ARIA renders a file path.
 *
 * One component so the same file reads as the same entity everywhere — the
 * explorer, centrality rail, relationship timeline, inspector and entry points.
 * The directory prefix recedes and the filename carries the emphasis, so a path
 * scans as "which file" first and "where" second.
 *
 * Long paths wrap rather than truncate: the filename must never disappear, and
 * the page must never scroll sideways. `overflow-wrap: anywhere` satisfies both,
 * with weight and colour keeping the filename the visual anchor even when the
 * directory prefix wraps above it.
 */
export const FilePath: React.FC<FilePathProps> = ({
  path,
  tone = 'primary',
  size = 'md',
  onActivate,
  active = false,
  marker,
  trailing,
  className = '',
}) => {
  const cut = path.lastIndexOf('/');
  const dir = cut >= 0 ? path.slice(0, cut + 1) : '';
  const name = cut >= 0 ? path.slice(cut + 1) : path;

  const classes = [
    'fp',
    TONE_CLASS[tone],
    SIZE_CLASS[size],
    onActivate ? 'fp--interactive' : '',
    active ? 'fp--active' : '',
    className,
  ]
    .filter(Boolean)
    .join(' ');

  const content = (
    <>
      {marker && (
        <span className="fp-marker" aria-hidden="true">
          →
        </span>
      )}
      {dir && <span className="fp-dir">{dir}</span>}
      <span className="fp-name">{name}</span>
      {trailing && <span className="fp-trailing">{trailing}</span>}
    </>
  );

  if (onActivate) {
    return (
      <button
        type="button"
        onClick={onActivate}
        className={classes}
        // The full path is the accessible name; the split rendering is visual.
        aria-label={path}
        aria-current={active ? 'true' : undefined}
        title={path}
      >
        {content}
      </button>
    );
  }

  return (
    <span className={classes} title={path} aria-label={path}>
      {content}
    </span>
  );
};

export default FilePath;