-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(mon-pix): extract auto-scroll logic in service
- Loading branch information
Showing
9 changed files
with
163 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { action } from '@ember/object'; | ||
import Service from '@ember/service'; | ||
|
||
export default class ModulixAutoScroll extends Service { | ||
#SCROLL_OFFSET_PX = 70; | ||
|
||
@action | ||
setHTMLElementScrollOffsetCssProperty(htmlElement) { | ||
htmlElement.style.setProperty('--scroll-offset', `${this.#SCROLL_OFFSET_PX}px`); | ||
} | ||
|
||
focusAndScroll( | ||
htmlElement, | ||
{ scroll, areUserPrefersMatches, getWindowScrollY } = { | ||
scroll: this.#scroll, | ||
areUserPrefersMatches: this.#areUserPrefersMatches, | ||
getWindowScrollY: this.#getWindowScrollY, | ||
}, | ||
) { | ||
htmlElement.focus({ preventScroll: true }); | ||
|
||
const elementY = htmlElement.getBoundingClientRect().top + getWindowScrollY(); | ||
scroll({ | ||
top: elementY - this.#SCROLL_OFFSET_PX, | ||
behavior: areUserPrefersMatches() ? 'instant' : 'smooth', | ||
}); | ||
} | ||
|
||
#scroll(args) { | ||
window.scroll(args); | ||
} | ||
|
||
#areUserPrefersMatches() { | ||
const userPrefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)'); | ||
return userPrefersReducedMotion.matches; | ||
} | ||
|
||
#getWindowScrollY() { | ||
return window.scrollY; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { setupTest } from 'ember-qunit'; | ||
import { module, test } from 'qunit'; | ||
import sinon from 'sinon'; | ||
|
||
module('Unit | Services | Module | ModulixAutoScroll', function (hooks) { | ||
setupTest(hooks); | ||
|
||
module('#setHTMLElementScrollOffsetCssProperty', function () { | ||
test('should set --scroll-offset to the given html element', function (assert) { | ||
// given | ||
const modulixAutoScrollService = this.owner.lookup('service:modulix-auto-scroll'); | ||
const htmlElement = document.createElement('div'); | ||
|
||
// when | ||
modulixAutoScrollService.setHTMLElementScrollOffsetCssProperty(htmlElement); | ||
|
||
// then | ||
assert.strictEqual(htmlElement.style.cssText, '--scroll-offset: 70px;'); | ||
}); | ||
}); | ||
|
||
module('#focusAndScroll', function () { | ||
test('should call focus on given html element', function (assert) { | ||
// given | ||
const modulixAutoScrollService = this.owner.lookup('service:modulix-auto-scroll'); | ||
const htmlElement = document.createElement('div'); | ||
htmlElement.focus = sinon.stub(); | ||
|
||
// when | ||
modulixAutoScrollService.focusAndScroll(htmlElement); | ||
|
||
// then | ||
sinon.assert.calledWith(htmlElement.focus, { preventScroll: true }); | ||
assert.ok(true); | ||
}); | ||
|
||
test('should call scroll to the given html element', function (assert) { | ||
// given | ||
const scrollOffset = 70; | ||
const htmlElementBoundingClientRectTop = 20; | ||
const windowScrollY = 5; | ||
const calculatedElementY = htmlElementBoundingClientRectTop + windowScrollY; | ||
const scrollToTop = calculatedElementY - scrollOffset; | ||
|
||
const modulixAutoScrollService = this.owner.lookup('service:modulix-auto-scroll'); | ||
const htmlElement = document.createElement('div'); | ||
htmlElement.getBoundingClientRect = sinon.stub().returns({ | ||
top: htmlElementBoundingClientRectTop, | ||
}); | ||
const scrollStub = sinon.stub(); | ||
const areUserPrefersMatchesStub = sinon.stub(); | ||
areUserPrefersMatchesStub.returns(true); | ||
const getWindowScrollYStub = sinon.stub(); | ||
getWindowScrollYStub.returns(windowScrollY); | ||
|
||
// when | ||
modulixAutoScrollService.focusAndScroll(htmlElement, { | ||
scroll: scrollStub, | ||
areUserPrefersMatches: areUserPrefersMatchesStub, | ||
getWindowScrollY: getWindowScrollYStub, | ||
}); | ||
|
||
// then | ||
sinon.assert.calledWith(scrollStub, { top: scrollToTop, behavior: 'instant' }); | ||
assert.ok(true); | ||
}); | ||
}); | ||
}); |