@vdegenne/highlight-manager 0.1.12 → 0.1.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.
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { VisibilityCheck } from './utils.js';
1
+ import { CheckIf } from './utils.js';
2
2
  interface Info {
3
3
  elements: HTMLElement[];
4
4
  highlightIndexStart: number;
@@ -15,9 +15,9 @@ interface ScrollStrategy {
15
15
  * The visibility check to use to determine
16
16
  * whether or not the scroll should be issued.
17
17
  *
18
- * @default 'top'
18
+ * @default when top is not visible
19
19
  */
20
- visibilityCheck: VisibilityCheck;
20
+ when: CheckIf;
21
21
  /**
22
22
  * @default 'smooth'
23
23
  */
package/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { querySelectorAll } from 'html-vision';
2
2
  import { checkVisibility, sleep } from './utils.js';
3
3
  const scrollStrategyDefaults = {
4
- visibilityCheck: 'top',
4
+ when: (is) => !is('top-visible'),
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
- !checkVisibility(elementsToHighlight[0], _options.scrollStrategy.visibilityCheck)) {
195
+ !checkVisibility(elementsToHighlight[0], _options.scrollStrategy.when)) {
196
196
  elementsToHighlight[0]?.scrollIntoView({
197
197
  behavior: _options.scrollStrategy.behavior,
198
198
  block: _options.scrollStrategy.block,
package/lib/utils.d.ts CHANGED
@@ -1,4 +1,9 @@
1
1
  export declare function sleep(milli?: number): Promise<unknown>;
2
- export type VisibilityCheck = 'top' | 'center' | 'bounding-rect' | 'bottom' | 'fully-visible' | 'not-fully-visible';
3
- export declare function checkVisibility(el: HTMLElement, visibilityCheck?: VisibilityCheck): boolean;
2
+ export type VisibilityCheck = 'top-visible' | 'center-visible' | 'bottom-visible' | 'partially-visible' | 'fully-visible';
3
+ export type CheckIf = (is: (visibility: VisibilityCheck) => boolean) => boolean;
4
+ export declare function checkVisibility(el: HTMLElement,
5
+ /**
6
+ * @default top is not visible
7
+ */
8
+ checkIf?: CheckIf): boolean;
4
9
  //# sourceMappingURL=utils.d.ts.map
package/lib/utils.js CHANGED
@@ -1,39 +1,41 @@
1
1
  export function sleep(milli = 1000) {
2
2
  return new Promise((r) => setTimeout(r, milli));
3
3
  }
4
- export function checkVisibility(el, visibilityCheck = 'top') {
4
+ export function checkVisibility(el,
5
+ /**
6
+ * @default top is not visible
7
+ */
8
+ checkIf = (is) => !is('top-visible')) {
5
9
  const rect = el.getBoundingClientRect();
6
10
  const viewHeight = window.innerHeight || document.documentElement.clientHeight;
7
11
  const viewWidth = window.innerWidth || document.documentElement.clientWidth;
8
- switch (visibilityCheck) {
9
- case 'top':
10
- return rect.top >= 0 && rect.top <= viewHeight;
11
- case 'bottom':
12
- return rect.bottom >= 0 && rect.bottom <= viewHeight;
13
- case 'center': {
14
- const centerY = rect.top + rect.height / 2;
15
- const centerX = rect.left + rect.width / 2;
16
- return (centerY >= 0 &&
17
- centerY <= viewHeight &&
18
- centerX >= 0 &&
19
- centerX <= viewWidth);
12
+ function is(visibilityCheck) {
13
+ switch (visibilityCheck) {
14
+ case 'top-visible':
15
+ return rect.top >= 0 && rect.top <= viewHeight;
16
+ case 'center-visible': {
17
+ const centerY = rect.top + rect.height / 2;
18
+ const centerX = rect.left + rect.width / 2;
19
+ return (centerY >= 0 &&
20
+ centerY <= viewHeight &&
21
+ centerX >= 0 &&
22
+ centerX <= viewWidth);
23
+ }
24
+ case 'bottom-visible':
25
+ return rect.bottom >= 0 && rect.bottom <= viewHeight;
26
+ case 'fully-visible':
27
+ return (rect.top >= 0 &&
28
+ rect.left >= 0 &&
29
+ rect.bottom <= viewHeight &&
30
+ rect.right <= viewWidth);
31
+ case 'partially-visible':
32
+ default:
33
+ return (rect.bottom > 0 &&
34
+ rect.right > 0 &&
35
+ rect.top < viewHeight &&
36
+ rect.left < viewWidth);
20
37
  }
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);
31
- case 'bounding-rect':
32
- default:
33
- return (rect.bottom > 0 &&
34
- rect.right > 0 &&
35
- rect.top < viewHeight &&
36
- rect.left < viewWidth);
37
38
  }
39
+ return checkIf(is);
38
40
  }
39
41
  //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vdegenne/highlight-manager",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "helper to navigate/highlight elements in a page based on a css selector",
5
5
  "type": "module",
6
6
  "exports": {