Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/bootstrap.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeoSot <geo.sotis@gmail.com>2022-07-18 15:20:31 +0300
committerGeoSot <geo.sotis@gmail.com>2022-07-27 17:40:21 +0300
commit417149bc20c459766c37d5d637f4130f740a3345 (patch)
tree33387107f6e82f8dee53ab9d1d9b4831f99fb82d
parent16723b52330370e97d48aa6470aed6bb02329e9a (diff)
add tests and support markup
-rw-r--r--js/src/scrollspy.js14
-rw-r--r--js/tests/unit/scrollspy.spec.js27
-rw-r--r--site/content/docs/5.2/components/scrollspy.md2
3 files changed, 36 insertions, 7 deletions
diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js
index 83080e800f..f4d6671013 100644
--- a/js/src/scrollspy.js
+++ b/js/src/scrollspy.js
@@ -112,6 +112,13 @@ class ScrollSpy extends BaseComponent {
// TODO: on v6 target should be given explicitly & remove the {target: 'ss-target'} case
config.target = getElement(config.target) || document.body
+ // TODO: v6 Only for backwards compatibility reasons. Use rootMargin only
+ config.rootMargin = config.offset ? `${config.offset}px 0px -30%` : config.rootMargin
+
+ if (typeof config.threshold === 'string') {
+ config.threshold = config.threshold.split(',').map(value => Number.parseFloat(value))
+ }
+
return config
}
@@ -144,7 +151,7 @@ class ScrollSpy extends BaseComponent {
const options = {
root: this._rootElement,
threshold: this._config.threshold,
- rootMargin: this._getRootMargin()
+ rootMargin: this._config.rootMargin
}
return new IntersectionObserver(entries => this._observerCallback(entries), options)
@@ -189,11 +196,6 @@ class ScrollSpy extends BaseComponent {
}
}
- // TODO: v6 Only for backwards compatibility reasons. Use rootMargin only
- _getRootMargin() {
- return this._config.offset ? `${this._config.offset}px 0px -30%` : this._config.rootMargin
- }
-
_initializeTargetsAndObservables() {
this._targetLinks = new Map()
this._observableSections = new Map()
diff --git a/js/tests/unit/scrollspy.spec.js b/js/tests/unit/scrollspy.spec.js
index 61edeb921a..b1c8053033 100644
--- a/js/tests/unit/scrollspy.spec.js
+++ b/js/tests/unit/scrollspy.spec.js
@@ -132,7 +132,7 @@ describe('ScrollSpy', () => {
' <a class="nav-link active" id="one-link" href="#">One</a>' +
'</ul>',
'<div id="content">',
- ' <div id="one-link" style="height: 300px;">test</div>',
+ ' <div id="one-link">test</div>',
'</div>'
].join('')
@@ -144,6 +144,31 @@ describe('ScrollSpy', () => {
expect(scrollSpy._observer.thresholds).toEqual([1])
})
+ it('should respect threshold option mark-up', () => {
+ fixtureEl.innerHTML = [
+ '<ul id="navigation" class="navbar">',
+ ' <a class="nav-link active" id="one-link" href="#">One</a>' +
+ '</ul>',
+ '<div id="content" data-bs-threshold="0,0.2,1">',
+ ' <div id="one-link">test</div>',
+ '</div>'
+ ].join('')
+
+ const scrollSpy = new ScrollSpy('#content', {
+ target: '#navigation'
+ })
+
+ const expectToBeCloseToArray = (actual, expected) => {
+ expect(actual.length).toBe(expected.length)
+ for (const x of actual) {
+ const i = actual.indexOf(x)
+ expect(x).withContext(`[${i}]`).toBeCloseTo(expected[i])
+ }
+ }
+
+ expectToBeCloseToArray(scrollSpy._observer.thresholds, [0, 0.2, 1])
+ })
+
it('should not take count to not visible sections', () => {
fixtureEl.innerHTML = [
'<nav id="navigation" class="navbar">',
diff --git a/site/content/docs/5.2/components/scrollspy.md b/site/content/docs/5.2/components/scrollspy.md
index 5e329dc85a..9b8b761d69 100644
--- a/site/content/docs/5.2/components/scrollspy.md
+++ b/site/content/docs/5.2/components/scrollspy.md
@@ -380,6 +380,8 @@ const scrollSpy = new bootstrap.ScrollSpy(document.body, {
| `rootMargin` | string | `0px 0px -40%` | Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin) valid units, when calculating scroll position. |
| `smoothScroll` | boolean | `false` | Enables smooth scrolling when a user clicks on a link that refers to ScrollSpy observables. |
| `target` | string \| jQuery object \| DOM element | | Specifies element to apply Scrollspy plugin. |
+| `threshold` | array | `[0.1, 0.5, 1]` | Intersection Observer [threshold](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#threshold) valid input, when calculating scroll position.|
+
{{< /bs-table >}}
{{< callout warning >}}