@vdegenne/highlight-manager 0.1.11 → 0.1.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.
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IsInViewportPartCheck } from './utils.js';
1
+ import { VisibilityCheck } from './utils.js';
2
2
  interface Info {
3
3
  elements: HTMLElement[];
4
4
  highlightIndexStart: number;
@@ -12,9 +12,12 @@ interface Info {
12
12
  }
13
13
  interface ScrollStrategy {
14
14
  /**
15
- * @default 'center'
15
+ * The visibility check to use to determine
16
+ * whether or not the scroll should be issued.
17
+ *
18
+ * @default 'top'
16
19
  */
17
- whenWhatPartIsHidden: IsInViewportPartCheck;
20
+ visibilityCheck: VisibilityCheck;
18
21
  /**
19
22
  * @default 'smooth'
20
23
  */
package/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { querySelectorAll } from 'html-vision';
2
- import { isInViewport, sleep } from './utils.js';
2
+ import { checkVisibility, sleep } from './utils.js';
3
3
  const scrollStrategyDefaults = {
4
- whenWhatPartIsHidden: 'center',
4
+ visibilityCheck: 'top',
5
5
  behavior: 'smooth',
6
6
  block: undefined,
7
7
  inline: undefined,
@@ -192,7 +192,7 @@ export class HighLightManager {
192
192
  return false;
193
193
  }
194
194
  if (_options.scrollStrategy &&
195
- !isInViewport(elementsToHighlight[0], _options.scrollStrategy.whenWhatPartIsHidden)) {
195
+ !checkVisibility(elementsToHighlight[0], _options.scrollStrategy.visibilityCheck)) {
196
196
  elementsToHighlight[0]?.scrollIntoView({
197
197
  behavior: _options.scrollStrategy.behavior,
198
198
  block: _options.scrollStrategy.block,
@@ -218,7 +218,7 @@ export class HighLightManager {
218
218
  : highlightIndexStart;
219
219
  if (currIndex === -1) {
220
220
  if (this.#options.fastTravel) {
221
- const found = [...elements].reverse().find((el) => isInViewport(el));
221
+ const found = [...elements].reverse().find((el) => checkVisibility(el));
222
222
  if (found) {
223
223
  const i = elements.indexOf(found);
224
224
  this.highlight(i, i, true, cache);
@@ -229,7 +229,7 @@ export class HighLightManager {
229
229
  return;
230
230
  }
231
231
  const currEl = elements[currIndex];
232
- const currIsVisible = currEl ? isInViewport(currEl) : false;
232
+ const currIsVisible = currEl ? checkVisibility(currEl) : false;
233
233
  const currIsBelow = currEl
234
234
  ? currEl.getBoundingClientRect().top > window.innerHeight
235
235
  : false;
@@ -238,7 +238,7 @@ export class HighLightManager {
238
238
  const found = elements
239
239
  .slice(0, currIndex)
240
240
  .reverse()
241
- .find((el) => isInViewport(el));
241
+ .find((el) => checkVisibility(el));
242
242
  if (found) {
243
243
  prevIndex = elements.indexOf(found);
244
244
  }
@@ -262,7 +262,7 @@ export class HighLightManager {
262
262
  : highlightIndexEnd;
263
263
  if (currIndex === -1) {
264
264
  if (this.#options.fastTravel) {
265
- const found = elements.find((el) => isInViewport(el));
265
+ const found = elements.find((el) => checkVisibility(el));
266
266
  if (found) {
267
267
  const i = elements.indexOf(found);
268
268
  this.highlight(i, i, true, cache);
@@ -273,13 +273,15 @@ export class HighLightManager {
273
273
  return;
274
274
  }
275
275
  const currEl = elements[currIndex];
276
- const currIsVisible = currEl ? isInViewport(currEl) : false;
276
+ const currIsVisible = currEl ? checkVisibility(currEl) : false;
277
277
  const currIsAbove = currEl
278
278
  ? currEl.getBoundingClientRect().bottom < 0
279
279
  : false;
280
280
  let nextIndex = -1;
281
281
  if (this.#options.fastTravel && !currIsVisible && currIsAbove) {
282
- const found = elements.slice(currIndex + 1).find((el) => isInViewport(el));
282
+ const found = elements
283
+ .slice(currIndex + 1)
284
+ .find((el) => checkVisibility(el));
283
285
  if (found) {
284
286
  nextIndex = elements.indexOf(found);
285
287
  }
package/lib/utils.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export declare function sleep(milli?: number): Promise<unknown>;
2
- export type IsInViewportPartCheck = 'top' | 'center' | 'bounding-rect' | 'bottom';
3
- export declare function isInViewport(el: HTMLElement, partCheck?: IsInViewportPartCheck): boolean;
2
+ export type VisibilityCheck = 'top' | 'center' | 'bounding-rect' | 'bottom' | 'fully-visible' | 'not-fully-visible';
3
+ export declare function checkVisibility(el: HTMLElement, visibilityCheck?: VisibilityCheck): boolean;
4
4
  //# sourceMappingURL=utils.d.ts.map
package/lib/utils.js CHANGED
@@ -1,11 +1,11 @@
1
1
  export function sleep(milli = 1000) {
2
2
  return new Promise((r) => setTimeout(r, milli));
3
3
  }
4
- export function isInViewport(el, partCheck = 'center') {
4
+ export function checkVisibility(el, visibilityCheck = 'top') {
5
5
  const rect = el.getBoundingClientRect();
6
6
  const viewHeight = window.innerHeight || document.documentElement.clientHeight;
7
7
  const viewWidth = window.innerWidth || document.documentElement.clientWidth;
8
- switch (partCheck) {
8
+ switch (visibilityCheck) {
9
9
  case 'top':
10
10
  return rect.top >= 0 && rect.top <= viewHeight;
11
11
  case 'bottom':
@@ -18,6 +18,16 @@ export function isInViewport(el, partCheck = 'center') {
18
18
  centerX >= 0 &&
19
19
  centerX <= viewWidth);
20
20
  }
21
+ case 'fully-visible':
22
+ return (rect.top >= 0 &&
23
+ rect.left >= 0 &&
24
+ rect.bottom <= viewHeight &&
25
+ rect.right <= viewWidth);
26
+ case 'not-fully-visible':
27
+ return (rect.top < 0 ||
28
+ rect.left < 0 ||
29
+ rect.bottom > viewHeight ||
30
+ rect.right > viewWidth);
21
31
  case 'bounding-rect':
22
32
  default:
23
33
  return (rect.bottom > 0 &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vdegenne/highlight-manager",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "helper to navigate/highlight elements in a page based on a css selector",
5
5
  "type": "module",
6
6
  "exports": {