@@ -16,13 +16,14 @@ import {
1616 ImageSourcePropType ,
1717 GestureResponderEvent ,
1818 PanResponderGestureState ,
19- TouchableOpacity
19+ TouchableOpacity ,
20+ type LayoutChangeEvent
2021} from 'react-native' ;
2122
2223import { page } from '../dateutils' ;
2324import { parseDate , toMarkingFormat } from '../interface' ;
2425import { DateData , Direction } from '../types' ;
25- import styleConstructor , { HEADER_HEIGHT , KNOB_CONTAINER_HEIGHT } from './style' ;
26+ import styleConstructor , { KNOB_CONTAINER_HEIGHT } from './style' ;
2627import WeekDaysNames from '../commons/WeekDaysNames' ;
2728import Calendar from '../calendar' ;
2829import CalendarList , { CalendarListProps } from '../calendar-list' ;
@@ -38,7 +39,6 @@ export enum Positions {
3839}
3940const SPEED = 20 ;
4041const BOUNCINESS = 6 ;
41- const CLOSED_HEIGHT = 120 ; // header + 1 week
4242const WEEK_HEIGHT = 46 ;
4343const DAY_NAMES_PADDING = 24 ;
4444const PAN_GESTURE_THRESHOLD = 30 ;
@@ -126,6 +126,10 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
126126 } = props ;
127127
128128 const [ screenReaderEnabled , setScreenReaderEnabled ] = useState ( false ) ;
129+ const [ headerHeight , setHeaderHeight ] = useState ( 0 ) ;
130+ const onHeaderLayout = useCallback ( ( { nativeEvent : { layout : { height} } } : LayoutChangeEvent ) => {
131+ setHeaderHeight ( height ) ;
132+ } , [ ] ) ;
129133
130134 /** Date */
131135
@@ -175,20 +179,24 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
175179 if ( ! horizontal ) {
176180 return Math . max ( constants . screenHeight , constants . screenWidth ) ;
177181 }
178- return CLOSED_HEIGHT + ( WEEK_HEIGHT * ( numberOfWeeks . current - 1 ) ) + ( hideKnob ? 12 : KNOB_CONTAINER_HEIGHT ) + ( constants . isAndroid ? 3 : 0 ) ;
182+ return headerHeight + ( WEEK_HEIGHT * ( numberOfWeeks . current ) ) + ( hideKnob ? 0 : KNOB_CONTAINER_HEIGHT ) ;
179183 } ;
180184 const openHeight = useRef ( getOpenHeight ( ) ) ;
181- const closedHeight = useMemo ( ( ) => CLOSED_HEIGHT + ( hideKnob || Number ( numberOfDays ) > 1 ? 0 : KNOB_CONTAINER_HEIGHT ) , [ numberOfDays , hideKnob ] ) ;
185+ const closedHeight = useMemo ( ( ) => headerHeight + WEEK_HEIGHT + ( hideKnob || Number ( numberOfDays ) > 1 ? 0 : KNOB_CONTAINER_HEIGHT ) , [ numberOfDays , hideKnob , headerHeight ] ) ;
182186 const startHeight = useMemo ( ( ) => isOpen ? openHeight . current : closedHeight , [ closedHeight , isOpen ] ) ;
183187 const _height = useRef ( startHeight ) ;
184188 const deltaY = useMemo ( ( ) => new Animated . Value ( startHeight ) , [ startHeight ] ) ;
185- const headerDeltaY = useRef ( new Animated . Value ( isOpen ? - HEADER_HEIGHT : 0 ) ) ;
189+ const headerDeltaY = useRef ( new Animated . Value ( isOpen ? - headerHeight : 0 ) ) ;
186190
187191 useEffect ( ( ) => {
188192 _height . current = startHeight ;
189193 deltaY . setValue ( startHeight ) ;
190194 } , [ startHeight ] ) ;
191195
196+ useEffect ( ( ) => {
197+ openHeight . current = getOpenHeight ( ) ;
198+ } , [ headerHeight ] ) ;
199+
192200
193201 useEffect ( ( ) => {
194202 if ( numberOfDays ) {
@@ -209,7 +217,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
209217 const themeObject = Object . assign ( headerStyleOverride , theme ) ;
210218
211219 const _wrapperStyles = useRef ( { style : { height : startHeight } } ) ;
212- const _headerStyles = { style : { top : isOpen ? - HEADER_HEIGHT : 0 } } ;
220+ const _headerStyles = { style : { top : isOpen ? - headerHeight : 0 } } ;
213221 const _weekCalendarStyles = { style : { opacity : isOpen ? 0 : 1 } } ;
214222
215223 const shouldHideArrows = ! horizontal ? true : hideArrows || false ;
@@ -238,16 +246,16 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
238246 } , [ calendarStyle ] ) ;
239247
240248 const animatedHeaderStyle = useMemo ( ( ) => {
241- return [ style . current . header , { height : HEADER_HEIGHT + 10 , top : headerDeltaY . current } ] ;
242- } , [ headerDeltaY . current ] ) ;
249+ return [ style . current . header , { height : headerHeight , top : headerDeltaY . current } ] ;
250+ } , [ headerDeltaY . current , headerHeight ] ) ;
243251
244252 const weekCalendarStyle = useMemo ( ( ) => {
245- return [ style . current . weekContainer , isOpen ? style . current . hidden : style . current . visible ] ;
246- } , [ isOpen ] ) ;
253+ return [ style . current . weekContainer , isOpen ? style . current . hidden : style . current . visible , { top : headerHeight } ] ;
254+ } , [ isOpen , headerHeight ] ) ;
247255
248256 const containerStyle = useMemo ( ( ) => {
249- return [ allowShadow && style . current . containerShadow , propsStyle ] ;
250- } , [ allowShadow , propsStyle ] ) ;
257+ return [ allowShadow && style . current . containerShadow , propsStyle , headerHeight === 0 && style . current . hidden ] ;
258+ } , [ allowShadow , propsStyle , headerHeight ] ) ;
251259
252260 const wrapperStyle = useMemo ( ( ) => {
253261 return { height : deltaY } ;
@@ -343,7 +351,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
343351
344352 if ( ! horizontal ) {
345353 // vertical CalenderList header
346- _headerStyles . style . top = Math . min ( Math . max ( - gestureState . dy , - HEADER_HEIGHT ) , 0 ) ;
354+ _headerStyles . style . top = Math . min ( Math . max ( - gestureState . dy , - headerHeight ) , 0 ) ;
347355 } else {
348356 // horizontal Week view
349357 if ( ! isOpen ) {
@@ -370,7 +378,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
370378 onPanResponderMove : handlePanResponderMove ,
371379 onPanResponderRelease : handlePanResponderEnd ,
372380 onPanResponderTerminate : handlePanResponderEnd
373- } ) : PanResponder . create ( { } ) , [ numberOfDays , position ] ) ;
381+ } ) : PanResponder . create ( { } ) , [ numberOfDays , position , headerHeight ] ) ; // All the functions here rely on headerHeight directly or indirectly through refs that are updated in useEffect
374382
375383 /** Animated */
376384
@@ -572,6 +580,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
572580 horizontal = { horizontal }
573581 firstDay = { firstDay }
574582 calendarStyle = { calendarStyle }
583+ onHeaderLayout = { onHeaderLayout }
575584 { ...others }
576585 current = { date }
577586 theme = { themeObject }
0 commit comments