-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added slider to blog home page
- Loading branch information
1 parent
30ec2c0
commit 1389ef8
Showing
6 changed files
with
178 additions
and
9 deletions.
There are no files selected for viewing
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,20 @@ | ||
import { View } from 'react-native'; | ||
|
||
const SliderDots = ({ count = 0, activeIndex = 0 }) => ( | ||
<View className="flex-row mb-2 self-center items-center"> | ||
{Array(count) | ||
.fill('') | ||
.map((_, index) => ( | ||
<View | ||
key={`${index}-dot`} | ||
className={ | ||
activeIndex === index | ||
? 'border-zinc-600 border-b1 h-2.5 w-2.5 rounded-md mr-2' | ||
: 'bg-zinc-600 h-2 w-2 rounded-md mr-2' | ||
} | ||
/> | ||
))} | ||
</View> | ||
); | ||
|
||
export default SliderDots; |
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,49 @@ | ||
import { text } from '@theme/text'; | ||
import { ImageBackground, Pressable, StyleSheet, Text, View } from 'react-native'; | ||
|
||
import { SLIDER_ITEM_HEIGHT, SLIDER_ITEM_WIDTH } from '.'; | ||
|
||
type Props = { | ||
image: string; | ||
title: string; | ||
onPress: () => void; | ||
categoryName: string; | ||
}; | ||
|
||
const SliderItem = ({ image, categoryName, title, onPress }: Props) => ( | ||
<Pressable className="bg-white mb-4 pl-4" style={styles.sliderItemContainer} onPress={onPress}> | ||
<View className="bg-zinc-100 mt-4 overflow-hidden rounded-lg self-center"> | ||
<ImageBackground | ||
source={{ uri: image }} | ||
className="rounded-lg justify-end overflow-hidden" | ||
style={styles.sliderImageBg}> | ||
<View className="rounded-lg absolute z-0 bg-black-o-40" style={styles.sliderImageBg} /> | ||
<View className="px-4 mb-4"> | ||
<View className="bg-zinc-50 rounded-full px-2 self-start mb-3"> | ||
<Text | ||
numberOfLines={1} | ||
className={text({ | ||
type: 'subtitle1', | ||
class: 'text-zinc-800 my-1 min-w-[50px] text-center', | ||
})}> | ||
{categoryName} | ||
</Text> | ||
</View> | ||
<Text | ||
numberOfLines={2} | ||
className={text({ type: 'title4', class: 'text-zinc-50 min-h-[36px]' })}> | ||
{title} | ||
</Text> | ||
</View> | ||
</ImageBackground> | ||
</View> | ||
</Pressable> | ||
); | ||
|
||
const styles = StyleSheet.create({ | ||
sliderContent: { paddingRight: 16 }, | ||
sliderItemContainer: { width: SLIDER_ITEM_WIDTH, height: SLIDER_ITEM_HEIGHT, overflow: 'hidden' }, | ||
sliderImageBg: { width: SLIDER_ITEM_WIDTH - 16, height: SLIDER_ITEM_HEIGHT - 16 }, | ||
}); | ||
|
||
export default SliderItem; |
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,73 @@ | ||
import categoryStore from '@store/CategoryStore'; | ||
import { PostType } from '@store/PostStore'; | ||
import { width } from '@utils/helpers'; | ||
import { observer } from 'mobx-react-lite'; | ||
import { useState } from 'react'; | ||
import { | ||
FlatList, | ||
ListRenderItem, | ||
NativeScrollEvent, | ||
NativeSyntheticEvent, | ||
StyleSheet, | ||
View, | ||
} from 'react-native'; | ||
|
||
import SliderDots from './SliderDots'; | ||
import SliderItem from './SliderItem'; | ||
|
||
export const SLIDER_ITEM_HEIGHT = width / 2; | ||
|
||
export const SLIDER_PADDING_HORIZONTAL = 32; | ||
|
||
export const SLIDER_ITEM_WIDTH = width - SLIDER_PADDING_HORIZONTAL; | ||
|
||
type Props = { | ||
posts: PostType[]; | ||
onPressPost: (post: PostType) => void; | ||
}; | ||
|
||
const HomeSlider = ({ posts, onPressPost }: Props) => { | ||
const [sliderIndex, setSliderIndex] = useState(0); | ||
const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => { | ||
const index = Math.round(event.nativeEvent.contentOffset.x / width); | ||
|
||
if (index >= 0) { | ||
setSliderIndex(index); | ||
} | ||
}; | ||
|
||
const RenderItem: ListRenderItem<PostType> = ({ item }) => ( | ||
<SliderItem | ||
title={item.title} | ||
image={item.headerImage || ''} | ||
onPress={() => onPressPost(item)} | ||
categoryName={item.category || categoryStore.categoryName(item.categories || [])} | ||
/> | ||
); | ||
|
||
return ( | ||
<View> | ||
<FlatList | ||
horizontal | ||
pagingEnabled | ||
key="homeSlider" | ||
data={posts || []} | ||
onScroll={onScroll} | ||
removeClippedSubviews | ||
renderItem={RenderItem} | ||
decelerationRate="fast" | ||
snapToInterval={SLIDER_ITEM_WIDTH} | ||
showsHorizontalScrollIndicator={false} | ||
contentContainerStyle={styles.sliderContent} | ||
/> | ||
<SliderDots count={posts?.length} activeIndex={sliderIndex} /> | ||
</View> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
sliderContent: { paddingRight: 16 }, | ||
sliderItemContainer: { width: SLIDER_ITEM_WIDTH, height: SLIDER_ITEM_HEIGHT }, | ||
}); | ||
|
||
export default observer(HomeSlider); |
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
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