Skip to content

Commit 522ce0d

Browse files
committed
Preserve existing sizing when no height is set
1 parent 7af7d46 commit 522ce0d

File tree

4 files changed

+24
-100
lines changed

4 files changed

+24
-100
lines changed

packages/gitbook/src/components/PageBody/PageCover.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,23 @@ export async function PageCover(props: {
2525
const { as, page, cover, context } = props;
2626
const height = getCoverHeight(cover);
2727

28-
if (!height) {
29-
return null;
30-
}
31-
3228
const [resolved, resolvedDark] = await Promise.all([
3329
cover.ref ? resolveContentRef(cover.ref, context) : null,
3430
cover.refDark ? resolveContentRef(cover.refDark, context) : null,
3531
]);
3632

37-
// Calculate sizes based on cover type and page layout
38-
// Hero covers: max-w-3xl (768px) on regular pages, max-w-screen-2xl (1536px) on wide pages
39-
// Full covers: Can expand to full viewport width with negative margins (up to ~1920px+ on large screens)
40-
const isWidePage = page.layout.width === 'wide';
41-
const maxWidth = as === 'full' ? 1920 : isWidePage ? 1536 : 768;
42-
4333
const sizes = [
44-
// Cover takes the full width on mobile
34+
// Cover takes the full width on mobile/table
4535
{
4636
media: '(max-width: 768px)',
4737
width: 768,
4838
},
49-
// Tablet sizes
5039
{
5140
media: '(max-width: 1024px)',
5241
width: 1024,
5342
},
54-
// Maximum size based on cover type and page layout
55-
{ width: maxWidth },
43+
// Maximum size of the cover
44+
{ width: 1248 },
5645
];
5746

5847
const getImage = async (resolved: ResolvedContentRef | null, returnNull = false) => {

packages/gitbook/src/components/PageBody/PageCoverImage.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use client';
22
import { tcls } from '@/lib/tailwind';
33
import type { ImageSize } from '../utils';
4-
import { getRecommendedCoverDimensions } from './coverDimensions';
54
import { useCoverPosition } from './useCoverPosition';
65

76
interface ImageAttributes {
@@ -18,14 +17,18 @@ interface Images {
1817
dark?: ImageAttributes;
1918
}
2019

21-
export function PageCoverImage({ imgs, y, height }: { imgs: Images; y: number; height: number }) {
22-
const { containerRef, objectPositionY, isLoading } = useCoverPosition(imgs, y);
20+
const PAGE_COVER_SIZE: ImageSize = { width: 1990, height: 480 };
21+
22+
interface PageCoverImageProps {
23+
imgs: Images;
24+
y: number;
25+
// Only if the `height` was customized by the user (and thus defined), we use it to set the cover's height and skip the default behaviour of fixed aspect-ratio.
26+
height: number | undefined;
27+
}
2328

24-
// Calculate the recommended aspect ratio for this height
25-
// This maintains the 4:1 ratio, allowing images to scale proportionally
26-
// and adapt their height when container width doesn't match the ideal ratio
27-
const recommendedDimensions = getRecommendedCoverDimensions(height);
28-
const aspectRatio = recommendedDimensions.width / recommendedDimensions.height;
29+
export function PageCoverImage(props: PageCoverImageProps) {
30+
const { imgs, y, height } = props;
31+
const { containerRef, objectPositionY, isLoading } = useCoverPosition(imgs, y);
2932

3033
if (isLoading) {
3134
return (
@@ -45,8 +48,11 @@ export function PageCoverImage({ imgs, y, height }: { imgs: Images; y: number; h
4548
alt="Page cover"
4649
className={tcls('w-full', 'object-cover', imgs.dark ? 'dark:hidden' : '')}
4750
style={{
48-
aspectRatio: `${aspectRatio}`,
51+
aspectRatio: height
52+
? undefined
53+
: `${PAGE_COVER_SIZE.width}/${PAGE_COVER_SIZE.height}`,
4954
objectPosition: `50% ${objectPositionY}%`,
55+
height, // if no height is passed, no height will be set.
5056
}}
5157
/>
5258
{imgs.dark && (
@@ -58,8 +64,11 @@ export function PageCoverImage({ imgs, y, height }: { imgs: Images; y: number; h
5864
alt="Page cover"
5965
className={tcls('w-full', 'object-cover', 'dark:inline', 'hidden')}
6066
style={{
61-
aspectRatio: `${aspectRatio}`,
67+
aspectRatio: height
68+
? undefined
69+
: `${PAGE_COVER_SIZE.width}/${PAGE_COVER_SIZE.height}`,
6270
objectPosition: `50% ${objectPositionY}%`,
71+
height, // if no height is passed, no height will be set.
6372
}}
6473
/>
6574
)}

packages/gitbook/src/components/PageBody/coverDimensions.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.

packages/gitbook/src/components/PageBody/coverHeight.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ function clampCoverHeight(height: number | null | undefined): number {
1313
return Math.min(MAX_COVER_HEIGHT, Math.max(MIN_COVER_HEIGHT, height));
1414
}
1515

16+
// 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.
1617
export function getCoverHeight(
1718
cover: RevisionPageDocumentCover | null | undefined
1819
): number | undefined {
1920
// Cover (and thus height) is not defined
20-
if (!cover) {
21+
if (!cover || !cover.height) {
2122
return undefined;
2223
}
2324

0 commit comments

Comments
 (0)