@averay/scroll-effects 1.0.0 → 1.0.2
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.
- package/dist/_virtual/_rolldown/runtime.mjs +12 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +3 -0
- package/dist/lib/entrances/elements.d.mts +7 -0
- package/dist/lib/entrances/elements.mjs +13 -0
- package/dist/lib/entrances/elements.mjs.map +1 -0
- package/dist/lib/entrances/segments.d.mts +13 -0
- package/dist/lib/entrances/segments.mjs +23 -0
- package/dist/lib/entrances/segments.mjs.map +1 -0
- package/dist/lib/entrances.d.mts +16 -0
- package/dist/lib/entrances.mjs +40 -0
- package/dist/lib/entrances.mjs.map +1 -0
- package/dist/lib/parallax/parallax.d.mts +22 -0
- package/dist/lib/parallax/parallax.mjs +96 -0
- package/dist/lib/parallax/parallax.mjs.map +1 -0
- package/dist/lib/parallax.d.mts +16 -0
- package/dist/lib/parallax.mjs +20 -0
- package/dist/lib/parallax.mjs.map +1 -0
- package/dist/lib/utils/intersection.mjs +15 -0
- package/dist/lib/utils/intersection.mjs.map +1 -0
- package/dist/lib/utils/segments.d.mts +7 -0
- package/dist/lib/utils/segments.mjs +59 -0
- package/dist/lib/utils/segments.mjs.map +1 -0
- package/dist/lib/utils/timing.mjs +25 -0
- package/dist/lib/utils/timing.mjs.map +1 -0
- package/dist/lib/utils/types.d.mts +2 -0
- package/package.json +45 -21
- package/index.ts +0 -2
- package/lib/entrances/elements.ts +0 -21
- package/lib/entrances/segments.ts +0 -62
- package/lib/entrances.ts +0 -34
- package/lib/parallax/parallax.ts +0 -183
- package/lib/parallax.ts +0 -22
- package/lib/utils/intersection.ts +0 -15
- package/lib/utils/segments.ts +0 -101
- package/lib/utils/timing.ts +0 -29
- package/lib/utils/types.ts +0 -3
- package/tsconfig.json +0 -29
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
export { __exportAll };
|
package/dist/index.d.mts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export interface ElementOptions {
|
|
2
|
+
/** A class to add to an element when it has entered the viewport. */
|
|
3
|
+
classEntered: string;
|
|
4
|
+
/** Options to pass to the IntersectionObserver instance used to monitor entrance. */
|
|
5
|
+
intersectionOptions?: IntersectionObserverInit;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=elements.d.mts.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { onFirstIntersection } from "../utils/intersection.mjs";
|
|
2
|
+
// lib/entrances/elements.ts
|
|
3
|
+
function enterElement(element, className) {
|
|
4
|
+
element.classList.add(className);
|
|
5
|
+
}
|
|
6
|
+
function setupElementEntrance(element, { classEntered, intersectionOptions }) {
|
|
7
|
+
onFirstIntersection(element, intersectionOptions, (target) => {
|
|
8
|
+
enterElement(target, classEntered);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
export { setupElementEntrance as default, enterElement };
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=elements.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"elements.mjs","names":[],"sources":["../../../lib/entrances/elements.ts"],"sourcesContent":["import { onFirstIntersection } from '../utils/intersection.ts';\n\nexport interface ElementOptions {\n /** A class to add to an element when it has entered the viewport. */\n classEntered: string;\n /** Options to pass to the IntersectionObserver instance used to monitor entrance. */\n intersectionOptions?: IntersectionObserverInit;\n}\n\nexport function enterElement(element: Element, className: ElementOptions['classEntered']): void {\n element.classList.add(className);\n}\n\nexport default function setupElementEntrance(\n element: HTMLElement,\n { classEntered, intersectionOptions }: ElementOptions,\n): void {\n onFirstIntersection(element, intersectionOptions, (target) => {\n enterElement(target, classEntered);\n });\n}\n"],"mappings":";;AASA,SAAgB,aAAa,SAAkB,WAAiD;CAC9F,QAAQ,UAAU,IAAI,SAAS;AACjC;AAEA,SAAwB,qBACtB,SACA,EAAE,cAAc,uBACV;CACN,oBAAoB,SAAS,sBAAsB,WAAW;EAC5D,aAAa,QAAQ,YAAY;CACnC,CAAC;AACH"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { WrapSegmentOptions } from "../utils/segments.mjs";
|
|
2
|
+
import { ElementOptions } from "./elements.mjs";
|
|
3
|
+
export interface SegmentsOptions extends ElementOptions, WrapSegmentOptions {
|
|
4
|
+
/** A duration in milliseconds to delay before entering the next segment within an element. */
|
|
5
|
+
stepDelay: number;
|
|
6
|
+
/** A class to add to elements with segments (regardless of whether the elements have entered or not). */
|
|
7
|
+
classHasSegments: string;
|
|
8
|
+
/** A class to add to an element after all its segments have finished entering. */
|
|
9
|
+
classFinished: string;
|
|
10
|
+
/** An element to wrap each segment in. The template element will be cloned for each segment, and the segment will be appended to it. */
|
|
11
|
+
segmentWrapperTemplate: HTMLElement;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=segments.d.mts.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { onFirstIntersection } from "../utils/intersection.mjs";
|
|
2
|
+
import { wrapSegments } from "../utils/segments.mjs";
|
|
3
|
+
import { stagger } from "../utils/timing.mjs";
|
|
4
|
+
import { enterElement } from "./elements.mjs";
|
|
5
|
+
// lib/entrances/segments.ts
|
|
6
|
+
function enterSegments(segments, stepDelay, onEnter, onLast) {
|
|
7
|
+
stagger(segments, stepDelay, onEnter, onLast);
|
|
8
|
+
}
|
|
9
|
+
function setupSegmentsEntrance(element, { classHasSegments, classEntered, classFinished, stepDelay, segmentWrapperTemplate, intersectionOptions, ...options }) {
|
|
10
|
+
const segments = wrapSegments(element, segmentWrapperTemplate, options) ?? [];
|
|
11
|
+
onFirstIntersection(element, intersectionOptions, () => {
|
|
12
|
+
enterElement(element, classEntered);
|
|
13
|
+
enterSegments(segments, stepDelay, (segment) => {
|
|
14
|
+
enterElement(segment, classEntered);
|
|
15
|
+
}, () => {
|
|
16
|
+
element.classList.add(classFinished);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
element.classList.add(classHasSegments);
|
|
20
|
+
}
|
|
21
|
+
export { setupSegmentsEntrance as default };
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=segments.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"segments.mjs","names":[],"sources":["../../../lib/entrances/segments.ts"],"sourcesContent":["import type { WrapSegmentOptions } from '../utils/segments.ts';\nimport type { ElementOptions } from './elements.ts';\n\nimport { onFirstIntersection } from '../utils/intersection.ts';\nimport { wrapSegments } from '../utils/segments.ts';\nimport { stagger } from '../utils/timing.ts';\nimport { enterElement } from './elements.ts';\n\nexport interface SegmentsOptions extends ElementOptions, WrapSegmentOptions {\n /** A duration in milliseconds to delay before entering the next segment within an element. */\n stepDelay: number;\n /** A class to add to elements with segments (regardless of whether the elements have entered or not). */\n classHasSegments: string;\n /** A class to add to an element after all its segments have finished entering. */\n classFinished: string;\n /** An element to wrap each segment in. The template element will be cloned for each segment, and the segment will be appended to it. */\n segmentWrapperTemplate: HTMLElement;\n}\n\nfunction enterSegments<T extends HTMLElement>(\n segments: T[],\n stepDelay: number,\n onEnter: (segment: T, index: number) => void,\n onLast: () => void,\n): void {\n stagger(segments, stepDelay, onEnter, onLast);\n}\n\nexport default function setupSegmentsEntrance(\n element: HTMLElement,\n {\n classHasSegments,\n classEntered,\n classFinished,\n stepDelay,\n segmentWrapperTemplate,\n intersectionOptions,\n ...options\n }: SegmentsOptions,\n): void {\n // Process element\n const segments = wrapSegments(element, segmentWrapperTemplate, options) ?? [];\n\n // Set up entrance monitoring\n onFirstIntersection(element, intersectionOptions, () => {\n enterElement(element, classEntered);\n\n enterSegments(\n segments,\n stepDelay,\n (segment) => {\n enterElement(segment, classEntered);\n },\n () => {\n element.classList.add(classFinished);\n },\n );\n });\n\n // Add configured attributes\n element.classList.add(classHasSegments);\n}\n"],"mappings":";;;;;AAmBA,SAAS,cACP,UACA,WACA,SACA,QACM;CACN,QAAQ,UAAU,WAAW,SAAS,MAAM;AAC9C;AAEA,SAAwB,sBACtB,SACA,EACE,kBACA,cACA,eACA,WACA,wBACA,qBACA,GAAG,WAEC;CAEN,MAAM,WAAW,aAAa,SAAS,wBAAwB,OAAO,KAAK,CAAC;CAG5E,oBAAoB,SAAS,2BAA2B;EACtD,aAAa,SAAS,YAAY;EAElC,cACE,UACA,YACC,YAAY;GACX,aAAa,SAAS,YAAY;EACpC,SACM;GACJ,QAAQ,UAAU,IAAI,aAAa;EACrC,CACF;CACF,CAAC;CAGD,QAAQ,UAAU,IAAI,gBAAgB;AACxC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ElementOptions } from "./entrances/elements.mjs";
|
|
2
|
+
import { SegmentsOptions } from "./entrances/segments.mjs";
|
|
3
|
+
import { RequiredOptions } from "./utils/types.mjs";
|
|
4
|
+
declare namespace entrances_d_exports {
|
|
5
|
+
export { element, segments };
|
|
6
|
+
}
|
|
7
|
+
declare const defaults: {
|
|
8
|
+
classHasSegments: string;
|
|
9
|
+
classEntered: string;
|
|
10
|
+
classFinished: string;
|
|
11
|
+
readonly segmentWrapperTemplate: HTMLElement;
|
|
12
|
+
};
|
|
13
|
+
export declare function element(target: HTMLElement, options?: RequiredOptions<ElementOptions, keyof typeof defaults & keyof ElementOptions>): void;
|
|
14
|
+
export declare function segments(target: HTMLElement, options: RequiredOptions<SegmentsOptions, keyof typeof defaults & keyof SegmentsOptions>): void;
|
|
15
|
+
export { entrances_d_exports };
|
|
16
|
+
//# sourceMappingURL=entrances.d.mts.map
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
import setupElementEntrance from "./entrances/elements.mjs";
|
|
3
|
+
import setupSegmentsEntrance from "./entrances/segments.mjs";
|
|
4
|
+
// lib/entrances.ts
|
|
5
|
+
var entrances_exports = /* @__PURE__ */ __exportAll({
|
|
6
|
+
element: () => element,
|
|
7
|
+
segments: () => segments
|
|
8
|
+
});
|
|
9
|
+
let defaultWrapperTemplate;
|
|
10
|
+
const getDefaultWrapperTemplate = () => {
|
|
11
|
+
if (defaultWrapperTemplate == null) {
|
|
12
|
+
defaultWrapperTemplate = document.createElement("span");
|
|
13
|
+
defaultWrapperTemplate.className = "segment";
|
|
14
|
+
defaultWrapperTemplate.setAttribute("role", "text");
|
|
15
|
+
}
|
|
16
|
+
return defaultWrapperTemplate;
|
|
17
|
+
};
|
|
18
|
+
const defaults = {
|
|
19
|
+
classHasSegments: "__segmented",
|
|
20
|
+
classEntered: "__entered",
|
|
21
|
+
classFinished: "__finished",
|
|
22
|
+
get segmentWrapperTemplate() {
|
|
23
|
+
return getDefaultWrapperTemplate();
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
function element(target, options = {}) {
|
|
27
|
+
setupElementEntrance(target, {
|
|
28
|
+
...defaults,
|
|
29
|
+
...options
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function segments(target, options) {
|
|
33
|
+
setupSegmentsEntrance(target, {
|
|
34
|
+
...defaults,
|
|
35
|
+
...options
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
export { element, entrances_exports, segments };
|
|
39
|
+
|
|
40
|
+
//# sourceMappingURL=entrances.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entrances.mjs","names":[],"sources":["../../lib/entrances.ts"],"sourcesContent":["import type { ElementOptions } from './entrances/elements.ts';\nimport type { SegmentsOptions } from './entrances/segments.ts';\nimport type { RequiredOptions } from './utils/types.ts';\n\nimport setupElementEntrance from './entrances/elements.ts';\nimport setupSegmentsEntrance from './entrances/segments.ts';\n\ntype Options = ElementOptions & SegmentsOptions;\n\nlet defaultWrapperTemplate: HTMLElement | undefined;\nconst getDefaultWrapperTemplate = (): HTMLElement => {\n if (defaultWrapperTemplate == null) {\n defaultWrapperTemplate = document.createElement('span');\n defaultWrapperTemplate.className = 'segment';\n defaultWrapperTemplate.setAttribute('role', 'text');\n }\n return defaultWrapperTemplate;\n};\n\nconst defaults = {\n classHasSegments: '__segmented',\n classEntered: '__entered',\n classFinished: '__finished',\n\n get segmentWrapperTemplate() {\n return getDefaultWrapperTemplate();\n },\n} satisfies Partial<Options>;\n\nexport function element(\n target: HTMLElement,\n options: RequiredOptions<ElementOptions, keyof typeof defaults & keyof ElementOptions> = {},\n): void {\n setupElementEntrance(target, { ...defaults, ...options });\n}\n\nexport function segments(\n target: HTMLElement,\n options: RequiredOptions<SegmentsOptions, keyof typeof defaults & keyof SegmentsOptions>,\n): void {\n setupSegmentsEntrance(target, { ...defaults, ...options });\n}\n"],"mappings":";;;;;;;;AASA,IAAI;AACJ,MAAM,kCAA+C;CACnD,IAAI,0BAA0B,MAAM;EAClC,yBAAyB,SAAS,cAAc,MAAM;EACtD,uBAAuB,YAAY;EACnC,uBAAuB,aAAa,QAAQ,MAAM;CACpD;CACA,OAAO;AACT;AAEA,MAAM,WAAW;CACf,kBAAkB;CAClB,cAAc;CACd,eAAe;CAEf,IAAI,yBAAyB;EAC3B,OAAO,0BAA0B;CACnC;AACF;AAEA,SAAgB,QACd,QACA,UAAyF,CAAC,GACpF;CACN,qBAAqB,QAAQ;EAAE,GAAG;EAAU,GAAG;CAAQ,CAAC;AAC1D;AAEA,SAAgB,SACd,QACA,SACM;CACN,sBAAsB,QAAQ;EAAE,GAAG;EAAU,GAAG;CAAQ,CAAC;AAC3D"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
type CssPropertyName = `--${string}`;
|
|
2
|
+
type AnchorX = 'left' | 'center' | 'right';
|
|
3
|
+
type AnchorY = 'top' | 'middle' | 'bottom';
|
|
4
|
+
type OrViewportAnchor<T extends string> = T | `viewport-${T}`;
|
|
5
|
+
export interface Options {
|
|
6
|
+
/** The locations to measure an element’s relative parallax distance from. Unset axes will be ignored. */
|
|
7
|
+
anchors: {
|
|
8
|
+
x?: OrViewportAnchor<AnchorX> | undefined;
|
|
9
|
+
y?: OrViewportAnchor<AnchorY> | undefined;
|
|
10
|
+
};
|
|
11
|
+
/** CSS custom property names to assign each element’s parallax values to. Unset axes will be ignored. */
|
|
12
|
+
cssPropertyNames: {
|
|
13
|
+
x?: CssPropertyName | undefined;
|
|
14
|
+
y?: CssPropertyName | undefined;
|
|
15
|
+
};
|
|
16
|
+
/** The strength of the parallax effect. A larger value results in a greater parallax number at the extremes, while zero effectively disables the parallax effect. */
|
|
17
|
+
strength: number;
|
|
18
|
+
/** Whether to skip updates to the parallax value when the element is outside the viewport, to prevent unnecessary DOM and CSS updates. This can cause issues if combined with a high strength value. */
|
|
19
|
+
ignoreOutsideViewport: boolean;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
22
|
+
//# sourceMappingURL=parallax.d.mts.map
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// lib/parallax/parallax.ts
|
|
2
|
+
const entries = [];
|
|
3
|
+
const scrollMetrics = {
|
|
4
|
+
top: 0,
|
|
5
|
+
bottom: 0,
|
|
6
|
+
middle: 0,
|
|
7
|
+
left: 0,
|
|
8
|
+
right: 0,
|
|
9
|
+
center: 0
|
|
10
|
+
};
|
|
11
|
+
const getMiddle = (top, bottom) => (top + bottom) / 2;
|
|
12
|
+
function measureElement(element) {
|
|
13
|
+
const rect = element.getBoundingClientRect();
|
|
14
|
+
const top = scrollMetrics.top + rect.y;
|
|
15
|
+
const bottom = top + rect.height;
|
|
16
|
+
const left = scrollMetrics.left + rect.x;
|
|
17
|
+
const right = left + rect.width;
|
|
18
|
+
return {
|
|
19
|
+
top,
|
|
20
|
+
bottom,
|
|
21
|
+
middle: getMiddle(top, bottom),
|
|
22
|
+
left,
|
|
23
|
+
right,
|
|
24
|
+
center: getMiddle(left, right)
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function calculateEntryX(entry) {
|
|
28
|
+
switch (entry.anchors.x) {
|
|
29
|
+
case "viewport-left": return -scrollMetrics.left;
|
|
30
|
+
case "viewport-center": return -scrollMetrics.center;
|
|
31
|
+
case "viewport-right": return -scrollMetrics.right;
|
|
32
|
+
case "left": return entry.left - scrollMetrics.left;
|
|
33
|
+
case "center": return entry.center - scrollMetrics.center;
|
|
34
|
+
case "right": return entry.right - scrollMetrics.right;
|
|
35
|
+
case void 0: return 0;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function calculateEntryY(entry) {
|
|
39
|
+
switch (entry.anchors.y) {
|
|
40
|
+
case "viewport-top": return -scrollMetrics.top;
|
|
41
|
+
case "viewport-middle": return -scrollMetrics.middle;
|
|
42
|
+
case "viewport-bottom": return -scrollMetrics.bottom;
|
|
43
|
+
case "top": return entry.top - scrollMetrics.top;
|
|
44
|
+
case "middle": return entry.middle - scrollMetrics.middle;
|
|
45
|
+
case "bottom": return entry.bottom - scrollMetrics.bottom;
|
|
46
|
+
case void 0: return 0;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function setEntryAxis(entry, propertyName, calculateAxisAmount) {
|
|
50
|
+
let value;
|
|
51
|
+
if (propertyName == null || (value = calculateAxisAmount(entry)) == null) return;
|
|
52
|
+
const amount = value * entry.strength;
|
|
53
|
+
entry.element.style.setProperty(propertyName, String(amount));
|
|
54
|
+
}
|
|
55
|
+
function positionElement(entry) {
|
|
56
|
+
if (entry.ignoreOutsideViewport && (entry.bottom < scrollMetrics.top || entry.top > scrollMetrics.bottom)) return;
|
|
57
|
+
setEntryAxis(entry, entry.cssPropertyNames.x, calculateEntryX);
|
|
58
|
+
setEntryAxis(entry, entry.cssPropertyNames.y, calculateEntryY);
|
|
59
|
+
}
|
|
60
|
+
function handleResize() {
|
|
61
|
+
for (const entry of entries) {
|
|
62
|
+
Object.assign(entry, measureElement(entry.element));
|
|
63
|
+
positionElement(entry);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function handleScroll() {
|
|
67
|
+
scrollMetrics.top = window.scrollY;
|
|
68
|
+
scrollMetrics.bottom = window.scrollY + window.innerHeight;
|
|
69
|
+
scrollMetrics.middle = getMiddle(scrollMetrics.top, scrollMetrics.bottom);
|
|
70
|
+
scrollMetrics.left = window.scrollX;
|
|
71
|
+
scrollMetrics.right = window.scrollX + window.innerWidth;
|
|
72
|
+
scrollMetrics.center = getMiddle(scrollMetrics.left, scrollMetrics.right);
|
|
73
|
+
for (const entry of entries) positionElement(entry);
|
|
74
|
+
}
|
|
75
|
+
function trackParallaxScrolling(element, options) {
|
|
76
|
+
trackParallaxScrolling.setup();
|
|
77
|
+
const entry = {
|
|
78
|
+
element,
|
|
79
|
+
...options,
|
|
80
|
+
...measureElement(element)
|
|
81
|
+
};
|
|
82
|
+
entries.push(entry);
|
|
83
|
+
positionElement(entry);
|
|
84
|
+
}
|
|
85
|
+
trackParallaxScrolling.isSetup = false;
|
|
86
|
+
trackParallaxScrolling.setup = function setup() {
|
|
87
|
+
if (this.isSetup) return;
|
|
88
|
+
new ResizeObserver(handleResize).observe(document.documentElement);
|
|
89
|
+
handleResize();
|
|
90
|
+
window.addEventListener("scroll", handleScroll);
|
|
91
|
+
handleScroll();
|
|
92
|
+
this.isSetup = true;
|
|
93
|
+
};
|
|
94
|
+
export { trackParallaxScrolling as default };
|
|
95
|
+
|
|
96
|
+
//# sourceMappingURL=parallax.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parallax.mjs","names":[],"sources":["../../../lib/parallax/parallax.ts"],"sourcesContent":["type CssPropertyName = `--${string}`;\n\ntype AnchorX = 'left' | 'center' | 'right';\ntype AnchorY = 'top' | 'middle' | 'bottom';\n\ntype OrViewportAnchor<T extends string> = T | `viewport-${T}`;\n\nexport interface Options {\n /** The locations to measure an element’s relative parallax distance from. Unset axes will be ignored. */\n anchors: {\n x?: OrViewportAnchor<AnchorX> | undefined;\n y?: OrViewportAnchor<AnchorY> | undefined;\n };\n /** CSS custom property names to assign each element’s parallax values to. Unset axes will be ignored. */\n cssPropertyNames: {\n x?: CssPropertyName | undefined;\n y?: CssPropertyName | undefined;\n };\n /** The strength of the parallax effect. A larger value results in a greater parallax number at the extremes, while zero effectively disables the parallax effect. */\n strength: number;\n /** Whether to skip updates to the parallax value when the element is outside the viewport, to prevent unnecessary DOM and CSS updates. This can cause issues if combined with a high strength value. */\n ignoreOutsideViewport: boolean;\n}\n\ntype Metrics = Record<AnchorX | AnchorY, number>;\n\ninterface ParallaxEntry extends Metrics, Options {\n element: HTMLElement;\n}\n\nconst entries: ParallaxEntry[] = [];\n\nconst scrollMetrics: Metrics = {\n top: 0,\n bottom: 0,\n middle: 0,\n\n left: 0,\n right: 0,\n center: 0,\n};\n\nconst getMiddle = (top: number, bottom: number): number => (top + bottom) / 2;\n\nfunction measureElement(element: HTMLElement): Metrics {\n const rect = element.getBoundingClientRect();\n\n const top = scrollMetrics.top + rect.y;\n const bottom = top + rect.height;\n\n const left = scrollMetrics.left + rect.x;\n const right = left + rect.width;\n\n return {\n top,\n bottom,\n middle: getMiddle(top, bottom),\n left,\n right,\n center: getMiddle(left, right),\n };\n}\n\nfunction calculateEntryX(entry: ParallaxEntry): number | undefined {\n switch (entry.anchors.x) {\n case 'viewport-left': {\n return -scrollMetrics.left;\n }\n case 'viewport-center': {\n return -scrollMetrics.center;\n }\n case 'viewport-right': {\n return -scrollMetrics.right;\n }\n case 'left': {\n return entry.left - scrollMetrics.left;\n }\n case 'center': {\n return entry.center - scrollMetrics.center;\n }\n case 'right': {\n return entry.right - scrollMetrics.right;\n }\n case undefined: {\n return 0;\n }\n }\n}\n\nfunction calculateEntryY(entry: ParallaxEntry): number | undefined {\n switch (entry.anchors.y) {\n case 'viewport-top': {\n return -scrollMetrics.top;\n }\n case 'viewport-middle': {\n return -scrollMetrics.middle;\n }\n case 'viewport-bottom': {\n return -scrollMetrics.bottom;\n }\n case 'top': {\n return entry.top - scrollMetrics.top;\n }\n case 'middle': {\n return entry.middle - scrollMetrics.middle;\n }\n case 'bottom': {\n return entry.bottom - scrollMetrics.bottom;\n }\n case undefined: {\n return 0;\n }\n }\n}\n\nfunction setEntryAxis(\n entry: ParallaxEntry,\n propertyName: CssPropertyName | undefined,\n calculateAxisAmount: (entry: ParallaxEntry) => number | undefined,\n): void {\n let value: number | undefined;\n if (propertyName == null || (value = calculateAxisAmount(entry)) == null) {\n return;\n }\n const amount = value * entry.strength;\n entry.element.style.setProperty(propertyName, String(amount));\n}\n\nfunction positionElement(entry: ParallaxEntry): void {\n if (entry.ignoreOutsideViewport && (entry.bottom < scrollMetrics.top || entry.top > scrollMetrics.bottom)) {\n // Out of view\n return;\n }\n\n setEntryAxis(entry, entry.cssPropertyNames.x, calculateEntryX);\n setEntryAxis(entry, entry.cssPropertyNames.y, calculateEntryY);\n}\n\nfunction handleResize(): void {\n for (const entry of entries) {\n Object.assign(entry, measureElement(entry.element));\n positionElement(entry);\n }\n}\n\nfunction handleScroll(): void {\n scrollMetrics.top = window.scrollY;\n scrollMetrics.bottom = window.scrollY + window.innerHeight;\n scrollMetrics.middle = getMiddle(scrollMetrics.top, scrollMetrics.bottom);\n\n scrollMetrics.left = window.scrollX;\n scrollMetrics.right = window.scrollX + window.innerWidth;\n scrollMetrics.center = getMiddle(scrollMetrics.left, scrollMetrics.right);\n\n for (const entry of entries) {\n positionElement(entry);\n }\n}\n\nexport default function trackParallaxScrolling(element: HTMLElement, options: Options): void {\n trackParallaxScrolling.setup();\n\n const entry = { element, ...options, ...measureElement(element) } satisfies ParallaxEntry;\n entries.push(entry);\n positionElement(entry);\n}\ntrackParallaxScrolling.isSetup = false;\ntrackParallaxScrolling.setup = function setup(): void {\n if (this.isSetup) {\n return;\n }\n\n // Handle document resizes\n const resizeObserver = new ResizeObserver(handleResize);\n resizeObserver.observe(document.documentElement);\n handleResize();\n\n // Track scrolling\n window.addEventListener('scroll', handleScroll);\n handleScroll();\n\n this.isSetup = true;\n};\n"],"mappings":";AA8BA,MAAM,UAA2B,CAAC;AAElC,MAAM,gBAAyB;CAC7B,KAAK;CACL,QAAQ;CACR,QAAQ;CAER,MAAM;CACN,OAAO;CACP,QAAQ;AACV;AAEA,MAAM,aAAa,KAAa,YAA4B,MAAM,UAAU;AAE5E,SAAS,eAAe,SAA+B;CACrD,MAAM,OAAO,QAAQ,sBAAsB;CAE3C,MAAM,MAAM,cAAc,MAAM,KAAK;CACrC,MAAM,SAAS,MAAM,KAAK;CAE1B,MAAM,OAAO,cAAc,OAAO,KAAK;CACvC,MAAM,QAAQ,OAAO,KAAK;CAE1B,OAAO;EACL;EACA;EACA,QAAQ,UAAU,KAAK,MAAM;EAC7B;EACA;EACA,QAAQ,UAAU,MAAM,KAAK;CAC/B;AACF;AAEA,SAAS,gBAAgB,OAA0C;CACjE,QAAQ,MAAM,QAAQ,GAAtB;EACE,KAAK,iBACH,OAAO,CAAC,cAAc;EAExB,KAAK,mBACH,OAAO,CAAC,cAAc;EAExB,KAAK,kBACH,OAAO,CAAC,cAAc;EAExB,KAAK,QACH,OAAO,MAAM,OAAO,cAAc;EAEpC,KAAK,UACH,OAAO,MAAM,SAAS,cAAc;EAEtC,KAAK,SACH,OAAO,MAAM,QAAQ,cAAc;EAErC,KAAK,KAAA,GACH,OAAO;CAEX;AACF;AAEA,SAAS,gBAAgB,OAA0C;CACjE,QAAQ,MAAM,QAAQ,GAAtB;EACE,KAAK,gBACH,OAAO,CAAC,cAAc;EAExB,KAAK,mBACH,OAAO,CAAC,cAAc;EAExB,KAAK,mBACH,OAAO,CAAC,cAAc;EAExB,KAAK,OACH,OAAO,MAAM,MAAM,cAAc;EAEnC,KAAK,UACH,OAAO,MAAM,SAAS,cAAc;EAEtC,KAAK,UACH,OAAO,MAAM,SAAS,cAAc;EAEtC,KAAK,KAAA,GACH,OAAO;CAEX;AACF;AAEA,SAAS,aACP,OACA,cACA,qBACM;CACN,IAAI;CACJ,IAAI,gBAAgB,SAAS,QAAQ,oBAAoB,KAAK,MAAM,MAClE;CAEF,MAAM,SAAS,QAAQ,MAAM;CAC7B,MAAM,QAAQ,MAAM,YAAY,cAAc,OAAO,MAAM,CAAC;AAC9D;AAEA,SAAS,gBAAgB,OAA4B;CACnD,IAAI,MAAM,0BAA0B,MAAM,SAAS,cAAc,OAAO,MAAM,MAAM,cAAc,SAEhG;CAGF,aAAa,OAAO,MAAM,iBAAiB,GAAG,eAAe;CAC7D,aAAa,OAAO,MAAM,iBAAiB,GAAG,eAAe;AAC/D;AAEA,SAAS,eAAqB;CAC5B,KAAK,MAAM,SAAS,SAAS;EAC3B,OAAO,OAAO,OAAO,eAAe,MAAM,OAAO,CAAC;EAClD,gBAAgB,KAAK;CACvB;AACF;AAEA,SAAS,eAAqB;CAC5B,cAAc,MAAM,OAAO;CAC3B,cAAc,SAAS,OAAO,UAAU,OAAO;CAC/C,cAAc,SAAS,UAAU,cAAc,KAAK,cAAc,MAAM;CAExE,cAAc,OAAO,OAAO;CAC5B,cAAc,QAAQ,OAAO,UAAU,OAAO;CAC9C,cAAc,SAAS,UAAU,cAAc,MAAM,cAAc,KAAK;CAExE,KAAK,MAAM,SAAS,SAClB,gBAAgB,KAAK;AAEzB;AAEA,SAAwB,uBAAuB,SAAsB,SAAwB;CAC3F,uBAAuB,MAAM;CAE7B,MAAM,QAAQ;EAAE;EAAS,GAAG;EAAS,GAAG,eAAe,OAAO;CAAE;CAChE,QAAQ,KAAK,KAAK;CAClB,gBAAgB,KAAK;AACvB;AACA,uBAAuB,UAAU;AACjC,uBAAuB,QAAQ,SAAS,QAAc;CACpD,IAAI,KAAK,SACP;CAKF,IAD2B,eAAe,YAC7B,CAAC,CAAC,QAAQ,SAAS,eAAe;CAC/C,aAAa;CAGb,OAAO,iBAAiB,UAAU,YAAY;CAC9C,aAAa;CAEb,KAAK,UAAU;AACjB"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Options } from "./parallax/parallax.mjs";
|
|
2
|
+
import { RequiredOptions } from "./utils/types.mjs";
|
|
3
|
+
declare const defaults: {
|
|
4
|
+
anchors: {
|
|
5
|
+
x: "center";
|
|
6
|
+
y: "middle";
|
|
7
|
+
};
|
|
8
|
+
cssPropertyNames: {
|
|
9
|
+
y: "--parallax";
|
|
10
|
+
};
|
|
11
|
+
strength: number;
|
|
12
|
+
ignoreOutsideViewport: true;
|
|
13
|
+
};
|
|
14
|
+
export default function trackParallaxScrolling(element: HTMLElement, options?: RequiredOptions<Options, keyof typeof defaults>): void;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=parallax.d.mts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import trackParallaxScrolling$1 from "./parallax/parallax.mjs";
|
|
2
|
+
// lib/parallax.ts
|
|
3
|
+
const defaults = {
|
|
4
|
+
anchors: {
|
|
5
|
+
x: "center",
|
|
6
|
+
y: "middle"
|
|
7
|
+
},
|
|
8
|
+
cssPropertyNames: { y: "--parallax" },
|
|
9
|
+
strength: .5,
|
|
10
|
+
ignoreOutsideViewport: true
|
|
11
|
+
};
|
|
12
|
+
function trackParallaxScrolling(element, options = {}) {
|
|
13
|
+
trackParallaxScrolling$1(element, {
|
|
14
|
+
...defaults,
|
|
15
|
+
...options
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
export { trackParallaxScrolling as default };
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=parallax.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parallax.mjs","names":[],"sources":["../../lib/parallax.ts"],"sourcesContent":["import type { Options } from './parallax/parallax.ts';\nimport type { RequiredOptions } from './utils/types.ts';\n\nimport innerTrackParallaxScrolling from './parallax/parallax.ts';\n\nconst defaults = {\n anchors: {\n x: 'center',\n y: 'middle',\n },\n cssPropertyNames: {\n y: '--parallax',\n },\n strength: 0.5,\n ignoreOutsideViewport: true,\n} satisfies Partial<Options>;\n\nexport default function trackParallaxScrolling(\n element: HTMLElement,\n options: RequiredOptions<Options, keyof typeof defaults> = {},\n): void {\n innerTrackParallaxScrolling(element, { ...defaults, ...options });\n}\n"],"mappings":";;AAKA,MAAM,WAAW;CACf,SAAS;EACP,GAAG;EACH,GAAG;CACL;CACA,kBAAkB,EAChB,GAAG,aACL;CACA,UAAU;CACV,uBAAuB;AACzB;AAEA,SAAwB,uBACtB,SACA,UAA2D,CAAC,GACtD;CACN,yBAA4B,SAAS;EAAE,GAAG;EAAU,GAAG;CAAQ,CAAC;AAClE"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// lib/utils/intersection.ts
|
|
2
|
+
function onFirstIntersection(element, options, fn) {
|
|
3
|
+
const observer = new IntersectionObserver((entries, innerObserver) => {
|
|
4
|
+
const [entry] = entries;
|
|
5
|
+
if (entry.isIntersecting) {
|
|
6
|
+
innerObserver.unobserve(entry.target);
|
|
7
|
+
fn(entry.target);
|
|
8
|
+
}
|
|
9
|
+
}, options);
|
|
10
|
+
observer.observe(element);
|
|
11
|
+
return observer;
|
|
12
|
+
}
|
|
13
|
+
export { onFirstIntersection };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=intersection.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intersection.mjs","names":[],"sources":["../../../lib/utils/intersection.ts"],"sourcesContent":["export function onFirstIntersection<T extends HTMLElement>(\n element: T,\n options: IntersectionObserverInit | undefined,\n fn: (element: T) => void,\n): IntersectionObserver | undefined {\n const observer = new IntersectionObserver((entries, innerObserver) => {\n const [entry] = entries as [entry: IntersectionObserverEntry];\n if (entry.isIntersecting) {\n innerObserver.unobserve(entry.target);\n fn(entry.target as T);\n }\n }, options);\n observer.observe(element);\n return observer;\n}\n"],"mappings":";AAAA,SAAgB,oBACd,SACA,SACA,IACkC;CAClC,MAAM,WAAW,IAAI,sBAAsB,SAAS,kBAAkB;EACpE,MAAM,CAAC,SAAS;EAChB,IAAI,MAAM,gBAAgB;GACxB,cAAc,UAAU,MAAM,MAAM;GACpC,GAAG,MAAM,MAAW;EACtB;CACF,GAAG,OAAO;CACV,SAAS,QAAQ,OAAO;CACxB,OAAO;AACT"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type WrapSegmentOptions = Intl.SegmenterOptions & {
|
|
2
|
+
/** Locales for `Intl.Segmenter`. */
|
|
3
|
+
locales?: Intl.LocalesArgument;
|
|
4
|
+
/** Whether to merge adjacent segments not separated by whitespace (e.g. to combine actual words with punctuation for word segmentation). This will result in a single segment for locales that do not use whitespace (e.g. `ja-JP`) so should be set dependent on the locale. */
|
|
5
|
+
combineSegments?: boolean;
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=segments.d.mts.map
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// lib/utils/segments.ts
|
|
2
|
+
const patternWhitespace = /^\s+$/u;
|
|
3
|
+
const isWhitespace = (string) => patternWhitespace.test(string);
|
|
4
|
+
function wrapNodeWords(segmenter, combineSegments, node, wrapperTemplate) {
|
|
5
|
+
const text = node.textContent ?? "";
|
|
6
|
+
const newNodes = [];
|
|
7
|
+
const segments = [...segmenter.segment(text)];
|
|
8
|
+
for (let i = 0; i < segments.length; i += 1) {
|
|
9
|
+
const segment = segments[i]?.segment ?? "";
|
|
10
|
+
if (segment === "") continue;
|
|
11
|
+
if (isWhitespace(segment)) {
|
|
12
|
+
newNodes.push(new Text(segment));
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
const wrapper = wrapperTemplate.cloneNode(true);
|
|
16
|
+
wrapper.textContent = segment;
|
|
17
|
+
if (combineSegments) {
|
|
18
|
+
let nextSegment;
|
|
19
|
+
while (i < segments.length && (nextSegment = segments[i + 1]?.segment) != null && !isWhitespace(nextSegment)) {
|
|
20
|
+
wrapper.textContent += nextSegment;
|
|
21
|
+
i += 1;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
newNodes.push(wrapper);
|
|
25
|
+
}
|
|
26
|
+
const prefix = /^\s+/u.exec(text)?.[0];
|
|
27
|
+
const suffix = /\s+$/u.exec(text)?.[0];
|
|
28
|
+
if (prefix != null) newNodes.unshift(new Text(prefix));
|
|
29
|
+
if (suffix != null) newNodes.push(new Text(suffix));
|
|
30
|
+
return newNodes;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Wraps each word in the given element with a wrapping element, to allow styling individually.
|
|
34
|
+
*
|
|
35
|
+
* @param element An element to process segments within. Only segments within direct Text child nodes will be processed.
|
|
36
|
+
* @param wrapperTemplate An element that will be cloned for each segment. Each clone will have its `textContent` set to the segment text.
|
|
37
|
+
* @param root0 Segmentation options.
|
|
38
|
+
* @param root0.combineSegments Whether to merge adjacent segments not separated by whitespace (e.g. to combine actual words with punctuation for word segmentation). This will result in a single segment for locales that do not use whitespace (e.g. `ja-JP`) so should be set dependent on the locale.
|
|
39
|
+
* @param root0.locales Locales for `Intl.Segmenter`.
|
|
40
|
+
* @param root0.options All other standard options for `Intl.Segmenter`.
|
|
41
|
+
* @returns The wrappers for each segment.
|
|
42
|
+
*/
|
|
43
|
+
function wrapSegments(element, wrapperTemplate, { combineSegments = false, locales, ...options } = {}) {
|
|
44
|
+
if (typeof Intl === "undefined" || Intl.Segmenter == null) return;
|
|
45
|
+
const segmenter = new Intl.Segmenter(locales, options);
|
|
46
|
+
const nodes = [...element.childNodes];
|
|
47
|
+
const segments = [];
|
|
48
|
+
for (const node of nodes) if (node instanceof Text) {
|
|
49
|
+
for (const newNode of wrapNodeWords(segmenter, combineSegments, node, wrapperTemplate)) {
|
|
50
|
+
node.before(newNode);
|
|
51
|
+
if (newNode instanceof Element) segments.push(newNode);
|
|
52
|
+
}
|
|
53
|
+
node.remove();
|
|
54
|
+
}
|
|
55
|
+
return segments;
|
|
56
|
+
}
|
|
57
|
+
export { wrapSegments };
|
|
58
|
+
|
|
59
|
+
//# sourceMappingURL=segments.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"segments.mjs","names":[],"sources":["../../../lib/utils/segments.ts"],"sourcesContent":["const patternWhitespace = /^\\s+$/u;\nconst isWhitespace = (string: string): boolean => patternWhitespace.test(string);\n\nfunction wrapNodeWords(\n segmenter: Intl.Segmenter,\n combineSegments: boolean,\n node: Node,\n wrapperTemplate: Element,\n): Node[] {\n const text = node.textContent ?? '';\n const newNodes: Node[] = [];\n\n // Process each segment\n const segments = [...segmenter.segment(text)];\n for (let i = 0; i < segments.length; i += 1) {\n const segment = segments[i]?.segment ?? '';\n if (segment === '') {\n continue;\n }\n\n if (isWhitespace(segment)) {\n // Whitespace only\n newNodes.push(new Text(segment));\n continue;\n }\n\n // Non-whitespace – wrap\n const wrapper = wrapperTemplate.cloneNode(true) as HTMLElement;\n wrapper.textContent = segment;\n if (combineSegments) {\n // Combine all subsequent non-whitespace segments\n let nextSegment: string | undefined;\n while (i < segments.length && (nextSegment = segments[i + 1]?.segment) != null && !isWhitespace(nextSegment)) {\n wrapper.textContent += nextSegment;\n i += 1;\n }\n }\n newNodes.push(wrapper);\n }\n\n // Restore wrapping whitespace\n const prefix = /^\\s+/u.exec(text)?.[0];\n const suffix = /\\s+$/u.exec(text)?.[0];\n if (prefix != null) {\n newNodes.unshift(new Text(prefix));\n }\n if (suffix != null) {\n newNodes.push(new Text(suffix));\n }\n\n return newNodes;\n}\n\nexport type WrapSegmentOptions = Intl.SegmenterOptions & {\n /** Locales for `Intl.Segmenter`. */\n locales?: Intl.LocalesArgument;\n /** Whether to merge adjacent segments not separated by whitespace (e.g. to combine actual words with punctuation for word segmentation). This will result in a single segment for locales that do not use whitespace (e.g. `ja-JP`) so should be set dependent on the locale. */\n combineSegments?: boolean;\n};\n/**\n * Wraps each word in the given element with a wrapping element, to allow styling individually.\n *\n * @param element An element to process segments within. Only segments within direct Text child nodes will be processed.\n * @param wrapperTemplate An element that will be cloned for each segment. Each clone will have its `textContent` set to the segment text.\n * @param root0 Segmentation options.\n * @param root0.combineSegments Whether to merge adjacent segments not separated by whitespace (e.g. to combine actual words with punctuation for word segmentation). This will result in a single segment for locales that do not use whitespace (e.g. `ja-JP`) so should be set dependent on the locale.\n * @param root0.locales Locales for `Intl.Segmenter`.\n * @param root0.options All other standard options for `Intl.Segmenter`.\n * @returns The wrappers for each segment.\n */\nexport function wrapSegments<TWrapper extends Element>(\n element: Element,\n wrapperTemplate: TWrapper,\n { combineSegments = false, locales, ...options }: WrapSegmentOptions = {},\n): TWrapper[] | undefined {\n // oxlint-disable-next-line typescript/no-unnecessary-condition, typescript/prefer-optional-chain -- Gracefully support legacy browsers.\n if (typeof Intl === 'undefined' || Intl.Segmenter == null) {\n // Unsupported\n return undefined;\n }\n\n const segmenter = new Intl.Segmenter(locales, options);\n\n const nodes = [...element.childNodes]; // Must convert to a fixed array first as `childNodes` is a live list so will cause an infinite loop if iterated directly while adding nodes\n const segments: TWrapper[] = [];\n for (const node of nodes) {\n if (node instanceof Text) {\n // Prepend new processed nodes\n for (const newNode of wrapNodeWords(segmenter, combineSegments, node, wrapperTemplate)) {\n node.before(newNode);\n if (newNode instanceof Element) {\n segments.push(newNode as TWrapper);\n }\n }\n // Remove unprocessed node\n node.remove();\n }\n }\n return segments;\n}\n"],"mappings":";AAAA,MAAM,oBAAoB;AAC1B,MAAM,gBAAgB,WAA4B,kBAAkB,KAAK,MAAM;AAE/E,SAAS,cACP,WACA,iBACA,MACA,iBACQ;CACR,MAAM,OAAO,KAAK,eAAe;CACjC,MAAM,WAAmB,CAAC;CAG1B,MAAM,WAAW,CAAC,GAAG,UAAU,QAAQ,IAAI,CAAC;CAC5C,KAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK,GAAG;EAC3C,MAAM,UAAU,SAAS,EAAE,EAAE,WAAW;EACxC,IAAI,YAAY,IACd;EAGF,IAAI,aAAa,OAAO,GAAG;GAEzB,SAAS,KAAK,IAAI,KAAK,OAAO,CAAC;GAC/B;EACF;EAGA,MAAM,UAAU,gBAAgB,UAAU,IAAI;EAC9C,QAAQ,cAAc;EACtB,IAAI,iBAAiB;GAEnB,IAAI;GACJ,OAAO,IAAI,SAAS,WAAW,cAAc,SAAS,IAAI,EAAE,EAAE,YAAY,QAAQ,CAAC,aAAa,WAAW,GAAG;IAC5G,QAAQ,eAAe;IACvB,KAAK;GACP;EACF;EACA,SAAS,KAAK,OAAO;CACvB;CAGA,MAAM,SAAS,QAAQ,KAAK,IAAI,CAAC,GAAG;CACpC,MAAM,SAAS,QAAQ,KAAK,IAAI,CAAC,GAAG;CACpC,IAAI,UAAU,MACZ,SAAS,QAAQ,IAAI,KAAK,MAAM,CAAC;CAEnC,IAAI,UAAU,MACZ,SAAS,KAAK,IAAI,KAAK,MAAM,CAAC;CAGhC,OAAO;AACT;;;;;;;;;;;;AAmBA,SAAgB,aACd,SACA,iBACA,EAAE,kBAAkB,OAAO,SAAS,GAAG,YAAgC,CAAC,GAChD;CAExB,IAAI,OAAO,SAAS,eAAe,KAAK,aAAa,MAEnD;CAGF,MAAM,YAAY,IAAI,KAAK,UAAU,SAAS,OAAO;CAErD,MAAM,QAAQ,CAAC,GAAG,QAAQ,UAAU;CACpC,MAAM,WAAuB,CAAC;CAC9B,KAAK,MAAM,QAAQ,OACjB,IAAI,gBAAgB,MAAM;EAExB,KAAK,MAAM,WAAW,cAAc,WAAW,iBAAiB,MAAM,eAAe,GAAG;GACtF,KAAK,OAAO,OAAO;GACnB,IAAI,mBAAmB,SACrB,SAAS,KAAK,OAAmB;EAErC;EAEA,KAAK,OAAO;CACd;CAEF,OAAO;AACT"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// lib/utils/timing.ts
|
|
2
|
+
/**
|
|
3
|
+
* Applies a function to a collection of items, delaying each subsequent by a specified duration.
|
|
4
|
+
*
|
|
5
|
+
* @param items Items to apply a function to.
|
|
6
|
+
* @param stepDelay A duration to delay each subsequent call by (in milliseconds).
|
|
7
|
+
* @param fn A function that will be called with each item.
|
|
8
|
+
* @param onFinish A function to call after the final iteration.
|
|
9
|
+
*/
|
|
10
|
+
function stagger(items, stepDelay, fn, onFinish) {
|
|
11
|
+
let index = 0;
|
|
12
|
+
let lastIndex = index;
|
|
13
|
+
for (const item of items) {
|
|
14
|
+
const thisIndex = index;
|
|
15
|
+
lastIndex = index;
|
|
16
|
+
setTimeout(() => {
|
|
17
|
+
fn(item, thisIndex);
|
|
18
|
+
if (thisIndex === lastIndex) onFinish();
|
|
19
|
+
}, index * stepDelay);
|
|
20
|
+
index += 1;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export { stagger };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=timing.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timing.mjs","names":[],"sources":["../../../lib/utils/timing.ts"],"sourcesContent":["/**\n * Applies a function to a collection of items, delaying each subsequent by a specified duration.\n *\n * @param items Items to apply a function to.\n * @param stepDelay A duration to delay each subsequent call by (in milliseconds).\n * @param fn A function that will be called with each item.\n * @param onFinish A function to call after the final iteration.\n */\nexport function stagger<T>(\n items: Iterable<T>,\n stepDelay: number,\n fn: (item: T, index: number) => void,\n onFinish: () => void,\n): void {\n let index = 0;\n let lastIndex = index;\n for (const item of items) {\n const thisIndex = index;\n lastIndex = index;\n // oxlint-disable-next-line eslint/no-loop-func -- The lastIndex value is _supposed_ to change.\n setTimeout(() => {\n fn(item, thisIndex);\n if (thisIndex === lastIndex) {\n onFinish();\n }\n }, index * stepDelay);\n index += 1;\n }\n}\n"],"mappings":";;;;;;;;;AAQA,SAAgB,QACd,OACA,WACA,IACA,UACM;CACN,IAAI,QAAQ;CACZ,IAAI,YAAY;CAChB,KAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,YAAY;EAClB,YAAY;EAEZ,iBAAiB;GACf,GAAG,MAAM,SAAS;GAClB,IAAI,cAAc,WAChB,SAAS;EAEb,GAAG,QAAQ,SAAS;EACpB,SAAS;CACX;AACF"}
|
package/package.json
CHANGED
|
@@ -1,35 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@averay/scroll-effects",
|
|
3
|
+
"version": "1.0.2",
|
|
3
4
|
"description": "Utilities to manage common browser scroll effects.",
|
|
5
|
+
"homepage": "https://codeberg.org/adamaveray/browser-scroll-effects",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://codeberg.org/adamaveray/browser-scroll-effects/issues"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
4
10
|
"author": {
|
|
5
11
|
"name": "Adam Averay",
|
|
6
12
|
"url": "https://adamaveray.au"
|
|
7
13
|
},
|
|
8
|
-
"
|
|
9
|
-
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://codeberg.org/adamaveray/browser-scroll-effects.git"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"/dist"
|
|
20
|
+
],
|
|
10
21
|
"type": "module",
|
|
11
22
|
"sideEffects": false,
|
|
12
23
|
"exports": {
|
|
13
|
-
".":
|
|
14
|
-
|
|
15
|
-
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.mts",
|
|
26
|
+
"default": "./dist/index.mjs"
|
|
27
|
+
},
|
|
28
|
+
"./entrances": {
|
|
29
|
+
"types": "./dist/lib/entrances.d.mts",
|
|
30
|
+
"default": "./dist/lib/entrances.mjs"
|
|
31
|
+
},
|
|
32
|
+
"./parallax": {
|
|
33
|
+
"types": "./dist/lib/parallax.d.mts",
|
|
34
|
+
"default": "./dist/lib/parallax.mjs"
|
|
35
|
+
}
|
|
16
36
|
},
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@averay/codeformat": "^0.4.8",
|
|
39
|
+
"bumpp": "^12.3.0",
|
|
40
|
+
"knip": "^6.48.0",
|
|
41
|
+
"typescript": "^7.0.2",
|
|
42
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@8.3.8000",
|
|
43
|
+
"vite-plus": "1.7.0"
|
|
24
44
|
},
|
|
25
|
-
"
|
|
26
|
-
"
|
|
45
|
+
"devEngines": {
|
|
46
|
+
"packageManager": {
|
|
47
|
+
"name": "pnpm",
|
|
48
|
+
"version": "12.21.0",
|
|
49
|
+
"onFail": "download"
|
|
50
|
+
}
|
|
27
51
|
},
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "vp pack",
|
|
54
|
+
"format": "vp exec codeformat fix",
|
|
55
|
+
"lint": "vp run lint:codeformat",
|
|
56
|
+
"lint:codeformat": "vp exec codeformat check",
|
|
57
|
+
"release": "vp exec bumpp && vp pm publish --access public"
|
|
34
58
|
}
|
|
35
|
-
}
|
|
59
|
+
}
|
package/index.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { onFirstIntersection } from '../utils/intersection.ts';
|
|
2
|
-
|
|
3
|
-
export interface ElementOptions {
|
|
4
|
-
/** A class to add to an element when it has entered the viewport. */
|
|
5
|
-
classEntered: string;
|
|
6
|
-
/** Options to pass to the IntersectionObserver instance used to monitor entrance. */
|
|
7
|
-
intersectionOptions?: IntersectionObserverInit;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function enterElement(element: Element, className: ElementOptions['classEntered']): void {
|
|
11
|
-
element.classList.add(className);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default function setupElementEntrance(
|
|
15
|
-
element: HTMLElement,
|
|
16
|
-
{ classEntered, intersectionOptions }: ElementOptions,
|
|
17
|
-
): void {
|
|
18
|
-
onFirstIntersection(element, intersectionOptions, (target) => {
|
|
19
|
-
enterElement(target, classEntered);
|
|
20
|
-
});
|
|
21
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { onFirstIntersection } from '../utils/intersection.ts';
|
|
2
|
-
import { wrapSegments } from '../utils/segments.ts';
|
|
3
|
-
import type { WrapSegmentOptions } from '../utils/segments.ts';
|
|
4
|
-
import { stagger } from '../utils/timing.ts';
|
|
5
|
-
|
|
6
|
-
import type { ElementOptions } from './elements.ts';
|
|
7
|
-
import { enterElement } from './elements.ts';
|
|
8
|
-
|
|
9
|
-
export interface SegmentsOptions extends ElementOptions, WrapSegmentOptions {
|
|
10
|
-
/** A duration in milliseconds to delay before entering the next segment within an element. */
|
|
11
|
-
stepDelay: number;
|
|
12
|
-
/** A class to add to elements with segments (regardless of whether the elements have entered or not). */
|
|
13
|
-
classHasSegments: string;
|
|
14
|
-
/** A class to add to an element after all its segments have finished entering. */
|
|
15
|
-
classFinished: string;
|
|
16
|
-
/** An element to wrap each segment in. The template element will be cloned for each segment, and the segment will be appended to it. */
|
|
17
|
-
segmentWrapperTemplate: HTMLElement;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function enterSegments<T extends HTMLElement>(
|
|
21
|
-
segments: T[],
|
|
22
|
-
stepDelay: number,
|
|
23
|
-
onEnter: (segment: T, index: number) => void,
|
|
24
|
-
onLast: () => void,
|
|
25
|
-
): void {
|
|
26
|
-
stagger(segments, stepDelay, onEnter, onLast);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export default function setupSegmentsEntrance(
|
|
30
|
-
element: HTMLElement,
|
|
31
|
-
{
|
|
32
|
-
classHasSegments,
|
|
33
|
-
classEntered,
|
|
34
|
-
classFinished,
|
|
35
|
-
stepDelay,
|
|
36
|
-
segmentWrapperTemplate,
|
|
37
|
-
intersectionOptions,
|
|
38
|
-
...options
|
|
39
|
-
}: SegmentsOptions,
|
|
40
|
-
): void {
|
|
41
|
-
// Process element
|
|
42
|
-
const segments = wrapSegments(element, segmentWrapperTemplate, options) ?? [];
|
|
43
|
-
|
|
44
|
-
// Set up entrance monitoring
|
|
45
|
-
onFirstIntersection(element, intersectionOptions, () => {
|
|
46
|
-
enterElement(element, classEntered);
|
|
47
|
-
|
|
48
|
-
enterSegments(
|
|
49
|
-
segments,
|
|
50
|
-
stepDelay,
|
|
51
|
-
(segment) => {
|
|
52
|
-
enterElement(segment, classEntered);
|
|
53
|
-
},
|
|
54
|
-
() => {
|
|
55
|
-
element.classList.add(classFinished);
|
|
56
|
-
},
|
|
57
|
-
);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
// Add configured attributes
|
|
61
|
-
element.classList.add(classHasSegments);
|
|
62
|
-
}
|
package/lib/entrances.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import setupElementEntrance from './entrances/elements.ts';
|
|
2
|
-
import type { ElementOptions } from './entrances/elements.ts';
|
|
3
|
-
import setupSegmentsEntrance from './entrances/segments.ts';
|
|
4
|
-
import type { SegmentsOptions } from './entrances/segments.ts';
|
|
5
|
-
import type { RequiredOptions } from './utils/types.ts';
|
|
6
|
-
|
|
7
|
-
type Options = ElementOptions & SegmentsOptions;
|
|
8
|
-
|
|
9
|
-
const defaults = {
|
|
10
|
-
classHasSegments: '__segmented',
|
|
11
|
-
classEntered: '__entered',
|
|
12
|
-
classFinished: '__finished',
|
|
13
|
-
|
|
14
|
-
segmentWrapperTemplate: (() => {
|
|
15
|
-
const template = document.createElement('span');
|
|
16
|
-
template.className = 'segment';
|
|
17
|
-
template.setAttribute('role', 'text');
|
|
18
|
-
return template;
|
|
19
|
-
})(),
|
|
20
|
-
} satisfies Partial<Options>;
|
|
21
|
-
|
|
22
|
-
export function element(
|
|
23
|
-
target: HTMLElement,
|
|
24
|
-
options: RequiredOptions<ElementOptions, keyof typeof defaults & keyof ElementOptions> = {},
|
|
25
|
-
): void {
|
|
26
|
-
setupElementEntrance(target, { ...defaults, ...options });
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function segments(
|
|
30
|
-
target: HTMLElement,
|
|
31
|
-
options: RequiredOptions<SegmentsOptions, keyof typeof defaults & keyof SegmentsOptions>,
|
|
32
|
-
): void {
|
|
33
|
-
setupSegmentsEntrance(target, { ...defaults, ...options });
|
|
34
|
-
}
|
package/lib/parallax/parallax.ts
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
type CssPropertyName = `--${string}`;
|
|
2
|
-
|
|
3
|
-
type AnchorX = 'left' | 'center' | 'right';
|
|
4
|
-
type AnchorY = 'top' | 'middle' | 'bottom';
|
|
5
|
-
|
|
6
|
-
type OrViewportAnchor<T extends string> = T | `viewport-${T}`;
|
|
7
|
-
|
|
8
|
-
export interface Options {
|
|
9
|
-
/** The locations to measure an element’s relative parallax distance from. Unset axes will be ignored. */
|
|
10
|
-
anchors: {
|
|
11
|
-
x?: OrViewportAnchor<AnchorX> | undefined;
|
|
12
|
-
y?: OrViewportAnchor<AnchorY> | undefined;
|
|
13
|
-
};
|
|
14
|
-
/** CSS custom property names to assign each element’s parallax values to. Unset axes will be ignored. */
|
|
15
|
-
cssPropertyNames: {
|
|
16
|
-
x?: CssPropertyName | undefined;
|
|
17
|
-
y?: CssPropertyName | undefined;
|
|
18
|
-
};
|
|
19
|
-
/** The strength of the parallax effect. A larger value results in a greater parallax number at the extremes, while zero effectively disables the parallax effect. */
|
|
20
|
-
strength: number;
|
|
21
|
-
/** Whether to skip updates to the parallax value when the element is outside the viewport, to prevent unnecessary DOM and CSS updates. This can cause issues if combined with a high strength value. */
|
|
22
|
-
ignoreOutsideViewport: boolean;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
type Metrics = Record<AnchorX | AnchorY, number>;
|
|
26
|
-
|
|
27
|
-
interface ParallaxEntry extends Metrics, Options {
|
|
28
|
-
element: HTMLElement;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const entries: ParallaxEntry[] = [];
|
|
32
|
-
|
|
33
|
-
const scrollMetrics: Metrics = {
|
|
34
|
-
top: 0,
|
|
35
|
-
bottom: 0,
|
|
36
|
-
middle: 0,
|
|
37
|
-
|
|
38
|
-
left: 0,
|
|
39
|
-
right: 0,
|
|
40
|
-
center: 0,
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const getMiddle = (top: number, bottom: number): number => (top + bottom) / 2;
|
|
44
|
-
|
|
45
|
-
function measureElement(element: HTMLElement): Metrics {
|
|
46
|
-
const rect = element.getBoundingClientRect();
|
|
47
|
-
|
|
48
|
-
const top = scrollMetrics.top + rect.y;
|
|
49
|
-
const bottom = top + rect.height;
|
|
50
|
-
|
|
51
|
-
const left = scrollMetrics.left + rect.x;
|
|
52
|
-
const right = left + rect.width;
|
|
53
|
-
|
|
54
|
-
return {
|
|
55
|
-
top,
|
|
56
|
-
bottom,
|
|
57
|
-
middle: getMiddle(top, bottom),
|
|
58
|
-
left,
|
|
59
|
-
right,
|
|
60
|
-
center: getMiddle(left, right),
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function calculateEntryX(entry: ParallaxEntry): number | undefined {
|
|
65
|
-
switch (entry.anchors.x) {
|
|
66
|
-
case 'viewport-left': {
|
|
67
|
-
return -scrollMetrics.left;
|
|
68
|
-
}
|
|
69
|
-
case 'viewport-center': {
|
|
70
|
-
return -scrollMetrics.center;
|
|
71
|
-
}
|
|
72
|
-
case 'viewport-right': {
|
|
73
|
-
return -scrollMetrics.right;
|
|
74
|
-
}
|
|
75
|
-
case 'left': {
|
|
76
|
-
return entry.left - scrollMetrics.left;
|
|
77
|
-
}
|
|
78
|
-
case 'center': {
|
|
79
|
-
return entry.center - scrollMetrics.center;
|
|
80
|
-
}
|
|
81
|
-
case 'right': {
|
|
82
|
-
return entry.right - scrollMetrics.right;
|
|
83
|
-
}
|
|
84
|
-
case undefined: {
|
|
85
|
-
return 0;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function calculateEntryY(entry: ParallaxEntry): number | undefined {
|
|
91
|
-
switch (entry.anchors.y) {
|
|
92
|
-
case 'viewport-top': {
|
|
93
|
-
return -scrollMetrics.top;
|
|
94
|
-
}
|
|
95
|
-
case 'viewport-middle': {
|
|
96
|
-
return -scrollMetrics.middle;
|
|
97
|
-
}
|
|
98
|
-
case 'viewport-bottom': {
|
|
99
|
-
return -scrollMetrics.bottom;
|
|
100
|
-
}
|
|
101
|
-
case 'top': {
|
|
102
|
-
return entry.top - scrollMetrics.top;
|
|
103
|
-
}
|
|
104
|
-
case 'middle': {
|
|
105
|
-
return entry.middle - scrollMetrics.middle;
|
|
106
|
-
}
|
|
107
|
-
case 'bottom': {
|
|
108
|
-
return entry.bottom - scrollMetrics.bottom;
|
|
109
|
-
}
|
|
110
|
-
case undefined: {
|
|
111
|
-
return 0;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function setEntryAxis(
|
|
117
|
-
entry: ParallaxEntry,
|
|
118
|
-
propertyName: CssPropertyName | undefined,
|
|
119
|
-
calculateAxisAmount: (entry: ParallaxEntry) => number | undefined,
|
|
120
|
-
): void {
|
|
121
|
-
let value: number | undefined;
|
|
122
|
-
if (propertyName == null || (value = calculateAxisAmount(entry)) == null) {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
const amount = value * entry.strength;
|
|
126
|
-
entry.element.style.setProperty(propertyName, String(amount));
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function positionElement(entry: ParallaxEntry): void {
|
|
130
|
-
if (entry.ignoreOutsideViewport && (entry.bottom < scrollMetrics.top || entry.top > scrollMetrics.bottom)) {
|
|
131
|
-
// Out of view
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
setEntryAxis(entry, entry.cssPropertyNames.x, calculateEntryX);
|
|
136
|
-
setEntryAxis(entry, entry.cssPropertyNames.y, calculateEntryY);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function handleResize(): void {
|
|
140
|
-
for (const entry of entries) {
|
|
141
|
-
Object.assign(entry, measureElement(entry.element));
|
|
142
|
-
positionElement(entry);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function handleScroll(): void {
|
|
147
|
-
scrollMetrics.top = window.scrollY;
|
|
148
|
-
scrollMetrics.bottom = window.scrollY + window.innerHeight;
|
|
149
|
-
scrollMetrics.middle = getMiddle(scrollMetrics.top, scrollMetrics.bottom);
|
|
150
|
-
|
|
151
|
-
scrollMetrics.left = window.scrollX;
|
|
152
|
-
scrollMetrics.right = window.scrollX + window.innerWidth;
|
|
153
|
-
scrollMetrics.center = getMiddle(scrollMetrics.left, scrollMetrics.right);
|
|
154
|
-
|
|
155
|
-
for (const entry of entries) {
|
|
156
|
-
positionElement(entry);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export default function trackParallaxScrolling(element: HTMLElement, options: Options): void {
|
|
161
|
-
trackParallaxScrolling.setup();
|
|
162
|
-
|
|
163
|
-
const entry = { element, ...options, ...measureElement(element) } satisfies ParallaxEntry;
|
|
164
|
-
entries.push(entry);
|
|
165
|
-
positionElement(entry);
|
|
166
|
-
}
|
|
167
|
-
trackParallaxScrolling.isSetup = false;
|
|
168
|
-
trackParallaxScrolling.setup = function setupParallax(): void {
|
|
169
|
-
if (this.isSetup) {
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// Handle document resizes
|
|
174
|
-
const resizeObserver = new ResizeObserver(handleResize);
|
|
175
|
-
resizeObserver.observe(document.documentElement);
|
|
176
|
-
handleResize();
|
|
177
|
-
|
|
178
|
-
// Track scrolling
|
|
179
|
-
window.addEventListener('scroll', handleScroll);
|
|
180
|
-
handleScroll();
|
|
181
|
-
|
|
182
|
-
this.isSetup = true;
|
|
183
|
-
};
|
package/lib/parallax.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import innerTrackParallaxScrolling from './parallax/parallax.ts';
|
|
2
|
-
import type { Options } from './parallax/parallax.ts';
|
|
3
|
-
import type { RequiredOptions } from './utils/types.ts';
|
|
4
|
-
|
|
5
|
-
const defaults = {
|
|
6
|
-
anchors: {
|
|
7
|
-
x: 'center',
|
|
8
|
-
y: 'middle',
|
|
9
|
-
},
|
|
10
|
-
cssPropertyNames: {
|
|
11
|
-
y: '--parallax',
|
|
12
|
-
},
|
|
13
|
-
strength: 0.5, // eslint-disable-line @typescript-eslint/no-magic-numbers -- Default value.
|
|
14
|
-
ignoreOutsideViewport: true,
|
|
15
|
-
} satisfies Partial<Options>;
|
|
16
|
-
|
|
17
|
-
export default function trackParallaxScrolling(
|
|
18
|
-
element: HTMLElement,
|
|
19
|
-
options: RequiredOptions<Options, keyof typeof defaults> = {},
|
|
20
|
-
): void {
|
|
21
|
-
innerTrackParallaxScrolling(element, { ...defaults, ...options });
|
|
22
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export function onFirstIntersection<T extends HTMLElement>(
|
|
2
|
-
element: T,
|
|
3
|
-
options: IntersectionObserverInit | undefined,
|
|
4
|
-
fn: (element: T) => void,
|
|
5
|
-
): IntersectionObserver | undefined {
|
|
6
|
-
const observer = new IntersectionObserver((entries, innerObserver) => {
|
|
7
|
-
const [entry] = entries as [entry: IntersectionObserverEntry];
|
|
8
|
-
if (entry.isIntersecting) {
|
|
9
|
-
innerObserver.unobserve(entry.target);
|
|
10
|
-
fn(entry.target as T);
|
|
11
|
-
}
|
|
12
|
-
}, options);
|
|
13
|
-
observer.observe(element);
|
|
14
|
-
return observer;
|
|
15
|
-
}
|
package/lib/utils/segments.ts
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
const patternWhitespace = /^\s+$/u;
|
|
2
|
-
const isWhitespace = (string: string): boolean => patternWhitespace.test(string);
|
|
3
|
-
|
|
4
|
-
function wrapNodeWords(
|
|
5
|
-
segmenter: Intl.Segmenter,
|
|
6
|
-
combineSegments: boolean,
|
|
7
|
-
node: Node,
|
|
8
|
-
wrapperTemplate: Element,
|
|
9
|
-
): Node[] {
|
|
10
|
-
const text = node.textContent ?? '';
|
|
11
|
-
const newNodes: Node[] = [];
|
|
12
|
-
|
|
13
|
-
// Process each segment
|
|
14
|
-
const segments = [...segmenter.segment(text)];
|
|
15
|
-
for (let i = 0; i < segments.length; i += 1) {
|
|
16
|
-
const segment = segments[i]?.segment ?? '';
|
|
17
|
-
if (segment === '') {
|
|
18
|
-
continue;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (isWhitespace(segment)) {
|
|
22
|
-
// Whitespace only
|
|
23
|
-
newNodes.push(new Text(segment));
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Non-whitespace – wrap
|
|
28
|
-
const wrapper = wrapperTemplate.cloneNode(true) as HTMLElement;
|
|
29
|
-
wrapper.textContent = segment;
|
|
30
|
-
if (combineSegments) {
|
|
31
|
-
// Combine all subsequent non-whitespace segments
|
|
32
|
-
let nextSegment: string | undefined;
|
|
33
|
-
while (i < segments.length && (nextSegment = segments[i + 1]?.segment) != null && !isWhitespace(nextSegment)) {
|
|
34
|
-
wrapper.textContent += nextSegment;
|
|
35
|
-
// eslint-disable-next-line sonarjs/updated-loop-counter -- Need to advance outer loop.
|
|
36
|
-
i += 1;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
newNodes.push(wrapper);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Restore wrapping whitespace
|
|
43
|
-
/* eslint-disable sonarjs/slow-regex -- Best way to access surrounding whitespace. */
|
|
44
|
-
const prefix = text.match(/^\s+/u)?.[0];
|
|
45
|
-
const suffix = text.match(/\s+$/u)?.[0];
|
|
46
|
-
/* eslint-enable sonarjs/slow-regex -- Best way to access surrounding whitespace. */
|
|
47
|
-
if (prefix != null) {
|
|
48
|
-
newNodes.unshift(new Text(prefix));
|
|
49
|
-
}
|
|
50
|
-
if (suffix != null) {
|
|
51
|
-
newNodes.push(new Text(suffix));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return newNodes;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export type WrapSegmentOptions = Intl.SegmenterOptions & {
|
|
58
|
-
/** Locales for `Intl.Segmenter`. */
|
|
59
|
-
locales?: Intl.LocalesArgument;
|
|
60
|
-
/** Whether to merge adjacent segments not separated by whitespace (e.g. to combine actual words with punctuation for word segmentation). This will result in a single segment for locales that do not use whitespace (e.g. `ja-JP`) so should be set dependent on the locale. */
|
|
61
|
-
combineSegments?: boolean;
|
|
62
|
-
};
|
|
63
|
-
/**
|
|
64
|
-
* Wraps each word in the given element with a wrapping element, to allow styling individually.
|
|
65
|
-
*
|
|
66
|
-
* @param element An element to process segments within. Only segments within direct Text child nodes will be processed.
|
|
67
|
-
* @param wrapperTemplate An element that will be cloned for each segment. Each clone will have its `textContent` set to the segment text.
|
|
68
|
-
* @param root0 Segmentation options.
|
|
69
|
-
* @param root0.combineSegments Whether to merge adjacent segments not separated by whitespace (e.g. to combine actual words with punctuation for word segmentation). This will result in a single segment for locales that do not use whitespace (e.g. `ja-JP`) so should be set dependent on the locale.
|
|
70
|
-
* @param root0.locales Locales for `Intl.Segmenter`.
|
|
71
|
-
* @param root0.options All other standard options for `Intl.Segmenter`.
|
|
72
|
-
* @returns The wrappers for each segment.
|
|
73
|
-
*/
|
|
74
|
-
export function wrapSegments<TWrapper extends Element>(
|
|
75
|
-
element: Element,
|
|
76
|
-
wrapperTemplate: TWrapper,
|
|
77
|
-
{ combineSegments = false, locales, ...options }: WrapSegmentOptions = {},
|
|
78
|
-
): TWrapper[] | undefined {
|
|
79
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- Gracefully support legacy browsers.
|
|
80
|
-
if (typeof Intl === 'undefined' || Intl.Segmenter == null) {
|
|
81
|
-
// Unsupported
|
|
82
|
-
return undefined;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const segmenter = new Intl.Segmenter(locales, options);
|
|
86
|
-
|
|
87
|
-
const nodes = [...element.childNodes]; // Must convert to a fixed array first as `childNodes` is a live list so will cause an infinite loop if iterated directly while adding nodes
|
|
88
|
-
const segments: TWrapper[] = [];
|
|
89
|
-
for (const node of nodes) {
|
|
90
|
-
if (node instanceof Text) {
|
|
91
|
-
// Prepend new processed nodes
|
|
92
|
-
for (const newNode of wrapNodeWords(segmenter, combineSegments, node, wrapperTemplate)) {
|
|
93
|
-
node.before(newNode);
|
|
94
|
-
segments.push(newNode as TWrapper);
|
|
95
|
-
}
|
|
96
|
-
// Remove unprocessed node
|
|
97
|
-
node.remove();
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return segments;
|
|
101
|
-
}
|
package/lib/utils/timing.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Applies a function to a collection of items, delaying each subsequent by a specified duration.
|
|
3
|
-
*
|
|
4
|
-
* @param items Items to apply a function to.
|
|
5
|
-
* @param stepDelay A duration to delay each subsequent call by (in milliseconds).
|
|
6
|
-
* @param fn A function that will be called with each item.
|
|
7
|
-
* @param onFinish A function to call after the final iteration.
|
|
8
|
-
*/
|
|
9
|
-
export function stagger<T>(
|
|
10
|
-
items: Iterable<T>,
|
|
11
|
-
stepDelay: number,
|
|
12
|
-
fn: (item: T, index: number) => void,
|
|
13
|
-
onFinish: () => void,
|
|
14
|
-
): void {
|
|
15
|
-
let index = 0;
|
|
16
|
-
let lastIndex = index;
|
|
17
|
-
for (const item of items) {
|
|
18
|
-
const thisIndex = index;
|
|
19
|
-
lastIndex = index;
|
|
20
|
-
// eslint-disable-next-line @typescript-eslint/no-loop-func -- The lastIndex value is _supposed_ to change.
|
|
21
|
-
setTimeout(() => {
|
|
22
|
-
fn(item, thisIndex);
|
|
23
|
-
if (thisIndex === lastIndex) {
|
|
24
|
-
onFinish();
|
|
25
|
-
}
|
|
26
|
-
}, index * stepDelay);
|
|
27
|
-
index += 1;
|
|
28
|
-
}
|
|
29
|
-
}
|
package/lib/utils/types.ts
DELETED
package/tsconfig.json
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"exclude": ["*.mjs"],
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
// Environment setup & latest features
|
|
5
|
-
"lib": ["DOM", "DOM.Iterable", "ESNext"],
|
|
6
|
-
"target": "ESNext",
|
|
7
|
-
"module": "ESNext",
|
|
8
|
-
"moduleDetection": "force",
|
|
9
|
-
"allowJs": true,
|
|
10
|
-
|
|
11
|
-
// Bundler mode
|
|
12
|
-
"moduleResolution": "bundler",
|
|
13
|
-
"allowImportingTsExtensions": true,
|
|
14
|
-
"verbatimModuleSyntax": true,
|
|
15
|
-
"noEmit": true,
|
|
16
|
-
|
|
17
|
-
// Strictness
|
|
18
|
-
"skipLibCheck": true,
|
|
19
|
-
"strict": true,
|
|
20
|
-
"allowUnreachableCode": false,
|
|
21
|
-
"allowUnusedLabels": false,
|
|
22
|
-
"exactOptionalPropertyTypes": true,
|
|
23
|
-
"noFallthroughCasesInSwitch": true,
|
|
24
|
-
"noImplicitOverride": true,
|
|
25
|
-
"noImplicitReturns": true,
|
|
26
|
-
"noPropertyAccessFromIndexSignature": true,
|
|
27
|
-
"noUncheckedIndexedAccess": true
|
|
28
|
-
}
|
|
29
|
-
}
|