@cntrl-site/sdk-nextjs 1.11.12 → 1.11.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ <component name="ProjectCodeStyleConfiguration">
2
+ <state>
3
+ <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
4
+ </state>
5
+ </component>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/sdk-nextjs.iml" filepath="$PROJECT_DIR$/.idea/sdk-nextjs.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="WEB_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$">
5
+ <excludeFolder url="file://$MODULE_DIR$/temp" />
6
+ <excludeFolder url="file://$MODULE_DIR$/.tmp" />
7
+ <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
+ </content>
9
+ <orderEntry type="inheritedJdk" />
10
+ <orderEntry type="sourceFolder" forTests="false" />
11
+ </component>
12
+ </module>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
@@ -43,7 +43,7 @@ exports.textColumnsRule = textColumnsRule;
43
43
  /**
44
44
  * The box a quote's text or a table is drawn in, as a CSS rule for `selector`: padded inside, its
45
45
  * sides grown out past the columns the block is placed on by as much as they pad it, so what it
46
- * holds stays on them; rounded, turned, and filled where it has a fill.
46
+ * holds stays on them; rounded, and filled where it has a fill.
47
47
  */
48
48
  function boxRule(selector, box) {
49
49
  return `
@@ -51,7 +51,6 @@ function boxRule(selector, box) {
51
51
  padding: ${[box.boxPaddingTop, box.boxPaddingRight, box.boxPaddingBottom, box.boxPaddingLeft].map(vw_1.vw).join(' ')};
52
52
  margin: 0 -${(0, vw_1.vw)(box.boxPaddingRight)} 0 -${(0, vw_1.vw)(box.boxPaddingLeft)};
53
53
  border-radius: ${(0, vw_1.vw)(box.boxRadius)};
54
- rotate: ${box.boxRotation ? `${box.boxRotation}deg` : 'none'};
55
54
  background-color: transparent;
56
55
  }
57
56
  ${box.boxBackground ? colorRule(selector, 'background-color', box.boxBackground) : ''}
@@ -9,9 +9,7 @@ const sdk_1 = require("@cntrl-site/sdk");
9
9
  const react_1 = require("react");
10
10
  const style_1 = __importDefault(require("styled-jsx/style"));
11
11
  const useCntrlContext_1 = require("../../provider/useCntrlContext");
12
- const useScrollFade_1 = require("../../common/useScrollFade");
13
12
  const blockPlacement_1 = require("../StructuredBlockItem/blockPlacement");
14
- const textStylesRule_1 = require("../StructuredBlockItem/textStylesRule");
15
13
  const blocksMap_1 = require("../StructuredBlockItem/blocksMap");
16
14
  const TEXT_KINDS = ['title', 'subtitle', 'date'];
17
15
  // an element a header stored before its kind was ordered goes last, not first
@@ -22,11 +20,9 @@ function orderOf(order, kind) {
22
20
  /**
23
21
  * A content-based section's header, drawn first. Variant A draws the key image full width with the
24
22
  * other elements on its bottom edge, the image covering a share of the viewport's height if set to,
25
- * staying put on the screen while the header scrolls over it if set to, and zooming in a little and
26
- * fading out as it scrolls off the screen if set to, the header's fill showing as it fades; variant B flows the
23
+ * and staying put on the screen while the header scrolls over it if set to; variant B flows the
27
24
  * shown elements in order, each placed as in the stack. The variant, the order and what is shown
28
- * are a layout's, so every element is drawn once and arranged per layout. The elements stack in the
29
- * header by their own z-indexes, so the text can go under the image.
25
+ * are a layout's, so every element is drawn once and arranged per layout.
30
26
  */
31
27
  const StructuredHeader = ({ header, blocks, readingWidth }) => {
32
28
  const reactId = (0, react_1.useId)();
@@ -35,9 +31,6 @@ const StructuredHeader = ({ header, blocks, readingWidth }) => {
35
31
  const image = elementOf('image');
36
32
  const imageUrl = (image === null || image === void 0 ? void 0 : image.type) === sdk_1.StructuredBlockType.HeaderImage ? image.commonParams.url : '';
37
33
  const hasImage = Boolean(imageUrl);
38
- const [imageElement, setImageElement] = (0, react_1.useState)(null);
39
- const isFading = hasImage && Object.values(header.layoutParams).some(params => params.variant === 'A' && params.fadeOnScroll);
40
- (0, useScrollFade_1.useScrollFade)(imageElement, isFading);
41
34
  const texts = TEXT_KINDS.flatMap((kind) => {
42
35
  const block = elementOf(kind);
43
36
  return block ? [{ kind, block }] : [];
@@ -46,10 +39,10 @@ const StructuredHeader = ({ header, blocks, readingWidth }) => {
46
39
  const stackClass = `structured-header-stack-${header.id}`;
47
40
  const elementClass = (block) => `structured-header-element-${block.id}`;
48
41
  const pinClass = (block, part = '') => `structured-header-pin${part}-${block.id}`;
49
- const renderElement = (block, extra, ref) => {
42
+ const renderElement = (block, extra) => {
50
43
  var _a;
51
44
  const BlockComponent = (_a = blocksMap_1.blocksMap[block.type]) !== null && _a !== void 0 ? _a : blocksMap_1.UnknownBlock;
52
- return ((0, jsx_runtime_1.jsxs)("div", { ref: ref, className: elementClass(block), children: [(0, jsx_runtime_1.jsx)(BlockComponent, { block: block, readingWidth: readingWidth }), extra] }, block.id));
45
+ return ((0, jsx_runtime_1.jsxs)("div", { className: elementClass(block), children: [(0, jsx_runtime_1.jsx)(BlockComponent, { block: block, readingWidth: readingWidth }), extra] }, block.id));
53
46
  };
54
47
  // a sticky backdrop, the way a section's fixed background is drawn: a screen-tall frame stuck
55
48
  // inside a track a screen longer at each end, clipped to the header; the picture itself only
@@ -77,26 +70,21 @@ const StructuredHeader = ({ header, blocks, readingWidth }) => {
77
70
  }
78
71
  `;
79
72
  const elementStyles = (kind, block) => (0, sdk_1.getLayoutStyles)(layouts, [header.layoutParams, block.area, block.layoutParams], ([layoutParams, area, blockLayoutParams]) => {
80
- var _a, _b;
73
+ var _a;
81
74
  const isBackdrop = kind === 'image' && layoutParams.variant === 'A';
82
75
  const isHidden = !isBackdrop && layoutParams.hiddenElements.includes(kind);
83
76
  const isCover = isBackdrop && layoutParams.imageFit === 'cover';
84
77
  const isSticky = isBackdrop && Boolean(layoutParams.stickyImage);
85
- // the fading picture zooms inside the image's box, and whichever picture shows fades with the box
86
- const isFaded = isBackdrop && Boolean(layoutParams.fadeOnScroll);
87
- const fade = `var(${useScrollFade_1.SCROLL_FADE_PROPERTY}, 0)`;
88
78
  // every value is written on every layout: the media queries stack upward, so a layout's
89
79
  // backdrop or cover would otherwise carry into the larger layouts drawn another way
90
80
  return (`
91
81
  .${elementClass(block)} {
92
82
  display: ${isHidden ? 'none' : 'block'};
93
83
  position: relative;
94
- z-index: ${(_a = area === null || area === void 0 ? void 0 : area.zIndex) !== null && _a !== void 0 ? _a : 0};
95
84
  order: ${orderOf(layoutParams.order, kind)};
96
85
  ${isBackdrop
97
86
  ? `width: 100%; left: 0; height: ${isCover ? '100%' : 'auto'}; padding: 0;`
98
87
  : (0, blockPlacement_1.blockPlacement)(area, blockLayoutParams)}
99
- ${kind === 'image' ? `overflow: ${isFaded ? 'clip' : 'visible'}; opacity: ${isFaded ? `calc(1 - ${fade})` : 1};` : ''}
100
88
  }
101
89
  .${elementClass(block)} .structured-caption-${block.id} {
102
90
  display: ${isBackdrop ? 'none' : 'block'};
@@ -117,16 +105,12 @@ const StructuredHeader = ({ header, blocks, readingWidth }) => {
117
105
  .${pinClass(block, '-frame')} {
118
106
  background-size: ${isCover ? 'cover' : '100% auto'};
119
107
  background-position: ${isCover ? 'center' : 'top center'};
120
- opacity: ${(_b = blockLayoutParams === null || blockLayoutParams === void 0 ? void 0 : blockLayoutParams.opacity) !== null && _b !== void 0 ? _b : 1};
121
- }
122
- .${elementClass(block)} .structured-header-image-${block.id},
123
- .${pinClass(block, '-frame')} {
124
- transform: ${isFaded ? `scale(calc(1 + ${useScrollFade_1.SCROLL_FADE_ZOOM} * ${fade}))` : 'none'};
108
+ opacity: ${(_a = blockLayoutParams === null || blockLayoutParams === void 0 ? void 0 : blockLayoutParams.opacity) !== null && _a !== void 0 ? _a : 1};
125
109
  }`
126
110
  : ''}
127
111
  `);
128
112
  });
129
- return ((0, jsx_runtime_1.jsxs)("div", { className: headerClass, children: [image && renderElement(image, hasImage && renderPin(image), setImageElement), (0, jsx_runtime_1.jsx)("div", { className: stackClass, children: texts.map(({ block }) => renderElement(block)) }), (0, jsx_runtime_1.jsx)(style_1.default, { id: `${reactId}-header-${header.id}`, children: `
113
+ return ((0, jsx_runtime_1.jsxs)("div", { className: headerClass, children: [image && renderElement(image, hasImage && renderPin(image)), (0, jsx_runtime_1.jsx)("div", { className: stackClass, children: texts.map(({ block }) => renderElement(block)) }), (0, jsx_runtime_1.jsx)(style_1.default, { id: `${reactId}-header-${header.id}`, children: `
130
114
  .${headerClass} {
131
115
  position: relative;
132
116
  width: 100%;
@@ -136,8 +120,6 @@ const StructuredHeader = ({ header, blocks, readingWidth }) => {
136
120
  var _a;
137
121
  const isA = layoutParams.variant === 'A';
138
122
  const isCover = isA && layoutParams.imageFit === 'cover';
139
- // the fill is drawn only behind a fading image, and cleared on every other layout
140
- const fill = isA && layoutParams.fadeOnScroll ? layoutParams.background : undefined;
141
123
  return (`
142
124
  .${headerClass} {
143
125
  display: ${isA ? 'block' : 'flex'};
@@ -146,9 +128,7 @@ const StructuredHeader = ({ header, blocks, readingWidth }) => {
146
128
  margin-top: ${(area === null || area === void 0 ? void 0 : area.paddingTop) ? `${area.paddingTop * 100}vw` : 0};
147
129
  height: ${isCover ? `${layoutParams.coverHeight * 100}vh` : 'auto'};
148
130
  height: ${isCover ? `${layoutParams.coverHeight * 100}svh` : 'auto'};
149
- background-color: transparent;
150
131
  }
151
- ${fill ? (0, textStylesRule_1.colorRule)(`.${headerClass}`, 'background-color', fill) : ''}
152
132
  .${stackClass} {
153
133
  ${isA
154
134
  ? `display: flex; flex-direction: column; position: ${hasImage ? 'absolute' : 'relative'}; left: 0; right: 0; bottom: 0;`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/sdk-nextjs",
3
- "version": "1.11.12",
3
+ "version": "1.11.13",
4
4
  "description": "SDK for Next.js",
5
5
  "author": "arsen@momdesign.nyc",
6
6
  "license": "MIT",
@@ -30,9 +30,9 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "@cntrl-site/color": "^1.0.0",
33
- "@cntrl-site/components": "^1.0.112",
33
+ "@cntrl-site/components": "^1.0.115",
34
34
  "@cntrl-site/effects": "^1.4.2",
35
- "@cntrl-site/sdk": "^1.29.12",
35
+ "@cntrl-site/sdk": "^1.29.11",
36
36
  "@types/vimeo__player": "^2.18.0",
37
37
  "@vimeo/player": "^2.25.0",
38
38
  "franc-min": "^5.0.0",
@@ -44,7 +44,7 @@ export function textColumnsRule(
44
44
  /**
45
45
  * The box a quote's text or a table is drawn in, as a CSS rule for `selector`: padded inside, its
46
46
  * sides grown out past the columns the block is placed on by as much as they pad it, so what it
47
- * holds stays on them; rounded, turned, and filled where it has a fill.
47
+ * holds stays on them; rounded, and filled where it has a fill.
48
48
  */
49
49
  export function boxRule(selector: string, box: StructuredBlockBoxParams): string {
50
50
  return `
@@ -52,7 +52,6 @@ export function boxRule(selector: string, box: StructuredBlockBoxParams): string
52
52
  padding: ${[box.boxPaddingTop, box.boxPaddingRight, box.boxPaddingBottom, box.boxPaddingLeft].map(vw).join(' ')};
53
53
  margin: 0 -${vw(box.boxPaddingRight)} 0 -${vw(box.boxPaddingLeft)};
54
54
  border-radius: ${vw(box.boxRadius)};
55
- rotate: ${box.boxRotation ? `${box.boxRotation}deg` : 'none'};
56
55
  background-color: transparent;
57
56
  }
58
57
  ${box.boxBackground ? colorRule(selector, 'background-color', box.boxBackground) : ''}
@@ -64,8 +64,7 @@ const quote = block(StructuredBlockType.Quote, 'quote', { ...paragraph('A quote'
64
64
  boxPaddingRight: 0.03,
65
65
  boxPaddingBottom: 0.02,
66
66
  boxPaddingLeft: 0.01,
67
- boxRadius: 0.01,
68
- boxRotation: -4
67
+ boxRadius: 0.01
69
68
  });
70
69
  const date = block(StructuredBlockType.Date, 'date', { ...paragraph('23 Sep 2026'), date: '2026-09-23', format: 'D MMM YYYY' }, {
71
70
  ...textStyles,
@@ -207,7 +206,6 @@ describe('StructuredContent', () => {
207
206
  expect(container.querySelector('.structured-quote-box-quote')!.textContent!.trim()).toBe('A quote');
208
207
  // its sides grow out past the columns the text is placed on, which it stays on
209
208
  expect(css()).toMatch(/\.structured-quote-box-quote \{[^}]*padding: 2vw 3vw 2vw 1vw;[^}]*margin: 0 -3vw 0 -1vw;[^}]*border-radius: 1vw;/);
210
- expect(css()).toMatch(/\.structured-quote-box-quote \{[^}]*rotate: -4deg;/);
211
209
  expect(css()).toMatch(/\.structured-quote-box-quote \{\s*background-color: oklch/);
212
210
  expect(container.querySelector('.structured-text-date')!.textContent!.trim()).toBe('23 Sep 2026');
213
211
  // a quote is a quotation of paragraphs, kept from the margins of both
@@ -286,8 +284,6 @@ describe('StructuredContent', () => {
286
284
  const { container } = draw({ ...section(false), structuredContent: [boxed] } as Section);
287
285
  expect(container.querySelector('.structured-table-box-boxed > .structured-table-boxed')).not.toBeNull();
288
286
  expect(css()).toMatch(/\.structured-table-box-boxed \{[^}]*padding: 1vw 2vw 1vw 2vw;[^}]*margin: 0 -2vw 0 -2vw;[^}]*border-radius: 0\.5vw;/);
289
- // a box with no turn of its own is drawn upright on every layout, whatever another layout turns it by
290
- expect(css()).toMatch(/\.structured-table-box-boxed \{[^}]*rotate: none;/);
291
287
  expect(css()).toMatch(/\.structured-table-box-boxed \{\s*background-color: oklch/);
292
288
  });
293
289
 
@@ -373,15 +369,6 @@ describe('StructuredContent', () => {
373
369
  expect(css()).toMatch(/\.structured-header-element-key-image \.structured-header-picture-key-image \{\s*object-fit: fill;/);
374
370
  });
375
371
 
376
- it('stacks the header\'s elements by their own z-indexes on each layout, so its text can go under its image', () => {
377
- const under = { ...title, area: { desktop: { ...onGrid, zIndex: 3 }, mobile: { ...onGrid, zIndex: 7 } } };
378
- const keyImage = { ...headerImage, area: { desktop: { ...onGrid, zIndex: 5 }, mobile: { ...onGrid, zIndex: 5 } } };
379
- draw({ ...section(true), structuredContent: [under, keyImage, ...stack] } as unknown as Section);
380
- expect(css()).toMatch(/@media \(min-width: 800px\) \{[\s\S]*\.structured-header-element-title \{[^}]*z-index: 3;/);
381
- expect(css()).toMatch(/\.structured-header-element-title \{[^}]*z-index: 7;/);
382
- expect(css()).toMatch(/\.structured-header-element-key-image \{[^}]*z-index: 5;/);
383
- });
384
-
385
372
  it('keeps a sticky key image still on the screen, as a fixed section background, on the layouts that stick it', () => {
386
373
  const sticky = { ...header, layoutParams: { ...header.layoutParams, desktop: { ...header.layoutParams.desktop, stickyImage: true } } };
387
374
  const { container } = draw({ ...section(true), header: sticky } as unknown as Section);
@@ -394,83 +381,6 @@ describe('StructuredContent', () => {
394
381
  expect(css()).toMatch(/\.structured-header-pin-key-image \{\s*display: none;/);
395
382
  });
396
383
 
397
- describe('a key image fading on scroll', () => {
398
- const fading = { ...header, layoutParams: { ...header.layoutParams, desktop: { ...header.layoutParams.desktop, fadeOnScroll: true } } };
399
- let frames: FrameRequestCallback[];
400
- const { requestAnimationFrame, cancelAnimationFrame } = window;
401
-
402
- beforeEach(() => {
403
- frames = [];
404
- window.requestAnimationFrame = callback => frames.push(callback);
405
- window.cancelAnimationFrame = () => {};
406
- (window as { ResizeObserver?: unknown }).ResizeObserver = class { observe() {} disconnect() {} };
407
- });
408
-
409
- afterEach(() => {
410
- Object.assign(window, { requestAnimationFrame, cancelAnimationFrame });
411
- delete (window as { ResizeObserver?: unknown }).ResizeObserver;
412
- });
413
-
414
- const scrolled = (element: HTMLElement, top: number, bottom: number) => {
415
- element.getBoundingClientRect = () => ({ top, bottom }) as DOMRect;
416
- window.dispatchEvent(new Event('scroll'));
417
- frames.splice(0).forEach(frame => frame(0));
418
- return element.style.getPropertyValue('--cntrl-scroll-fade');
419
- };
420
-
421
- it('zooms it inside its box and fades it on the layouts that fade it, and draws it still on the others', () => {
422
- draw({ ...section(true), header: fading } as unknown as Section);
423
- // desktop, variant A: whichever picture shows, the image's own or its sticky copy
424
- expect(css()).toMatch(/@media \(min-width: 800px\) \{[\s\S]*\.structured-header-element-key-image \{[^}]*overflow: clip; opacity: calc\(1 - var\(--cntrl-scroll-fade, 0\)\);/);
425
- expect(css()).toMatch(/\.structured-header-element-key-image \.structured-header-image-key-image,\s*\.structured-header-pin-frame-key-image \{\s*transform: scale\(calc\(1 \+ 0\.15 \* var\(--cntrl-scroll-fade, 0\)\)\);/);
426
- // mobile, variant B: written back, so the fade ends where the layout does
427
- expect(css()).toMatch(/\.structured-header-element-key-image \{[^}]*overflow: visible; opacity: 1;/);
428
- expect(css()).toMatch(/\.structured-header-pin-frame-key-image \{\s*transform: none;/);
429
- });
430
-
431
- it('fills the header behind its key image on the layouts that fade it, and leaves it clear on the others', () => {
432
- const fill = 'oklch(0.9 0.02 80 / 1)';
433
- const layoutCss = (layoutParams: object) => {
434
- const { unmount } = draw({ ...section(true), header: { ...header, layoutParams } } as unknown as Section);
435
- // the header's own styles: the smallest layout's bare, each larger one's in a query of its own
436
- const headerCss = [...document.body.querySelectorAll('style')].map(style => style.textContent ?? '').find(text => text.includes('.structured-header-stack-header')) ?? '';
437
- const rules = headerCss.split('@media').filter(rule => rule.includes('.structured-header-header {'));
438
- unmount();
439
- return {
440
- desktop: rules.find(rule => rule.startsWith(' (min-width: 800px)')) ?? '',
441
- mobile: rules.find(rule => !rule.startsWith(' (min-width')) ?? ''
442
- };
443
- };
444
- // desktop fades its variant A image, mobile draws variant B: the fill is desktop's alone
445
- const fading = layoutCss({
446
- desktop: { ...header.layoutParams.desktop, fadeOnScroll: true, background: fill },
447
- mobile: { ...header.layoutParams.mobile, fadeOnScroll: true, background: fill }
448
- });
449
- expect(fading.desktop).toMatch(/\.structured-header-header \{\s*background-color: oklch\(/);
450
- expect(fading.mobile).toMatch(/\.structured-header-header \{[^}]*background-color: transparent;/);
451
- expect(fading.mobile).not.toMatch(/background-color: oklch/);
452
- // a fill kept from before the image stopped fading is not drawn
453
- const still = layoutCss({ ...header.layoutParams, desktop: { ...header.layoutParams.desktop, background: fill } });
454
- expect(still.desktop).not.toMatch(/background-color: oklch/);
455
- });
456
-
457
- it('fades it from the first pixel scrolled off the screen\'s top, all the way once it is gone, and back as the page scrolls back', () => {
458
- const { container } = draw({ ...section(true), header: fading } as unknown as Section);
459
- const element = container.querySelector<HTMLElement>('.structured-header-element-key-image')!;
460
- expect(scrolled(element, 0, 600)).toBe('0');
461
- expect(Number(scrolled(element, -300, 300))).toBeCloseTo(0.5);
462
- expect(scrolled(element, -700, -100)).toBe('1');
463
- expect(scrolled(element, 0, 600)).toBe('0');
464
- });
465
-
466
- it('leaves a key image no layout fades alone', () => {
467
- const { container } = draw(section(true));
468
- const element = container.querySelector<HTMLElement>('.structured-header-element-key-image')!;
469
- expect(scrolled(element, -500, 100)).toBe('');
470
- expect(css()).not.toMatch(/overflow: clip; opacity: calc/);
471
- });
472
- });
473
-
474
384
  it('draws a key image no layout sticks as it is', () => {
475
385
  draw(section(true));
476
386
  expect(css()).not.toMatch(/\.structured-header-pin-key-image \{\s*display: block;/);
@@ -6,12 +6,10 @@ import {
6
6
  StructuredBlockAny,
7
7
  StructuredBlockType
8
8
  } from '@cntrl-site/sdk';
9
- import { FC, ReactNode, useId, useState } from 'react';
9
+ import { FC, ReactNode, useId } from 'react';
10
10
  import JSXStyle from 'styled-jsx/style';
11
11
  import { useCntrlContext } from '../../provider/useCntrlContext';
12
- import { SCROLL_FADE_PROPERTY, SCROLL_FADE_ZOOM, useScrollFade } from '../../common/useScrollFade';
13
12
  import { blockPlacement } from '../StructuredBlockItem/blockPlacement';
14
- import { colorRule } from '../StructuredBlockItem/textStylesRule';
15
13
  import { blocksMap, UnknownBlock } from '../StructuredBlockItem/blocksMap';
16
14
 
17
15
  interface Props {
@@ -32,11 +30,9 @@ function orderOf(order: HeaderElementKind[], kind: HeaderElementKind): number {
32
30
  /**
33
31
  * A content-based section's header, drawn first. Variant A draws the key image full width with the
34
32
  * other elements on its bottom edge, the image covering a share of the viewport's height if set to,
35
- * staying put on the screen while the header scrolls over it if set to, and zooming in a little and
36
- * fading out as it scrolls off the screen if set to, the header's fill showing as it fades; variant B flows the
33
+ * and staying put on the screen while the header scrolls over it if set to; variant B flows the
37
34
  * shown elements in order, each placed as in the stack. The variant, the order and what is shown
38
- * are a layout's, so every element is drawn once and arranged per layout. The elements stack in the
39
- * header by their own z-indexes, so the text can go under the image.
35
+ * are a layout's, so every element is drawn once and arranged per layout.
40
36
  */
41
37
  export const StructuredHeader: FC<Props> = ({ header, blocks, readingWidth }) => {
42
38
  const reactId = useId();
@@ -45,9 +41,6 @@ export const StructuredHeader: FC<Props> = ({ header, blocks, readingWidth }) =>
45
41
  const image = elementOf('image');
46
42
  const imageUrl = image?.type === StructuredBlockType.HeaderImage ? (image as HeaderImageStructuredBlock).commonParams.url : '';
47
43
  const hasImage = Boolean(imageUrl);
48
- const [imageElement, setImageElement] = useState<HTMLDivElement | null>(null);
49
- const isFading = hasImage && Object.values(header.layoutParams).some(params => params.variant === 'A' && params.fadeOnScroll);
50
- useScrollFade(imageElement, isFading);
51
44
  const texts = TEXT_KINDS.flatMap((kind) => {
52
45
  const block = elementOf(kind);
53
46
  return block ? [{ kind, block }] : [];
@@ -56,10 +49,10 @@ export const StructuredHeader: FC<Props> = ({ header, blocks, readingWidth }) =>
56
49
  const stackClass = `structured-header-stack-${header.id}`;
57
50
  const elementClass = (block: StructuredBlockAny) => `structured-header-element-${block.id}`;
58
51
  const pinClass = (block: StructuredBlockAny, part = '') => `structured-header-pin${part}-${block.id}`;
59
- const renderElement = (block: StructuredBlockAny, extra?: ReactNode, ref?: (element: HTMLDivElement | null) => void) => {
52
+ const renderElement = (block: StructuredBlockAny, extra?: ReactNode) => {
60
53
  const BlockComponent = blocksMap[block.type] ?? UnknownBlock;
61
54
  return (
62
- <div key={block.id} ref={ref} className={elementClass(block)}>
55
+ <div key={block.id} className={elementClass(block)}>
63
56
  <BlockComponent block={block} readingWidth={readingWidth} />
64
57
  {extra}
65
58
  </div>
@@ -104,21 +97,16 @@ export const StructuredHeader: FC<Props> = ({ header, blocks, readingWidth }) =>
104
97
  const isHidden = !isBackdrop && layoutParams.hiddenElements.includes(kind);
105
98
  const isCover = isBackdrop && layoutParams.imageFit === 'cover';
106
99
  const isSticky = isBackdrop && Boolean(layoutParams.stickyImage);
107
- // the fading picture zooms inside the image's box, and whichever picture shows fades with the box
108
- const isFaded = isBackdrop && Boolean(layoutParams.fadeOnScroll);
109
- const fade = `var(${SCROLL_FADE_PROPERTY}, 0)`;
110
100
  // every value is written on every layout: the media queries stack upward, so a layout's
111
101
  // backdrop or cover would otherwise carry into the larger layouts drawn another way
112
102
  return (`
113
103
  .${elementClass(block)} {
114
104
  display: ${isHidden ? 'none' : 'block'};
115
105
  position: relative;
116
- z-index: ${area?.zIndex ?? 0};
117
106
  order: ${orderOf(layoutParams.order, kind)};
118
107
  ${isBackdrop
119
108
  ? `width: 100%; left: 0; height: ${isCover ? '100%' : 'auto'}; padding: 0;`
120
109
  : blockPlacement(area, blockLayoutParams)}
121
- ${kind === 'image' ? `overflow: ${isFaded ? 'clip' : 'visible'}; opacity: ${isFaded ? `calc(1 - ${fade})` : 1};` : ''}
122
110
  }
123
111
  .${elementClass(block)} .structured-caption-${block.id} {
124
112
  display: ${isBackdrop ? 'none' : 'block'};
@@ -140,10 +128,6 @@ export const StructuredHeader: FC<Props> = ({ header, blocks, readingWidth }) =>
140
128
  background-size: ${isCover ? 'cover' : '100% auto'};
141
129
  background-position: ${isCover ? 'center' : 'top center'};
142
130
  opacity: ${blockLayoutParams?.opacity ?? 1};
143
- }
144
- .${elementClass(block)} .structured-header-image-${block.id},
145
- .${pinClass(block, '-frame')} {
146
- transform: ${isFaded ? `scale(calc(1 + ${SCROLL_FADE_ZOOM} * ${fade}))` : 'none'};
147
131
  }`
148
132
  : ''}
149
133
  `);
@@ -151,7 +135,7 @@ export const StructuredHeader: FC<Props> = ({ header, blocks, readingWidth }) =>
151
135
  );
152
136
  return (
153
137
  <div className={headerClass}>
154
- {image && renderElement(image, hasImage && renderPin(image), setImageElement)}
138
+ {image && renderElement(image, hasImage && renderPin(image))}
155
139
  <div className={stackClass}>
156
140
  {texts.map(({ block }) => renderElement(block))}
157
141
  </div>
@@ -164,8 +148,6 @@ export const StructuredHeader: FC<Props> = ({ header, blocks, readingWidth }) =>
164
148
  ${getLayoutStyles(layouts, [header.layoutParams, header.area] as Record<string, any>[], ([layoutParams, area]) => {
165
149
  const isA = layoutParams.variant === 'A';
166
150
  const isCover = isA && layoutParams.imageFit === 'cover';
167
- // the fill is drawn only behind a fading image, and cleared on every other layout
168
- const fill: string | undefined = isA && layoutParams.fadeOnScroll ? layoutParams.background : undefined;
169
151
  return (`
170
152
  .${headerClass} {
171
153
  display: ${isA ? 'block' : 'flex'};
@@ -174,9 +156,7 @@ export const StructuredHeader: FC<Props> = ({ header, blocks, readingWidth }) =>
174
156
  margin-top: ${area?.paddingTop ? `${area.paddingTop * 100}vw` : 0};
175
157
  height: ${isCover ? `${layoutParams.coverHeight * 100}vh` : 'auto'};
176
158
  height: ${isCover ? `${layoutParams.coverHeight * 100}svh` : 'auto'};
177
- background-color: transparent;
178
159
  }
179
- ${fill ? colorRule(`.${headerClass}`, 'background-color', fill) : ''}
180
160
  .${stackClass} {
181
161
  ${isA
182
162
  ? `display: flex; flex-direction: column; position: ${hasImage ? 'absolute' : 'relative'}; left: 0; right: 0; bottom: 0;`
Binary file
@@ -1,60 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useScrollFade = exports.scrollFadeProgress = exports.SCROLL_FADE_ZOOM = exports.SCROLL_FADE_PROPERTY = void 0;
4
- const react_1 = require("react");
5
- // the share of an element left on the screen as it leaves the top at which it starts to fade: all
6
- // of it, so it fades from the first pixel scrolled, as the editor draws it
7
- const FADE_START = 1;
8
- /** The property `useScrollFade` writes the fade to, from 0 to 1, for the element's styles to draw it. */
9
- exports.SCROLL_FADE_PROPERTY = '--cntrl-scroll-fade';
10
- /** How much larger a fading element is drawn once it has faded all the way. */
11
- exports.SCROLL_FADE_ZOOM = 0.15;
12
- /**
13
- * How far an element leaving the screen's top has faded: nothing while all of it is on the screen,
14
- * as much as has left it as it scrolls off, all of it once it is gone. An element taller than the
15
- * screen counts the screen it fills instead, so it fades once its bottom is on the screen, over a
16
- * screen's height of scrolling. An element yet to reach the screen's top, or entering from below,
17
- * has not faded.
18
- */
19
- function scrollFadeProgress(top, bottom, screenHeight) {
20
- const full = Math.min(bottom - top, screenHeight);
21
- if (full <= 0)
22
- return 0;
23
- const left = bottom / full;
24
- return Math.min(Math.max(1 - left / FADE_START, 0), 1);
25
- }
26
- exports.scrollFadeProgress = scrollFadeProgress;
27
- /**
28
- * Writes to the element how far it has faded leaving the screen, as the page scrolls or the element
29
- * resizes, while enabled; nothing is drawn here, the element's styles draw the fade. Until it runs,
30
- * as the page is first drawn, the element is unfaded.
31
- */
32
- function useScrollFade(element, isEnabled) {
33
- (0, react_1.useEffect)(() => {
34
- if (!element || !isEnabled)
35
- return;
36
- let frame = 0;
37
- const update = () => {
38
- frame = 0;
39
- const { top, bottom } = element.getBoundingClientRect();
40
- element.style.setProperty(exports.SCROLL_FADE_PROPERTY, String(scrollFadeProgress(top, bottom, window.innerHeight)));
41
- };
42
- const schedule = () => {
43
- if (!frame)
44
- frame = requestAnimationFrame(update);
45
- };
46
- update();
47
- window.addEventListener('scroll', schedule, { passive: true });
48
- window.addEventListener('resize', schedule);
49
- const observer = new ResizeObserver(schedule);
50
- observer.observe(element);
51
- return () => {
52
- cancelAnimationFrame(frame);
53
- window.removeEventListener('scroll', schedule);
54
- window.removeEventListener('resize', schedule);
55
- observer.disconnect();
56
- element.style.removeProperty(exports.SCROLL_FADE_PROPERTY);
57
- };
58
- }, [element, isEnabled]);
59
- }
60
- exports.useScrollFade = useScrollFade;
@@ -1,25 +0,0 @@
1
- import { scrollFadeProgress } from './useScrollFade';
2
-
3
- describe('scrollFadeProgress', () => {
4
- it('has not faded while all of the element is on the screen', () => {
5
- expect(scrollFadeProgress(0, 600, 900)).toBe(0);
6
- expect(scrollFadeProgress(100, 700, 900)).toBe(0);
7
- });
8
-
9
- it('fades from the first pixel scrolled off the screen\'s top, all the way once it is gone', () => {
10
- expect(scrollFadeProgress(-150, 450, 900)).toBeCloseTo(0.25);
11
- expect(scrollFadeProgress(-300, 300, 900)).toBeCloseTo(0.5);
12
- expect(scrollFadeProgress(-600, 0, 900)).toBe(1);
13
- expect(scrollFadeProgress(-700, -100, 900)).toBe(1);
14
- });
15
-
16
- it('counts the screen an element taller than it fills, not the element', () => {
17
- expect(scrollFadeProgress(-1000, 1000, 900)).toBe(0);
18
- expect(scrollFadeProgress(-1100, 900, 900)).toBe(0);
19
- expect(scrollFadeProgress(-1550, 450, 900)).toBeCloseTo(0.5);
20
- });
21
-
22
- it('has not faded entering from below', () => {
23
- expect(scrollFadeProgress(800, 1400, 900)).toBe(0);
24
- });
25
- });
@@ -1,57 +0,0 @@
1
- import { useEffect } from 'react';
2
-
3
- // the share of an element left on the screen as it leaves the top at which it starts to fade: all
4
- // of it, so it fades from the first pixel scrolled, as the editor draws it
5
- const FADE_START = 1;
6
-
7
- /** The property `useScrollFade` writes the fade to, from 0 to 1, for the element's styles to draw it. */
8
- export const SCROLL_FADE_PROPERTY = '--cntrl-scroll-fade';
9
-
10
- /** How much larger a fading element is drawn once it has faded all the way. */
11
- export const SCROLL_FADE_ZOOM = 0.15;
12
-
13
- /**
14
- * How far an element leaving the screen's top has faded: nothing while all of it is on the screen,
15
- * as much as has left it as it scrolls off, all of it once it is gone. An element taller than the
16
- * screen counts the screen it fills instead, so it fades once its bottom is on the screen, over a
17
- * screen's height of scrolling. An element yet to reach the screen's top, or entering from below,
18
- * has not faded.
19
- */
20
- export function scrollFadeProgress(top: number, bottom: number, screenHeight: number): number {
21
- const full = Math.min(bottom - top, screenHeight);
22
- if (full <= 0) return 0;
23
- const left = bottom / full;
24
- return Math.min(Math.max(1 - left / FADE_START, 0), 1);
25
- }
26
-
27
- /**
28
- * Writes to the element how far it has faded leaving the screen, as the page scrolls or the element
29
- * resizes, while enabled; nothing is drawn here, the element's styles draw the fade. Until it runs,
30
- * as the page is first drawn, the element is unfaded.
31
- */
32
- export function useScrollFade(element: HTMLElement | null, isEnabled: boolean): void {
33
- useEffect(() => {
34
- if (!element || !isEnabled) return;
35
- let frame = 0;
36
- const update = () => {
37
- frame = 0;
38
- const { top, bottom } = element.getBoundingClientRect();
39
- element.style.setProperty(SCROLL_FADE_PROPERTY, String(scrollFadeProgress(top, bottom, window.innerHeight)));
40
- };
41
- const schedule = () => {
42
- if (!frame) frame = requestAnimationFrame(update);
43
- };
44
- update();
45
- window.addEventListener('scroll', schedule, { passive: true });
46
- window.addEventListener('resize', schedule);
47
- const observer = new ResizeObserver(schedule);
48
- observer.observe(element);
49
- return () => {
50
- cancelAnimationFrame(frame);
51
- window.removeEventListener('scroll', schedule);
52
- window.removeEventListener('resize', schedule);
53
- observer.disconnect();
54
- element.style.removeProperty(SCROLL_FADE_PROPERTY);
55
- };
56
- }, [element, isEnabled]);
57
- }