-
Notifications
You must be signed in to change notification settings - Fork 642
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14927 from craftcms/feature/cms-14765-sticky-scro…
…llbar Feature/cms 14765 sticky scrollbar
- Loading branch information
Showing
9 changed files
with
144 additions
and
26 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,104 @@ | ||
/** | ||
* Proxy scrollbar | ||
* | ||
* Display a scrollbar that is synced with another element | ||
* | ||
* @property {string} scroller - The selector of the element that will be scrolled | ||
* @property {string} content - The selector of the element within the scroller containing the overflow content | ||
* @property {boolean} hidden - Whether the scrollbar should be hidden | ||
* @property {HTMLElement} proxy - The element that represents the scrollbar | ||
* @property {HTMLElement} scroller - The element that will be scrolled | ||
* @property {HTMLElement} content - The element within the scroller containing the overflow content | ||
*/ | ||
class CraftProxyScrollbar extends HTMLElement { | ||
static observedAttributes = ['hidden']; | ||
|
||
get hidden() { | ||
return this.getAttribute('hidden'); | ||
} | ||
|
||
get hasOverflow() { | ||
return this.content?.scrollWidth > this.scroller?.clientWidth; | ||
} | ||
|
||
connectedCallback() { | ||
this.ignoreScrollEvent = false; | ||
this.animation = false; | ||
|
||
this.scroller = document.querySelector(this.getAttribute('scroller')); | ||
this.content = document.querySelector(this.getAttribute('content')); | ||
|
||
if (!this.scroller || !this.content) { | ||
return; | ||
} | ||
|
||
this.proxy = document.createElement('div'); | ||
this.proxy.style.height = '1px'; | ||
this.proxy.style.width = this.content.getBoundingClientRect().width + 'px'; | ||
|
||
this.appendChild(this.proxy); | ||
|
||
this.addEventListener('scroll', this.syncScroll(this.scroller, this)); | ||
this.scroller.addEventListener( | ||
'scroll', | ||
this.syncScroll(this, this.scroller) | ||
); | ||
window.addEventListener('resize', this.handleResize.bind(this)); | ||
|
||
Object.assign(this.style, { | ||
display: this.hasOverflow ? 'block' : 'none', | ||
overflowX: 'scroll', | ||
}); | ||
} | ||
|
||
attributeChangedCallback(name, oldValue, newValue) { | ||
if (name === 'hidden') { | ||
this.style.display = newValue ? 'none' : 'block'; | ||
} | ||
} | ||
|
||
disconnectedCallback() { | ||
this.proxy.remove(); | ||
|
||
this.scroller.removeEventListener( | ||
'scroll', | ||
this.syncScroll(this.scroller, this) | ||
); | ||
this.scroller.removeEventListener( | ||
'scroll', | ||
this.syncScroll(this, this.scroller) | ||
); | ||
|
||
window.removeEventListener('resize', this.handleResize.bind(this)); | ||
} | ||
|
||
handleResize() { | ||
this.proxy.style.width = this.content.getBoundingClientRect().width + 'px'; | ||
|
||
if (this.hasOverflow) { | ||
this.removeAttribute('hidden'); | ||
} else { | ||
this.setAttribute('hidden', 'true'); | ||
} | ||
} | ||
|
||
syncScroll(a, b) { | ||
return () => { | ||
if (this.ignoreScrollEvent) { | ||
return false; | ||
} | ||
|
||
if (this.animation) { | ||
cancelAnimationFrame(this.animation); | ||
} | ||
|
||
this.animation = requestAnimationFrame(() => { | ||
this.ignoreScrollEvent = true; | ||
a.scrollLeft = b.scrollLeft; | ||
this.ignoreScrollEvent = false; | ||
}); | ||
}; | ||
} | ||
} | ||
|
||
customElements.define('craft-proxy-scrollbar', CraftProxyScrollbar); |
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