Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

StyledDiffCodeView.tsx

268 lines9.1 KB

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