@smartimpact-it/scroll-utils 1.1.0

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 (49) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +158 -0
  3. package/dist/cjs/Bootstrap4AccordionScrollIntoView.js +74 -0
  4. package/dist/cjs/BootstrapAccordionScrollIntoView.js +68 -0
  5. package/dist/cjs/FixHashScrollPosition.js +38 -0
  6. package/dist/cjs/FoundationAccordionScrollIntoView.js +52 -0
  7. package/dist/cjs/ScrollDirection.js +120 -0
  8. package/dist/cjs/ScrollOffset.js +346 -0
  9. package/dist/cjs/ScrollPages.js +97 -0
  10. package/dist/cjs/index.js +93 -0
  11. package/dist/cjs/scrollWithMarginTop.js +34 -0
  12. package/dist/cjs/utils/onReady.js +46 -0
  13. package/dist/cjs/utils/utils.js +77 -0
  14. package/dist/esm/Bootstrap4AccordionScrollIntoView.js +57 -0
  15. package/dist/esm/BootstrapAccordionScrollIntoView.js +51 -0
  16. package/dist/esm/FixHashScrollPosition.js +24 -0
  17. package/dist/esm/FoundationAccordionScrollIntoView.js +35 -0
  18. package/dist/esm/ScrollDirection.js +98 -0
  19. package/dist/esm/ScrollOffset.js +283 -0
  20. package/dist/esm/ScrollPages.js +80 -0
  21. package/dist/esm/index.js +9 -0
  22. package/dist/esm/scrollWithMarginTop.js +25 -0
  23. package/dist/esm/utils/onReady.js +33 -0
  24. package/dist/esm/utils/utils.js +62 -0
  25. package/dist/types/Bootstrap4AccordionScrollIntoView.d.ts +11 -0
  26. package/dist/types/BootstrapAccordionScrollIntoView.d.ts +11 -0
  27. package/dist/types/FixHashScrollPosition.d.ts +7 -0
  28. package/dist/types/FoundationAccordionScrollIntoView.d.ts +11 -0
  29. package/dist/types/ScrollDirection.d.ts +29 -0
  30. package/dist/types/ScrollOffset.d.ts +49 -0
  31. package/dist/types/ScrollPages.d.ts +21 -0
  32. package/dist/types/index.d.ts +9 -0
  33. package/dist/types/scrollWithMarginTop.d.ts +5 -0
  34. package/dist/types/utils/onReady.d.ts +20 -0
  35. package/dist/types/utils/utils.d.ts +4 -0
  36. package/dist/umd/index.js +2 -0
  37. package/dist/umd/index.js.LICENSE.txt +8 -0
  38. package/package.json +101 -0
  39. package/src/Bootstrap4AccordionScrollIntoView.ts +51 -0
  40. package/src/BootstrapAccordionScrollIntoView.ts +45 -0
  41. package/src/FixHashScrollPosition.ts +31 -0
  42. package/src/FoundationAccordionScrollIntoView.ts +31 -0
  43. package/src/ScrollDirection.ts +122 -0
  44. package/src/ScrollOffset.ts +316 -0
  45. package/src/ScrollPages.ts +81 -0
  46. package/src/index.js +9 -0
  47. package/src/scrollWithMarginTop.ts +33 -0
  48. package/src/utils/onReady.ts +38 -0
  49. package/src/utils/utils.ts +78 -0
package/README.md ADDED
@@ -0,0 +1,158 @@
1
+ # Scroll-related utilities
2
+
3
+ This library contains multiple scroll-related utilities, very useful in websites.
4
+
5
+ ## ScrollDirection
6
+
7
+ This will put some classes on the `body` element depending on the scroll direction.
8
+
9
+ ```javascript
10
+ import { ScrollDirection } from '@smartimpact-it/scroll-utils';
11
+
12
+ window.addEventListener('DOMContentLoaded', () => {
13
+ new ScrollDirection({
14
+ onlyFor: () => window.innerWidth > 992,
15
+ threshold: 30,
16
+ });
17
+ });
18
+ ```
19
+
20
+ The `options` argument of the constructor has the following parameters:
21
+
22
+ - `onlyFor` - a callback to enable/disable the functionality based on some external factors (e.g. based on the viewport size)
23
+ - `threshold` - the minimum number of pixels for a change to be taken into consideration.
24
+
25
+ The classes it adds to the `body` element are:
26
+
27
+ - `scrolling-down`
28
+ - `scrolling-up`
29
+
30
+ Events:
31
+
32
+ - `scrollDirectionChange` - dispatched on the window
33
+
34
+ ## ScrollPages
35
+
36
+ This plugin will add some classes to the body depending on the scroll position: above or below the fold (the first "visible part" of the website).
37
+
38
+ ```javascript
39
+ import { ScrollPages } from '@smartimpact-it/scroll-utils';
40
+
41
+ window.addEventListener('DOMContentLoaded', () => {
42
+ new ScrollPages();
43
+
44
+ // or, with options:
45
+ new ScrollPages({
46
+ belowTheFoldClass: 'below-the-fold',
47
+ aboveTheFoldClass: 'above-the-fold',
48
+ });
49
+ });
50
+ ```
51
+
52
+ The plugin can also accept an `intersectionElement` if you want to provide yourself the HTML element that will "separate" the "above-the-fold" part from the "below-the-fold" part. If not provided, the plugin generates this element automatically.
53
+
54
+ ## ScrollOffset
55
+
56
+ This plugin monitors the size of specific UI elements (e.g. header elements) and creates CSS variables for their sizes. These are extremely useful when you have sticky headers and you need to adjust the `scroll-margin-top` property to take into account the sticky header.
57
+
58
+ ```javascript
59
+ import { ScrollOffsetPart, ScrollOffset } from '@smartimpact-it/scroll-utils';
60
+
61
+ window.addEventListener('DOMContentLoaded', () => {
62
+ const header = document.querySelector('.page-header'),
63
+ headerTop = header.querySelector('.page-header-inner-top'),
64
+ headerBottom = header.querySelector('.page-header-bottom'),
65
+ anchorSection = document.querySelector('.anchors-section');
66
+ const topPart = new ScrollOffsetPart({
67
+ name: 'top',
68
+ elements: [headerTop],
69
+ fixedHeight: headerTopHeight,
70
+ condition: () => !body.classList.contains('sticky-pinned') && isLarge(),
71
+ });
72
+ const bottomPart = new ScrollOffsetPart({
73
+ name: 'bottom',
74
+ elements: [headerBottom],
75
+ });
76
+ const anchorsPart = new ScrollOffsetPart({
77
+ name: 'anchors',
78
+ elements: [anchorSection],
79
+ });
80
+
81
+ new ScrollOffset({
82
+ variables: {
83
+ 'menu-space': [topPart, bottomPart],
84
+ 'full-menu-space': [topPart, bottomPart, anchorsPart],
85
+ },
86
+ });
87
+ });
88
+ ```
89
+
90
+ You begin by defining `ScrollOffsetPart` objects - these are the UI elements that we are monitoring.
91
+ Then, you create the `ScrollOffset` instance and you pass the variables that you want to be created and which elements those variables should contain. For example, in the code above, the "--menu-space" CSS variable will contain the size of the `topPart` + `bottomPart`.
92
+
93
+ In CSS, you can use those variables to set `scroll-margin-top` or for other purposes:
94
+
95
+ ```css
96
+ * {
97
+ scroll-margin-top: var(--full-menu-space, 0);
98
+ }
99
+ ```
100
+
101
+ ## ScrollWithMarginTop
102
+
103
+ This is a function that allows you to scroll to an element using the correct `scroll-margin-top` or `scroll-snap-margin-top`. Safari < 14.5 doesn't properly support `scroll-margin-top`, so we are using `scroll-snap-margin-top` (which it can read, but doesn't do anything in this case).
104
+
105
+ You can then force the correct scroll-margin-top when the `hash` of the page changes (when you click on an anchor):
106
+
107
+ ```javascript
108
+ import { scrollWithMarginTop, FixHashScrollPosition } from '@smartimpact-it/scroll-utils';
109
+
110
+ window.addEventListener('DOMContentLoaded', () => {
111
+ // fix for scroll-margin-top
112
+ window.addEventListener(
113
+ 'hashchange',
114
+ () => {
115
+ const hash = window.location.hash;
116
+ const element = document.querySelector(hash);
117
+ if (element) {
118
+ setTimeout(() => scrollWithMarginTop(element));
119
+ }
120
+ },
121
+ false
122
+ );
123
+
124
+ // or use the existing class:
125
+ new FixHashScrollPosition();
126
+ });
127
+ ```
128
+
129
+ And in CSS you do the following (put `scroll-snap-margin-top` above `scroll-margin-top`):
130
+
131
+ ```css
132
+ * {
133
+ scroll-snap-margin-top: var(--full-menu-space, 0);
134
+ scroll-margin-top: var(--full-menu-space, 0);
135
+ }
136
+ ```
137
+
138
+ The `scrollWithMarginTop` function has 3 parameters:
139
+
140
+ - `element` (HTMLElement) - the element to scroll to
141
+ - `offset` (numeric, default = `0`) - the offset for the scroll position (number of pixels)
142
+ - `onlyWhenNeeded` (boolean, default = `false`) - if this is `true`, it will not scroll if the element is already visible in the first half of the viewport
143
+
144
+ ## Fix the scroll position for Bootstrap or Foundation accordions
145
+
146
+ When accordions open while another accordion tab above is open, the result is that some part of the new tab may be hidden. To fix this, we need to fix the scroll. You can use the `BootstrapAccordionScrollIntoView` and `FoundationAccordionScrollIntoView` classes, which in turn use the `scrollWithMarginTop` function.
147
+
148
+ ```javascript
149
+ import { BootstrapAccordionScrollIntoView, FoundationAccordionScrollIntoView } from '@smartimpact-it/scroll-utils';
150
+
151
+ window.addEventListener('DOMContentLoaded', () => {
152
+ // for bootstrap accordion
153
+ new BootstrapAccordionScrollIntoView();
154
+
155
+ // for foundation accordion
156
+ new FoundationAccordionScrollIntoView();
157
+ }
158
+ ```
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
5
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
6
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
7
+ function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
8
+ function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
9
+ function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
10
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
11
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
12
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
13
+ function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
14
+ function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
15
+ Object.defineProperty(exports, "__esModule", {
16
+ value: true
17
+ });
18
+ exports.Bootstrap4AccordionScrollIntoView = void 0;
19
+ var scrollWithMarginTop_1 = require("./scrollWithMarginTop");
20
+ var onReady_1 = require("./utils/onReady");
21
+ /**
22
+ * Scroll to the opened accordion tab
23
+ */
24
+ var _Bootstrap4AccordionScrollIntoView_brand = /*#__PURE__*/new WeakSet();
25
+ var _handle = /*#__PURE__*/new WeakMap();
26
+ var Bootstrap4AccordionScrollIntoView = /*#__PURE__*/_createClass(function Bootstrap4AccordionScrollIntoView() {
27
+ var _this = this;
28
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
29
+ _ref$extraOffset = _ref.extraOffset,
30
+ extraOffset = _ref$extraOffset === void 0 ? 0 : _ref$extraOffset;
31
+ _classCallCheck(this, Bootstrap4AccordionScrollIntoView);
32
+ _classPrivateMethodInitSpec(this, _Bootstrap4AccordionScrollIntoView_brand);
33
+ _defineProperty(this, "extraOffset", void 0);
34
+ _classPrivateFieldInitSpec(this, _handle, function (e) {
35
+ var _target$closest;
36
+ var header = null;
37
+ var target = e.target;
38
+ // Skip scrolling if the clicked element or any of its parents has the class 'skip-scroll-into-view'.
39
+ if ((_target$closest = target.closest) !== null && _target$closest !== void 0 && _target$closest.call(target, '.skip-scroll-into-view, [data-skip-scroll-into-view]')) {
40
+ return;
41
+ }
42
+ var tabId = target.id;
43
+ var headerId = target.getAttribute('aria-labelledby');
44
+ if (headerId) {
45
+ header = document.querySelector("#".concat(headerId));
46
+ }
47
+ if (!header && tabId) {
48
+ header = document.querySelector("[data-target=\"#".concat(tabId, "\"]"));
49
+ }
50
+ if (header) {
51
+ (0, scrollWithMarginTop_1.scrollWithMarginTop)(header, _this.extraOffset, true);
52
+ }
53
+ });
54
+ this.extraOffset = extraOffset;
55
+ (0, onReady_1.onComplete)(function () {
56
+ return setTimeout(function () {
57
+ return _assertClassBrand(_Bootstrap4AccordionScrollIntoView_brand, _this, _addEventListeners).call(_this);
58
+ }, 1000);
59
+ });
60
+ });
61
+ function _addEventListeners() {
62
+ var _this2 = this;
63
+ if ('$' in window) {
64
+ window.$(document).on('shown.collapse', function (e) {
65
+ _classPrivateFieldGet(_handle, _this2).call(_this2, e);
66
+ });
67
+ } else {
68
+ document.addEventListener('shown.collapse', function (e) {
69
+ _classPrivateFieldGet(_handle, _this2).call(_this2, e);
70
+ });
71
+ }
72
+ }
73
+ exports.Bootstrap4AccordionScrollIntoView = Bootstrap4AccordionScrollIntoView;
74
+ exports["default"] = Bootstrap4AccordionScrollIntoView;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
5
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
6
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
7
+ function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
8
+ function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
9
+ function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
10
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
11
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
12
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
13
+ function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
14
+ function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
15
+ Object.defineProperty(exports, "__esModule", {
16
+ value: true
17
+ });
18
+ exports.BootstrapAccordionScrollIntoView = void 0;
19
+ var scrollWithMarginTop_1 = require("./scrollWithMarginTop");
20
+ var onReady_1 = require("./utils/onReady");
21
+ /**
22
+ * Scroll to the opened accordion tab
23
+ */
24
+ var _BootstrapAccordionScrollIntoView_brand = /*#__PURE__*/new WeakSet();
25
+ var _handle = /*#__PURE__*/new WeakMap();
26
+ var BootstrapAccordionScrollIntoView = /*#__PURE__*/_createClass(function BootstrapAccordionScrollIntoView() {
27
+ var _this = this;
28
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
29
+ _ref$extraOffset = _ref.extraOffset,
30
+ extraOffset = _ref$extraOffset === void 0 ? 0 : _ref$extraOffset;
31
+ _classCallCheck(this, BootstrapAccordionScrollIntoView);
32
+ _classPrivateMethodInitSpec(this, _BootstrapAccordionScrollIntoView_brand);
33
+ _defineProperty(this, "extraOffset", void 0);
34
+ _classPrivateFieldInitSpec(this, _handle, function (e) {
35
+ var _target$closest;
36
+ var header = null;
37
+ var target = e.target;
38
+ // Skip scrolling if the clicked element or any of its parents has the class 'skip-scroll-into-view'.
39
+ if ((_target$closest = target.closest) !== null && _target$closest !== void 0 && _target$closest.call(target, '.skip-scroll-into-view, [data-skip-scroll-into-view]')) {
40
+ return;
41
+ }
42
+ var tabId = target.id;
43
+ var headerId = target.getAttribute('aria-labelledby');
44
+ if (headerId) {
45
+ header = document.querySelector("#".concat(headerId));
46
+ }
47
+ if (!header && tabId) {
48
+ header = document.querySelector("[data-bs-target=\"#".concat(tabId, "\"]"));
49
+ }
50
+ if (header) {
51
+ (0, scrollWithMarginTop_1.scrollWithMarginTop)(header, _this.extraOffset, true);
52
+ }
53
+ });
54
+ this.extraOffset = extraOffset;
55
+ (0, onReady_1.onComplete)(function () {
56
+ return setTimeout(function () {
57
+ return _assertClassBrand(_BootstrapAccordionScrollIntoView_brand, _this, _addEventListeners).call(_this);
58
+ }, 1000);
59
+ });
60
+ });
61
+ function _addEventListeners() {
62
+ var _this2 = this;
63
+ document.addEventListener('shown.bs.collapse', function (e) {
64
+ _classPrivateFieldGet(_handle, _this2).call(_this2, e);
65
+ });
66
+ }
67
+ exports.BootstrapAccordionScrollIntoView = BootstrapAccordionScrollIntoView;
68
+ exports["default"] = BootstrapAccordionScrollIntoView;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
5
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
6
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
7
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
8
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
9
+ Object.defineProperty(exports, "__esModule", {
10
+ value: true
11
+ });
12
+ exports.FixHashScrollPosition = void 0;
13
+ var scrollWithMarginTop_1 = require("./scrollWithMarginTop");
14
+ /**
15
+ * Fix the scroll offset when loading a page with a #hash (anchor)
16
+ */
17
+ var FixHashScrollPosition = /*#__PURE__*/_createClass(function FixHashScrollPosition() {
18
+ _classCallCheck(this, FixHashScrollPosition);
19
+ // fix for scroll-margin-top
20
+ window.addEventListener('hashchange', function () {
21
+ var hash = window.location.hash;
22
+ if (!hash || hash === '#') {
23
+ return;
24
+ }
25
+ try {
26
+ var element = document.querySelector(hash);
27
+ if (element) {
28
+ setTimeout(function () {
29
+ return (0, scrollWithMarginTop_1.scrollWithMarginTop)(element);
30
+ });
31
+ }
32
+ } catch (e) {
33
+ console.warn(e);
34
+ }
35
+ }, false);
36
+ });
37
+ exports.FixHashScrollPosition = FixHashScrollPosition;
38
+ exports["default"] = FixHashScrollPosition;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
5
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
6
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
7
+ function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
8
+ function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
9
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
10
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
11
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
12
+ function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
13
+ Object.defineProperty(exports, "__esModule", {
14
+ value: true
15
+ });
16
+ exports.FoundationAccordionScrollIntoView = void 0;
17
+ var scrollWithMarginTop_1 = require("./scrollWithMarginTop");
18
+ var onReady_1 = require("./utils/onReady");
19
+ /**
20
+ * Scroll to the opened accordion tab
21
+ */
22
+ var _FoundationAccordionScrollIntoView_brand = /*#__PURE__*/new WeakSet();
23
+ var FoundationAccordionScrollIntoView = /*#__PURE__*/_createClass(function FoundationAccordionScrollIntoView() {
24
+ var _this = this;
25
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
26
+ _ref$extraOffset = _ref.extraOffset,
27
+ extraOffset = _ref$extraOffset === void 0 ? 0 : _ref$extraOffset;
28
+ _classCallCheck(this, FoundationAccordionScrollIntoView);
29
+ _classPrivateMethodInitSpec(this, _FoundationAccordionScrollIntoView_brand);
30
+ _defineProperty(this, "extraOffset", void 0);
31
+ this.extraOffset = extraOffset;
32
+ (0, onReady_1.onComplete)(function () {
33
+ return setTimeout(function () {
34
+ return _assertClassBrand(_FoundationAccordionScrollIntoView_brand, _this, _addEventListeners).call(_this);
35
+ }, 1000);
36
+ });
37
+ });
38
+ function _addEventListeners() {
39
+ var _this2 = this;
40
+ if (!('$' in window)) return;
41
+ window.$(document).on('down.zf.accordion', function (e, $content) {
42
+ var _target$closest;
43
+ var target = $content.get(0).parentNode;
44
+ // Skip scrolling if the clicked element or any of its parents has the class 'skip-scroll-into-view'.
45
+ if ((_target$closest = target.closest) !== null && _target$closest !== void 0 && _target$closest.call(target, '.skip-scroll-into-view, [data-skip-scroll-into-view]')) {
46
+ return;
47
+ }
48
+ (0, scrollWithMarginTop_1.scrollWithMarginTop)(target, _this2.extraOffset, true);
49
+ });
50
+ }
51
+ exports.FoundationAccordionScrollIntoView = FoundationAccordionScrollIntoView;
52
+ exports["default"] = FoundationAccordionScrollIntoView;
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
5
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
6
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
7
+ function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
8
+ function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
9
+ function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
10
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
11
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
12
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
13
+ function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
14
+ function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
15
+ function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
16
+ var __importDefault = this && this.__importDefault || function (mod) {
17
+ return mod && mod.__esModule ? mod : {
18
+ "default": mod
19
+ };
20
+ };
21
+ Object.defineProperty(exports, "__esModule", {
22
+ value: true
23
+ });
24
+ exports.ScrollDirection = void 0;
25
+ var isFunction_1 = __importDefault(require("lodash/isFunction"));
26
+ var throttle_1 = __importDefault(require("lodash/throttle"));
27
+ var _lastScrollTop = /*#__PURE__*/new WeakMap();
28
+ var _throttle = /*#__PURE__*/new WeakMap();
29
+ var _throttleOptions = /*#__PURE__*/new WeakMap();
30
+ var _ScrollDirection_brand = /*#__PURE__*/new WeakSet();
31
+ var ScrollDirection = /*#__PURE__*/_createClass(
32
+ /**
33
+ * start an instance
34
+ * @param {{onlyFor: a callback to determine where to modify classes; threshold: amount in px to take into account }} param0
35
+ */
36
+ function ScrollDirection() {
37
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
38
+ _ref$onlyFor = _ref.onlyFor,
39
+ onlyFor = _ref$onlyFor === void 0 ? null : _ref$onlyFor,
40
+ _ref$threshold = _ref.threshold,
41
+ threshold = _ref$threshold === void 0 ? 0 : _ref$threshold,
42
+ _ref$thresholdCallbac = _ref.thresholdCallback,
43
+ thresholdCallback = _ref$thresholdCallbac === void 0 ? null : _ref$thresholdCallbac,
44
+ _ref$throttle = _ref.throttle,
45
+ throttle = _ref$throttle === void 0 ? 16 : _ref$throttle,
46
+ _ref$throttleOptions = _ref.throttleOptions,
47
+ throttleOptions = _ref$throttleOptions === void 0 ? {
48
+ trailing: false
49
+ } : _ref$throttleOptions;
50
+ _classCallCheck(this, ScrollDirection);
51
+ /**
52
+ * setup the scroll event listener
53
+ */
54
+ _classPrivateMethodInitSpec(this, _ScrollDirection_brand);
55
+ _defineProperty(this, "onlyForCallback", null);
56
+ _classPrivateFieldInitSpec(this, _lastScrollTop, 0);
57
+ _defineProperty(this, "threshold", 0);
58
+ _defineProperty(this, "thresholdCallback", null);
59
+ _classPrivateFieldInitSpec(this, _throttle, void 0);
60
+ _classPrivateFieldInitSpec(this, _throttleOptions, void 0);
61
+ this.onlyForCallback = onlyFor;
62
+ this.threshold = threshold;
63
+ this.thresholdCallback = thresholdCallback;
64
+ _classPrivateFieldSet(_throttle, this, throttle);
65
+ _classPrivateFieldSet(_throttleOptions, this, throttleOptions);
66
+ _classPrivateFieldSet(_lastScrollTop, this, window.pageYOffset || document.documentElement.scrollTop);
67
+ _assertClassBrand(_ScrollDirection_brand, this, _setupListeners).call(this);
68
+ });
69
+ function _setupListeners() {
70
+ var _this = this;
71
+ var update = function update() {
72
+ var isValid = !_this.onlyForCallback || _this.onlyForCallback();
73
+ if (!isValid) return;
74
+ _assertClassBrand(_ScrollDirection_brand, _this, _determineDirection).call(_this);
75
+ };
76
+ if (_classPrivateFieldGet(_throttle, this)) {
77
+ var throttledUpdate = (0, throttle_1["default"])(update, _classPrivateFieldGet(_throttle, this), _classPrivateFieldGet(_throttleOptions, this) || {});
78
+ window.addEventListener('scroll', throttledUpdate, {
79
+ passive: true
80
+ });
81
+ } else {
82
+ window.addEventListener('scroll', update, {
83
+ passive: true
84
+ });
85
+ }
86
+ }
87
+ /**
88
+ * dispatch an event on the window
89
+ */
90
+ function _dispatchEvent(name) {
91
+ requestAnimationFrame(function () {
92
+ window.dispatchEvent(new CustomEvent(name));
93
+ });
94
+ }
95
+ /**
96
+ * determine the scroll direction
97
+ */
98
+ function _determineDirection() {
99
+ var body = document.body,
100
+ classList = body.classList;
101
+ var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
102
+ var localThreshold = this.thresholdCallback && (0, isFunction_1["default"])(this.thresholdCallback) ? this.thresholdCallback({
103
+ scrollTop: scrollTop,
104
+ threshold: this.threshold,
105
+ lastScrollTop: _classPrivateFieldGet(_lastScrollTop, this)
106
+ }) : this.threshold;
107
+ if (Math.abs(scrollTop - _classPrivateFieldGet(_lastScrollTop, this)) < localThreshold) return;
108
+ if (scrollTop > _classPrivateFieldGet(_lastScrollTop, this) + localThreshold && !classList.contains('scrolling-down')) {
109
+ classList.add('scrolling-down');
110
+ classList.remove('scrolling-up');
111
+ _assertClassBrand(_ScrollDirection_brand, this, _dispatchEvent).call(this, 'scrollDirectionChange');
112
+ } else if (scrollTop < _classPrivateFieldGet(_lastScrollTop, this) - localThreshold && !classList.contains('scrolling-up')) {
113
+ classList.add('scrolling-up');
114
+ classList.remove('scrolling-down');
115
+ _assertClassBrand(_ScrollDirection_brand, this, _dispatchEvent).call(this, 'scrollDirectionChange');
116
+ }
117
+ _classPrivateFieldSet(_lastScrollTop, this, Math.max(scrollTop, 0));
118
+ }
119
+ exports.ScrollDirection = ScrollDirection;
120
+ exports["default"] = ScrollDirection;