@carbon/ibmdotcom-utilities 2.10.0 → 2.11.0-rc.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 (33) hide show
  1. package/es/utilities/StickyHeader/StickyHeader.js +380 -163
  2. package/es/utilities/focuswrap/focuswrap.js +4 -4
  3. package/es/utilities/ipcinfoCookie/ipcinfoCookie.js +3 -3
  4. package/es/utilities/settings/settings.js +2 -2
  5. package/lib/utilities/StickyHeader/StickyHeader.js +381 -164
  6. package/lib/utilities/StickyHeader/index.js +1 -1
  7. package/lib/utilities/altlangs/altlangs.js +1 -1
  8. package/lib/utilities/altlangs/index.js +1 -1
  9. package/lib/utilities/calculateTotalWidth/index.js +1 -1
  10. package/lib/utilities/decodeString/index.js +1 -1
  11. package/lib/utilities/deprecate/index.js +1 -1
  12. package/lib/utilities/escaperegexp/index.js +1 -1
  13. package/lib/utilities/featureflag/index.js +1 -1
  14. package/lib/utilities/focuswrap/focuswrap.js +4 -4
  15. package/lib/utilities/ipcinfoCookie/index.js +1 -1
  16. package/lib/utilities/ipcinfoCookie/ipcinfoCookie.js +4 -4
  17. package/lib/utilities/loadNonLatinPlex/index.js +1 -1
  18. package/lib/utilities/markdownToHtml/index.js +1 -1
  19. package/lib/utilities/markdownToHtml/markdownToHtml.js +1 -1
  20. package/lib/utilities/on/index.js +1 -1
  21. package/lib/utilities/parseAspectRatio/index.js +1 -1
  22. package/lib/utilities/removeHtmlTagEntities/index.js +1 -1
  23. package/lib/utilities/removeHtmlTagEntities/removeHtmlTagEntities.js +1 -1
  24. package/lib/utilities/sameHeight/index.js +1 -1
  25. package/lib/utilities/serialize/index.js +1 -1
  26. package/lib/utilities/settings/index.js +1 -1
  27. package/lib/utilities/settings/settings.js +2 -2
  28. package/lib/utilities/smoothScroll/index.js +1 -1
  29. package/lib/utilities/stripHTML/index.js +1 -1
  30. package/lib/utilities/uniqueid/index.js +1 -1
  31. package/package.json +2 -2
  32. package/umd/ibmdotcom-utilities.js +34 -29
  33. package/umd/ibmdotcom-utilities.min.js +3 -3
@@ -1,7 +1,7 @@
1
1
  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); }
2
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
3
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
4
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
2
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
3
+ 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); } }
4
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
5
5
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
6
6
  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); }
7
7
  /**
@@ -16,25 +16,39 @@ import root from 'window-or-global';
16
16
  import settings from '../settings/settings.js';
17
17
  var prefix = settings.prefix,
18
18
  c4dPrefix = settings.stablePrefix;
19
+ var ddsPrefix = 'dds';
20
+ var bxPrefix = 'bx';
19
21
  var gridBreakpoint = parseFloat(breakpoints.lg.width) * baseFontSize;
20
22
  var StickyHeader = /*#__PURE__*/function () {
21
23
  function StickyHeader() {
22
24
  _classCallCheck(this, StickyHeader);
23
25
  this.ownerDocument = root.document;
24
- this._banner = undefined;
25
- this._cumulativeHeight = 0;
26
- this._hasBanner = false;
27
- this._lastScrollPosition = 0;
28
- this._leadspaceWithSearch = undefined;
29
- this._leadspaceSearchBar = undefined;
30
- this._leadspaceWithSearchStickyThreshold = 0;
31
- this._localeModal = undefined;
32
- this._masthead = undefined;
33
- this._mastheadL0 = undefined;
34
- this._mastheadL1 = undefined;
35
- this._tableOfContents = undefined;
36
- this._tableOfContentsInnerBar = undefined;
37
- this._tableOfContentsLayout = undefined;
26
+ this._state = {
27
+ cumulativeOffset: 0,
28
+ hasBanner: false,
29
+ leadspaceSearchThreshold: 0,
30
+ mastheadL0IsActive: false,
31
+ mastheadL1IsActive: false,
32
+ maxScrollaway: 0,
33
+ scrollPosPrevious: 0,
34
+ scrollPos: 0,
35
+ searchIsAtTop: false,
36
+ tocShouldStick: false,
37
+ tocIsAtTop: false,
38
+ tocIsAtSearch: false
39
+ };
40
+ this._elements = {
41
+ banner: undefined,
42
+ leadspaceSearch: undefined,
43
+ leadspaceSearchBar: undefined,
44
+ leadspaceSearchInput: undefined,
45
+ localeModal: undefined,
46
+ masthead: undefined,
47
+ mastheadL0: undefined,
48
+ mastheadL1: undefined,
49
+ tableOfContents: undefined,
50
+ tableOfContentsInnerBar: undefined
51
+ };
38
52
  this._throttled = false;
39
53
  this._resizeObserver = new ResizeObserver(this._handleResize.bind(this));
40
54
  root.addEventListener('scroll', this._throttledHandler.bind(this));
@@ -46,7 +60,7 @@ var StickyHeader = /*#__PURE__*/function () {
46
60
  return _createClass(StickyHeader, [{
47
61
  key: "height",
48
62
  get: function get() {
49
- return this._cumulativeHeight;
63
+ return this._state.cumulativeOffset;
50
64
  }
51
65
 
52
66
  /**
@@ -62,78 +76,176 @@ var StickyHeader = /*#__PURE__*/function () {
62
76
  value: function _validateComponent(component, expected) {
63
77
  var received = component.tagName.toLowerCase();
64
78
  if (received !== expected) {
65
- throw new TypeError("".concat(expected, " expected, ").concat(received, " provided"));
79
+ // TODO: don't check for v1/v2 compatibility after v1 EOL.
80
+ if (received.split('-').splice(1).join('-') !== expected.split('-').splice(1).join('-')) {
81
+ throw new TypeError("".concat(expected, " expected, ").concat(received, " provided"));
82
+ } else {
83
+ var message = ["Mixed prefixes detected.\n", "expected ".concat(expected, ", found ").concat(received, ".")];
84
+ console.warn(message.join(''));
85
+ return true;
86
+ }
66
87
  } else {
67
88
  return true;
68
89
  }
69
90
  }
91
+
92
+ /**
93
+ * Helper method to query for either C4IBM v1.x or v2.x sub-elements;
94
+ *
95
+ * @param {*} element The C4IBM element.
96
+ * @param {*} v1Func The querying function to run if using a C4IBM v1.x element.
97
+ * @param {*} v2Func The querying function to run if using a C4IBM v2.x element.
98
+ */
70
99
  }, {
71
- key: "_tableOfContentsStickyUpdate",
72
- value: function _tableOfContentsStickyUpdate() {
73
- var toc = this._tableOfContents;
100
+ key: "_updateRefsV1orV2",
101
+ value: function _updateRefsV1orV2(element, v1Func, v2Func) {
102
+ var elementPrefix = element.tagName.toLowerCase().split('-')[0];
103
+ if (elementPrefix === ddsPrefix) {
104
+ v1Func.bind(this)();
105
+ } else if (elementPrefix === c4dPrefix) {
106
+ v2Func.bind(this)();
107
+ } else {
108
+ throw new Error("\n Could not find sub-elements for ".concat(element.tagName.toLowerCase(), ".\n "));
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Temporary method to find v1 leadspace sub-elements.
114
+ */
115
+ }, {
116
+ key: "_updateLeadspaceRefsV1",
117
+ value: function _updateLeadspaceRefsV1() {
118
+ var leadspaceSearch = this._elements.leadspaceSearch;
119
+ this._elements.leadspaceSearchBar = leadspaceSearch.shadowRoot.querySelector(".".concat(bxPrefix, "--search-container"));
120
+ this._elements.leadspaceSearchInput = leadspaceSearch.querySelector("".concat(ddsPrefix, "-search-with-typeahead"));
121
+ }
122
+
123
+ /**
124
+ * Temporary method to find v2 leadspace sub-elements.
125
+ */
126
+ }, {
127
+ key: "_updateLeadspaceRefsV2",
128
+ value: function _updateLeadspaceRefsV2() {
129
+ var leadspaceSearch = this._elements.leadspaceSearch;
130
+ this._elements.leadspaceSearchBar = leadspaceSearch.shadowRoot.querySelector(".".concat(prefix, "--search-container"));
131
+ this._elements.leadspaceSearchInput = leadspaceSearch.querySelector("".concat(c4dPrefix, "-search-with-typeahead"));
132
+ }
133
+
134
+ /**
135
+ * Temporary method to find v1 masthead sub-elements.
136
+ */
137
+ }, {
138
+ key: "_updateMastheadRefsV1",
139
+ value: function _updateMastheadRefsV1() {
140
+ var masthead = this._elements.masthead;
141
+ this._elements.mastheadL0 = masthead.shadowRoot.querySelector(".".concat(bxPrefix, "--masthead__l0"));
142
+ this._elements.mastheadL1 = masthead.querySelector("".concat(ddsPrefix, "-masthead-l1"));
143
+ }
144
+
145
+ /**
146
+ * Temporary method to find v2 masthead sub-elements.
147
+ */
148
+ }, {
149
+ key: "_updateMastheadRefsV2",
150
+ value: function _updateMastheadRefsV2() {
151
+ var masthead = this._elements.masthead;
152
+ this._elements.mastheadL0 = masthead.shadowRoot.querySelector(".".concat(prefix, "--masthead__l0"));
153
+ this._elements.mastheadL1 = masthead.querySelector("".concat(c4dPrefix, "-masthead-l1"));
154
+ }
155
+
156
+ /**
157
+ * Temporary method to find v1 table of contents sub-elements.
158
+ */
159
+ }, {
160
+ key: "_updateTableOfContentsRefsV1",
161
+ value: function _updateTableOfContentsRefsV1() {
162
+ var toc = this._elements.tableOfContents;
74
163
  var tocRoot = toc.shadowRoot;
75
- this._tableOfContentsInnerBar = tocRoot.querySelector(".".concat(prefix, "--tableofcontents__navbar"));
76
- if (window.innerWidth > gridBreakpoint) {
77
- if (toc.layout === 'horizontal') {
78
- this._tableOfContentsLayout = 'horizontal';
79
- } else {
80
- this._tableOfContentsInnerBar = tocRoot.querySelector(".".concat(c4dPrefix, "-ce--table-of-contents__items-container"));
164
+ var selectors = {
165
+ desktop: {
166
+ vertical: ".".concat(ddsPrefix, "-ce--table-of-contents__items-container"),
167
+ horizontal: ".".concat(bxPrefix, "--tableofcontents__navbar")
168
+ },
169
+ mobile: {
170
+ vertical: ".".concat(bxPrefix, "--tableofcontents__sidebar"),
171
+ horizontal: ".".concat(bxPrefix, "--tableofcontents__navbar")
81
172
  }
82
- }
173
+ };
174
+ var viewportDimension = window.innerWidth >= gridBreakpoint ? 'desktop' : 'mobile';
175
+ this._elements.tableOfContentsInnerBar = tocRoot.querySelector(selectors[viewportDimension][toc.layout || 'vertical']);
176
+ }
177
+
178
+ /**
179
+ * Temporary method to find v2 table of contents sub-elements.
180
+ */
181
+ }, {
182
+ key: "_updateTableOfContentsRefsV2",
183
+ value: function _updateTableOfContentsRefsV2() {
184
+ var toc = this._elements.tableOfContents;
185
+ var tocRoot = toc.shadowRoot;
186
+ this._elements.tableOfContentsInnerBar = tocRoot.querySelector(window.innerWidth >= gridBreakpoint && (toc === null || toc === void 0 ? void 0 : toc.layout) !== 'horizontal' ? ".".concat(c4dPrefix, "-ce--table-of-contents__items-container") : ".".concat(prefix, "--tableofcontents__navbar"));
187
+ }
188
+
189
+ /**
190
+ * Stores references to TOC sub-elements that are relevant to current viewport
191
+ * dimensions.
192
+ */
193
+ }, {
194
+ key: "_updateTableOfContentsRefs",
195
+ value: function _updateTableOfContentsRefs() {
196
+ var toc = this._elements.tableOfContents;
197
+ this._updateRefsV1orV2(toc, this._updateTableOfContentsRefsV1, this._updateTableOfContentsRefsV2);
83
198
  }
84
199
  }, {
85
200
  key: "banner",
86
201
  set: function set(component) {
87
202
  if (this._validateComponent(component, "".concat(c4dPrefix, "-global-banner"))) {
88
- this._banner = component;
89
- this.hasBanner = true;
90
- if (this._masthead) {
91
- this._masthead.setAttribute('with-banner', '');
203
+ this._elements.banner = component;
204
+ this._state.hasBanner = true;
205
+ if (this._elements.masthead) {
206
+ this._elements.masthead.setAttribute('with-banner', '');
92
207
  }
93
- this._calculateCumulativeHeight();
208
+ this._manageStickyElements();
94
209
  }
95
210
  }
96
211
  }, {
97
- key: "leadspaceWithSearch",
212
+ key: "leadspaceSearch",
98
213
  set: function set(component) {
99
214
  if (this._validateComponent(component, "".concat(c4dPrefix, "-leadspace-with-search"))) {
100
- this._leadspaceWithSearch = component;
101
- var leadspaceSearchBar = component.shadowRoot.querySelector(".".concat(prefix, "--search-container"));
102
- this._leadspaceSearchBar = leadspaceSearchBar;
103
- this._leadspaceWithSearchInput = component.querySelector("".concat(c4dPrefix, "-search-with-typeahead"));
104
- this._leadspaceWithSearchStickyThreshold = parseInt(window.getComputedStyle(leadspaceSearchBar).paddingBottom) - 16;
105
- this._calculateCumulativeHeight();
215
+ this._elements.leadspaceSearch = component;
216
+ this._updateRefsV1orV2(component, this._updateLeadspaceRefsV1, this._updateLeadspaceRefsV2);
217
+ this._state.leadspaceSearchThreshold = parseInt(window.getComputedStyle(this._elements.leadspaceSearchBar).paddingBottom) - 16;
218
+ this._manageStickyElements();
106
219
  }
107
220
  }
108
221
  }, {
109
222
  key: "localeModal",
110
223
  set: function set(component) {
111
224
  if (this._validateComponent(component, "".concat(c4dPrefix, "-locale-modal"))) {
112
- this._localeModal = component;
113
- this._calculateCumulativeHeight();
225
+ this._elements.localeModal = component;
226
+ this._manageStickyElements();
114
227
  }
115
228
  }
116
229
  }, {
117
230
  key: "masthead",
118
231
  set: function set(component) {
119
232
  if (this._validateComponent(component, "".concat(c4dPrefix, "-masthead"))) {
120
- this._masthead = component;
121
- if (this._banner) {
122
- this._masthead.setAttribute('with-banner', '');
233
+ this._elements.masthead = component;
234
+ if (this._elements.banner) {
235
+ this._elements.masthead.setAttribute('with-banner', '');
123
236
  }
124
- this._mastheadL0 = component.shadowRoot.querySelector(".".concat(prefix, "--masthead__l0"));
125
- this._mastheadL1 = component.querySelector("".concat(c4dPrefix, "-masthead-l1"));
126
- this._calculateCumulativeHeight();
237
+ this._updateRefsV1orV2(component, this._updateMastheadRefsV1, this._updateMastheadRefsV2);
238
+ this._manageStickyElements();
127
239
  }
128
240
  }
129
241
  }, {
130
242
  key: "tableOfContents",
131
243
  set: function set(component) {
132
244
  if (this._validateComponent(component, "".concat(c4dPrefix, "-table-of-contents"))) {
133
- this._tableOfContents = component;
134
- this._tableOfContentsStickyUpdate();
135
- this._resizeObserver.observe(this._tableOfContents);
136
- this._calculateCumulativeHeight();
245
+ this._elements.tableOfContents = component;
246
+ this._updateTableOfContentsRefs();
247
+ this._resizeObserver.observe(this._elements.tableOfContents);
248
+ this._manageStickyElements();
137
249
  }
138
250
  }
139
251
 
@@ -146,7 +258,7 @@ var StickyHeader = /*#__PURE__*/function () {
146
258
  var _this = this;
147
259
  if (!this._throttled) {
148
260
  this._throttled = true;
149
- this._calculateCumulativeHeight();
261
+ this._manageStickyElements();
150
262
  setTimeout(function () {
151
263
  _this._throttled = false;
152
264
  }, 20);
@@ -155,95 +267,197 @@ var StickyHeader = /*#__PURE__*/function () {
155
267
  }, {
156
268
  key: "_handleResize",
157
269
  value: function _handleResize() {
158
- var hasBanner = this._hasBanner,
159
- masthead = this._masthead,
160
- toc = this._tableOfContents,
161
- tocLayout = this._tableOfContentsLayout,
162
- leadspaceSearchBar = this._leadspaceSearchBar;
270
+ var hasBanner = this._state._hasBanner;
271
+ var _this$_elements = this._elements,
272
+ masthead = _this$_elements.masthead,
273
+ toc = _this$_elements.tableOfContents,
274
+ leadspaceSearchBar = _this$_elements.leadspaceSearchBar;
163
275
  if (toc && masthead) {
164
- this._tableOfContentsStickyUpdate();
165
- if (window.innerWidth >= gridBreakpoint && tocLayout !== 'horizontal' && !hasBanner) {
166
- masthead.style.top = '0';
276
+ this._updateTableOfContentsRefs();
277
+ if (window.innerWidth >= gridBreakpoint && toc.layout !== 'horizontal' && !hasBanner) {
278
+ masthead.style.insetBlockStart = '0';
167
279
  } else {
168
- // This has to happen after the tocStickyUpdate method.
169
- var tocInner = this._tableOfContentsInnerBar;
280
+ // This has to happen after the _updateTableOfContentsRefs method.
281
+ var tocInner = this._elements.tableOfContentsInnerBar;
170
282
  if (masthead.offsetTop === 0) {
171
- tocInner.style.top = "".concat(masthead.offsetHeight, "px");
283
+ tocInner.style.insetBlockStart = "".concat(masthead.offsetHeight, "px");
172
284
  }
173
285
  }
174
- this._calculateCumulativeHeight();
286
+ this._manageStickyElements();
175
287
  }
176
288
  if (leadspaceSearchBar) {
177
- this._leadspaceWithSearchStickyThreshold = parseInt(window.getComputedStyle(leadspaceSearchBar).paddingBottom) - 16;
289
+ this._state.leadspaceSearchThreshold = parseInt(window.getComputedStyle(leadspaceSearchBar).paddingBottom) - 16;
178
290
  }
179
291
  }
292
+
293
+ /**
294
+ * Handles the banner given the current scroll position.
295
+ */
180
296
  }, {
181
- key: "_calculateCumulativeHeight",
182
- value: function _calculateCumulativeHeight() {
183
- var _StickyHeader$global = StickyHeader.global,
184
- oldY = _StickyHeader$global._lastScrollPosition,
185
- banner = _StickyHeader$global._banner,
186
- masthead = _StickyHeader$global._masthead,
187
- mastheadL0 = _StickyHeader$global._mastheadL0,
188
- mastheadL1 = _StickyHeader$global._mastheadL1,
189
- localeModal = _StickyHeader$global._localeModal,
190
- toc = _StickyHeader$global._tableOfContents,
191
- tocInner = _StickyHeader$global._tableOfContentsInnerBar,
192
- leadspaceSearch = _StickyHeader$global._leadspaceWithSearch,
193
- leadspaceSearchBar = _StickyHeader$global._leadspaceSearchBar,
194
- leadspaceSearchInput = _StickyHeader$global._leadspaceWithSearchInput,
195
- leadspaceSearchThreshold = _StickyHeader$global._leadspaceWithSearchStickyThreshold;
196
- var customPropertyName = this.constructor.customPropertyName;
197
- if (localeModal && localeModal.hasAttribute('open')) {
198
- return;
199
- }
200
- var newY = window.scrollY;
201
- this._lastScrollPosition = Math.max(0, newY);
297
+ key: "_handleBanner",
298
+ value: function _handleBanner() {
299
+ var banner = this._elements.banner;
300
+ var scrollPos = this._state.scrollPos;
301
+ this._state.cumulativeOffset += Math.max(banner.offsetHeight - scrollPos, 0);
302
+ }
202
303
 
203
- /**
204
- * maxScrollaway is a calculated value matching the height of all components
205
- * that are allowed to hide above the viewport.
206
- *
207
- * We should only have one sticky header showing as the page scrolls down.
208
- *
209
- * Items that stick, in order
210
- * - L0
211
- * - L1
212
- * - The TOC in horizontal bar form
213
- * - The leadspace with search (if no TOC)
214
- */
215
- var maxScrollaway = 0;
304
+ /**
305
+ * Handles the masthead given the current scroll position.
306
+ */
307
+ }, {
308
+ key: "_handleMasthead",
309
+ value: function _handleMasthead() {
310
+ var masthead = this._elements.masthead;
311
+ masthead.style.transition = 'none';
312
+ masthead.style.insetBlockStart = "".concat(this._state.cumulativeOffset, "px");
216
313
 
217
- // Calculate maxScrollaway values based on TOC positon
218
- var tocIsAtTop = false;
219
- var tocShouldStick = false;
220
- if (tocInner) {
221
- tocIsAtTop = tocInner.getBoundingClientRect().top <= (masthead ? masthead.offsetTop + masthead.offsetHeight : 0) + 1;
222
- tocShouldStick = toc.layout === 'horizontal' || window.innerWidth < gridBreakpoint;
223
- if (masthead && tocIsAtTop && (tocShouldStick || mastheadL1)) {
224
- maxScrollaway += masthead.offsetHeight;
225
- if (mastheadL1 && !tocShouldStick) {
226
- maxScrollaway -= mastheadL1.offsetHeight;
227
- }
228
- } else if (mastheadL0 && mastheadL1) {
229
- maxScrollaway += mastheadL0.offsetHeight;
314
+ // Masthead always sticks, therefore always add its height.
315
+ this._state.cumulativeOffset += masthead.offsetHeight;
316
+ }
317
+
318
+ /**
319
+ * Handles the table of contents given the current scroll position.
320
+ */
321
+ }, {
322
+ key: "_handleToc",
323
+ value: function _handleToc() {
324
+ var tableOfContentsInnerBar = this._elements.tableOfContentsInnerBar;
325
+ var tocShouldStick = this._state.tocShouldStick;
326
+ tableOfContentsInnerBar.style.transition = 'none';
327
+ tableOfContentsInnerBar.style.insetBlockStart = "".concat(this._state.cumulativeOffset, "px");
328
+ var tocIsStuck = Math.round(tableOfContentsInnerBar.getBoundingClientRect().top) <= this._state.cumulativeOffset + 1;
329
+ if (tocShouldStick && tocIsStuck) {
330
+ this._state.cumulativeOffset += tableOfContentsInnerBar.offsetHeight;
331
+ }
332
+ }
333
+
334
+ /**
335
+ * Handles the leadspace search given the current scroll position.
336
+ */
337
+ }, {
338
+ key: "_handleLeadspaceSearch",
339
+ value: function _handleLeadspaceSearch() {
340
+ var _this$_elements2 = this._elements,
341
+ leadspaceSearch = _this$_elements2.leadspaceSearch,
342
+ leadspaceSearchBar = _this$_elements2.leadspaceSearchBar,
343
+ leadspaceSearchInput = _this$_elements2.leadspaceSearchInput;
344
+ var leadspaceSearchThreshold = this._state.leadspaceSearchThreshold;
345
+ var searchShouldBeSticky = leadspaceSearch.getBoundingClientRect().bottom <= leadspaceSearchThreshold;
346
+ var searchIsSticky = leadspaceSearch.hasAttribute('sticky-search');
347
+ if (searchShouldBeSticky) {
348
+ if (!searchIsSticky) {
349
+ leadspaceSearch.style.paddingBottom = "".concat(leadspaceSearchBar.offsetHeight, "px");
350
+ leadspaceSearch.setAttribute('sticky-search', '');
351
+ leadspaceSearchInput.setAttribute('large', '');
352
+ window.requestAnimationFrame(function () {
353
+ leadspaceSearchBar.style.transitionDuration = '110ms';
354
+ leadspaceSearchBar.style.transform = 'translateY(0)';
355
+ });
230
356
  }
357
+ leadspaceSearchBar.style.insetBlockStart = "".concat(this._state.cumulativeOffset, "px");
358
+ this._state.cumulativeOffset += leadspaceSearchBar.offsetHeight;
359
+ } else if (searchIsSticky) {
360
+ leadspaceSearch.style.paddingBottom = '';
361
+ leadspaceSearch.removeAttribute('sticky-search');
362
+ leadspaceSearchInput.removeAttribute('large');
363
+ leadspaceSearchBar.style.transitionDuration = '';
364
+ leadspaceSearchBar.style.transform = '';
365
+ leadspaceSearchBar.style.insetBlockStart = '';
366
+ }
367
+ }
368
+
369
+ /**
370
+ * Calculates a value matching the height of all components that are allowed
371
+ * to hide above the viewport.
372
+ *
373
+ * Adding an item's height to this value indicates we expect it to be hidden
374
+ * above the viewport.
375
+ *
376
+ * Items that stick, in order
377
+ * - L0
378
+ * - L1
379
+ * - The TOC in horizontal bar form
380
+ * - The leadspace with search (if no TOC)
381
+ */
382
+ }, {
383
+ key: "_calculateMaxScrollaway",
384
+ value: function _calculateMaxScrollaway() {
385
+ var _this$_elements3 = this._elements,
386
+ masthead = _this$_elements3.masthead,
387
+ mastheadL0 = _this$_elements3.mastheadL0,
388
+ mastheadL1 = _this$_elements3.mastheadL1,
389
+ tableOfContents = _this$_elements3.tableOfContents,
390
+ tableOfContentsInnerBar = _this$_elements3.tableOfContentsInnerBar,
391
+ leadspaceSearchBar = _this$_elements3.leadspaceSearchBar;
392
+
393
+ // Reset the value before performing any further calculations.
394
+ this._state.maxScrollaway = 0;
395
+
396
+ // Collect conditions we may want to test for to make logic easier to read.
397
+ this._state.tocShouldStick = tableOfContents ? tableOfContents.layout === 'horizontal' || window.innerWidth < gridBreakpoint : false;
398
+ this._state.tocIsAtTop = tableOfContentsInnerBar ? tableOfContentsInnerBar.getBoundingClientRect().top <= this.height + 1 : false;
399
+ this._state.searchIsAtTop = leadspaceSearchBar ? leadspaceSearchBar.getBoundingClientRect().top <= this.height + 1 : false;
400
+ this._state.tocIsAtSearch = leadspaceSearchBar && tableOfContentsInnerBar ? tableOfContentsInnerBar.getBoundingClientRect().top <= leadspaceSearchBar.getBoundingClientRect().bottom : false;
401
+ this._state.mastheadL0IsActive = Boolean(masthead === null || masthead === void 0 ? void 0 : masthead.querySelector('[expanded]'));
402
+ this._state.mastheadL1IsActive = mastheadL1 && mastheadL1.hasAttribute('active');
403
+ var _this$_state = this._state,
404
+ tocShouldStick = _this$_state.tocShouldStick,
405
+ tocIsAtTop = _this$_state.tocIsAtTop,
406
+ searchIsAtTop = _this$_state.searchIsAtTop,
407
+ tocIsAtSearch = _this$_state.tocIsAtSearch,
408
+ mastheadL0IsActive = _this$_state.mastheadL0IsActive,
409
+ mastheadL1IsActive = _this$_state.mastheadL1IsActive;
410
+
411
+ // Begin calculating maxScrollAway.
412
+
413
+ // If L0 is open, lock it to the top of the page.
414
+ if (mastheadL0 && mastheadL0IsActive) {
415
+ this._state.maxScrollaway = 0;
231
416
  }
417
+ // If L1 is open, lock it to the top of the page.
418
+ else if (mastheadL1IsActive && mastheadL0) {
419
+ this._state.maxScrollaway = mastheadL0.offsetHeight;
420
+ } else {
421
+ // In cases where we have both an eligible ToC and leadspace search, we want
422
+ // the ToC to take precedence. Scroll away leadspace search.
423
+ if (searchIsAtTop && tocIsAtSearch && tocShouldStick) {
424
+ this._state.maxScrollaway += leadspaceSearchBar.offsetHeight;
425
+ }
232
426
 
233
- // Calculate maxScrollaway values based on leadspace search position
234
- if (!tocInner && leadspaceSearchBar) {
235
- var searchIsAtTop = leadspaceSearchBar.getBoundingClientRect().top <= (masthead ? masthead.offsetTop + masthead.offsetHeight : 0) + 1;
236
- if (masthead && searchIsAtTop) {
237
- maxScrollaway += masthead.offsetHeight;
427
+ // Scroll away entire masthead if either ToC or leadspace search is eligible
428
+ // to be the stuck element (unless L1 is open). Otherwise, scroll away the
429
+ // L0 if we have an L1.
430
+ if (searchIsAtTop || tocIsAtTop && tocShouldStick) {
431
+ if (masthead) {
432
+ this._state.maxScrollaway += masthead.offsetHeight;
433
+ }
434
+ } else if (masthead && mastheadL0 && mastheadL1) {
435
+ this._state.maxScrollaway += mastheadL0.offsetHeight;
238
436
  }
239
437
  }
438
+ }
439
+
440
+ /**
441
+ * Positions sticky elements. Does so by checking the scroll position and where
442
+ * tracked elements are in relation to it, then applying the correct styles to
443
+ * each element in succession to ensure that only one element is stuck to the
444
+ * top of the page, and all other elements that have been scrolled past can be
445
+ * revealed when scrolling back up.
446
+ */
447
+ }, {
448
+ key: "_positionElements",
449
+ value: function _positionElements() {
450
+ var _this$_elements4 = this._elements,
451
+ banner = _this$_elements4.banner,
452
+ masthead = _this$_elements4.masthead,
453
+ tocInner = _this$_elements4.tableOfContentsInnerBar,
454
+ leadspaceSearchBar = _this$_elements4.leadspaceSearchBar;
455
+ var oldY = this._state.scrollPosPrevious;
240
456
 
241
457
  /**
242
- * Cumulative offset is a calculated value used to set the `top` property of
243
- * components that stick to the top of the viewport.
244
- *
245
- * This value is equal to the difference between the previous scrollY and
246
- * the current scrollY values, but is positively and negatively limited.
458
+ * Reset to a value that is equal to the difference between the previous
459
+ * scrollY and the current scrollY values, but is positively and negatively
460
+ * limited.
247
461
  *
248
462
  * Positive limit: 0
249
463
  * all elements visible, starting at the top of the viewport.
@@ -253,55 +467,58 @@ var StickyHeader = /*#__PURE__*/function () {
253
467
  * with the elements that should be visible starting at the top of the
254
468
  * viewport.
255
469
  */
256
- var cumulativeOffset = Math.max(Math.min((masthead ? masthead.offsetTop : 0) + oldY - newY, 0), maxScrollaway * -1);
470
+ this._state.cumulativeOffset = Math.max(Math.min((masthead ? masthead.offsetTop : 0) + oldY - this._state.scrollPos, 0), this._state.maxScrollaway * -1);
471
+
472
+ /**
473
+ * Handle each potentially sticky element in the order we expect them to
474
+ * appear on the page. Important to do this sequentially for
475
+ * cumulativeOffset to be correctly calculated by the time each of these
476
+ * methods accesses it.
477
+ *
478
+ * To-do: One idea for improving this so the execution order doesn't matter
479
+ * is to collect our elements into an array ordered by document position,
480
+ * then loop over that array and execute a corresponding handler method.
481
+ */
257
482
  if (banner) {
258
- cumulativeOffset += Math.max(banner.offsetHeight - newY, 0);
483
+ this._handleBanner();
259
484
  }
260
485
  if (masthead) {
261
- masthead.style.transition = 'none';
262
- masthead.style.top = "".concat(cumulativeOffset, "px");
263
- cumulativeOffset += masthead.offsetHeight;
486
+ this._handleMasthead();
487
+ }
488
+ if (leadspaceSearchBar) {
489
+ this._handleLeadspaceSearch();
264
490
  }
265
491
  if (tocInner) {
266
- tocInner.style.transition = 'none';
267
- tocInner.style.top = "".concat(cumulativeOffset, "px");
268
- tocShouldStick = toc.layout === 'horizontal' || window.innerWidth < gridBreakpoint;
269
- var tocIsStuck = Math.round(tocInner.getBoundingClientRect().top) <= cumulativeOffset + 1;
270
- if (tocShouldStick && tocIsStuck) {
271
- cumulativeOffset += tocInner.offsetHeight;
272
- }
492
+ this._handleToc();
273
493
  }
274
- if (!tocInner && leadspaceSearchBar) {
275
- var searchShouldBeSticky = leadspaceSearch.getBoundingClientRect().bottom <= leadspaceSearchThreshold;
276
- var searchIsSticky = leadspaceSearch.hasAttribute('sticky-search');
277
- if (searchShouldBeSticky) {
278
- if (!searchIsSticky) {
279
- leadspaceSearch.style.paddingBottom = "".concat(leadspaceSearchBar.offsetHeight, "px");
280
- leadspaceSearch.setAttribute('sticky-search', '');
281
- leadspaceSearchInput.setAttribute('large', '');
282
- window.requestAnimationFrame(function () {
283
- leadspaceSearchBar.style.transitionDuration = '110ms';
284
- leadspaceSearchBar.style.transform = 'translateY(0)';
285
- });
286
- }
287
- leadspaceSearchBar.style.top = "".concat(cumulativeOffset, "px");
288
- cumulativeOffset += leadspaceSearchBar.offsetHeight;
289
- }
290
- if (!searchShouldBeSticky && searchIsSticky) {
291
- leadspaceSearch.removeAttribute('sticky-search');
292
- leadspaceSearch.style.paddingBottom = '';
293
- leadspaceSearchBar.style.top = '';
294
- leadspaceSearchBar.style.transitionDuration = '';
295
- leadspaceSearchBar.style.transform = '';
296
- leadspaceSearchInput.removeAttribute('large');
297
- }
494
+ }
495
+
496
+ /**
497
+ * Manages which elements are stuck and where they are positioned. We should
498
+ * only have one element stuck to the top of the viewport as the page scrolls
499
+ * down.
500
+ */
501
+ }, {
502
+ key: "_manageStickyElements",
503
+ value: function _manageStickyElements() {
504
+ var localeModal = this._elements.localeModal;
505
+ var scrollPosPrevious = this._state.scrollPos;
506
+
507
+ // Exit early if locale modal is open.
508
+ if (localeModal && localeModal.hasAttribute('open')) {
509
+ return;
298
510
  }
299
511
 
300
- // Set internal property for use in scripts
301
- this._cumulativeHeight = cumulativeOffset;
512
+ // Store scroll positions.
513
+ this._state.scrollPosPrevious = scrollPosPrevious;
514
+ this._state.scrollPos = Math.max(0, window.scrollY);
515
+
516
+ // Given the current state, calculate how elements should be positioned.
517
+ this._calculateMaxScrollaway();
518
+ this._positionElements();
302
519
 
303
520
  // Set custom property for use in stylesheets
304
- root.document.documentElement.style.setProperty(customPropertyName, "".concat(this._cumulativeHeight, "px"));
521
+ root.document.documentElement.style.setProperty(this.constructor.customPropertyName, "".concat(this._state.cumulativeOffset, "px"));
305
522
  }
306
523
  }], [{
307
524
  key: "global",