Skip to content

Commit

Permalink
chore: add react-native-web example. closes #671
Browse files Browse the repository at this point in the history
This example is missing the drawer, but all other examples are there. There are rendering issues with few components on the web. Hopefully this example will make it easier to identify these issues and fix them in future.
  • Loading branch information
satya164 committed Jan 27, 2019
1 parent 4cfc1e6 commit 865aef8
Show file tree
Hide file tree
Showing 57 changed files with 3,697 additions and 337 deletions.
130 changes: 1 addition & 129 deletions example/App.js
Original file line number Diff line number Diff line change
@@ -1,129 +1 @@
/* @flow */

import { KeepAwake, Util } from 'expo';
import * as React from 'react';
import { StatusBar, I18nManager, AsyncStorage, Platform } from 'react-native';
import {
Provider as PaperProvider,
DarkTheme,
DefaultTheme,
type Theme,
} from 'react-native-paper';
import createReactContext from 'create-react-context';
import { createDrawerNavigator } from 'react-navigation';
import RootNavigator from './src/RootNavigator';
import DrawerItems from './DrawerItems';

type State = {
theme: Theme,
rtl: boolean,
};

const PreferencesContext: any = createReactContext();

const App = createDrawerNavigator(
{ Home: { screen: RootNavigator } },
{
contentComponent: () => (
<PreferencesContext.Consumer>
{preferences => (
<DrawerItems
toggleTheme={preferences.theme}
toggleRTL={preferences.rtl}
isRTL={preferences.isRTL}
isDarkTheme={preferences.isDarkTheme}
/>
)}
</PreferencesContext.Consumer>
),
// set drawerPosition to support rtl toggle on android
drawerPosition:
Platform.OS === 'android' && I18nManager.isRTL ? 'right' : 'left',
}
);

export default class PaperExample extends React.Component<{}, State> {
state = {
theme: DefaultTheme,
rtl: I18nManager.isRTL,
};

async componentDidMount() {
StatusBar.setBarStyle('light-content');

try {
const prefString = await AsyncStorage.getItem('preferences');
const preferences = JSON.parse(prefString);

if (preferences) {
// eslint-disable-next-line react/no-did-mount-set-state
this.setState(state => ({
theme: preferences.theme === 'dark' ? DarkTheme : DefaultTheme,
rtl:
typeof preferences.rtl === 'boolean' ? preferences.rtl : state.rtl,
}));
}
} catch (e) {
// ignore error
}
}

_savePreferences = async () => {
try {
AsyncStorage.setItem(
'preferences',
JSON.stringify({
theme: this.state.theme === DarkTheme ? 'dark' : 'light',
rtl: this.state.rtl,
})
);
} catch (e) {
// ignore error
}
};

_toggleTheme = () =>
this.setState(
state => ({
theme: state.theme === DarkTheme ? DefaultTheme : DarkTheme,
}),
this._savePreferences
);

_toggleRTL = () =>
this.setState(
state => ({
rtl: !state.rtl,
}),
async () => {
await this._savePreferences();

I18nManager.forceRTL(this.state.rtl);
Util.reload();
}
);

render() {
return (
<PaperProvider theme={this.state.theme}>
<PreferencesContext.Provider
value={{
theme: this._toggleTheme,
rtl: this._toggleRTL,
isRTL: this.state.rtl,
isDarkTheme: this.state.theme === DarkTheme,
}}
>
<App
persistenceKey={
process.env.NODE_ENV !== 'production'
? 'NavigationStateDEV'
: null
}
/>
</PreferencesContext.Provider>
<KeepAwake />
</PaperProvider>
);
}
}
export { default } from './src/index';
5 changes: 5 additions & 0 deletions example/App.web.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as React from 'react';
import { render } from 'react-dom';
import App from './src/index';

render(<App />, document.getElementById('root'));
28 changes: 20 additions & 8 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
## Run the example

- [View and edit it with Expo Snack](https://snack.expo.io/@satya164/github.com-callstack-react-native-paper:example)
- Run the example locally
+ Clone the repository and `cd` to this directory
+ Run `yarn` to install the dependencies
+ Run `yarn start` to start the packager
+ Follow the instructions to open it with the [Expo app](https://expo.io/)
# Example App for React Native Paper

## React Native App

You can run the React Native app with [this Snack](https://snack.expo.io/@satya164/github.com-callstack-react-native-paper:example). Snack allows you to make changes to the example app directly in the online editor and see changes on your phone instantly using the [Expo](https://expo.io/) app without having to install or setup anything on your computer. You can also "Export" it to download as a standalone Expo app to run locally.

If you want to run the example from the repo,

- Clone the repository and `cd` to this directory
- Run `yarn` to install the dependencies
- Run `yarn start` to start the packager
- Follow the instructions to open it with the [Expo app](https://expo.io/)

## Web App

You can also run the example app as a web app using [react-native-web](https://github.com/necolas/react-native-web),

- Clone the repository and `cd` to this directory
- Run `yarn` to install the dependencies
- Run `yarn web` to start the webpack server and open the app in your browser
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
5 changes: 5 additions & 0 deletions example/assets/styles/icons.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@font-face {
font-family: 'MaterialIcons';
src: url('../../node_modules/react-native-vector-icons/Fonts/MaterialIcons.ttf')
format('truetype');
}
20 changes: 18 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,39 @@
"scripts": {
"start": "expo start",
"android": "expo android",
"ios": "expo ios"
"ios": "expo ios",
"web": "webpack-dev-server --mode development --open --hot"
},
"main": "node_modules/expo/AppEntry.js",
"dependencies": {
"@callstack/react-theme-provider": "^1.0.7",
"@expo/vector-icons": "^8.0.0",
"@reach/router": "^1.2.1",
"create-react-context": "^0.2.3",
"expo": "^31.0.3",
"react": "16.5.0",
"react-art": "^16.7.0",
"react-lifecycles-compat": "^3.0.4",
"react-native": "https://github.com/expo/react-native/archive/sdk-31.0.1.tar.gz",
"react-native-web": "^0.10.0-alpha.3",
"react-navigation": "^2.18.2"
},
"devDependencies": {
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@babel/plugin-proposal-object-rest-spread": "^7.3.1",
"@babel/preset-env": "^7.3.1",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",
"babel-plugin-module-resolver": "^3.1.1",
"babel-preset-expo": "^5.0.0",
"expo-cli": "^2.3.8"
"css-loader": "^2.1.0",
"expo-cli": "^2.3.8",
"style-loader": "^0.23.1",
"url-loader": "^1.1.2",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1",
"webpack-dev-server": "^3.1.14"
},
"resolutions": {
"**/create-react-context": "0.2.3",
Expand Down
19 changes: 19 additions & 0 deletions example/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<head>
<title>React Native Paper - Example</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500" rel="stylesheet">
<style>
html, body, #root {
height: 100%;
font-family: 'Roboto', sans-serif;
}

#root {
display: flex;
flex-direction: column;
}
</style>
</head>
<body>
<div id="root"></div>
<script src="app.bundle.js"></script>
</body>
File renamed without changes.
54 changes: 27 additions & 27 deletions example/src/ExampleList.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@
import * as React from 'react';
import { FlatList } from 'react-native';
import { List, Divider, withTheme, type Theme } from 'react-native-paper';
import AppbarExample from './AppbarExample';
import AvatarExample from './AvatarExample';
import BadgeExample from './BadgeExample';
import BannerExample from './BannerExample';
import BottomNavigationExample from './BottomNavigationExample';
import ButtonExample from './ButtonExample';
import CardExample from './CardExample';
import CheckboxExample from './CheckboxExample';
import ChipExample from './ChipExample';
import DataTableExample from './DataTableExample';
import DialogExample from './DialogExample';
import DividerExample from './DividerExample';
import FABExample from './FABExample';
import IconButtonExample from './IconButtonExample';
import ListAccordionExample from './ListAccordionExample';
import ListSectionExample from './ListSectionExample';
import ProgressBarExample from './ProgressBarExample';
import RadioButtonExample from './RadioButtonExample';
import RadioButtonGroupExample from './RadioButtonGroupExample';
import SearchbarExample from './SearchbarExample';
import SnackbarExample from './SnackbarExample';
import SurfaceExample from './SurfaceExample';
import SwitchExample from './SwitchExample';
import TextExample from './TextExample';
import TextInputExample from './TextInputExample';
import ToggleButtonExample from './ToggleButtonExample';
import TouchableRippleExample from './TouchableRippleExample';
import AppbarExample from './Examples/AppbarExample';
import AvatarExample from './Examples/AvatarExample';
import BadgeExample from './Examples/BadgeExample';
import BannerExample from './Examples/BannerExample';
import BottomNavigationExample from './Examples/BottomNavigationExample';
import ButtonExample from './Examples/ButtonExample';
import CardExample from './Examples/CardExample';
import CheckboxExample from './Examples/CheckboxExample';
import ChipExample from './Examples/ChipExample';
import DataTableExample from './Examples/DataTableExample';
import DialogExample from './Examples/DialogExample';
import DividerExample from './Examples/DividerExample';
import FABExample from './Examples/FABExample';
import IconButtonExample from './Examples/IconButtonExample';
import ListAccordionExample from './Examples/ListAccordionExample';
import ListSectionExample from './Examples/ListSectionExample';
import ProgressBarExample from './Examples/ProgressBarExample';
import RadioButtonExample from './Examples/RadioButtonExample';
import RadioButtonGroupExample from './Examples/RadioButtonGroupExample';
import SearchbarExample from './Examples/SearchbarExample';
import SnackbarExample from './Examples/SnackbarExample';
import SurfaceExample from './Examples/SurfaceExample';
import SwitchExample from './Examples/SwitchExample';
import TextExample from './Examples/TextExample';
import TextInputExample from './Examples/TextInputExample';
import ToggleButtonExample from './Examples/ToggleButtonExample';
import TouchableRippleExample from './Examples/TouchableRippleExample';

type Props = {
theme: Theme,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ class AvatarExample extends React.Component<Props> {
<View style={styles.row}>
<Avatar.Image
style={styles.avatar}
source={require('../assets/avatar.png')}
source={require('../../assets/images/avatar.png')}
/>
<Avatar.Image
style={styles.avatar}
source={require('../assets/avatar.png')}
source={require('../../assets/images/avatar.png')}
size={80}
/>
</View>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class BannerExample extends React.Component<Props, State> {
]}
image={({ size }) => (
<Image
source={require('../assets/email-icon.png')}
source={require('../../assets/images/email-icon.png')}
style={{
width: size,
height: size,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class ButtonExample extends React.Component<Props, State> {
</Button>
<Button
mode="outlined"
icon={require('../assets/favorite.png')}
icon={require('../../assets/images/favorite.png')}
onPress={() => {}}
style={styles.button}
>
Expand All @@ -148,7 +148,7 @@ class ButtonExample extends React.Component<Props, State> {
mode="outlined"
icon={({ size }) => (
<Image
source={require('../assets/chameleon.jpg')}
source={require('../../assets/images/chameleon.jpg')}
style={{ width: size, height: size, borderRadius: size / 2 }}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class CardExample extends React.Component<Props> {
contentContainerStyle={styles.content}
>
<Card style={styles.card}>
<Card.Cover source={require('../assets/wrecked-ship.jpg')} />
<Card.Cover
source={require('../../assets/images/wrecked-ship.jpg')}
/>
<Card.Content>
<Title>Abandoned Ship</Title>
<Paragraph>
Expand All @@ -43,7 +45,7 @@ class CardExample extends React.Component<Props> {
</Card.Content>
</Card>
<Card style={styles.card}>
<Card.Cover source={require('../assets/forest.jpg')} />
<Card.Cover source={require('../../assets/images/forest.jpg')} />
<Card.Actions>
<Button onPress={() => {}}>Share</Button>
<Button onPress={() => {}}>Explore</Button>
Expand All @@ -64,15 +66,17 @@ class CardExample extends React.Component<Props> {
</Card>
<Card style={styles.card}>
<Title>Just Strawberries</Title>
<Card.Cover source={require('../assets/strawberries.jpg')} />
<Card.Cover
source={require('../../assets/images/strawberries.jpg')}
/>
</Card>
<Card
style={styles.card}
onPress={() => {
Alert.alert('The Chameleon is Pressed');
}}
>
<Card.Cover source={require('../assets/chameleon.jpg')} />
<Card.Cover source={require('../../assets/images/chameleon.jpg')} />
<Card.Content>
<Title>Pressable Chameleon</Title>
<Paragraph>
Expand All @@ -86,7 +90,7 @@ class CardExample extends React.Component<Props> {
Alert.alert('The City is Long Pressed');
}}
>
<Card.Cover source={require('../assets/city.jpg')} />
<Card.Cover source={require('../../assets/images/city.jpg')} />
<Card.Content>
<Title>Long Pressable City</Title>
<Paragraph>
Expand Down
File renamed without changes.
Loading

0 comments on commit 865aef8

Please sign in to comment.