-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handling different item sizes in virtualized list
- Loading branch information
Showing
17 changed files
with
1,970 additions
and
1,415 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
packages/example/src/modules/program/view/ProgramLandscape.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import styled from '@emotion/native'; | ||
import React from 'react'; | ||
import { Animated, Image, View } from 'react-native'; | ||
import { ProgramInfo } from '../domain/programInfo'; | ||
import { useFocusAnimation } from '../../../design-system/helpers/useFocusAnimation'; | ||
import { useSpatialNavigatorFocusableAccessibilityProps } from 'react-tv-space-navigation'; | ||
|
||
type ProgramProps = { | ||
isFocused?: boolean; | ||
programInfo: ProgramInfo; | ||
}; | ||
|
||
export const ProgramLandscape = React.forwardRef<View, ProgramProps>( | ||
({ isFocused = false, programInfo }, ref) => { | ||
const imageSource = programInfo.image; | ||
|
||
const scaleAnimation = useFocusAnimation(isFocused); | ||
|
||
const accessibilityProps = useSpatialNavigatorFocusableAccessibilityProps(); | ||
|
||
return ( | ||
<ProgramContainer | ||
style={scaleAnimation} // Apply the animated scale transform | ||
ref={ref} | ||
isFocused={isFocused} | ||
{...accessibilityProps} | ||
> | ||
<ProgramImage source={imageSource} /> | ||
</ProgramContainer> | ||
); | ||
}, | ||
); | ||
|
||
ProgramLandscape.displayName = 'ProgramSquare'; | ||
|
||
const ProgramContainer = styled(Animated.View)<{ isFocused: boolean }>(({ isFocused, theme }) => ({ | ||
height: theme.sizes.program.portrait.height, | ||
width: theme.sizes.program.landscape.width * 2, | ||
overflow: 'hidden', | ||
borderRadius: 20, | ||
borderColor: isFocused ? theme.colors.primary.light : 'transparent', | ||
borderWidth: 3, | ||
})); | ||
|
||
const ProgramImage = styled(Image)({ | ||
height: '100%', | ||
width: '100%', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/example/src/modules/program/view/ProgramListVariableSize.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import styled from '@emotion/native'; | ||
import { useTheme } from '@emotion/react'; | ||
import { useNavigation } from '@react-navigation/native'; | ||
import { NativeStackNavigationProp } from '@react-navigation/native-stack'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { SpatialNavigationVirtualizedList } from 'react-tv-space-navigation'; | ||
import { RootStackParamList } from '../../../../App'; | ||
import { ProgramNode } from './ProgramNode'; | ||
import { scaledPixels } from '../../../design-system/helpers/scaledPixels'; | ||
import { VariableSizeProgramInfo } from '../domain/variableSizeProgramInfo'; | ||
import { ProgramNodeLandscape } from './ProgramNodeLandscape'; | ||
import { getVariableSizePrograms } from '../infra/variableSizeProgramInfo'; | ||
|
||
const NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN = 7; | ||
const WINDOW_SIZE = NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN + 8; | ||
const ROW_PADDING = scaledPixels(70); | ||
|
||
export const ProgramList = ({ | ||
orientation, | ||
containerStyle, | ||
}: { | ||
orientation?: 'vertical' | 'horizontal'; | ||
containerStyle?: object; | ||
}) => { | ||
const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>(); | ||
|
||
const renderItem = useCallback( | ||
({ item }: { item: VariableSizeProgramInfo }) => | ||
item.type === 'portrait' ? ( | ||
<ProgramNode | ||
programInfo={item} | ||
onSelect={() => navigation.push('ProgramDetail', { programInfo: item })} | ||
/> | ||
) : ( | ||
<ProgramNodeLandscape | ||
programInfo={item} | ||
onSelect={() => navigation.push('ProgramDetail', { programInfo: item })} | ||
/> | ||
), | ||
[navigation], | ||
); | ||
const theme = useTheme(); | ||
|
||
const programInfos = useMemo(() => getVariableSizePrograms(1000), []); | ||
|
||
return ( | ||
<Container style={containerStyle}> | ||
<SpatialNavigationVirtualizedList | ||
orientation={orientation} | ||
data={programInfos} | ||
renderItem={renderItem} | ||
itemSize={(item) => | ||
item.type === 'portrait' | ||
? theme.sizes.program.portrait.width + 30 | ||
: theme.sizes.program.landscape.width * 2 + 45 | ||
} | ||
numberOfRenderedItems={WINDOW_SIZE} | ||
numberOfItemsVisibleOnScreen={NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN} | ||
onEndReachedThresholdItemsNumber={NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN} | ||
scrollBehavior="stick-to-end" | ||
/> | ||
</Container> | ||
); | ||
}; | ||
|
||
export const ProgramsRowVariableSize = ({ containerStyle }: { containerStyle?: object }) => { | ||
const theme = useTheme(); | ||
return ( | ||
<ProgramList | ||
containerStyle={{ | ||
...containerStyle, | ||
height: theme.sizes.program.portrait.height + ROW_PADDING, | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
const Container = styled.View(({ theme }) => ({ | ||
backgroundColor: theme.colors.background.mainHover, | ||
padding: theme.spacings.$8, | ||
borderRadius: scaledPixels(20), | ||
overflow: 'hidden', | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/example/src/modules/program/view/ProgramNodeLandscape.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { SpatialNavigationNode } from 'react-tv-space-navigation'; | ||
|
||
import { ProgramInfo } from '../domain/programInfo'; | ||
import { ProgramLandscape } from './ProgramLandscape'; | ||
|
||
type Props = { | ||
programInfo: ProgramInfo; | ||
onSelect?: () => void; | ||
}; | ||
|
||
export const ProgramNodeLandscape = ({ programInfo, onSelect }: Props) => { | ||
return ( | ||
<SpatialNavigationNode isFocusable onSelect={onSelect}> | ||
{({ isFocused }) => <ProgramLandscape isFocused={isFocused} programInfo={programInfo} />} | ||
</SpatialNavigationNode> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.