Skip to content

Commit

Permalink
+ core: add sticky helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
catouse committed Jan 26, 2024
1 parent 05b7a3f commit 22b0641
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/core/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './download-file';
export * from './bus';
export * from './hotkeys';
export * from './fullscreen';
export * from './sticky';
30 changes: 30 additions & 0 deletions lib/core/src/helpers/sticky.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {Component} from '../component';

export type StickyOptios = {
side?: 'top' | 'bottom';
offset?: number;
zIndex?: number;
pinnedClass?: string;
};

export class Sticky extends Component<StickyOptios> {
static NAME = 'Sticky';

protected declare _ob: IntersectionObserver;

init() {
const {offset = 1, side, zIndex, pinnedClass = 'is-pinned'} = this.options;
const {$element} = this;
$element.css({position: 'sticky', zIndex});
if (side) $element.css(side, -offset);
this._ob = new IntersectionObserver(
([e]) => e.target.classList.toggle(pinnedClass, e.intersectionRatio < offset),
{threshold: [1]},
);
this._ob.observe($element[0] as HTMLElement);
}

destroy() {
this._ob.disconnect();
}
}

0 comments on commit 22b0641

Please sign in to comment.