From 04f39f1472c4d8d20363f79e8ef7d39d4c100290 Mon Sep 17 00:00:00 2001 From: Taran Vohra Date: Fri, 30 Oct 2026 14:27:21 +0530 Subject: [PATCH] Lazy webframes and lighter markup on large pages (#4726) --- .changeset/fix-component-layer-order.md | 5 ++ .changeset/lazy-webframes.md | 5 ++ .changeset/lighter-document-markup.md | 5 ++ .../src/components/DocumentView/Blocks.css | 18 +++++++ .../src/components/DocumentView/Blocks.tsx | 5 +- .../DocumentView/HashLinkButton.css | 30 ++++++++++++ .../DocumentView/HashLinkButton.tsx | 47 ++----------------- .../DocumentView/Table/RecordRow.tsx | 26 +++++----- .../components/DocumentView/Table/styles.css | 28 +++++++++++ .../DocumentView/utils/textAlignment.css | 23 +++++++++ .../DocumentView/utils/textAlignment.ts | 8 ++-- .../react-contentkit/src/ElementWebframe.tsx | 4 ++ 12 files changed, 141 insertions(+), 63 deletions(-) create mode 100644 .changeset/fix-component-layer-order.md create mode 100644 .changeset/lazy-webframes.md create mode 100644 .changeset/lighter-document-markup.md create mode 100644 packages/gitbook/src/components/DocumentView/Blocks.css create mode 100644 packages/gitbook/src/components/DocumentView/HashLinkButton.css create mode 100644 packages/gitbook/src/components/DocumentView/Table/styles.css create mode 100644 packages/gitbook/src/components/DocumentView/utils/textAlignment.css diff --git a/.changeset/fix-component-layer-order.md b/.changeset/fix-component-layer-order.md new file mode 100644 index 000000000..8706ffa87 --- /dev/null +++ b/.changeset/fix-component-layer-order.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Declare the Tailwind layer order in each component stylesheet, so a route whose first stylesheet is one of them still puts `base` before `components` and preflight does not strip their padding and borders. diff --git a/.changeset/lazy-webframes.md b/.changeset/lazy-webframes.md new file mode 100644 index 000000000..3ef6b1390 --- /dev/null +++ b/.changeset/lazy-webframes.md @@ -0,0 +1,5 @@ +--- +"@gitbook/react-contentkit": patch +--- + +Load a webframe only once it is near the viewport. A page can hold hundreds of them inside collapsed expandables, and loading every one on mount held up the first paint. diff --git a/.changeset/lighter-document-markup.md b/.changeset/lighter-document-markup.md new file mode 100644 index 000000000..f8efcba4e --- /dev/null +++ b/.changeset/lighter-document-markup.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Pack the class lists that every block, heading anchor and table cell repeats into shared CSS classes, and carry a table cell's width once as a custom property instead of spelling it out as both width and min-width, so a large page carries far less markup. diff --git a/packages/gitbook/src/components/DocumentView/Blocks.css b/packages/gitbook/src/components/DocumentView/Blocks.css new file mode 100644 index 000000000..04a653f1f --- /dev/null +++ b/packages/gitbook/src/components/DocumentView/Blocks.css @@ -0,0 +1,18 @@ +@reference "../RootLayout/globals.css"; + +/* This file can be the first stylesheet a route loads, and layer order follows first + appearance. Without this, `components` would be created here and Tailwind's `base` + only later, letting preflight's `padding: 0` and `border: 0` win over the rules below. */ +@layer theme, base, components, utilities; + +/* Every block on every page carries these, so they live here rather than in each class attribute. */ +@layer components { + .gb-block { + @apply decoration-primary/6; + } + + /* Lists opt out: a long list is expected to break across printed pages. */ + .gb-block:not(.gb-block-list) { + @apply print:break-inside-avoid; + } +} diff --git a/packages/gitbook/src/components/DocumentView/Blocks.tsx b/packages/gitbook/src/components/DocumentView/Blocks.tsx index 623369e95..15c5df8d8 100644 --- a/packages/gitbook/src/components/DocumentView/Blocks.tsx +++ b/packages/gitbook/src/components/DocumentView/Blocks.tsx @@ -2,6 +2,7 @@ import type { DocumentBlock, JSONDocument } from '@gitbook/api'; import { CONTENT_STYLE, CONTENT_STYLE_REDUCED } from '../layout'; import { Block } from './Block'; +import './Blocks.css'; import type { DocumentContextProps } from './DocumentView'; import { isBlockOffscreen } from './utils'; import { type ClassValue, tcls } from '@/lib/tailwind'; @@ -93,10 +94,10 @@ export function UnwrappedBlocks(props: UnwrappedBl key={node.key || `${node.type}-${index}`} block={node} style={[ - 'decoration-primary/6', + 'gb-block', FULL_WIDTH_BLOCKS.has(node.type) ? CONTENT_STYLE : CONTENT_STYLE_REDUCED, - !LIST_BLOCKS.has(node.type) && 'print:break-inside-avoid', + LIST_BLOCKS.has(node.type) && 'gb-block-list', blockStyle, ]} isEstimatedOffscreen={isOffscreen} diff --git a/packages/gitbook/src/components/DocumentView/HashLinkButton.css b/packages/gitbook/src/components/DocumentView/HashLinkButton.css new file mode 100644 index 000000000..16a0722ce --- /dev/null +++ b/packages/gitbook/src/components/DocumentView/HashLinkButton.css @@ -0,0 +1,30 @@ +@reference "../RootLayout/globals.css"; + +/* This file can be the first stylesheet a route loads, and layer order follows first + appearance. Without this, `components` would be created here and Tailwind's `base` + only later, letting preflight's `padding: 0` and `border: 0` win over the rules below. */ +@layer theme, base, components, utilities; + +/* A page carries one of these per heading, so the shared utilities live here instead of in a class + attribute repeated hundreds of times — on a large page that repetition is most of the HTML. + They sit in `components` so the utilities passed as props still override them. */ +@layer components { + .hash { + @apply relative inline-grid grid-area-1-1 border-0 rounded site-background; + @apply pointer-events-none opacity-0 transition-opacity; + @apply pointer-fine:h-[1em] pointer-fine:pointer-events-auto; + @apply pointer-fine:group-hover/hash:opacity-100 pointer-fine:group-focus-within/hash:opacity-100; + @apply [.hash-revealed_&]:pointer-events-auto [.hash-revealed_&]:opacity-100; + } + + .hash-link { + @apply inline-flex items-center p-1 pointer-fine:h-full pointer-fine:p-0; + } + + .hash-icon { + @apply size-3 self-center text-transparent transition-colors; + @apply pointer-fine:group-hover/hash:text-tint-subtle contrast-more:pointer-fine:group-hover/hash:text-tint-strong; + @apply pointer-fine:group-focus-within/hash:text-tint-subtle contrast-more:pointer-fine:group-focus-within/hash:text-tint-strong; + @apply [.hash-revealed_&]:text-tint-subtle contrast-more:[.hash-revealed_&]:text-tint-strong; + } +} diff --git a/packages/gitbook/src/components/DocumentView/HashLinkButton.tsx b/packages/gitbook/src/components/DocumentView/HashLinkButton.tsx index 093570111..645f508cf 100644 --- a/packages/gitbook/src/components/DocumentView/HashLinkButton.tsx +++ b/packages/gitbook/src/components/DocumentView/HashLinkButton.tsx @@ -3,6 +3,7 @@ import { Icon } from '@gitbook/icons'; import { Link } from '../primitives'; import type { DocumentContext } from './DocumentView'; +import './HashLinkButton.css'; import { getBlockTextStyle } from './spacing'; import { type ClassValue, tcls } from '@/lib/tailwind'; @@ -35,53 +36,13 @@ export function HashLinkButton(props: { const textStyle = getBlockTextStyle(block); return ( - + - + ); diff --git a/packages/gitbook/src/components/DocumentView/Table/RecordRow.tsx b/packages/gitbook/src/components/DocumentView/Table/RecordRow.tsx index e535e1b9e..7398de4e7 100644 --- a/packages/gitbook/src/components/DocumentView/Table/RecordRow.tsx +++ b/packages/gitbook/src/components/DocumentView/Table/RecordRow.tsx @@ -4,6 +4,7 @@ import { getColumnWidth } from './layout'; import { RecordColumnValue } from './RecordColumnValue'; import type { TableGridViewProps, TableRecordKV } from './Table'; import { TableSearchRecord } from './TableSearch'; +import './styles.css'; import { getColumnVerticalAlignment } from './utils'; import { tcls } from '@/lib/tailwind'; @@ -23,12 +24,7 @@ export function RecordRow( role="row" recordId={record[0]} visibleClassName="flex" - className={tcls( - 'group/row', - 'border-tint-subtle', - 'transition-colors', - 'hover:bg-tint-hover' - )} + className={tcls('group/row', 'table-grid-row')} > {view.columns.map((column) => { const cellMerge = getTableCellMerge(cellMergeLayout, record[0], column); @@ -58,16 +54,16 @@ export function RecordRow( {...getCommentCellProps(context, record[0], column)} aria-colspan={cellMerge?.merge.colSpan} className={tcls( - 'relative flex flex-1 border-r px-3 py-2 align-middle text-sm last:border-r-0', - 'border-tint-subtle', - isStickyFirstColumnCell - ? 'sticky left-0 z-10 bg-tint-base group-hover/row:bg-tint-hover' - : undefined + 'table-grid-cell', + isStickyFirstColumnCell ? 'table-grid-cell-sticky' : undefined )} - style={{ - width: columnWidth, - minWidth: columnWidth || '100px', - }} + // The cell's width and min-width both come from this, so it is carried once + // rather than spelled out twice in every cell's style attribute. + style={ + { + '--table-column-width': columnWidth || '100px', + } as React.CSSProperties + } >