Skip to content

Commit 9cfddb9

Browse files
committed
Only allow resizing on 'full', not 'hero' covers
1 parent 2c773b9 commit 9cfddb9

File tree

4 files changed

+31
-101
lines changed

4 files changed

+31
-101
lines changed

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ export async function PageCover(props: {
2222
cover: RevisionPageDocumentCover;
2323
context: GitBookSiteContext;
2424
}) {
25-
const { as, page, cover, context } = props;
26-
const height = getCoverHeight(cover);
25+
const { as: coverType, page, cover, context } = props;
26+
27+
const height = getCoverHeight(cover, coverType);
2728

2829
if (!height) {
2930
return null;
@@ -34,25 +35,18 @@ export async function PageCover(props: {
3435
cover.refDark ? resolveContentRef(cover.refDark, context) : null,
3536
]);
3637

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-
4338
const sizes = [
44-
// Cover takes the full width on mobile
39+
// Cover takes the full width on mobile/table
4540
{
4641
media: '(max-width: 768px)',
4742
width: 768,
4843
},
49-
// Tablet sizes
5044
{
5145
media: '(max-width: 1024px)',
5246
width: 1024,
5347
},
54-
// Maximum size based on cover type and page layout
55-
{ width: maxWidth },
48+
// Maximum size of the cover
49+
{ width: 1248 },
5650
];
5751

5852
const getImage = async (resolved: ResolvedContentRef | null, returnNull = false) => {
@@ -91,12 +85,12 @@ export async function PageCover(props: {
9185
return (
9286
<div
9387
id="page-cover"
94-
data-full={String(as === 'full')}
88+
data-full={String(coverType === 'full')}
9589
className={tcls(
9690
'overflow-hidden',
9791
// Negative margin to balance the container padding
9892
'-mx-4',
99-
as === 'full'
93+
coverType === 'full'
10094
? [
10195
'sm:-mx-6',
10296
'md:-mx-8',
@@ -123,6 +117,7 @@ export async function PageCover(props: {
123117
}}
124118
y={cover.yPos}
125119
height={height}
120+
coverType={coverType}
126121
/>
127122
</div>
128123
);

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

Lines changed: 16 additions & 11 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,15 @@ 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 };
2321

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;
22+
export function PageCoverImage({
23+
imgs,
24+
y,
25+
height,
26+
coverType,
27+
}: { imgs: Images; y: number; height: number; coverType: 'hero' | 'full' }) {
28+
const { containerRef, objectPositionY, isLoading } = useCoverPosition(imgs, y);
2929

3030
if (isLoading) {
3131
return (
@@ -43,9 +43,14 @@ export function PageCoverImage({ imgs, y, height }: { imgs: Images; y: number; h
4343
sizes={imgs.light.sizes}
4444
fetchPriority="high"
4545
alt="Page cover"
46-
className={tcls('w-full', 'object-cover', imgs.dark ? 'dark:hidden' : '')}
46+
className={tcls(
47+
'w-full',
48+
coverType === 'hero' ? 'object-contain' : 'object-cover',
49+
imgs.dark ? 'dark:hidden' : ''
50+
)}
4751
style={{
48-
aspectRatio: `${aspectRatio}`,
52+
aspectRatio:
53+
coverType === 'hero' ? `${height}/${PAGE_COVER_SIZE.height}` : 'unset',
4954
objectPosition: `50% ${objectPositionY}%`,
5055
}}
5156
/>
@@ -58,7 +63,7 @@ export function PageCoverImage({ imgs, y, height }: { imgs: Images; y: number; h
5863
alt="Page cover"
5964
className={tcls('w-full', 'object-cover', 'dark:inline', 'hidden')}
6065
style={{
61-
aspectRatio: `${aspectRatio}`,
66+
aspectRatio: `${PAGE_COVER_SIZE.width}/${PAGE_COVER_SIZE.height}`,
6267
objectPosition: `50% ${objectPositionY}%`,
6368
}}
6469
/>

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ function clampCoverHeight(height: number | null | undefined): number {
1414
}
1515

1616
export function getCoverHeight(
17-
cover: RevisionPageDocumentCover | null | undefined
17+
cover: RevisionPageDocumentCover | null | undefined,
18+
as: 'hero' | 'full'
1819
): number | undefined {
1920
// Cover (and thus height) is not defined
2021
if (!cover) {
2122
return undefined;
2223
}
2324

25+
if (as === 'hero') {
26+
return DEFAULT_COVER_HEIGHT;
27+
}
28+
2429
return clampCoverHeight((cover as RevisionPageDocumentCover).height ?? DEFAULT_COVER_HEIGHT);
2530
}

0 commit comments

Comments
 (0)