Skip to content

Commit ffb576f

Browse files
Revert "Support cover image heights and positioning changes (#3758)"
This reverts commit f682df1.
1 parent 1d1f0a7 commit ffb576f

File tree

7 files changed

+31
-176
lines changed

7 files changed

+31
-176
lines changed

packages/gitbook/e2e/internal.spec.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import {
3333
headerLinks,
3434
runTestCases,
3535
waitForCookiesDialog,
36-
waitForCoverImages,
3736
waitForNotFound,
3837
} from './util';
3938

@@ -907,10 +906,7 @@ const testCases: TestsCase[] = [
907906
{
908907
name: 'With cover',
909908
url: 'page-options/page-with-cover',
910-
run: async (page) => {
911-
await waitForCookiesDialog(page);
912-
await waitForCoverImages(page);
913-
},
909+
run: waitForCookiesDialog,
914910
},
915911
{
916912
name: 'With cover for dark mode',
@@ -925,18 +921,12 @@ const testCases: TestsCase[] = [
925921
{
926922
name: 'With hero cover',
927923
url: 'page-options/page-with-hero-cover',
928-
run: async (page) => {
929-
await waitForCookiesDialog(page);
930-
await waitForCoverImages(page);
931-
},
924+
run: waitForCookiesDialog,
932925
},
933926
{
934927
name: 'With cover and no TOC',
935928
url: 'page-options/page-with-cover-and-no-toc',
936-
run: async (page) => {
937-
await waitForCookiesDialog(page);
938-
await waitForCoverImages(page);
939-
},
929+
run: waitForCookiesDialog,
940930
screenshot: {
941931
waitForTOCScrolling: false,
942932
},

packages/gitbook/e2e/util.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,6 @@ export async function waitForNotFound(_page: Page, response: Response | null) {
154154
expect(response?.status()).toBe(404);
155155
}
156156

157-
export async function waitForCoverImages(page: Page) {
158-
// Wait for cover images to exist (not the shimmer placeholder)
159-
await expect(page.locator('img[alt="Page cover"]').first()).toBeVisible({
160-
timeout: 10_000,
161-
});
162-
}
163-
164157
/**
165158
* Transform test cases into Playwright tests and run it.
166159
*/

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { tcls } from '@/lib/tailwind';
88

99
import { assert } from 'ts-essentials';
1010
import { PageCoverImage } from './PageCoverImage';
11-
import { getCoverHeight } from './coverHeight';
1211
import defaultPageCoverSVG from './default-page-cover.svg';
1312

1413
const defaultPageCover = defaultPageCoverSVG as StaticImageData;
@@ -23,12 +22,6 @@ export async function PageCover(props: {
2322
context: GitBookSiteContext;
2423
}) {
2524
const { as, page, cover, context } = props;
26-
const height = getCoverHeight(cover);
27-
28-
if (!height) {
29-
return null;
30-
}
31-
3225
const [resolved, resolvedDark] = await Promise.all([
3326
cover.ref ? resolveContentRef(cover.ref, context) : null,
3427
cover.refDark ? resolveContentRef(cover.refDark, context) : null,
@@ -85,7 +78,6 @@ export async function PageCover(props: {
8578
<div
8679
id="page-cover"
8780
data-full={String(as === 'full')}
88-
style={{ height }}
8981
className={tcls(
9082
'overflow-hidden',
9183
// Negative margin to balance the container padding

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use client';
22
import { tcls } from '@/lib/tailwind';
3+
import { useRef } from 'react';
4+
import { useResizeObserver } from 'usehooks-ts';
35
import type { ImageSize } from '../utils';
4-
import { useCoverPosition } from './useCoverPosition';
56

67
interface ImageAttributes {
78
src: string;
@@ -17,16 +18,28 @@ interface Images {
1718
dark?: ImageAttributes;
1819
}
1920

21+
const PAGE_COVER_SIZE: ImageSize = { width: 1990, height: 480 };
22+
23+
function getTop(container: { height?: number; width?: number }, y: number, img: ImageAttributes) {
24+
// When the size of the image hasn't been determined, we fallback to the center position
25+
if (!img.size || y === 0) return '50%';
26+
const ratio =
27+
container.height && container.width
28+
? Math.max(container.width / img.size.width, container.height / img.size.height)
29+
: 1;
30+
const scaledHeight = img.size ? img.size.height * ratio : PAGE_COVER_SIZE.height;
31+
const top =
32+
container.height && img.size ? (container.height - scaledHeight) / 2 + y * ratio : y;
33+
return `${top}px`;
34+
}
35+
2036
export function PageCoverImage({ imgs, y }: { imgs: Images; y: number }) {
21-
const { containerRef, objectPositionY, isLoading } = useCoverPosition(imgs, y);
37+
const containerRef = useRef<HTMLDivElement>(null);
2238

23-
if (isLoading) {
24-
return (
25-
<div className="h-full w-full overflow-hidden" ref={containerRef}>
26-
<div className="h-full w-full animate-pulse bg-gradient-to-br from-gray-100 to-gray-200 dark:from-gray-800 dark:to-gray-900" />
27-
</div>
28-
);
29-
}
39+
const container = useResizeObserver({
40+
// @ts-expect-error wrong types
41+
ref: containerRef,
42+
});
3043

3144
return (
3245
<div className="h-full w-full overflow-hidden" ref={containerRef}>
@@ -36,9 +49,10 @@ export function PageCoverImage({ imgs, y }: { imgs: Images; y: number }) {
3649
sizes={imgs.light.sizes}
3750
fetchPriority="high"
3851
alt="Page cover"
39-
className={tcls('h-full', 'w-full', 'object-cover', imgs.dark ? 'dark:hidden' : '')}
52+
className={tcls('w-full', 'object-cover', imgs.dark ? 'dark:hidden' : '')}
4053
style={{
41-
objectPosition: `50% ${objectPositionY}%`,
54+
aspectRatio: `${PAGE_COVER_SIZE.width}/${PAGE_COVER_SIZE.height}`,
55+
objectPosition: `50% ${getTop(container, y, imgs.light)}`,
4256
}}
4357
/>
4458
{imgs.dark && (
@@ -48,9 +62,10 @@ export function PageCoverImage({ imgs, y }: { imgs: Images; y: number }) {
4862
sizes={imgs.dark.sizes}
4963
fetchPriority="low"
5064
alt="Page cover"
51-
className={tcls('h-full', 'w-full', 'object-cover', 'dark:inline', 'hidden')}
65+
className={tcls('w-full', 'object-cover', 'dark:inline', 'hidden')}
5266
style={{
53-
objectPosition: `50% ${objectPositionY}%`,
67+
aspectRatio: `${PAGE_COVER_SIZE.width}/${PAGE_COVER_SIZE.height}`,
68+
objectPosition: `50% ${getTop(container, y, imgs.dark)}`,
5469
}}
5570
/>
5671
)}

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

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export * from './PageBody';
22
export * from './PageCover';
3-
export * from './useCoverPosition';

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

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

0 commit comments

Comments
 (0)