Skip to content

Commit

Permalink
Merge branch 'main' into main-lmp-navbar-version-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-deramond authored Aug 23, 2022
2 parents 2f22730 + 723d920 commit b093404
Show file tree
Hide file tree
Showing 23 changed files with 631 additions and 295 deletions.
2 changes: 1 addition & 1 deletion .bundlewatch.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
{
"path": "./dist/js/boosted.bundle.min.js",
"maxSize": "24.5 kB"
"maxSize": "24.75 kB"
},
{
"path": "./dist/js/boosted.esm.js",
Expand Down
3 changes: 1 addition & 2 deletions .stylelintrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"ignore": "local"
}
],
"scss/selector-no-union-class-name": true,
"scss/selector-no-redundant-nesting-selector": null
"scss/selector-no-union-class-name": true
}
}
22 changes: 13 additions & 9 deletions js/src/scrollspy.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ const Default = {
offset: null, // TODO: v6 @deprecated, keep it for backwards compatibility reasons
rootMargin: '0px 0px -25%',
smoothScroll: false,
target: null
target: null,
threshold: [0.1, 0.5, 1]
}

const DefaultType = {
offset: '(number|null)', // TODO v6 @deprecated, keep it for backwards compatibility reasons
rootMargin: 'string',
smoothScroll: 'boolean',
target: 'element'
target: 'element',
threshold: 'array'
}

/**
Expand Down Expand Up @@ -110,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
}

Expand Down Expand Up @@ -141,8 +150,8 @@ class ScrollSpy extends BaseComponent {
_getNewObserver() {
const options = {
root: this._rootElement,
threshold: [0.1, 0.5, 1],
rootMargin: this._getRootMargin()
threshold: this._config.threshold,
rootMargin: this._config.rootMargin
}

return new IntersectionObserver(entries => this._observerCallback(entries), options)
Expand Down Expand Up @@ -187,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()
Expand Down
4 changes: 4 additions & 0 deletions js/src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ class Tooltip extends BaseComponent {
this.tip.remove()
}

if (this._config.originalTitle) {
this._element.setAttribute('title', this._config.originalTitle)
}

this._disposePopper()
super.dispose()
}
Expand Down
44 changes: 44 additions & 0 deletions js/tests/unit/scrollspy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,50 @@ describe('ScrollSpy', () => {
expect(scrollSpy._rootElement).toBeNull()
})

it('should respect threshold option', () => {
fixtureEl.innerHTML = [
'<ul id="navigation" class="navbar">',
' <a class="nav-link active" id="one-link" href="#">One</a>' +
'</ul>',
'<div id="content">',
' <div id="one-link">test</div>',
'</div>'
].join('')

const scrollSpy = new ScrollSpy('#content', {
target: '#navigation',
threshold: [1]
})

expect(scrollSpy._observer.thresholds).toEqual([1])
})

it('should respect threshold option markup', () => {
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'
})

// See https://stackoverflow.com/a/45592926
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">',
Expand Down
19 changes: 19 additions & 0 deletions js/tests/unit/tooltip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,25 @@ describe('Tooltip', () => {
tooltip.show()
})
})

it('should destroy a tooltip and reset it\'s initial title', () => {
fixtureEl.innerHTML = [
'<span id="tooltipWithTitle" rel="tooltip" title="tooltipTitle"></span>',
'<span id="tooltipWithoutTitle" rel="tooltip" data-bs-title="tooltipTitle"></span>'
].join('')

const tooltipWithTitleEl = fixtureEl.querySelector('#tooltipWithTitle')
const tooltip = new Tooltip('#tooltipWithTitle')
expect(tooltipWithTitleEl.getAttribute('title')).toBeNull()
tooltip.dispose()
expect(tooltipWithTitleEl.getAttribute('title')).toBe('tooltipTitle')

const tooltipWithoutTitleEl = fixtureEl.querySelector('#tooltipWithoutTitle')
const tooltip2 = new Tooltip('#tooltipWithTitle')
expect(tooltipWithoutTitleEl.getAttribute('title')).toBeNull()
tooltip2.dispose()
expect(tooltipWithoutTitleEl.getAttribute('title')).toBeNull()
})
})

describe('show', () => {
Expand Down
Loading

0 comments on commit b093404

Please sign in to comment.