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

StyledDiffCodeView.tsx

260 lines8.6 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​
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);
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;
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;
58​
59[data-indicators="bars"]
60 :is([data-column-number], [data-gutter-buffer="annotation"])[data-selected-line] {
61 position: relative;
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;
79​
80[data-file-info] {
81 background-color: var(--code-background) !important;
82 border-block-color: transparent !important;
83 color: var(--code-foreground) !important;
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 min-height: 32px !important;
97 padding-block: 6px !important;
98 padding-inline: 8px 12px !important;
100​
101[data-diffs-header]:hover {
102 /* A native scrollbar gutter cannot be painted by descendants. Use an inset edge cue instead
103 of a full-width band that would look accidentally clipped at the gutter. */
104 background-color: var(--code-background) !important;
105 box-shadow: inset 3px 0 color-mix(in srgb, var(--code-foreground) 24%, transparent);
107​
108/* Hunk separators are the way to the unchanged lines between hunks. They keep the library's
109 layout, with the expand button in its own cell, but sit one step off the code surface so they
110 read as quiet controls; hover brings them forward. */
111:is([data-separator="line-info"], [data-separator="line-info-basic"]) {
112 /* Exact and margin-free: the virtualizer places rows from hunkSeparatorHeight below. */
113 height: 24px !important;
114 margin-block: 0 !important;
115 --diffs-bg-separator: color-mix(in srgb, var(--code-background) 97%, var(--code-foreground));
117​
118:is([data-separator="line-info"], [data-separator="line-info-basic"]) [data-separator-content] {
119 color: color-mix(in srgb, var(--code-foreground) 50%, var(--code-background)) !important;
120 font-family: var(--font-sans) !important;
121 font-size: 11px !important;
122 text-decoration: none !important;
124​
125:is([data-separator="line-info"], [data-separator="line-info-basic"]) [data-expand-button] {
126 color: color-mix(in srgb, var(--code-foreground) 50%, var(--code-background)) !important;
128​
129:is([data-separator="line-info"], [data-separator="line-info-basic"])[data-expand-index]
130 :is([data-separator-content], [data-expand-button]):hover {
131 color: var(--code-foreground) !important;
132 background-color: color-mix(
133 in srgb,
134 var(--code-background) 92%,
135 var(--code-foreground)
136 ) !important;
138​
139[data-diffs-header] [data-header-content] {
140 align-items: center !important;
141 line-height: 1 !important;
143​
144/* Pierre clips file names with overflow: hidden for its start ellipsis. A 1em line box cuts off
145 descenders. Only the clipped names get room: 16px plus the header's 12px of padding stays
146 inside its 32px min-height, which diffHeaderHeight assumes. */
147[data-diffs-header] :is([data-title], [data-prev-name]) {
148 line-height: 16px !important;
150​
151[data-diffs-header] [data-metadata] {
152 align-items: center !important;
153 line-height: 1 !important;
154 font-variant-numeric: tabular-nums;
156​
157[data-diffs-header] [data-additions-count],
158[data-diffs-header] [data-deletions-count] {
159 font-family: var(--font-mono) !important;
160 font-size: 11px !important;
161 font-variant-numeric: tabular-nums;
162 line-height: 1 !important;
164​
165[data-diffs-header] [data-change-icon],
166[data-diffs-header] [data-rename-icon] {
167 display: block;
168 flex-shrink: 0;
170​
171[data-title] {
172 cursor: pointer;
173 transition: color 120ms ease;
174 font-family: var(--font-sans) !important;
176​
177[data-title]:hover {
178 color: color-mix(in srgb, var(--code-foreground) 84%, var(--primary)) !important;
180​
181/* Expanding a file mounts its body all at once; easing it in matches the 200ms the app's
182 collapsibles take. Appearance only — the viewer owns geometry, so height cannot animate.
183 Departing content cuts, the same one-way rule the pull request chrome fold follows. */
184[data-diff],
185[data-file] {
186 transition: opacity 200ms ease-out;
188​
189@starting-style {
190 [data-diff],
191 [data-file] {
192 opacity: 0;
193 }
195​
196@media (prefers-reduced-motion: reduce) {
197 [data-diff],
198 [data-file] {
199 transition: none;
200 }
202`;
203​
204export type StyledDiffCodeViewOptions<LAnnotation> = Omit<
205 NonNullable<CodeViewProps<LAnnotation, undefined>["options"]>,
206 "unsafeCSS" | "itemMetrics" | "layout"
207>;
208​
209type StyledDiffCodeViewProps<LAnnotation> = (
210 | Omit<ControlledCodeViewProps<LAnnotation, undefined>, "options">
211 | Omit<UncontrolledCodeViewProps<LAnnotation, undefined>, "options">
212) & {
213 readonly options?: StyledDiffCodeViewOptions<LAnnotation>;
214 readonly viewerRef?: Ref<CodeViewHandle<LAnnotation, undefined>>;
215 readonly unsafeCSSExtra?: string;
216};
217​
218export function StyledDiffCodeView<LAnnotation = undefined>({
219 options,
220 viewerRef,
221 className,
222 unsafeCSSExtra,
223 ...props
224}: StyledDiffCodeViewProps<LAnnotation>) {
225 return (
226 <DiffWorkerPoolProvider>
227 <CodeView<LAnnotation>
228 {...props}
229 {...(viewerRef ? { ref: viewerRef } : {})}
230 className={
231 className
232 ? `diff-render-surface [--code-background:var(--background)] outline-none ${className}`
233 : "diff-render-surface [--code-background:var(--background)] outline-none"
234 }
235 options={{
236 ...options,
237 unsafeCSS: unsafeCSSExtra
238 ? `${DIFF_VIEW_UNSAFE_CSS}\n${unsafeCSSExtra}`
239 : DIFF_VIEW_UNSAFE_CSS,
240 itemMetrics: {
241 diffHeaderHeight: 32,
242 hunkSeparatorHeight: 24,
243 // Pierre uses its general file spacing as a fallback in expanded-file layout paths.
244 // Keep it zero alongside the explicit paddingTop or expanding the first file can
245 // reintroduce the library's default 8px gap above its header.
246 spacing: 0,
247 paddingTop: 0,
248 // Unlike the gap above, the 8px under a file's last line is painted
249 // unconditionally by Pierre's stylesheet (`--diffs-gap-fallback`), so the metric has
250 // to count it: at zero every expanded file's virtual height ran 8px short of its
251 // rendered height, and the end of the list sat past the reachable scroll range —
252 // one clipped file row per expanded file above it.
253 paddingBottom: 8,
254 },
255 layout: { paddingTop: 0, paddingBottom: 0, gap: 0 },
256 }}
257 />
258 </DiffWorkerPoolProvider>
259 );