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