Skip to content

Commit

Permalink
fix: replace Dimensions by useWindowDimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroBern committed Jan 25, 2021
1 parent 2f88e66 commit d21b4b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
21 changes: 17 additions & 4 deletions example/src/Shared/MaterialTabBar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import React from 'react'
import { View, Text, StyleSheet, Pressable, Dimensions } from 'react-native'
import {
View,
Text,
StyleSheet,
Pressable,
useWindowDimensions,
} from 'react-native'
import { TabBarProps } from 'react-native-collapsible-tab-view'
import Animated, { useAnimatedStyle } from 'react-native-reanimated'

Expand All @@ -8,8 +14,6 @@ type TabItemProps = {
onPress: () => void
}

const windowWidth = Dimensions.get('window').width

export const TABBAR_HEIGHT = 48

const TabItem: React.FC<TabItemProps> = ({ name, onPress }) => {
Expand All @@ -35,8 +39,17 @@ const TabBar: React.FC<TabBarProps<any>> = ({
index,
containerRef,
}) => {
const windowWidth = useWindowDimensions().width
const [nTabs] = React.useState(Object.keys(refMap).length)
const [indicatorWidth] = React.useState(windowWidth / nTabs)
const [indicatorWidth, setIndicatorWidth] = React.useState(
windowWidth / nTabs
)

React.useEffect(() => {
if (indicatorWidth * nTabs !== windowWidth) {
setIndicatorWidth(windowWidth / nTabs)
}
}, [windowWidth, nTabs, indicatorWidth])

const scrollTo = React.useCallback(
(i: number) => {
Expand Down
25 changes: 16 additions & 9 deletions src/createCollapsibleTabs.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react'
import {
FlatList as RNFlatList,
Dimensions,
View,
StyleSheet,
LayoutChangeEvent,
useWindowDimensions,
} from 'react-native'
import Animated, {
useSharedValue,
Expand All @@ -19,16 +19,8 @@ import Animated, {

import { Props, ContextType, ScrollViewProps, FlatListProps } from './types'

const windowWidth = Dimensions.get('window').width

const AnimatedFlatList = Animated.createAnimatedComponent(RNFlatList)

export const getItemLayout = (_: unknown, index: number) => ({
length: windowWidth,
offset: windowWidth * index,
index,
})

const createCollapsibleTabs = <T extends string>() => {
const Context = React.createContext<ContextType<T> | undefined>(undefined)

Expand All @@ -55,6 +47,8 @@ const createCollapsibleTabs = <T extends string>() => {
lazy,
cancelLazyFazeIn,
}) => {
const windowWidth = useWindowDimensions().width

const [containerHeight, setContainerHeight] = React.useState<
number | undefined
>(undefined)
Expand All @@ -78,6 +72,15 @@ const createCollapsibleTabs = <T extends string>() => {
// @ts-ignore
const focusedTab = useSharedValue<T>(tabNames.value[index.value])

const getItemLayout = React.useCallback(
(_: unknown, index: number) => ({
length: windowWidth,
offset: windowWidth * index,
index,
}),
[windowWidth]
)

// derived from scrollX, to calculate the next offset and index
useAnimatedReaction(
() => {
Expand Down Expand Up @@ -450,6 +453,8 @@ const createCollapsibleTabs = <T extends string>() => {

const scrollHandler = useScrollHandlerY(name)

const windowWidth = useWindowDimensions().width

return (
<AnimatedFlatList
// @ts-ignore
Expand Down Expand Up @@ -488,6 +493,8 @@ const createCollapsibleTabs = <T extends string>() => {

const scrollHandler = useScrollHandlerY(name)

const windowWidth = useWindowDimensions().width

return (
<Animated.ScrollView
ref={refMap[name] as any}
Expand Down

0 comments on commit d21b4b5

Please sign in to comment.