Skip to content

Jon/fix/ios-carousel #5548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- added: Moonpay Sell support for ACH.
- added: Maestro `testID` support in `NotificationCard`
- changed: Scanning private key button copy updated from "Import" -> "Confirm"
- fixed: iOS not immediately showing `EdgeCarousel` cards if only one card is present
- fixed: `SceneWrapper` bottom inset calculations for scenes that do not `avoidKeyboard`

## 4.26.0 (2025-04-14)
Expand Down
35 changes: 32 additions & 3 deletions src/components/common/EdgeCarousel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useState } from 'react'
import { ListRenderItem, View } from 'react-native'
import { InteractionManager, ListRenderItem, Platform, View } from 'react-native'
import Carousel, { Pagination } from 'react-native-snap-carousel'

import { useAsyncEffect } from '../../hooks/useAsyncEffect'
import { useHandler } from '../../hooks/useHandler'
import { cacheStyles, Theme, useTheme } from '../services/ThemeContext'

Expand All @@ -26,16 +27,44 @@ export function EdgeCarousel<T>(props: Props<T>): JSX.Element {
const carouselRef = React.useRef<Carousel<any>>(null)

const [activeIndex, setActiveIndex] = useState(0)
const [dataLocal, setDataLocal] = useState(data)

React.useEffect(() => {
setDataLocal(data)
}, [data])

const renderItem = useHandler<ListRenderItem<T>>(info => (
<View style={[styles.childContainer, { width: width * 0.9, height }]}>{props.renderItem(info)}</View>
))

/**
* Carousel's FlatList bug workaround. Fixes the issue where items are
* hidden until scroll actions are performed either in the carousel or on the
* scene itself.
*/
useAsyncEffect(
async () => {
// HACK: With 1 item, this is the only way to force a render in iOS
if (Platform.OS === 'ios' && dataLocal.length === 1) {
setDataLocal([])
setTimeout(() => setDataLocal(data), 50)
}
// The built-in hack fn works for all other cases
else if (carouselRef.current != null) {
await InteractionManager.runAfterInteractions(() => {
carouselRef.current?.triggerRenderingHack()
})
}
},
[data],
'triggerRenderingHack'
)

return (
<View style={styles.carouselContainer}>
<Carousel
ref={carouselRef}
data={data}
data={dataLocal}
keyExtractor={keyExtractor}
renderItem={renderItem}
sliderWidth={width}
Expand All @@ -55,7 +84,7 @@ export function EdgeCarousel<T>(props: Props<T>): JSX.Element {
marginTop: -theme.rem(1),
marginBottom: -theme.rem(1)
}}
dotsLength={data.length}
dotsLength={dataLocal.length}
activeDotIndex={activeIndex}
tappableDots={carouselRef.current != null}
dotStyle={styles.dotStyle}
Expand Down
Loading