@@ -19,7 +19,7 @@ import * as React from "react";
19
19
20
20
import { Container } from "@tiller-ds/core" ;
21
21
import { ComponentTokens , cx , TokenProps , useTokens } from "@tiller-ds/theme" ;
22
- import { createNamedContext } from "@tiller-ds/util" ;
22
+ import { createNamedContext , useWindowEvent } from "@tiller-ds/util" ;
23
23
24
24
type StackedLayoutProps = {
25
25
/**
@@ -48,6 +48,12 @@ type StackedLayoutProps = {
48
48
*/
49
49
navigation : React . ReactNode ;
50
50
51
+ /**
52
+ * Defines whether the StackedLayout header statys fixed or hides when page is scrolled.
53
+ * Defaults to true.
54
+ */
55
+ hideOnScroll ?: boolean ;
56
+
51
57
/**
52
58
* Custom additional class name for the main container component.
53
59
*/
@@ -82,8 +88,6 @@ type StackedLayoutContext = {
82
88
containerVariant ?: "max" | "fullWidth" | "constrainedPadded" | "fullWidthContainer" | "narrowConstrained" | undefined ;
83
89
isScrolling ?: boolean ;
84
90
isFixed ?: boolean ;
85
- headingHeight : number ;
86
- setHeadingHeight : React . Dispatch < number > ;
87
91
} ;
88
92
89
93
const StackedLayoutContext = createNamedContext < StackedLayoutContext > ( "StackedLayoutContext" ) ;
@@ -94,15 +98,16 @@ function StackedLayout({
94
98
isFixed,
95
99
children,
96
100
background = "gray" ,
101
+ hideOnScroll = true ,
97
102
className,
98
103
...props
99
104
} : StackedLayoutProps ) {
100
105
const tokens = useTokens ( "StackedLayout" , props . tokens ) ;
101
106
const [ isScrolling , setIsScrolling ] = React . useState < boolean > ( false ) ;
102
- const [ headingHeight , setHeadingHeight ] = React . useState < number > ( 0 ) ;
103
107
104
108
const navigationContainerClassName = cx ( {
105
- "transition duration-300 w-full sticky top-0 z-30" : isFixed ,
109
+ [ tokens . navigationContainer . fixed ] : isFixed ,
110
+ [ tokens . navigationContainer . scrolled ] : isScrolling ,
106
111
} ) ;
107
112
108
113
const containerClassName = cx (
@@ -112,21 +117,20 @@ function StackedLayout({
112
117
{ [ tokens . grayBackgroundColor ] : background !== "white" }
113
118
) ;
114
119
115
- window . onscroll = function ( ) {
116
- if ( window . pageYOffset > 80 ) {
117
- setIsScrolling ( true ) ;
118
- } else {
119
- setIsScrolling ( false ) ;
120
+ useWindowEvent ( "scroll" , ( ) => {
121
+ if ( hideOnScroll ) {
122
+ if ( window . pageYOffset > 80 ) {
123
+ setIsScrolling ( true ) ;
124
+ } else {
125
+ setIsScrolling ( false ) ;
126
+ }
120
127
}
121
- } ;
128
+ } ) ;
122
129
123
130
return (
124
- < StackedLayoutContext . Provider value = { { containerVariant, isScrolling, isFixed, headingHeight , setHeadingHeight } } >
131
+ < StackedLayoutContext . Provider value = { { containerVariant, isScrolling, isFixed } } >
125
132
< div className = { containerClassName } >
126
- < div
127
- className = { navigationContainerClassName }
128
- style = { { transform : isScrolling && isFixed ? "translate(0, -64px)" : "" } }
129
- >
133
+ < div className = { navigationContainerClassName } >
130
134
{ navigation }
131
135
</ div >
132
136
{ children }
@@ -143,14 +147,9 @@ function StackedLayoutHeading({
143
147
} : StackedLayoutHeadingProps ) {
144
148
const tokens = useTokens ( "StackedLayout" , props . tokens ) ;
145
149
const layoutContext = React . useContext ( StackedLayoutContext ) as StackedLayoutContext ;
146
- const ref = React . useRef < HTMLDivElement > ( null ) ;
147
-
148
- React . useEffect ( ( ) => {
149
- layoutContext . setHeadingHeight ( ref . current ?. clientHeight ?? 0 ) ;
150
- } , [ ref . current ] ) ;
151
150
152
151
const headingContainerClassName = cx ( {
153
- "bg-gray-100 transition duration-300 sticky top-0 w-full z-20" : layoutContext ?. isFixed ,
152
+ [ tokens . heading . fixed ] : layoutContext ?. isFixed ,
154
153
} ) ;
155
154
156
155
const headingClassName = cx (
@@ -160,7 +159,7 @@ function StackedLayoutHeading({
160
159
) ;
161
160
162
161
return (
163
- < div ref = { ref } className = { headingContainerClassName } >
162
+ < div className = { headingContainerClassName } >
164
163
< div className = { headingClassName } >
165
164
< Container variant = { layoutContext . containerVariant } > { children } </ Container >
166
165
</ div >
0 commit comments