@cntrl-site/sdk-nextjs 1.11.11 → 1.11.12
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.
|
@@ -2,17 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useScrollFade = exports.scrollFadeProgress = exports.SCROLL_FADE_ZOOM = exports.SCROLL_FADE_PROPERTY = void 0;
|
|
4
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
|
|
6
|
-
|
|
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;
|
|
7
8
|
/** The property `useScrollFade` writes the fade to, from 0 to 1, for the element's styles to draw it. */
|
|
8
9
|
exports.SCROLL_FADE_PROPERTY = '--cntrl-scroll-fade';
|
|
9
10
|
/** How much larger a fading element is drawn once it has faded all the way. */
|
|
10
11
|
exports.SCROLL_FADE_ZOOM = 0.15;
|
|
11
12
|
/**
|
|
12
|
-
* How far an element leaving the screen's top has faded: nothing while
|
|
13
|
-
* left
|
|
14
|
-
* screen it fills instead, so it fades
|
|
15
|
-
* An element yet to reach the screen's top, or entering from below,
|
|
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.
|
|
16
18
|
*/
|
|
17
19
|
function scrollFadeProgress(top, bottom, screenHeight) {
|
|
18
20
|
const full = Math.min(bottom - top, screenHeight);
|
package/package.json
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { scrollFadeProgress } from './useScrollFade';
|
|
2
2
|
|
|
3
3
|
describe('scrollFadeProgress', () => {
|
|
4
|
-
it('has not faded while
|
|
4
|
+
it('has not faded while all of the element is on the screen', () => {
|
|
5
5
|
expect(scrollFadeProgress(0, 600, 900)).toBe(0);
|
|
6
|
-
expect(scrollFadeProgress(
|
|
6
|
+
expect(scrollFadeProgress(100, 700, 900)).toBe(0);
|
|
7
7
|
});
|
|
8
8
|
|
|
9
|
-
it('fades
|
|
10
|
-
expect(scrollFadeProgress(-
|
|
11
|
-
expect(scrollFadeProgress(-
|
|
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
12
|
expect(scrollFadeProgress(-600, 0, 900)).toBe(1);
|
|
13
13
|
expect(scrollFadeProgress(-700, -100, 900)).toBe(1);
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
it('counts the screen an element taller than it fills, not the element', () => {
|
|
17
17
|
expect(scrollFadeProgress(-1000, 1000, 900)).toBe(0);
|
|
18
|
-
expect(scrollFadeProgress(-
|
|
19
|
-
expect(scrollFadeProgress(-
|
|
18
|
+
expect(scrollFadeProgress(-1100, 900, 900)).toBe(0);
|
|
19
|
+
expect(scrollFadeProgress(-1550, 450, 900)).toBeCloseTo(0.5);
|
|
20
20
|
});
|
|
21
21
|
|
|
22
22
|
it('has not faded entering from below', () => {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
2
|
|
|
3
|
-
// the share of an element left on the screen as it leaves the top at which it starts to fade
|
|
4
|
-
|
|
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;
|
|
5
6
|
|
|
6
7
|
/** The property `useScrollFade` writes the fade to, from 0 to 1, for the element's styles to draw it. */
|
|
7
8
|
export const SCROLL_FADE_PROPERTY = '--cntrl-scroll-fade';
|
|
@@ -10,10 +11,11 @@ export const SCROLL_FADE_PROPERTY = '--cntrl-scroll-fade';
|
|
|
10
11
|
export const SCROLL_FADE_ZOOM = 0.15;
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
|
-
* How far an element leaving the screen's top has faded: nothing while
|
|
14
|
-
* left
|
|
15
|
-
* screen it fills instead, so it fades
|
|
16
|
-
* An element yet to reach the screen's top, or entering from below,
|
|
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.
|
|
17
19
|
*/
|
|
18
20
|
export function scrollFadeProgress(top: number, bottom: number, screenHeight: number): number {
|
|
19
21
|
const full = Math.min(bottom - top, screenHeight);
|
|
@@ -454,11 +454,11 @@ describe('StructuredContent', () => {
|
|
|
454
454
|
expect(still.desktop).not.toMatch(/background-color: oklch/);
|
|
455
455
|
});
|
|
456
456
|
|
|
457
|
-
it('fades it
|
|
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
458
|
const { container } = draw({ ...section(true), header: fading } as unknown as Section);
|
|
459
459
|
const element = container.querySelector<HTMLElement>('.structured-header-element-key-image')!;
|
|
460
|
-
expect(scrolled(element,
|
|
461
|
-
expect(Number(scrolled(element, -
|
|
460
|
+
expect(scrolled(element, 0, 600)).toBe('0');
|
|
461
|
+
expect(Number(scrolled(element, -300, 300))).toBeCloseTo(0.5);
|
|
462
462
|
expect(scrolled(element, -700, -100)).toBe('1');
|
|
463
463
|
expect(scrolled(element, 0, 600)).toBe('0');
|
|
464
464
|
});
|