@@ -2,15 +2,19 @@ import { EventData } from "../../components/app_context";
22import BasicWidget from "../basic_widget" ;
33import Container from "./container" ;
44import NoteContext from "../../components/note_context" ;
5+ import "./content_header.css" ;
56
67export default class ContentHeader extends Container < BasicWidget > {
7-
8+
89 noteContext ?: NoteContext ;
910 thisElement ?: HTMLElement ;
1011 parentElement ?: HTMLElement ;
1112 resizeObserver : ResizeObserver ;
1213 currentHeight : number = 0 ;
1314 currentSafeMargin : number = NaN ;
15+ previousScrollTop : number = 0 ;
16+ isFloating : boolean = false ;
17+ scrollThreshold : number = 10 ; // pixels before triggering float
1418
1519 constructor ( ) {
1620 super ( ) ;
@@ -35,7 +39,36 @@ export default class ContentHeader extends Container<BasicWidget> {
3539 this . thisElement = this . $widget . get ( 0 ) ! ;
3640
3741 this . resizeObserver . observe ( this . thisElement ) ;
38- this . parentElement . addEventListener ( "scroll" , this . updateSafeMargin . bind ( this ) ) ;
42+ this . parentElement . addEventListener ( "scroll" , this . updateScrollState . bind ( this ) , { passive : true } ) ;
43+ }
44+
45+ updateScrollState ( ) {
46+ const currentScrollTop = this . parentElement ! . scrollTop ;
47+ const isScrollingUp = currentScrollTop < this . previousScrollTop ;
48+ const hasMovedEnough = Math . abs ( currentScrollTop - this . previousScrollTop ) > this . scrollThreshold ;
49+
50+ if ( hasMovedEnough ) {
51+ this . setFloating ( isScrollingUp ) ;
52+ this . previousScrollTop = currentScrollTop ;
53+ }
54+
55+ this . updateSafeMargin ( ) ;
56+ }
57+
58+ setFloating ( shouldFloat : boolean ) {
59+ if ( shouldFloat !== this . isFloating ) {
60+ this . isFloating = shouldFloat ;
61+
62+ if ( shouldFloat ) {
63+ this . $widget . addClass ( "floating" ) ;
64+ // Set CSS variable so ribbon can position itself below the floating header
65+ this . parentElement ! . style . setProperty ( "--content-header-height" , `${ this . currentHeight } px` ) ;
66+ } else {
67+ this . $widget . removeClass ( "floating" ) ;
68+ // Reset CSS variable when header is not floating
69+ this . parentElement ! . style . removeProperty ( "--content-header-height" ) ;
70+ }
71+ }
3972 }
4073
4174 updateSafeMargin ( ) {
@@ -60,4 +93,4 @@ export default class ContentHeader extends Container<BasicWidget> {
6093 }
6194 }
6295
63- }
96+ }
0 commit comments