Open History to see the last commit
1/* oxlint-disable eslint/no-restricted-imports -- This is the single styled adapter around Pierre's raw viewer. */
2import {
3 CodeView,
4 type CodeViewHandle,
5 type CodeViewProps,
6 type ControlledCodeViewProps,
7 type UncontrolledCodeViewProps,
8} from "@pierre/diffs/react";
9/* oxlint-enable eslint/no-restricted-imports */
10import { useGT } from "gt-react";
11import type { Ref } from "react";
12
13import { DIFF_SURFACE_THEME_UNSAFE_CSS } from "~/lib/diffRendering";
14import { DiffWorkerPoolProvider } from "../DiffWorkerPoolProvider";
15
16export const DIFF_VIEW_UNSAFE_CSS = `${DIFF_SURFACE_THEME_UNSAFE_CSS}
17/* A 1fr track's automatic minimum grows with the currently mounted lines. In split
18 wrap mode that shifts width between columns, rewraps rows and makes the virtualizer
19 repeatedly correct the scroll position. Keep both code columns at the same width. */
20[data-diff-type="split"][data-overflow="wrap"] {
21 --diffs-code-grid: var(--diffs-grid-number-column-width) minmax(0, 1fr);
22}
23
24/* Translucent find matches keep the syntax colours under them. */
25:host {
26 --diffs-bg-search-match-override: light-dark(#ff963288, #ff963266);
27 --diffs-bg-search-match-current-override: color-mix(
28 in lab,
29 var(--diffs-modified-base) 45%,
30 transparent
31 );
32 --diffs-fg-search-match-override: currentColor;
33}
34
35:is(
36 [data-line],
37 [data-line-annotation],
38 [data-merge-conflict],
39 [data-merge-conflict-actions],
40 [data-no-newline]
41)[data-selected-line] {
42 --diffs-line-bg: light-dark(
43 color-mix(
44 in lab,
45 var(--code-background) 88%,
46 color-mix(in srgb, var(--code-background) 50%, var(--diffs-modified-base))
47 ),
48 color-mix(
49 in lab,
50 var(--code-background) 80%,
51 color-mix(in srgb, var(--code-background) 70%, var(--diffs-modified-base))
52 )
53 ) !important;
54}
55
56:is([data-gutter-buffer], [data-column-number])[data-selected-line] {
57 --diffs-line-bg: light-dark(
58 color-mix(
59 in lab,
60 var(--code-background) 91%,
61 color-mix(in srgb, var(--code-background) 35%, var(--diffs-modified-base))
62 ),
63 color-mix(
64 in lab,
65 var(--code-background) 85%,
66 color-mix(in srgb, var(--code-background) 60%, var(--diffs-modified-base))
67 )
68 ) !important;
69}
70
71[data-indicators="bars"]
72 :is([data-column-number], [data-gutter-buffer="annotation"])[data-selected-line] {
73 position: relative;
74}
75
76[data-indicators="bars"]
77 :is([data-column-number], [data-gutter-buffer="annotation"])[data-selected-line]::before {
78 position: absolute !important;
79 inset-block: 0 !important;
80 inset-inline-start: 0 !important;
81 display: block !important;
82 width: 4px !important;
83 min-width: 4px !important;
84 max-width: 4px !important;
85 height: auto !important;
86 padding: 0 !important;
87 content: "" !important;
88 background-color: var(--diffs-modified-base) !important;
89 background-image: none !important;
90}
91
92[data-file-info] {
93 background-color: var(--code-background) !important;
94 border-block-color: transparent !important;
95 color: var(--code-foreground) !important;
96}
97
98[data-diffs-header] {
99 position: sticky !important;
100 top: 0;
101 z-index: 4;
102 background-color: var(--code-background) !important;
103 border-bottom-color: transparent !important;
104 align-items: center !important;
105 font-family: var(--font-sans) !important;
106 font-size: 12px !important;
107 line-height: 1 !important;
108 /* Collapsed items are never remeasured: the painted header must match diffHeaderHeight.
109 Center controls in that height instead of adding padding to their intrinsic height. */
110 height: 32px !important;
111 min-height: 32px !important;
112 padding-block: 0 !important;
113 padding-inline: 8px 12px !important;
114}
115
116[data-diffs-header]:hover {
117 /* A native scrollbar gutter cannot be painted by descendants. Use an inset edge cue instead
118 of a full-width band that would look accidentally clipped at the gutter. */
119 background-color: var(--code-background) !important;
120 box-shadow: inset 3px 0 color-mix(in srgb, var(--code-foreground) 24%, transparent);
121}
122
123/* Hunk separators are the way to the unchanged lines between hunks. They keep the library's
124 layout, with the expand button in its own cell, but sit one step off the code surface so they
125 read as quiet controls; hover brings them forward. */
126:is([data-separator="line-info"], [data-separator="line-info-basic"]) {
127 /* Exact and margin-free: the virtualizer places rows from hunkSeparatorHeight below. */
128 height: 24px !important;
129 margin-block: 0 !important;
130 --diffs-bg-separator: color-mix(in srgb, var(--code-background) 97%, var(--code-foreground));
131}
132
133:is([data-separator="line-info"], [data-separator="line-info-basic"]) [data-separator-content] {
134 color: color-mix(in srgb, var(--code-foreground) 50%, var(--code-background)) !important;
135 font-family: var(--font-sans) !important;
136 font-size: 11px !important;
137 text-decoration: none !important;
138}
139
140:is([data-separator="line-info"], [data-separator="line-info-basic"]) [data-expand-button] {
141 color: color-mix(in srgb, var(--code-foreground) 50%, var(--code-background)) !important;
142}
143
144:is([data-separator="line-info"], [data-separator="line-info-basic"])[data-expand-index]
145 :is([data-separator-content], [data-expand-button]):hover {
146 color: var(--code-foreground) !important;
147 background-color: color-mix(
148 in srgb,
149 var(--code-background) 92%,
150 var(--code-foreground)
151 ) !important;
152}
153
154[data-diffs-header] [data-header-content] {
155 align-items: center !important;
156 line-height: 1 !important;
157}
158
159/* Pierre clips file names with overflow: hidden for its start ellipsis. A 1em line box cuts off
160 descenders; give the clipped names room inside the fixed-height header. */
161[data-diffs-header] :is([data-title], [data-prev-name]) {
162 line-height: 16px !important;
163}
164
165[data-diffs-header] [data-metadata] {
166 align-items: center !important;
167 line-height: 1 !important;
168 font-variant-numeric: tabular-nums;
169}
170
171[data-diffs-header] [data-additions-count],
172[data-diffs-header] [data-deletions-count] {
173 font-family: var(--font-mono) !important;
174 font-size: 11px !important;
175 font-variant-numeric: tabular-nums;
176 line-height: 1 !important;
177}
178
179[data-diffs-header] [data-change-icon],
180[data-diffs-header] [data-rename-icon] {
181 display: block;
182 flex-shrink: 0;
183}
184
185[data-title] {
186 cursor: pointer;
187 transition: color 120ms ease;
188 font-family: var(--font-sans) !important;
189}
190
191[data-title]:hover {
192 color: color-mix(in srgb, var(--code-foreground) 84%, var(--primary)) !important;
193}
194
195/* Expanding a file mounts its body all at once; easing it in matches the 200ms the app's
196 collapsibles take. Appearance only: the viewer owns geometry, so height cannot animate.
197 Departing content cuts, the same one-way rule the pull request chrome fold follows. */
198[data-diff],
199[data-file] {
200 transition: opacity 200ms ease-out;
201}
202
203@starting-style {
204 [data-diff],
205 [data-file] {
206 opacity: 0;
207 }
208}
209
210@media (prefers-reduced-motion: reduce) {
211 [data-diff],
212 [data-file] {
213 transition: none;
214 }
215}
216`;
217
218export type StyledDiffCodeViewOptions<LAnnotation> = Omit<
219 NonNullable<CodeViewProps<LAnnotation, undefined>["options"]>,
220 "unsafeCSS" | "itemMetrics" | "layout"
221>;
222
223type StyledDiffCodeViewProps<LAnnotation> = (
224 | Omit<ControlledCodeViewProps<LAnnotation, undefined>, "options">
225 | Omit<UncontrolledCodeViewProps<LAnnotation, undefined>, "options">
226) & {
227 readonly options?: StyledDiffCodeViewOptions<LAnnotation>;
228 readonly viewerRef?: Ref<CodeViewHandle<LAnnotation, undefined>>;
229 readonly unsafeCSSExtra?: string;
230};
231
232export function StyledDiffCodeView<LAnnotation = undefined>({
233 options,
234 viewerRef,
235 className,
236 unsafeCSSExtra,
237 ...props
238}: StyledDiffCodeViewProps<LAnnotation>) {
239 const gt = useGT();
240 // The change bars are colour alone; this names each changed line for screen readers.
241 const lineKindCss = `[data-indicators="bars"] [data-line][data-line-type="change-deletion"]::before {
242 content: "" / ${JSON.stringify(gt("Removed", { $context: "diff line, read by screen readers" }))};
243}
244[data-indicators="bars"] [data-line][data-line-type="change-addition"]::before {
245 content: "" / ${JSON.stringify(gt("Added", { $context: "diff line, read by screen readers" }))};
246}`;
247 return (
248 <DiffWorkerPoolProvider>
249 <CodeView<LAnnotation>
250 {...props}
251 {...(viewerRef ? { ref: viewerRef } : {})}
252 className={
253 className
254 ? `diff-render-surface [--code-background:var(--background)] outline-none ${className}`
255 : "diff-render-surface [--code-background:var(--background)] outline-none"
256 }
257 options={{
258 ...options,
259 unsafeCSS: unsafeCSSExtra
260 ? `${DIFF_VIEW_UNSAFE_CSS}\n${lineKindCss}\n${unsafeCSSExtra}`
261 : `${DIFF_VIEW_UNSAFE_CSS}\n${lineKindCss}`,
262 itemMetrics: {
263 diffHeaderHeight: 32,
264 hunkSeparatorHeight: 24,
265 // Zero both spacing and paddingTop; Pierre's expanded-file fallback otherwise adds an 8px header
266 // gap.
267 spacing: 0,
268 paddingTop: 0,
269 // Include Pierre's 8px --diffs-gap-fallback in metrics or each expanded file loses a scrollable
270 // row.
271 paddingBottom: 8,
272 },
273 layout: { paddingTop: 0, paddingBottom: 0, gap: 0 },
274 }}
275 />
276 </DiffWorkerPoolProvider>
277 );
278}