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 type { Ref } from "react";
11
12import { DIFF_SURFACE_THEME_UNSAFE_CSS } from "~/lib/diffRendering";
13import { DiffWorkerPoolProvider } from "../DiffWorkerPoolProvider";
14
15const DIFF_VIEW_UNSAFE_CSS = `${DIFF_SURFACE_THEME_UNSAFE_CSS}
16:is(
17 [data-line],
18 [data-line-annotation],
19 [data-merge-conflict],
20 [data-merge-conflict-actions],
21 [data-no-newline]
22)[data-selected-line] {
23 --diffs-line-bg: light-dark(
24 color-mix(
25 in lab,
26 var(--code-background) 88%,
27 color-mix(in srgb, var(--code-background) 50%, var(--diffs-modified-base))
28 ),
29 color-mix(
30 in lab,
31 var(--code-background) 80%,
32 color-mix(in srgb, var(--code-background) 70%, var(--diffs-modified-base))
33 )
34 ) !important;
35}
36
37:is([data-gutter-buffer], [data-column-number])[data-selected-line] {
38 --diffs-line-bg: light-dark(
39 color-mix(
40 in lab,
41 var(--code-background) 91%,
42 color-mix(in srgb, var(--code-background) 35%, var(--diffs-modified-base))
43 ),
44 color-mix(
45 in lab,
46 var(--code-background) 85%,
47 color-mix(in srgb, var(--code-background) 60%, var(--diffs-modified-base))
48 )
49 ) !important;
50}
51
52[data-indicators="bars"]
53 :is([data-column-number], [data-gutter-buffer="annotation"])[data-selected-line] {
54 position: relative;
55}
56
57[data-indicators="bars"]
58 :is([data-column-number], [data-gutter-buffer="annotation"])[data-selected-line]::before {
59 position: absolute !important;
60 inset-block: 0 !important;
61 inset-inline-start: 0 !important;
62 display: block !important;
63 width: 4px !important;
64 min-width: 4px !important;
65 max-width: 4px !important;
66 height: auto !important;
67 padding: 0 !important;
68 content: "" !important;
69 background-color: var(--diffs-modified-base) !important;
70 background-image: none !important;
71}
72
73[data-file-info] {
74 background-color: var(--code-background) !important;
75 border-block-color: transparent !important;
76 color: var(--code-foreground) !important;
77}
78
79[data-diffs-header] {
80 position: sticky !important;
81 top: 0;
82 z-index: 4;
83 background-color: var(--code-background) !important;
84 border-bottom-color: transparent !important;
85 align-items: center !important;
86 font-family: var(--font-sans) !important;
87 font-size: 12px !important;
88 line-height: 1 !important;
89 min-height: 32px !important;
90 padding-block: 6px !important;
91 padding-inline: 8px 12px !important;
92}
93
94[data-diffs-header]:hover {
95 /* A native scrollbar gutter cannot be painted by descendants. Use an inset edge cue instead
96 of a full-width band that would look accidentally clipped at the gutter. */
97 background-color: var(--code-background) !important;
98 box-shadow: inset 3px 0 color-mix(in srgb, var(--code-foreground) 24%, transparent);
99}
100
101:is([data-separator="line-info"], [data-separator="line-info-basic"]) {
102 height: 24px !important;
103 margin-block: 0 !important;
104 background-color: var(--code-background) !important;
105}
106
107:is([data-separator="line-info"], [data-separator="line-info-basic"])
108 [data-separator-wrapper] {
109 padding-inline: 8px 12px !important;
110 background-color: transparent !important;
111}
112
113:is([data-separator="line-info"], [data-separator="line-info-basic"])
114 [data-separator-content] {
115 gap: 8px;
116 padding-inline: 0 !important;
117 background-color: transparent !important;
118 color: color-mix(in srgb, var(--code-foreground) 52%, var(--code-background)) !important;
119 font-family: var(--font-sans) !important;
120 font-size: 11px !important;
121 text-decoration: none !important;
122}
123
124:is([data-separator="line-info"], [data-separator="line-info-basic"])
125 [data-unmodified-lines] {
126 display: flex !important;
127 min-width: 0;
128 flex: 1 1 auto;
129 align-items: center;
130 gap: 8px;
131}
132
133:is([data-separator="line-info"], [data-separator="line-info-basic"])[data-expand-index]
134 [data-unmodified-lines] {
135 cursor: pointer;
136}
137
138:is([data-separator="line-info"], [data-separator="line-info-basic"])
139 [data-unmodified-lines]::before,
140:is([data-separator="line-info"], [data-separator="line-info-basic"])
141 [data-unmodified-lines]::after {
142 width: auto;
143 height: 1px;
144 flex: 1 1 auto;
145 content: "";
146 background-color: color-mix(in srgb, var(--code-background) 92%, var(--code-foreground));
147}
148
149:is([data-separator="line-info"], [data-separator="line-info-basic"])[data-expand-index]
150 [data-separator-wrapper] {
151 grid-template-columns: 0 minmax(0, 1fr) !important;
152}
153
154:is([data-separator="line-info"], [data-separator="line-info-basic"])[data-expand-index]
155 [data-separator-content] {
156 grid-column: 2 !important;
157}
158
159/* Visually hidden rather than display: none so the expand action stays keyboard-reachable. */
160:is([data-separator="line-info"], [data-separator="line-info-basic"])
161 [data-expand-button] {
162 position: absolute !important;
163 width: 1px !important;
164 height: 1px !important;
165 margin: -1px !important;
166 padding: 0 !important;
167 overflow: hidden !important;
168 clip-path: inset(50%) !important;
169 border: 0 !important;
170 white-space: nowrap !important;
171}
172
173:is([data-separator="line-info"], [data-separator="line-info-basic"]):has(
174 [data-expand-button]
175 )
176 [data-separator-content] {
177 cursor: pointer;
178}
179
180:is([data-separator="line-info"], [data-separator="line-info-basic"]):has(
181 [data-expand-button]
182 ):is(:hover, :focus-within)
183 [data-separator-content] {
184 color: color-mix(in srgb, var(--code-foreground) 76%, var(--code-background)) !important;
185}
186
187:is([data-separator="line-info"], [data-separator="line-info-basic"]):has(
188 [data-expand-button]
189 ):is(:hover, :focus-within)
190 [data-unmodified-lines]::before,
191:is([data-separator="line-info"], [data-separator="line-info-basic"]):has(
192 [data-expand-button]
193 ):is(:hover, :focus-within)
194 [data-unmodified-lines]::after {
195 background-color: color-mix(in srgb, var(--code-background) 84%, var(--code-foreground));
196}
197
198[data-diffs-header] [data-header-content] {
199 align-items: center !important;
200 line-height: 1 !important;
201}
202
203/* Pierre clips file names with overflow: hidden for its start ellipsis. A 1em line box cuts off
204 descenders. Only the clipped names get room: 16px plus the header's 12px of padding stays
205 inside its 32px min-height, which diffHeaderHeight assumes. */
206[data-diffs-header] :is([data-title], [data-prev-name]) {
207 line-height: 16px !important;
208}
209
210[data-diffs-header] [data-metadata] {
211 align-items: center !important;
212 line-height: 1 !important;
213 font-variant-numeric: tabular-nums;
214}
215
216[data-diffs-header] [data-additions-count],
217[data-diffs-header] [data-deletions-count] {
218 font-family: var(--font-mono) !important;
219 font-size: 11px !important;
220 font-variant-numeric: tabular-nums;
221 line-height: 1 !important;
222}
223
224[data-diffs-header] [data-change-icon],
225[data-diffs-header] [data-rename-icon] {
226 display: block;
227 flex-shrink: 0;
228}
229
230[data-title] {
231 cursor: pointer;
232 transition: color 120ms ease;
233 font-family: var(--font-sans) !important;
234}
235
236[data-title]:hover {
237 color: color-mix(in srgb, var(--code-foreground) 84%, var(--primary)) !important;
238}
239
240/* Expanding a file mounts its body all at once; easing it in matches the 200ms the app's
241 collapsibles take. Appearance only — the viewer owns geometry, so height cannot animate.
242 Departing content cuts, the same one-way rule the pull request chrome fold follows. */
243[data-diff],
244[data-file] {
245 transition: opacity 200ms ease-out;
246}
247
248@starting-style {
249 [data-diff],
250 [data-file] {
251 opacity: 0;
252 }
253}
254
255@media (prefers-reduced-motion: reduce) {
256 [data-diff],
257 [data-file] {
258 transition: none;
259 }
260}
261`;
262
263export type StyledDiffCodeViewOptions<LAnnotation> = Omit<
264 NonNullable<CodeViewProps<LAnnotation>["options"]>,
265 "unsafeCSS" | "itemMetrics" | "layout"
266>;
267
268type StyledDiffCodeViewProps<LAnnotation> = (
269 | Omit<ControlledCodeViewProps<LAnnotation>, "options">
270 | Omit<UncontrolledCodeViewProps<LAnnotation>, "options">
271) & {
272 readonly options?: StyledDiffCodeViewOptions<LAnnotation>;
273 readonly viewerRef?: Ref<CodeViewHandle<LAnnotation>>;
274 /**
275 * Appended to the shared stylesheet inside the viewer's shadow root, for a surface that has
276 * to restyle chrome the viewer owns — such as replacing its per-file line counts.
277 */
278 readonly unsafeCSSExtra?: string;
279};
280
281/** The shared web CodeView surface: app styling and virtualized geometry stay paired here. */
282export function StyledDiffCodeView<LAnnotation = undefined>({
283 options,
284 viewerRef,
285 className,
286 unsafeCSSExtra,
287 ...props
288}: StyledDiffCodeViewProps<LAnnotation>) {
289 return (
290 <DiffWorkerPoolProvider>
291 <CodeView<LAnnotation>
292 {...props}
293 {...(viewerRef ? { ref: viewerRef } : {})}
294 // The custom element itself is focusable for keyboard scrolling. Its native outline sits
295 // outside the panel clipping boundary; actual controls inside retain their own indicators.
296 className={
297 className
298 ? `diff-render-surface [--code-background:var(--background)] outline-none ${className}`
299 : "diff-render-surface [--code-background:var(--background)] outline-none"
300 }
301 options={{
302 ...options,
303 unsafeCSS: unsafeCSSExtra
304 ? `${DIFF_VIEW_UNSAFE_CSS}\n${unsafeCSSExtra}`
305 : DIFF_VIEW_UNSAFE_CSS,
306 itemMetrics: {
307 diffHeaderHeight: 32,
308 hunkSeparatorHeight: 24,
309 // Pierre uses its general file spacing as a fallback in expanded-file layout paths.
310 // Keep it zero alongside the explicit paddingTop or expanding the first file can
311 // reintroduce the library's default 8px gap above its header.
312 spacing: 0,
313 paddingTop: 0,
314 // Unlike the gap above, the 8px under a file's last line is painted
315 // unconditionally by Pierre's stylesheet (`--diffs-gap-fallback`), so the metric has
316 // to count it: at zero every expanded file's virtual height ran 8px short of its
317 // rendered height, and the end of the list sat past the reachable scroll range —
318 // one clipped file row per expanded file above it.
319 paddingBottom: 8,
320 },
321 layout: { paddingTop: 0, paddingBottom: 0, gap: 0 },
322 }}
323 />
324 </DiffWorkerPoolProvider>
325 );
326}