summaryrefslogtreecommitdiff
path: root/source/features/sticky-sidebar.tsx
blob: c084459354abdb9dc22d6a50f8bfcee821475c53 (plain) (blame)
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
import './sticky-sidebar.css';
import select from 'select-dom';
import debounce from 'debounce-fn';
import * as pageDetect from 'github-url-detection';

import features from '.';
import onReplacedElement from '../helpers/on-replaced-element';

const sideBarSelector = [
	'#partial-discussion-sidebar', // Conversations
	'.repository-content .flex-column > :last-child [data-pjax]' // `isRepoRoot`
].join();

function updateStickiness(): void {
	const sidebar = select(sideBarSelector)!;
	const margin = pageDetect.isConversation() ? 60 : 0; // 60 matches sticky header's height
	const sidebarHeight = sidebar.offsetHeight + margin;
	sidebar.classList.toggle('rgh-sticky-sidebar', sidebarHeight < window.innerHeight);
}

const onResize = debounce(updateStickiness, {wait: 100});

function deinit(): void {
	window.removeEventListener('resize', onResize);
}

void features.add(__filebasename, {
	include: [
		pageDetect.isRepoRoot,
		pageDetect.isConversation
	],
	additionalListeners: [
		() => window.addEventListener('resize', onResize),
		() => void onReplacedElement(sideBarSelector, updateStickiness)
	],
	init: updateStickiness,
	deinit
});