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