@smartimpact-it/scroll-utils 1.1.0 → 1.1.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/ScrollOffset.ts +16 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smartimpact-it/scroll-utils",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "A library for scroll-related utils.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -4,24 +4,24 @@ import isFunction from 'lodash/isFunction';
4
4
  export type ScrollOffsetPartSettings = {
5
5
  name?: string;
6
6
  selectors?: string[];
7
- elements?: HTMLElement[];
7
+ elements?: (HTMLElement | null | undefined)[];
8
8
  fixedHeight?: number | false;
9
9
  condition?: Function | false;
10
10
  resizeCondition?: Function | false;
11
11
  };
12
12
 
13
- export type ScrollOffsetVariable = {
13
+ export type ScrollOffsetVariable<TPartName extends string = string> = {
14
14
  name: string;
15
- offsetParts: Array<string | ScrollOffsetPart>;
15
+ offsetParts: Array<NoInfer<TPartName> | ScrollOffsetPart>;
16
16
  };
17
17
 
18
- export type ScrollOffsetMap = {
19
- [key: string]: Array<string | ScrollOffsetPart>;
18
+ export type ScrollOffsetMap<TPartName extends string = string> = {
19
+ [key: string]: Array<NoInfer<TPartName> | ScrollOffsetPart>;
20
20
  };
21
21
 
22
- export type ScrollOffsetSettings = {
23
- offsetParts?: ScrollOffsetPart[] | ScrollOffsetPartSettings[];
24
- variables?: ScrollOffsetVariable[] | ScrollOffsetMap;
22
+ export type ScrollOffsetSettings<TPartName extends string = string> = {
23
+ offsetParts?: (ScrollOffsetPart | (Omit<ScrollOffsetPartSettings, 'name'> & { name?: TPartName }))[];
24
+ variables?: ScrollOffsetVariable<TPartName>[] | ScrollOffsetMap<TPartName>;
25
25
  extraEvents?: string[];
26
26
  registerForHoudini?: boolean;
27
27
  useResizeObserver?: boolean;
@@ -30,7 +30,7 @@ export type ScrollOffsetSettings = {
30
30
  export class ScrollOffsetPart {
31
31
  #name: string;
32
32
  #selectors: string[];
33
- #elements: HTMLElement[];
33
+ #elements: (HTMLElement | null | undefined)[];
34
34
  #fixedHeight: number | false;
35
35
  #condition: Function | false;
36
36
  #resizeCondition: Function | false;
@@ -50,7 +50,7 @@ export class ScrollOffsetPart {
50
50
  }: ScrollOffsetPartSettings) {
51
51
  this.#name = name;
52
52
  this.#selectors = (selectors || []).filter(Boolean);
53
- this.#elements = (elements || []).filter(Boolean);
53
+ this.#elements = elements || [];
54
54
  this.#fixedHeight = fixedHeight;
55
55
  this.#condition = condition;
56
56
  this.#resizeCondition = resizeCondition;
@@ -78,7 +78,7 @@ export class ScrollOffsetPart {
78
78
  }
79
79
 
80
80
  get elements(): HTMLElement[] {
81
- return this.#elements;
81
+ return this.#elements.filter((el): el is HTMLElement => el != null);
82
82
  }
83
83
 
84
84
  setElementHeight = (element: HTMLElement, height: number) => {
@@ -86,7 +86,7 @@ export class ScrollOffsetPart {
86
86
  this.#heightCache.set(element, Math.round(height));
87
87
  };
88
88
 
89
- #getElementHeight = (element: HTMLElement) => {
89
+ #getElementHeight = (element: HTMLElement | null | undefined) => {
90
90
  if (!element) return 0;
91
91
  const cachedHeight = this.#heightCache.get(element);
92
92
  if (cachedHeight !== undefined) return cachedHeight;
@@ -97,7 +97,7 @@ export class ScrollOffsetPart {
97
97
  };
98
98
 
99
99
  calculate = (): this => {
100
- this.#isValid = !!this.#elements?.length;
100
+ this.#isValid = this.#elements?.some(el => el != null) ?? false;
101
101
  if (this.#isValid && this.#condition && isFunction(this.#condition)) {
102
102
  this.#isValid = this.#isValid && this.#condition();
103
103
  }
@@ -141,9 +141,9 @@ export class ScrollOffsetPart {
141
141
  * Author: Bogdan Barbu
142
142
  * Team: Codingheads (codingheads.com)
143
143
  */
144
- export class ScrollOffset {
144
+ export class ScrollOffset<TPartName extends string = string> {
145
145
  #offsetParts: ScrollOffsetPart[];
146
- #variables: ScrollOffsetVariable[];
146
+ #variables: ScrollOffsetVariable<TPartName>[];
147
147
  #extraEvents: string[];
148
148
  #registerForHoudini: boolean;
149
149
  #useResizeObserver: boolean;
@@ -158,7 +158,7 @@ export class ScrollOffset {
158
158
  registerForHoudini = true,
159
159
  useResizeObserver = true,
160
160
  extraEvents = [],
161
- }: ScrollOffsetSettings = {}) {
161
+ }: ScrollOffsetSettings<TPartName> = {}) {
162
162
  // parse the variables
163
163
  if (Array.isArray(variables)) {
164
164
  this.#variables = variables;