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