-
Notifications
You must be signed in to change notification settings - Fork 4k
Support custom cover heights #3779
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
Open
viktorrenkema
wants to merge
5
commits into
main
Choose a base branch
from
viktor/support-custom-cover-heights
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+187
−27
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c773b9
Support custom cover heights
viktorrenkema 9cd0359
Only allow resizing on 'full', not 'hero' covers
viktorrenkema 7af7d46
Revert 'only allow resizing for non-hero'
viktorrenkema 522ce0d
Preserve existing sizing when no `height` is set
viktorrenkema cc4de25
Merge branch 'main' into viktor/support-custom-cover-heights
viktorrenkema 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,26 @@ | ||
| 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)); | ||
| } | ||
|
|
||
| // When a user set a custom cover height, we return the clamped cover height. If no height is set, we want to preserve the existing logic for sizing of the cover image and return `undefined` for height. | ||
| export function getCoverHeight( | ||
| cover: RevisionPageDocumentCover | null | undefined | ||
| ): number | undefined { | ||
| // Cover (and thus height) is not defined | ||
| if (!cover || !cover.height) { | ||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we take the dark one here. Everywhere else it tries from light image first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get what you mean, but it was intentional though. Its to account for if the dark img exists, it means it was intentionally uploaded as the light is the default and dark an explicit opt-in. So when we get into the situation where the img didnt ship with a size payload and we manually need to check the dimensions, we just first check if the dark one exists here to avoid always going for the light one directly and possibly not accounting for a dark mode with differign dimensions. We still fallback to light anyway if there is no dark one.