mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-25 11:52:10 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 49bbe04213 | |||
| 872321f44d | |||
| 4519ea183d | |||
| db9fbab15e |
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"gitbook": patch
|
||||
---
|
||||
|
||||
Skip redundant class and style writes on `<html>` and the site header during initial load; each one re-styled the whole document.
|
||||
@@ -8,6 +8,12 @@ import { useEffect } from 'react';
|
||||
export function TableOfContentsScript() {
|
||||
useEffect(() => {
|
||||
const root = document.documentElement;
|
||||
// Writing the <html> style attribute re-styles the whole document, so skip unchanged values.
|
||||
const setVar = (name: string, value: string) => {
|
||||
if (root.style.getPropertyValue(name) !== value) {
|
||||
root.style.setProperty(name, value);
|
||||
}
|
||||
};
|
||||
|
||||
// Calculate and set TOC dimensions
|
||||
const updateTocLayout = () => {
|
||||
@@ -42,8 +48,8 @@ export function TableOfContentsScript() {
|
||||
}
|
||||
|
||||
// Update height
|
||||
root.style.setProperty('--toc-height', `${Math.max(height, 0)}px`);
|
||||
root.style.setProperty('--toc-top-offset', `${Math.max(offset, 0)}px`);
|
||||
setVar('--toc-height', `${Math.max(height, 0)}px`);
|
||||
setVar('--toc-top-offset', `${Math.max(offset, 0)}px`);
|
||||
|
||||
// Subtract visible pageCover (if any)
|
||||
if (
|
||||
@@ -64,8 +70,8 @@ export function TableOfContentsScript() {
|
||||
}
|
||||
}
|
||||
|
||||
root.style.setProperty('--outline-height', `${Math.max(height, 0)}px`);
|
||||
root.style.setProperty('--outline-top-offset', `${Math.max(offset, 0)}px`);
|
||||
setVar('--outline-height', `${Math.max(height, 0)}px`);
|
||||
setVar('--outline-top-offset', `${Math.max(offset, 0)}px`);
|
||||
};
|
||||
|
||||
// Initial update
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
import { usePathname } from 'next/navigation';
|
||||
import { useEffect, useLayoutEffect } from 'react';
|
||||
import { useEffect, useLayoutEffect, useRef } from 'react';
|
||||
|
||||
import { useIsNavigating } from '../hooks';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
@@ -15,8 +15,14 @@ export const NavigationLoader = () => {
|
||||
}, []);
|
||||
|
||||
// On route changes, add a transient class for the first paint of the new page.
|
||||
// Skipped on the initial mount: toggling a class on <html> re-styles the whole document.
|
||||
const isInitialMount = useRef(true);
|
||||
useLayoutEffect(() => {
|
||||
void pathname;
|
||||
if (isInitialMount.current) {
|
||||
isInitialMount.current = false;
|
||||
return;
|
||||
}
|
||||
const root = document.documentElement;
|
||||
root.classList.add('route-change');
|
||||
let raf2 = 0;
|
||||
|
||||
Reference in New Issue
Block a user