-
Notifications
You must be signed in to change notification settings - Fork 4k
Support cover image heights and positioning changes #3758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
viktorrenkema
merged 3 commits into
main
from
codex/add-support-for-coverheight-property-2
Oct 27, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import type { RevisionPageDocumentCover } from '@gitbook/api'; | ||
|
|
||
| export const DEFAULT_COVER_HEIGHT = 240; | ||
| export const MIN_COVER_HEIGHT = 10; | ||
| export const MAX_COVER_HEIGHT = 700; | ||
|
|
||
| // Normalize and clamp the cover height between the minimum and maximum heights | ||
| function clampCoverHeight(height: number | null | undefined): number { | ||
| if (typeof height !== 'number' || Number.isNaN(height)) { | ||
| return DEFAULT_COVER_HEIGHT; | ||
| } | ||
|
|
||
| return Math.min(MAX_COVER_HEIGHT, Math.max(MIN_COVER_HEIGHT, height)); | ||
| } | ||
|
|
||
| export function getCoverHeight( | ||
| cover: RevisionPageDocumentCover | null | undefined | ||
| ): number | undefined { | ||
| // Cover (and thus height) is not defined | ||
| if (!cover) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return clampCoverHeight((cover as RevisionPageDocumentCover).height ?? DEFAULT_COVER_HEIGHT); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from './PageBody'; | ||
| export * from './PageCover'; | ||
| export * from './useCoverPosition'; |
109 changes: 109 additions & 0 deletions
109
packages/gitbook/src/components/PageBody/useCoverPosition.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| 'use client'; | ||
| import { useLayoutEffect, useMemo, useRef, useState } from 'react'; | ||
| import { useResizeObserver } from 'usehooks-ts'; | ||
|
|
||
| interface ImageSize { | ||
| width: number; | ||
| height: number; | ||
| } | ||
|
|
||
| interface ImageAttributes { | ||
| src: string; | ||
| srcSet?: string; | ||
| sizes?: string; | ||
| width?: number; | ||
| height?: number; | ||
| size?: ImageSize; | ||
| } | ||
|
|
||
| interface Images { | ||
| light: ImageAttributes; | ||
| dark?: ImageAttributes; | ||
| } | ||
|
|
||
| /** | ||
| * Hook to calculate the object position Y percentage for a cover image | ||
| * based on the y offset, image dimensions, and container dimensions. | ||
| */ | ||
| export function useCoverPosition(imgs: Images, y: number) { | ||
| const containerRef = useRef<HTMLDivElement>(null); | ||
| const [loadedDimensions, setLoadedDimensions] = useState<ImageSize | null>(null); | ||
| const [isLoading, setIsLoading] = useState(!imgs.light.size && !imgs.dark?.size); | ||
|
|
||
| const container = useResizeObserver({ | ||
| // @ts-expect-error wrong types | ||
| ref: containerRef, | ||
| }); | ||
|
|
||
| // Load original image dimensions if not provided in `imgs` | ||
| useLayoutEffect(() => { | ||
| // Check if we have dimensions from either light or dark image | ||
| const hasDimensions = imgs.light.size || imgs.dark?.size; | ||
|
|
||
| if (hasDimensions) { | ||
| return; // Already have dimensions | ||
| } | ||
|
|
||
| setIsLoading(true); | ||
|
|
||
| // Load the original image (using src, not srcSet) to get true dimensions | ||
| // Use dark image if available, otherwise fall back to light | ||
| const imageToLoad = imgs.dark || imgs.light; | ||
| const img = new Image(); | ||
| img.onload = () => { | ||
| setLoadedDimensions({ | ||
| width: img.naturalWidth, | ||
| height: img.naturalHeight, | ||
| }); | ||
| setIsLoading(false); | ||
| }; | ||
| img.onerror = () => { | ||
| // If image fails to load, use a fallback | ||
| setIsLoading(false); | ||
| }; | ||
| img.src = imageToLoad.src; | ||
| }, [imgs.light, imgs.dark]); | ||
|
|
||
| // Use provided dimensions or fall back to loaded dimensions | ||
| // Check light first, then dark, then loaded dimensions | ||
| const imageDimensions = imgs.light.size ?? imgs.dark?.size ?? loadedDimensions; | ||
|
|
||
| // Calculate ratio and dimensions similar to useCoverPosition hook | ||
| const ratio = | ||
| imageDimensions && container.height && container.width | ||
| ? Math.max( | ||
| container.width / imageDimensions.width, | ||
| container.height / imageDimensions.height | ||
| ) | ||
| : 1; | ||
| const safeRatio = ratio || 1; | ||
|
|
||
| const scaledHeight = | ||
| imageDimensions && container.height ? imageDimensions.height * safeRatio : null; | ||
| const maxOffset = | ||
| scaledHeight && container.height | ||
| ? Math.max(0, (scaledHeight - container.height) / 2 / safeRatio) | ||
| : 0; | ||
|
|
||
| // Parse the position between the allowed min/max | ||
| const objectPositionY = useMemo(() => { | ||
| if (!container.height || !imageDimensions) { | ||
| return 50; | ||
| } | ||
|
|
||
| const scaled = imageDimensions.height * safeRatio; | ||
| if (scaled <= container.height || maxOffset === 0) { | ||
| return 50; | ||
| } | ||
|
|
||
| const clampedOffset = Math.max(-maxOffset, Math.min(maxOffset, y)); | ||
| const relative = (maxOffset - clampedOffset) / (2 * maxOffset); | ||
| return relative * 100; | ||
| }, [container.height, imageDimensions, maxOffset, safeRatio, y]); | ||
|
|
||
| return { | ||
| containerRef, | ||
| objectPositionY, | ||
| isLoading: !imageDimensions || isLoading, | ||
| }; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.