-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add htmlToNative component - refactored stores and screens
- Loading branch information
1 parent
11c91ca
commit 3cb3e61
Showing
15 changed files
with
263 additions
and
18 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import IframeRenderer, { iframeModel } from '@native-html/iframe-plugin'; | ||
import colors from '@theme/colors'; | ||
import { width } from '@utils/helpers'; | ||
import { observer } from 'mobx-react-lite'; | ||
import { StyleSheet } from 'react-native'; | ||
import RenderHtml from 'react-native-render-html'; | ||
import { WebView } from 'react-native-webview'; | ||
|
||
type Props = { | ||
html?: string; | ||
customContentWidth?: number; | ||
}; | ||
|
||
const customHTMLElementModels = { | ||
iframe: iframeModel, | ||
}; | ||
|
||
const renderers = { | ||
iframe: IframeRenderer, | ||
}; | ||
|
||
const HtmlToNativeViewer = ({ html, customContentWidth }: Props) => { | ||
if (!html) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<RenderHtml | ||
WebView={WebView} | ||
source={{ html }} | ||
tagsStyles={styles} | ||
renderers={renderers} | ||
renderersProps={{ | ||
iframe: { | ||
webViewProps: {}, | ||
scalesPageToFit: true, | ||
}, | ||
img: { | ||
enableExperimentalPercentWidth: true, | ||
}, | ||
}} | ||
contentWidth={customContentWidth || width - 40} | ||
customHTMLElementModels={customHTMLElementModels} | ||
/> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
a: {}, | ||
li: { | ||
marginVertical: 4, | ||
color: colors.zinc[700], | ||
}, | ||
h1: { | ||
marginVertical: 6, | ||
color: colors.zinc[700], | ||
}, | ||
h2: { | ||
marginVertical: 6, | ||
color: colors.zinc[700], | ||
}, | ||
h3: { | ||
marginVertical: 6, | ||
color: colors.zinc[700], | ||
}, | ||
h4: { | ||
marginVertical: 6, | ||
color: colors.zinc[700], | ||
}, | ||
h5: { | ||
marginVertical: 6, | ||
color: colors.zinc[700], | ||
}, | ||
h6: { | ||
marginVertical: 6, | ||
color: colors.zinc[700], | ||
}, | ||
img: { | ||
resizeMode: 'contain', | ||
alignSelf: 'flex-start', | ||
}, | ||
p: { | ||
marginVertical: 4, | ||
fontSize: 16, | ||
width: width - 40, | ||
color: colors.zinc[700], | ||
}, | ||
b: { | ||
color: colors.zinc[700], | ||
}, | ||
}); | ||
|
||
export default observer(HtmlToNativeViewer); |
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,43 @@ | ||
import colors from '@theme/colors'; | ||
import { getHitSlop, width } from '@utils/helpers'; | ||
import { ArrowLeft, Bookmark } from 'lucide-react-native'; | ||
import { Image, Pressable, StyleSheet, View } from 'react-native'; | ||
|
||
type Props = { | ||
topMargin: number; | ||
isSavedPost: boolean; | ||
headerImage?: string; | ||
onPressSave: () => void; | ||
onPressBack: () => void; | ||
}; | ||
|
||
const PostDetailHeader = ({ | ||
topMargin, | ||
headerImage, | ||
onPressSave, | ||
onPressBack, | ||
isSavedPost, | ||
}: Props) => ( | ||
<View className="w-full absolute overflow-hidden"> | ||
<View | ||
style={{ marginTop: topMargin + 16 }} | ||
className="absolute z-40 px-5 flex-row justify-between items-center content-between w-full"> | ||
<Pressable onPress={onPressBack} hitSlop={getHitSlop({ value: 40 })}> | ||
<ArrowLeft className="text-zinc-50" /> | ||
</Pressable> | ||
<Pressable onPress={onPressSave} hitSlop={getHitSlop({ value: 40 })}> | ||
<Bookmark className="text-zinc-50" fill={isSavedPost ? colors.zinc[50] : 'transparent'} /> | ||
</Pressable> | ||
</View> | ||
<View style={styles.header} className="absolute bg-zinc-600 z-20 w-full opacity-50" /> | ||
<Image resizeMode="cover" style={styles.header} source={{ uri: headerImage }} /> | ||
</View> | ||
); | ||
|
||
const styles = StyleSheet.create({ | ||
header: { | ||
height: width * 0.6, | ||
}, | ||
}); | ||
|
||
export default PostDetailHeader; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import HtmlToNativeViewer from '@components/HtmlToNativeViewer'; | ||
import PostDetailHeader from '@components/PostDetail/PostDetailHeader'; | ||
import { BlogStackNavigatorParamList } from '@navigators/BlogNavigator'; | ||
import { RouteProp, useNavigation, useRoute } from '@react-navigation/native'; | ||
import savedStore from '@store/SavedStore'; | ||
import { text } from '@theme/text'; | ||
import { width } from '@utils/helpers'; | ||
import { observer } from 'mobx-react-lite'; | ||
import { SafeAreaView, ScrollView, Text, View } from 'react-native'; | ||
import { useSafeAreaInsets } from 'react-native-safe-area-context'; | ||
|
||
const HEADER_HEIGHT = width * 0.6; | ||
const CONTENT_MARGIN_TOP = HEADER_HEIGHT * 0.5; | ||
|
||
const PostDetailScreen = () => { | ||
const { | ||
params: { post }, | ||
} = useRoute<RouteProp<BlogStackNavigatorParamList, 'PostDetail'>>(); | ||
const { goBack } = useNavigation(); | ||
const { top } = useSafeAreaInsets(); | ||
const onPressSave = () => post && savedStore.addPost(post); | ||
|
||
return ( | ||
<SafeAreaView className="bg-zinc-50 flex-1"> | ||
<PostDetailHeader | ||
topMargin={top} | ||
onPressBack={goBack} | ||
onPressSave={onPressSave} | ||
headerImage={post?.headerImage} | ||
isSavedPost={savedStore.isSavedPost(post.id)} | ||
/> | ||
<ScrollView className="rounded-md bg-zinc-50" style={{ marginTop: CONTENT_MARGIN_TOP + top }}> | ||
<View className="px-5 pb-8"> | ||
<Text className={text({ type: 'subhead', class: 'text-zinc-500 mt-4 mb-1' })}> | ||
{post?.category} | ||
</Text> | ||
<Text numberOfLines={3} className={text({ type: 'title4', class: 'text-zinc-900 mb-2' })}> | ||
{post?.title} | ||
</Text> | ||
<HtmlToNativeViewer html={post?.content} /> | ||
</View> | ||
</ScrollView> | ||
</SafeAreaView> | ||
); | ||
}; | ||
|
||
export default observer(PostDetailScreen); |
Oops, something went wrong.