Skip to content

Commit 0604f77

Browse files
authored
fix: Transparent ExpandableCalendar wix#2653
Refactor header layout and height calculations
1 parent 474ec8a commit 0604f77

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/expandableCalendar/index.tsx

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,21 @@ const ExpandableCalendar = forwardRef<ExpandableCalendarRef, ExpandableCalendarP
143143

144144
const [screenReaderEnabled, setScreenReaderEnabled] = useState(false);
145145
const [headerHeight, setHeaderHeight] = useState(0);
146-
const onHeaderLayout = useCallback(({nativeEvent: {layout: {height}}}: LayoutChangeEvent) => {
147-
setHeaderHeight(height || DEFAULT_HEADER_HEIGHT);
148-
}, []);
146+
const shouldMeasureHeader = useRef(true);
147+
const onHeaderLayout = useCallback(
148+
({
149+
nativeEvent: {
150+
layout: {height}
151+
}
152+
}: LayoutChangeEvent) => {
153+
const _height = (height || DEFAULT_HEADER_HEIGHT) + 5;
154+
if (height !== headerHeight) {
155+
setHeaderHeight(_height);
156+
}
157+
shouldMeasureHeader.current = false;
158+
},
159+
[headerHeight]
160+
);
149161

150162
/** Date */
151163

@@ -191,22 +203,24 @@ const ExpandableCalendar = forwardRef<ExpandableCalendarRef, ExpandableCalendarP
191203

192204
const [position, setPosition] = useState(numberOfDays ? Positions.CLOSED : initialPosition);
193205
const isOpen = position === Positions.OPEN;
194-
const getOpenHeight = () => {
206+
const getOpenHeight = useCallback(() => {
195207
if (!horizontal) {
196208
return Math.max(constants.screenHeight, constants.screenWidth);
197209
}
198210
return headerHeight + (WEEK_HEIGHT * (numberOfWeeks.current)) + (hideKnob ? 0 : KNOB_CONTAINER_HEIGHT);
199-
};
211+
}, [headerHeight, horizontal, hideKnob, numberOfWeeks]);
212+
200213
const openHeight = useRef(getOpenHeight());
201214
const closedHeight = useMemo(() => headerHeight + WEEK_HEIGHT + (hideKnob || Number(numberOfDays) > 1 ? 0 : KNOB_CONTAINER_HEIGHT), [numberOfDays, hideKnob, headerHeight]);
202-
const startHeight = useMemo(() => isOpen ? openHeight.current : closedHeight, [closedHeight, isOpen]);
215+
const startHeight = useMemo(() => isOpen ? getOpenHeight() : closedHeight, [closedHeight, isOpen, getOpenHeight]);
203216
const _height = useRef(startHeight);
204217
const deltaY = useMemo(() => new Animated.Value(startHeight), [startHeight]);
205218
const headerDeltaY = useRef(new Animated.Value(isOpen ? -headerHeight : 0));
206219

207220
useEffect(() => {
208221
_height.current = startHeight;
209222
deltaY.setValue(startHeight);
223+
_wrapperStyles.current.style.height = startHeight;
210224
}, [startHeight]);
211225

212226
useEffect(() => {
@@ -637,7 +651,13 @@ const ExpandableCalendar = forwardRef<ExpandableCalendarRef, ExpandableCalendarP
637651
renderArrow={_renderArrow}
638652
/>
639653
) : (
640-
<Animated.View testID={`${testID}.expandableContainer`} ref={wrapper} style={wrapperStyle} {...panResponder.panHandlers}>
654+
<Animated.View
655+
testID={`${testID}.expandableContainer`}
656+
ref={wrapper}
657+
style={wrapperStyle}
658+
onLayout={shouldMeasureHeader.current ? onHeaderLayout : undefined}
659+
{...panResponder.panHandlers}
660+
>
641661
{renderCalendarList()}
642662
{renderWeekCalendar()}
643663
{!hideKnob && renderKnob()}

0 commit comments

Comments
 (0)