Skip to content

Commit 0e36ea5

Browse files
committed
(thtme): 增加hooks useTheme
1 parent cbe56fb commit 0e36ea5

File tree

6 files changed

+36
-31
lines changed

6 files changed

+36
-31
lines changed

example/examples/src/Layout/index.tsx

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import React, { PureComponent } from 'react';
2-
import { StyleSheet, View, ViewProps, ViewStyle, SafeAreaView, ScrollView, ScrollViewProps, Text, TextStyle, Image, StyleProp } from 'react-native';
1+
import React, {PureComponent} from 'react';
2+
import {StyleSheet, View, ViewProps, ViewStyle, SafeAreaView, ScrollView, ScrollViewProps, Text, TextStyle, Image, StyleProp} from 'react-native';
33
import PropTypes from 'prop-types';
4-
import { Theme } from '@uiw/react-native';
5-
import { useTheme } from '@shopify/restyle';
4+
import {Theme, useTheme} from '@uiw/react-native';
65

76
export interface HeaderProps {
87
title?: string;
@@ -15,8 +14,8 @@ export interface HeaderProps {
1514
}
1615

1716
const Header = (props: HeaderProps) => {
18-
const { children, title, description, style, hasLogo, titleStyle, descriptionStyle } = props;
19-
const theme = useTheme<Theme>();
17+
const {children, title, description, style, hasLogo, titleStyle, descriptionStyle} = props;
18+
const theme = useTheme() as Theme;
2019
const styles = createStyles({
2120
backgroundColor: theme.colors.background,
2221
shadowColor: theme.colors.border,
@@ -27,7 +26,7 @@ const Header = (props: HeaderProps) => {
2726
{title && <Text style={[styles.title, titleStyle]}>{title}</Text>}
2827
{hasLogo && (
2928
<View style={styles.logo}>
30-
<Image source={require('../image/logo.png')} style={{ width: 103, height: 18 }} />
29+
<Image source={require('../image/logo.png')} style={{width: 103, height: 18}} />
3130
</View>
3231
)}
3332
{description && <Text style={[styles.description, descriptionStyle]}>{description}</Text>}
@@ -43,8 +42,8 @@ export interface BodyProps extends ScrollViewProps {
4342
}
4443

4544
const Body = (props: BodyProps) => {
46-
const { children, style, isScroll, ...other } = props;
47-
const theme = useTheme<Theme>();
45+
const {children, style, isScroll, ...other} = props;
46+
const theme = useTheme() as Theme;
4847
const styles = createStyles({
4948
backgroundColor: theme.colors.background,
5049
shadowColor: theme.colors.border,
@@ -65,8 +64,8 @@ export interface FooterProps {
6564
}
6665

6766
const Footer = (props: FooterProps) => {
68-
const { children, copyright, style, isShowCopyRight, ...other } = props;
69-
const theme = useTheme<Theme>();
67+
const {children, copyright, style, isShowCopyRight, ...other} = props;
68+
const theme = useTheme() as Theme;
7069
const styles = createStyles({
7170
backgroundColor: theme.colors.background,
7271
shadowColor: theme.colors.border,
@@ -91,8 +90,8 @@ export interface CardProps extends ViewProps {
9190
}
9291

9392
const Card = (props: CardProps) => {
94-
const { title, titleStyle, bodyStyle, children, style, showTitle, extra, ...other } = props;
95-
const theme = useTheme<Theme>();
93+
const {title, titleStyle, bodyStyle, children, style, showTitle, extra, ...other} = props;
94+
const theme = useTheme() as Theme;
9695
const styles = createStyles({
9796
backgroundColor: theme.colors.background,
9897
shadowColor: theme.colors.border,
@@ -103,7 +102,7 @@ const Card = (props: CardProps) => {
103102
{extra ? (
104103
<View style={[styles.extra, styles.cardTitle]}>
105104
{showTitle && (
106-
<View style={{ flex: 2 }}>
105+
<View style={{flex: 2}}>
107106
<Text style={[titleStyle]}>{title}</Text>
108107
</View>
109108
)}
@@ -123,32 +122,32 @@ export interface ContainerProps extends ScrollViewProps {
123122
}
124123

125124
export const Container = (props: ContainerProps) => {
126-
const { children, ...others } = props;
127-
const theme = useTheme<Theme>();
125+
const {children, ...others} = props;
126+
const theme = useTheme() as Theme;
128127
return (
129-
<SafeAreaView style={{ backgroundColor: theme.colors.background }} {...others}>
130-
<ScrollView style={{ height: '100%' }} {...others}>
128+
<SafeAreaView style={{backgroundColor: theme.colors.background}} {...others}>
129+
<ScrollView style={{height: '100%'}} {...others}>
131130
{children}
132131
</ScrollView>
133132
</SafeAreaView>
134133
);
135134
};
136135

137-
export interface LayoutProps extends ViewProps { }
136+
export interface LayoutProps extends ViewProps {}
138137

139138
export default class Layout extends PureComponent<LayoutProps> {
140139
static Header = Header;
141140
static Body = Body;
142141
static Footer = Footer;
143142
static Card = Card;
144143
render() {
145-
const { children, style } = this.props;
144+
const {children, style} = this.props;
146145
const styles = createStyles({});
147146
return <View style={[styles.container, style]}>{children}</View>;
148147
}
149148
}
150149

151-
function createStyles({ backgroundColor = '#F7F7F7', shadowColor = '#E6E6E6', color = '#9A9A9A' }) {
150+
function createStyles({backgroundColor = '#F7F7F7', shadowColor = '#E6E6E6', color = '#9A9A9A'}) {
152151
return StyleSheet.create({
153152
container: {
154153
backgroundColor: backgroundColor,
@@ -162,7 +161,7 @@ function createStyles({ backgroundColor = '#F7F7F7', shadowColor = '#E6E6E6', co
162161
paddingRight: 16,
163162
shadowColor: shadowColor,
164163
backgroundColor: backgroundColor,
165-
shadowOffset: { width: 0, height: 3 },
164+
shadowOffset: {width: 0, height: 3},
166165
shadowRadius: 3,
167166
// shadowOpacity: 0.8,
168167
shadowOpacity: 0,

example/examples/src/routes/Card/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const CardDemo = (props: any) => {
1010
const TITLE = 'Welcome to @uiw';
1111
const [selected, setSelected] = useState(false);
1212
const [loading, setLoad] = useState(true);
13+
1314
const basicRender = (
1415
<Fragment>
1516
<View style={{display: 'flex', alignItems: 'center', flexDirection: 'row'}}>

example/examples/src/routes/Divider/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React from 'react';
22
import {Text, View} from 'react-native';
3-
import {Divider, Theme} from '@uiw/react-native';
3+
import {Divider, useTheme, Theme} from '@uiw/react-native';
44
import {ComProps} from '../../routes';
55
import Layout, {Container} from '../../Layout';
6-
import {useTheme} from '@shopify/restyle';
76

87
const {Header, Body, Card, Footer} = Layout;
98

@@ -13,7 +12,7 @@ export default function DividerView(props: DividerViewProps) {
1312
const {route} = props;
1413
const description = route.params.description;
1514
const title = route.params.title;
16-
const theme = useTheme<Theme>();
15+
const theme = useTheme() as Theme;
1716
return (
1817
<Container>
1918
<Layout style={{backgroundColor: theme.colors.background}}>

packages/core/src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import theme from './theme';
2-
import ThemeProvider from './theme-provider';
2+
import ThemeProvider, { useTheme } from './theme-provider';
33
import type { Theme } from './theme';
44

55
export { default as Avatar } from './Avatar';
@@ -172,5 +172,5 @@ export * from './Typography/RnText';
172172
export * from './Typography/Text';
173173
export * from './Typography/Box';
174174

175-
export { theme, ThemeProvider };
175+
export { theme, ThemeProvider, useTheme };
176176
export type { Theme };
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { FC, PropsWithChildren } from 'react';
2-
import { ThemeProvider as ShopifyThemeProvider } from '@shopify/restyle';
1+
import React, { FC, PropsWithChildren, useContext } from 'react';
2+
import { ThemeProvider as ShopifyThemeProvider, ThemeContext } from '@shopify/restyle';
33
import theme, { Theme } from '../theme';
44

55
const { lightTheme } = theme;
@@ -10,6 +10,12 @@ const ThemeProvider: FC<
1010
> = ({ theme = lightTheme, children }) => {
1111
return <ShopifyThemeProvider theme={theme}>{children}</ShopifyThemeProvider>;
1212
};
13-
ThemeProvider.displayName = 'ThemeProvider';
1413

14+
function useTheme() {
15+
const theme = useContext(ThemeContext);
16+
return theme as Theme;
17+
}
18+
19+
ThemeProvider.displayName = 'ThemeProvider';
20+
export { useTheme };
1521
export default ThemeProvider;

packages/react-native-image-picker/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"@shopify/restyle": "~2.4.2",
3131
"@types/react-native": "0.69.1",
3232
"@uiw/icons": "2.5.3",
33-
"@uiw/react-native": "~3.2.3",
33+
"@uiw/react-native": "4.0.1",
3434
"ahooks": "3.7.6",
3535
"prop-types": "15.7.2",
3636
"react-native-image-picker": "^5.3.1",

0 commit comments

Comments
 (0)