mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-28 22:43:30 +03:00
Converted <GhCanvasHeader> to glimmer component
no issue - added `{{on-scroll}}` modifier to replace custom setup and teardown of event handlers inside the component
This commit is contained in:
parent
96a2e016b1
commit
99bfde4417
@ -1,6 +1,5 @@
|
||||
<div
|
||||
{{did-insert (action "initScrollWatch")}}
|
||||
{{will-destroy (action "clearScrollWatch")}}
|
||||
{{on-scroll this.onScroll scrollContainer=".gh-main"}}
|
||||
...attributes
|
||||
>
|
||||
<header class="gh-canvas-header-content">
|
||||
|
@ -1,33 +1,13 @@
|
||||
import Component from '@ember/component';
|
||||
import classic from 'ember-classic-decorator';
|
||||
import Component from '@glimmer/component';
|
||||
import {action} from '@ember/object';
|
||||
import {run} from '@ember/runloop';
|
||||
import {tagName} from '@ember-decorators/component';
|
||||
|
||||
@classic
|
||||
@tagName('')
|
||||
export default class GhCanvasHeader extends Component {
|
||||
@action
|
||||
initScrollWatch(element) {
|
||||
this._onScroll = run.bind(this, this.onScroll, element);
|
||||
this._scrollContainer = element.closest('.gh-main');
|
||||
if (this._scrollContainer) {
|
||||
this._scrollContainer.addEventListener('scroll', this._onScroll, {passive: true});
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
clearScrollWatch() {
|
||||
if (this._scrollContainer) {
|
||||
this._scrollContainer.removeEventListener('scroll', this._onScroll);
|
||||
}
|
||||
}
|
||||
|
||||
onScroll(element) {
|
||||
if (this._isSticky && this._scrollContainer.scrollTop < 10) {
|
||||
onScroll(element, scrollContainer) {
|
||||
if (this._isSticky && scrollContainer.scrollTop < 10) {
|
||||
element.classList.remove('gh-canvas-header--sticky');
|
||||
this._isSticky = false;
|
||||
} else if (!this._isSticky && this._scrollContainer.scrollTop > 10) {
|
||||
} else if (!this._isSticky && scrollContainer.scrollTop > 10) {
|
||||
element.classList.add('gh-canvas-header--sticky');
|
||||
this._isSticky = true;
|
||||
}
|
||||
|
23
ghost/admin/app/modifiers/on-scroll.js
Normal file
23
ghost/admin/app/modifiers/on-scroll.js
Normal file
@ -0,0 +1,23 @@
|
||||
import Modifier from 'ember-modifier';
|
||||
import {action} from '@ember/object';
|
||||
|
||||
export default class OnScrollModifier extends Modifier {
|
||||
@action
|
||||
onScroll(event) {
|
||||
this.args.positional[0](this.element, this.scrollContainer, event);
|
||||
}
|
||||
|
||||
didInstall() {
|
||||
this.scrollContainer = this.element;
|
||||
|
||||
if (this.args.named.scrollContainer) {
|
||||
this.scrollContainer = this.element.closest(this.args.named.scrollContainer);
|
||||
}
|
||||
|
||||
this.scrollContainer?.addEventListener('scroll', this.onScroll, {passive: true});
|
||||
}
|
||||
|
||||
willDestroy() {
|
||||
this.scrollContainer?.removeEventListener('scroll', this.onScroll, {passive: true});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user