Skip to content

Commit 7d4a5dc

Browse files
feat: sticky file headers
Add support for sticky file headers by adding a `stickyFileHeaders` option to the `Diff2HtmlUI`. By default this feature is disabled. Also document this option in the README. The feature is implemented through an optional CSS class on top of the pre-existing `.d2h-file-header` class. The new class is added on all file headers if the option is set to `true` (or the `stickyFileHeaders` method is called). This class, `.d2h-sticky-header`, has the minimum amount of styling to get the desired effect. The `position` and `top` values make the headers stick to the top as long as the wrapper is in the view. The `z-index` value is needed to ensure the header is displayed over all other content in the wrapper. In particular, from my testing in Firefox (106.0.2), the line numbers would display over the header if the `z-index` value isn't set.
1 parent 59ff295 commit 7d4a5dc

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ draw(): void
156156
synchronisedScroll(): void
157157
fileListToggle(startVisible: boolean): void
158158
highlightCode(): void
159+
stickyFileHeader(): void
159160
```
160161

161162
### Diff2HtmlUI Configuration
@@ -165,6 +166,7 @@ highlightCode(): void
165166
- `fileListToggle`: allow the file summary list to be toggled: `true` or `false`, default is `true`
166167
- `fileListStartVisible`: choose if the file summary list starts visible: `true` or `false`, default is `false`
167168
- `fileContentToggle`: allow each file contents to be toggled: `true` or `false`, default is `true`
169+
- `stickyFileHeader`: make file headers sticky: `true` or `false`, default is `false`
168170
- [All the options](#diff2html-configuration) from Diff2Html are also valid configurations in Diff2HtmlUI
169171

170172
### Diff2HtmlUI Browser

src/ui/css/diff2html.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
background-color: #f7f7f7;
1818
font-family: 'Source Sans Pro', 'Helvetica Neue', Helvetica, Arial, sans-serif;
1919
}
20+
.d2h-file-header.d2h-sticky-header {
21+
position: sticky;
22+
top: 0;
23+
z-index: 1;
24+
}
2025

2126
.d2h-file-stats {
2227
display: -webkit-box;

src/ui/js/diff2html-ui-base.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface Diff2HtmlUIConfig extends Diff2HtmlConfig {
1616
*/
1717
smartSelection?: boolean;
1818
fileContentToggle?: boolean;
19+
stickyFileHeaders?: boolean;
1920
}
2021

2122
export const defaultDiff2HtmlUIConfig = {
@@ -31,6 +32,7 @@ export const defaultDiff2HtmlUIConfig = {
3132
*/
3233
smartSelection: true,
3334
fileContentToggle: true,
35+
stickyFileHeaders: false,
3436
};
3537

3638
export class Diff2HtmlUI {
@@ -54,6 +56,7 @@ export class Diff2HtmlUI {
5456
if (this.config.highlight) this.highlightCode();
5557
if (this.config.fileListToggle) this.fileListToggle(this.config.fileListStartVisible);
5658
if (this.config.fileContentToggle) this.fileContentToggle();
59+
if (this.config.stickyFileHeaders) this.stickyFileHeaders();
5760
}
5861

5962
synchronisedScroll(): void {
@@ -188,6 +191,12 @@ export class Diff2HtmlUI {
188191
});
189192
}
190193

194+
stickyFileHeaders(): void {
195+
this.targetElement.querySelectorAll('.d2h-file-header').forEach(header => {
196+
header.classList.add('d2h-sticky-header');
197+
});
198+
}
199+
191200
/**
192201
* @deprecated since version 3.1.0
193202
*/

0 commit comments

Comments
 (0)