├── .babelrc ├── .editorconfig ├── .github └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── .gitignore ├── LICENSE ├── README.md ├── docs ├── ios-custom.png └── ios-native.png ├── example └── app │ ├── .babelrc │ ├── .flowconfig │ ├── .gitignore │ ├── .watchmanconfig │ ├── App.js │ ├── App.test.js │ ├── ExampleA.js │ ├── ExampleB.js │ ├── README.md │ ├── app.json │ ├── lib │ ├── ActionSheetCustom.js │ ├── ActionSheetIOS.js │ ├── index.js │ ├── options.js │ ├── styles.js │ └── utils.js │ ├── package.json │ └── yarn.lock ├── lib ├── ActionSheetCustom.js ├── ActionSheetIOS.js ├── BlurView.ios.js ├── BlurView.js ├── index.js ├── options.js ├── styles.js ├── ts │ └── index.d.ts └── utils.js ├── package.json ├── tests ├── ActionSheetCustom.test.js └── __snapshots__ │ └── ActionSheetCustom.test.js.snap └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["env", "react-native"] 3 | } 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | charset = utf-8 7 | end_of_line = lf 8 | indent_size = 2 9 | indent_style = space 10 | insert_final_newline = true 11 | trim_trailing_whitespace = true 12 | 13 | [*.md] 14 | trim_trailing_whitespace = false 15 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: bug 6 | assignees: alessiocancian 7 | 8 | --- 9 | 10 | # Bug 11 | 12 | 20 | 21 | ## Environment info 22 | 23 | 26 | 27 | | Library | Version | 28 | | ----------------------------------------- | ------- | 29 | | @alessiocancian/react-native-actionsheet | x.x.x | 30 | | react-native | x.x.x | 31 | 32 | ## Steps To Reproduce 33 | 34 | 40 | 41 | 1. 42 | 2. 43 | 3. 44 | 45 | Describe what you expected to happen: 46 | 47 | 1. 48 | 2. 49 | 50 | ## Reproducible sample code 51 | 52 | 55 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: enhancement 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Additional context** 17 | Add any other context or screenshots about the feature request here. 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | # 3 | .DS_Store 4 | 5 | # Xcode 6 | # 7 | build/ 8 | *.pbxuser 9 | !default.pbxuser 10 | *.mode1v3 11 | !default.mode1v3 12 | *.mode2v3 13 | !default.mode2v3 14 | *.perspectivev3 15 | !default.perspectivev3 16 | xcuserdata 17 | *.xccheckout 18 | *.moved-aside 19 | DerivedData 20 | *.hmap 21 | *.ipa 22 | *.xcuserstate 23 | project.xcworkspace 24 | 25 | # Android/IJ 26 | # 27 | .idea 28 | .gradle 29 | local.properties 30 | 31 | # node.js 32 | # 33 | node_modules/ 34 | npm-debug.log 35 | 36 | .vscode 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 beefe 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-actionsheet 2 | [![npm](https://img.shields.io/npm/v/@alessiocancian/react-native-actionsheet)](https://www.npmjs.com/package/@alessiocancian/react-native-actionsheet)\ 3 | Cross platform ActionSheet. This component implements a custom ActionSheet and provides the same way to drawing it on the defferent platforms(iOS and Android). Actually, In order to keep the best effect, it still uses the ActionSheetIOS on iOS. 4 | 5 | 6 | 7 | 8 | 11 | 14 | 15 | 16 |
9 | 10 | 12 | 13 |
17 | 18 | 19 | Starting from v3.0.0 a custom ActionSheet with a native-like UI is available and used as default option for iOS: 20 | 21 | 22 | 23 | 26 | 29 | 30 | 31 |
24 | 25 | 27 | 28 |
32 | 33 | A similar UI is available for Android too by passing `theme="ios"`: 34 | 35 | 36 | 37 | 40 | 43 | 44 | 45 |
38 | 39 | 41 | 42 |
46 | 47 | 48 | 49 | ## Install 50 | 51 | ``` 52 | npm i -S @alessiocancian/react-native-actionsheet 53 | ``` 54 | 55 | ## Usage 56 | 57 | 58 | ### Functional components 59 | 60 | ```js 61 | import { useRef } from 'react' 62 | import ActionSheet from '@alessiocancian/react-native-actionsheet' 63 | 64 | const Demo = () => { 65 | const actionSheetRef = useRef() 66 | 67 | const showActionSheet = () => { 68 | actionSheetRef.current.show() 69 | } 70 | 71 | render() { 72 | return ( 73 | 74 | Open ActionSheet 75 | { /* do something */ }} 82 | /> 83 | 84 | ) 85 | } 86 | } 87 | ``` 88 | 89 | ### Class components 90 | 91 | ```js 92 | import ActionSheet from '@alessiocancian/react-native-actionsheet' 93 | 94 | class Demo extends React.Component { 95 | showActionSheet = () => { 96 | this.ActionSheet.show() 97 | } 98 | render() { 99 | return ( 100 | 101 | Open ActionSheet 102 | this.ActionSheet = o} 104 | title={'Which one do you like ?'} 105 | options={['Apple', 'Banana', 'cancel']} 106 | cancelButtonIndex={2} 107 | destructiveButtonIndex={1} 108 | onPress={(index) => { /* do something */ }} 109 | /> 110 | 111 | ) 112 | } 113 | } 114 | ``` 115 | 116 | 117 | ### Use ActionSheetCustom directly 118 | 119 | so you can customize option and title 120 | 121 | ```js 122 | import { ActionSheetCustom as ActionSheet } from '@alessiocancian/react-native-actionsheet' 123 | 124 | const options = [ 125 | 'Cancel', 126 | 'Apple', 127 | Banana, 128 | 'Watermelon', 129 | Durian 130 | ] 131 | 132 | class Demo extends React.Component { 133 | showActionSheet = () => { 134 | this.ActionSheet.show() 135 | } 136 | render() { 137 | return ( 138 | 139 | Open ActionSheet 140 | this.ActionSheet = o} 142 | title={Which one do you like?} 143 | options={options} 144 | cancelButtonIndex={0} 145 | destructiveButtonIndex={4} 146 | onPress={(index) => { /* do something */ }} 147 | /> 148 | 149 | ) 150 | } 151 | } 152 | ``` 153 | 154 | ### How to redesign style ? 155 | 156 | The style of ActionSheet is defined in [lib/styles.js](https://github.com/alessiocancian/react-native-actionsheet/blob/master/lib/styles.js). We can pass the `styles` prop to cover default style. See [Example](https://github.com/alessiocancian/react-native-actionsheet/blob/master/example/app/ExampleB.js#L48) . 157 | 158 | ```javascript 159 | // example 160 | 161 | const styles = { 162 | titleBox: { 163 | background: 'pink' 164 | }, 165 | titleText: { 166 | fontSize: 16, 167 | color: '#000' 168 | } 169 | } 170 | 171 | 175 | ``` 176 | 177 | ## Props 178 | 179 | https://github.com/alessiocancian/react-native-actionsheet/blob/master/lib/options.js 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 |
Prop nameTypeDescriptionDefault
titlestring or ReactNodeActionSheetCustom needed to use ReactNode for iOS
messagestring or ReactNodeActionSheetCustom needed to use ReactNode for iOS
options(string | ReactChild)[]ActionSheetCustom needed to use ReactChild options for iOS
tintColorstring
cancelButtonIndexnumber
destructiveButtonIndexnumber
onPressfunction
userInterfaceStyle"light" | "dark"RN version >=0.63System theme
theme"flat" | "ios"flat for Android and native or native-like for iOS
stylesonly for ActionSheetCustom{}
statusBarTranslucentbooleanonly for Androidfalse
255 | -------------------------------------------------------------------------------- /docs/ios-custom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alessiocancian/react-native-actionsheet/b431605b5f6b1a1a2b071c60213b62900dc32c2e/docs/ios-custom.png -------------------------------------------------------------------------------- /docs/ios-native.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alessiocancian/react-native-actionsheet/b431605b5f6b1a1a2b071c60213b62900dc32c2e/docs/ios-native.png -------------------------------------------------------------------------------- /example/app/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["babel-preset-expo"], 3 | "env": { 4 | "development": { 5 | "plugins": ["transform-react-jsx-source"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /example/app/.flowconfig: -------------------------------------------------------------------------------- 1 | [ignore] 2 | ; We fork some components by platform 3 | .*/*[.]android.js 4 | 5 | ; Ignore templates for 'react-native init' 6 | /node_modules/react-native/local-cli/templates/.* 7 | 8 | ; Ignore RN jest 9 | /node_modules/react-native/jest/.* 10 | 11 | ; Ignore RNTester 12 | /node_modules/react-native/RNTester/.* 13 | 14 | ; Ignore the website subdir 15 | /node_modules/react-native/website/.* 16 | 17 | ; Ignore the Dangerfile 18 | /node_modules/react-native/danger/dangerfile.js 19 | 20 | ; Ignore Fbemitter 21 | /node_modules/fbemitter/.* 22 | 23 | ; Ignore "BUCK" generated dirs 24 | /node_modules/react-native/\.buckd/ 25 | 26 | ; Ignore unexpected extra "@providesModule" 27 | .*/node_modules/.*/node_modules/fbjs/.* 28 | 29 | ; Ignore polyfills 30 | /node_modules/react-native/Libraries/polyfills/.* 31 | 32 | ; Ignore various node_modules 33 | /node_modules/react-native-gesture-handler/.* 34 | /node_modules/expo/.* 35 | /node_modules/react-navigation/.* 36 | /node_modules/xdl/.* 37 | /node_modules/reqwest/.* 38 | /node_modules/metro-bundler/.* 39 | 40 | [include] 41 | 42 | [libs] 43 | node_modules/react-native/Libraries/react-native/react-native-interface.js 44 | node_modules/react-native/flow/ 45 | node_modules/expo/flow/ 46 | 47 | [options] 48 | emoji=true 49 | 50 | module.system=haste 51 | 52 | module.file_ext=.js 53 | module.file_ext=.jsx 54 | module.file_ext=.json 55 | module.file_ext=.ios.js 56 | 57 | munge_underscores=true 58 | 59 | module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub' 60 | 61 | suppress_type=$FlowIssue 62 | suppress_type=$FlowFixMe 63 | suppress_type=$FlowFixMeProps 64 | suppress_type=$FlowFixMeState 65 | suppress_type=$FixMe 66 | 67 | suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(>=0\\.\\(5[0-6]\\|[1-4][0-9]\\|[0-9]\\).[0-9]\\)? *\\(site=[a-z,_]*react_native_oss[a-z,_]*\\)?)\\) 68 | suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(>=0\\.\\(5[0-6]\\|[1-4][0-9]\\|[0-9]\\).[0-9]\\)? *\\(site=[a-z,_]*react_native_oss[a-z,_]*\\)?)\\)?:? #[0-9]+ 69 | suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy 70 | suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError 71 | 72 | unsafe.enable_getters_and_setters=true 73 | 74 | [version] 75 | ^0.56.0 76 | -------------------------------------------------------------------------------- /example/app/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # expo 4 | .expo/ 5 | 6 | # dependencies 7 | /node_modules 8 | 9 | # misc 10 | .env.local 11 | .env.development.local 12 | .env.test.local 13 | .env.production.local 14 | 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | -------------------------------------------------------------------------------- /example/app/.watchmanconfig: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /example/app/App.js: -------------------------------------------------------------------------------- 1 | import { View, StyleSheet, Text } from 'react-native' 2 | import React from 'react' 3 | import ExampleA from './ExampleA' 4 | import ExampleB from './ExampleB' 5 | 6 | class Example extends React.Component { 7 | render () { 8 | return ( 9 | 10 | 11 | 12 | 13 | ) 14 | } 15 | } 16 | 17 | const styles = StyleSheet.create({ 18 | wrapper: { 19 | flex: 1, 20 | alignItems: 'center', 21 | justifyContent: 'center' 22 | } 23 | }) 24 | 25 | export default Example 26 | -------------------------------------------------------------------------------- /example/app/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import App from './App'; 3 | 4 | import renderer from 'react-test-renderer'; 5 | 6 | it('renders without crashing', () => { 7 | const rendered = renderer.create().toJSON(); 8 | expect(rendered).toBeTruthy(); 9 | }); 10 | -------------------------------------------------------------------------------- /example/app/ExampleA.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { View, Text, StyleSheet } from 'react-native' 3 | import ActionSheet from './lib' 4 | 5 | const CANCEL_INDEX = 0 6 | const DESTRUCTIVE_INDEX = 4 7 | const options = [ 'Cancel', 'Apple', 'Banana', 'Watermelon', 'Durian' ] 8 | const title = 'Which one do you like ?' 9 | const message = 'In botany, a fruit is the seed-bearing structure in flowering plants (also known as angiosperms) formed from the ovary after flowering.' 10 | 11 | class ExampleA extends React.Component { 12 | state = { 13 | selected: '' 14 | } 15 | 16 | showActionSheet = () => { 17 | this.ActionSheet.show() 18 | } 19 | 20 | handlePress = (buttonIndex) => { 21 | this.setState({ selected: buttonIndex }) 22 | } 23 | 24 | render () { 25 | return ( 26 | 27 | I like {options[this.state.selected] || '...'} 28 | Example A 29 | { this.ActionSheet = o }} 31 | title={title} 32 | message={message} 33 | options={options} 34 | cancelButtonIndex={CANCEL_INDEX} 35 | destructiveButtonIndex={DESTRUCTIVE_INDEX} 36 | onPress={this.handlePress} 37 | /> 38 | 39 | ) 40 | } 41 | } 42 | 43 | const styles = StyleSheet.create({ 44 | wrapper: { 45 | flex: 1, 46 | alignItems: 'center', 47 | justifyContent: 'center' 48 | }, 49 | button: { 50 | width: 200, 51 | marginBottom: 10, 52 | paddingTop: 15, 53 | paddingBottom: 15, 54 | textAlign: 'center', 55 | color: '#fff', 56 | backgroundColor: '#38f' 57 | } 58 | }) 59 | 60 | export default ExampleA 61 | -------------------------------------------------------------------------------- /example/app/ExampleB.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { View, Text, StyleSheet } from 'react-native' 3 | import { ActionSheetCustom as ActionSheet } from './lib' 4 | 5 | const CANCEL_INDEX = 0 6 | const DESTRUCTIVE_INDEX = 4 7 | const options = [ 8 | 'Cancel', 9 | 'Apple', 10 | 'Banana', 11 | 'Watermelon', 12 | 'Durian' 13 | ] 14 | const title = 'Which one do you like?' 15 | const message = 'In botany, a fruit is the seed-bearing structure in flowering plants (also known as angiosperms) formed from the ovary after flowering.' 16 | 17 | class ExampleB extends React.Component { 18 | constructor (props) { 19 | super(props) 20 | this.state = { 21 | selected: '' 22 | } 23 | this.handlePress = this.handlePress.bind(this) 24 | this.showActionSheet = this.showActionSheet.bind(this) 25 | } 26 | 27 | showActionSheet () { 28 | this.ActionSheet.show() 29 | } 30 | 31 | handlePress (buttonIndex) { 32 | this.setState({ selected: buttonIndex }) 33 | } 34 | 35 | render () { 36 | return ( 37 | 38 | I like {options[this.state.selected] || '...'} 39 | Example B 40 | { this.ActionSheet = o }} 42 | title={title} 43 | message={message} 44 | options={options} 45 | cancelButtonIndex={CANCEL_INDEX} 46 | destructiveButtonIndex={DESTRUCTIVE_INDEX} 47 | onPress={this.handlePress} 48 | styles={{messageBox: { height: 60 }}} 49 | /> 50 | 51 | ) 52 | } 53 | } 54 | 55 | const styles = StyleSheet.create({ 56 | wrapper: { 57 | flex: 1, 58 | alignItems: 'center', 59 | justifyContent: 'center' 60 | }, 61 | button: { 62 | width: 200, 63 | marginBottom: 10, 64 | paddingTop: 15, 65 | paddingBottom: 15, 66 | textAlign: 'center', 67 | color: '#fff', 68 | backgroundColor: '#38f' 69 | } 70 | }) 71 | 72 | export default ExampleB 73 | -------------------------------------------------------------------------------- /example/app/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React Native App](https://github.com/react-community/create-react-native-app). 2 | 3 | Below you'll find information about performing common tasks. The most recent version of this guide is available [here](https://github.com/react-community/create-react-native-app/blob/master/react-native-scripts/template/README.md). 4 | 5 | ## Table of Contents 6 | 7 | * [Updating to New Releases](#updating-to-new-releases) 8 | * [Available Scripts](#available-scripts) 9 | * [npm start](#npm-start) 10 | * [npm test](#npm-test) 11 | * [npm run ios](#npm-run-ios) 12 | * [npm run android](#npm-run-android) 13 | * [npm run eject](#npm-run-eject) 14 | * [Writing and Running Tests](#writing-and-running-tests) 15 | * [Environment Variables](#environment-variables) 16 | * [Configuring Packager IP Address](#configuring-packager-ip-address) 17 | * [Adding Flow](#adding-flow) 18 | * [Customizing App Display Name and Icon](#customizing-app-display-name-and-icon) 19 | * [Sharing and Deployment](#sharing-and-deployment) 20 | * [Publishing to Expo's React Native Community](#publishing-to-expos-react-native-community) 21 | * [Building an Expo "standalone" app](#building-an-expo-standalone-app) 22 | * [Ejecting from Create React Native App](#ejecting-from-create-react-native-app) 23 | * [Build Dependencies (Xcode & Android Studio)](#build-dependencies-xcode-android-studio) 24 | * [Should I Use ExpoKit?](#should-i-use-expokit) 25 | * [Troubleshooting](#troubleshooting) 26 | * [Networking](#networking) 27 | * [iOS Simulator won't open](#ios-simulator-wont-open) 28 | * [QR Code does not scan](#qr-code-does-not-scan) 29 | 30 | ## Updating to New Releases 31 | 32 | You should only need to update the global installation of `create-react-native-app` very rarely, ideally never. 33 | 34 | Updating the `react-native-scripts` dependency of your app should be as simple as bumping the version number in `package.json` and reinstalling your project's dependencies. 35 | 36 | Upgrading to a new version of React Native requires updating the `react-native`, `react`, and `expo` package versions, and setting the correct `sdkVersion` in `app.json`. See the [versioning guide](https://github.com/react-community/create-react-native-app/blob/master/VERSIONS.md) for up-to-date information about package version compatibility. 37 | 38 | ## Available Scripts 39 | 40 | If Yarn was installed when the project was initialized, then dependencies will have been installed via Yarn, and you should probably use it to run these commands as well. Unlike dependency installation, command running syntax is identical for Yarn and NPM at the time of this writing. 41 | 42 | ### `npm start` 43 | 44 | Runs your app in development mode. 45 | 46 | Open it in the [Expo app](https://expo.io) on your phone to view it. It will reload if you save edits to your files, and you will see build errors and logs in the terminal. 47 | 48 | Sometimes you may need to reset or clear the React Native packager's cache. To do so, you can pass the `--reset-cache` flag to the start script: 49 | 50 | ``` 51 | npm start --reset-cache 52 | # or 53 | yarn start --reset-cache 54 | ``` 55 | 56 | #### `npm test` 57 | 58 | Runs the [jest](https://github.com/facebook/jest) test runner on your tests. 59 | 60 | #### `npm run ios` 61 | 62 | Like `npm start`, but also attempts to open your app in the iOS Simulator if you're on a Mac and have it installed. 63 | 64 | #### `npm run android` 65 | 66 | Like `npm start`, but also attempts to open your app on a connected Android device or emulator. Requires an installation of Android build tools (see [React Native docs](https://facebook.github.io/react-native/docs/getting-started.html) for detailed setup). We also recommend installing Genymotion as your Android emulator. Once you've finished setting up the native build environment, there are two options for making the right copy of `adb` available to Create React Native App: 67 | 68 | ##### Using Android Studio's `adb` 69 | 70 | 1. Make sure that you can run adb from your terminal. 71 | 2. Open Genymotion and navigate to `Settings -> ADB`. Select “Use custom Android SDK tools” and update with your [Android SDK directory](https://stackoverflow.com/questions/25176594/android-sdk-location). 72 | 73 | ##### Using Genymotion's `adb` 74 | 75 | 1. Find Genymotion’s copy of adb. On macOS for example, this is normally `/Applications/Genymotion.app/Contents/MacOS/tools/`. 76 | 2. Add the Genymotion tools directory to your path (instructions for [Mac](http://osxdaily.com/2014/08/14/add-new-path-to-path-command-line/), [Linux](http://www.computerhope.com/issues/ch001647.htm), and [Windows](https://www.howtogeek.com/118594/how-to-edit-your-system-path-for-easy-command-line-access/)). 77 | 3. Make sure that you can run adb from your terminal. 78 | 79 | #### `npm run eject` 80 | 81 | This will start the process of "ejecting" from Create React Native App's build scripts. You'll be asked a couple of questions about how you'd like to build your project. 82 | 83 | **Warning:** Running eject is a permanent action (aside from whatever version control system you use). An ejected app will require you to have an [Xcode and/or Android Studio environment](https://facebook.github.io/react-native/docs/getting-started.html) set up. 84 | 85 | ## Customizing App Display Name and Icon 86 | 87 | You can edit `app.json` to include [configuration keys](https://docs.expo.io/versions/latest/guides/configuration.html) under the `expo` key. 88 | 89 | To change your app's display name, set the `expo.name` key in `app.json` to an appropriate string. 90 | 91 | To set an app icon, set the `expo.icon` key in `app.json` to be either a local path or a URL. It's recommended that you use a 512x512 png file with transparency. 92 | 93 | ## Writing and Running Tests 94 | 95 | This project is set up to use [jest](https://facebook.github.io/jest/) for tests. You can configure whatever testing strategy you like, but jest works out of the box. Create test files in directories called `__tests__` or with the `.test` extension to have the files loaded by jest. See the [the template project](https://github.com/react-community/create-react-native-app/blob/master/react-native-scripts/template/App.test.js) for an example test. The [jest documentation](https://facebook.github.io/jest/docs/en/getting-started.html) is also a wonderful resource, as is the [React Native testing tutorial](https://facebook.github.io/jest/docs/en/tutorial-react-native.html). 96 | 97 | ## Environment Variables 98 | 99 | You can configure some of Create React Native App's behavior using environment variables. 100 | 101 | ### Configuring Packager IP Address 102 | 103 | When starting your project, you'll see something like this for your project URL: 104 | 105 | ``` 106 | exp://192.168.0.2:19000 107 | ``` 108 | 109 | The "manifest" at that URL tells the Expo app how to retrieve and load your app's JavaScript bundle, so even if you load it in the app via a URL like `exp://localhost:19000`, the Expo client app will still try to retrieve your app at the IP address that the start script provides. 110 | 111 | In some cases, this is less than ideal. This might be the case if you need to run your project inside of a virtual machine and you have to access the packager via a different IP address than the one which prints by default. In order to override the IP address or hostname that is detected by Create React Native App, you can specify your own hostname via the `REACT_NATIVE_PACKAGER_HOSTNAME` environment variable: 112 | 113 | Mac and Linux: 114 | 115 | ``` 116 | REACT_NATIVE_PACKAGER_HOSTNAME='my-custom-ip-address-or-hostname' npm start 117 | ``` 118 | 119 | Windows: 120 | ``` 121 | set REACT_NATIVE_PACKAGER_HOSTNAME='my-custom-ip-address-or-hostname' 122 | npm start 123 | ``` 124 | 125 | The above example would cause the development server to listen on `exp://my-custom-ip-address-or-hostname:19000`. 126 | 127 | ## Adding Flow 128 | 129 | Flow is a static type checker that helps you write code with fewer bugs. Check out this [introduction to using static types in JavaScript](https://medium.com/@preethikasireddy/why-use-static-types-in-javascript-part-1-8382da1e0adb) if you are new to this concept. 130 | 131 | React Native works with [Flow](http://flowtype.org/) out of the box, as long as your Flow version matches the one used in the version of React Native. 132 | 133 | To add a local dependency to the correct Flow version to a Create React Native App project, follow these steps: 134 | 135 | 1. Find the Flow `[version]` at the bottom of the included [.flowconfig](.flowconfig) 136 | 2. Run `npm install --save-dev flow-bin@x.y.z` (or `yarn add --dev flow-bin@x.y.z`), where `x.y.z` is the .flowconfig version number. 137 | 3. Add `"flow": "flow"` to the `scripts` section of your `package.json`. 138 | 4. Add `// @flow` to any files you want to type check (for example, to `App.js`). 139 | 140 | Now you can run `npm run flow` (or `yarn flow`) to check the files for type errors. 141 | You can optionally use a [plugin for your IDE or editor](https://flow.org/en/docs/editors/) for a better integrated experience. 142 | 143 | To learn more about Flow, check out [its documentation](https://flow.org/). 144 | 145 | ## Sharing and Deployment 146 | 147 | Create React Native App does a lot of work to make app setup and development simple and straightforward, but it's very difficult to do the same for deploying to Apple's App Store or Google's Play Store without relying on a hosted service. 148 | 149 | ### Publishing to Expo's React Native Community 150 | 151 | Expo provides free hosting for the JS-only apps created by CRNA, allowing you to share your app through the Expo client app. This requires registration for an Expo account. 152 | 153 | Install the `exp` command-line tool, and run the publish command: 154 | 155 | ``` 156 | $ npm i -g exp 157 | $ exp publish 158 | ``` 159 | 160 | ### Building an Expo "standalone" app 161 | 162 | You can also use a service like [Expo's standalone builds](https://docs.expo.io/versions/latest/guides/building-standalone-apps.html) if you want to get an IPA/APK for distribution without having to build the native code yourself. 163 | 164 | ### Ejecting from Create React Native App 165 | 166 | If you want to build and deploy your app yourself, you'll need to eject from CRNA and use Xcode and Android Studio. 167 | 168 | This is usually as simple as running `npm run eject` in your project, which will walk you through the process. Make sure to install `react-native-cli` and follow the [native code getting started guide for React Native](https://facebook.github.io/react-native/docs/getting-started.html). 169 | 170 | #### Should I Use ExpoKit? 171 | 172 | If you have made use of Expo APIs while working on your project, then those API calls will stop working if you eject to a regular React Native project. If you want to continue using those APIs, you can eject to "React Native + ExpoKit" which will still allow you to build your own native code and continue using the Expo APIs. See the [ejecting guide](https://github.com/react-community/create-react-native-app/blob/master/EJECTING.md) for more details about this option. 173 | 174 | ## Troubleshooting 175 | 176 | ### Networking 177 | 178 | If you're unable to load your app on your phone due to a network timeout or a refused connection, a good first step is to verify that your phone and computer are on the same network and that they can reach each other. Create React Native App needs access to ports 19000 and 19001 so ensure that your network and firewall settings allow access from your device to your computer on both of these ports. 179 | 180 | Try opening a web browser on your phone and opening the URL that the packager script prints, replacing `exp://` with `http://`. So, for example, if underneath the QR code in your terminal you see: 181 | 182 | ``` 183 | exp://192.168.0.1:19000 184 | ``` 185 | 186 | Try opening Safari or Chrome on your phone and loading 187 | 188 | ``` 189 | http://192.168.0.1:19000 190 | ``` 191 | 192 | and 193 | 194 | ``` 195 | http://192.168.0.1:19001 196 | ``` 197 | 198 | If this works, but you're still unable to load your app by scanning the QR code, please open an issue on the [Create React Native App repository](https://github.com/react-community/create-react-native-app) with details about these steps and any other error messages you may have received. 199 | 200 | If you're not able to load the `http` URL in your phone's web browser, try using the tethering/mobile hotspot feature on your phone (beware of data usage, though), connecting your computer to that WiFi network, and restarting the packager. If you are using a VPN you may need to disable it. 201 | 202 | ### iOS Simulator won't open 203 | 204 | If you're on a Mac, there are a few errors that users sometimes see when attempting to `npm run ios`: 205 | 206 | * "non-zero exit code: 107" 207 | * "You may need to install Xcode" but it is already installed 208 | * and others 209 | 210 | There are a few steps you may want to take to troubleshoot these kinds of errors: 211 | 212 | 1. Make sure Xcode is installed and open it to accept the license agreement if it prompts you. You can install it from the Mac App Store. 213 | 2. Open Xcode's Preferences, the Locations tab, and make sure that the `Command Line Tools` menu option is set to something. Sometimes when the CLI tools are first installed by Homebrew this option is left blank, which can prevent Apple utilities from finding the simulator. Make sure to re-run `npm/yarn run ios` after doing so. 214 | 3. If that doesn't work, open the Simulator, and under the app menu select `Reset Contents and Settings...`. After that has finished, quit the Simulator, and re-run `npm/yarn run ios`. 215 | 216 | ### QR Code does not scan 217 | 218 | If you're not able to scan the QR code, make sure your phone's camera is focusing correctly, and also make sure that the contrast on the two colors in your terminal is high enough. For example, WebStorm's default themes may [not have enough contrast](https://github.com/react-community/create-react-native-app/issues/49) for terminal QR codes to be scannable with the system barcode scanners that the Expo app uses. 219 | 220 | If this causes problems for you, you may want to try changing your terminal's color theme to have more contrast, or running Create React Native App from a different terminal. You can also manually enter the URL printed by the packager script in the Expo app's search bar to load it manually. 221 | -------------------------------------------------------------------------------- /example/app/app.json: -------------------------------------------------------------------------------- 1 | { 2 | "expo": { 3 | "sdkVersion": "26.0.0" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /example/app/lib/ActionSheetCustom.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { Text, View, Dimensions, Modal, TouchableHighlight, Animated, ScrollView, Easing } from 'react-native' 3 | import * as utils from './utils' 4 | import styles2 from './styles' 5 | 6 | const WARN_COLOR = '#FF3B30' 7 | const MAX_HEIGHT = Dimensions.get('window').height * 0.7 8 | 9 | class ActionSheet extends React.Component { 10 | static defaultProps = { 11 | tintColor: '#007AFF', 12 | buttonUnderlayColor: '#F4F4F4', 13 | onPress: () => {}, 14 | styles: {} 15 | } 16 | 17 | constructor (props) { 18 | super(props) 19 | this.scrollEnabled = false 20 | this.translateY = this._calculateHeight(props) 21 | this.state = { 22 | visible: false, 23 | sheetAnim: new Animated.Value(this.translateY) 24 | } 25 | } 26 | 27 | componentWillReceiveProps (nextProps) { 28 | this.translateY = this._calculateHeight(nextProps) 29 | } 30 | 31 | get styles () { 32 | const { styles } = this.props 33 | const obj = {} 34 | Object.keys(styles2).forEach((key) => { 35 | const arr = [styles2[key]] 36 | if (styles[key]) { 37 | arr.push(styles[key]) 38 | } 39 | obj[key] = arr 40 | }) 41 | return obj 42 | } 43 | 44 | show = () => { 45 | this.setState({visible: true}, () => { 46 | this._showSheet() 47 | }) 48 | } 49 | 50 | hide = (index) => { 51 | this._hideSheet(() => { 52 | this.setState({visible: false}, () => { 53 | this.props.onPress(index) 54 | }) 55 | }) 56 | } 57 | 58 | _cancel = () => { 59 | const { cancelButtonIndex } = this.props 60 | // 保持和 ActionSheetIOS 一致, 61 | // 未设置 cancelButtonIndex 时,点击背景不隐藏 ActionSheet 62 | if (utils.isset(cancelButtonIndex)) { 63 | this.hide(cancelButtonIndex) 64 | } 65 | } 66 | 67 | _showSheet = () => { 68 | Animated.timing(this.state.sheetAnim, { 69 | toValue: 0, 70 | duration: 250, 71 | easing: Easing.out(Easing.ease) 72 | }).start() 73 | } 74 | 75 | _hideSheet (callback) { 76 | Animated.timing(this.state.sheetAnim, { 77 | toValue: this.translateY, 78 | duration: 200 79 | }).start(callback) 80 | } 81 | 82 | /** 83 | * elements: titleBox, messageBox, buttonBox, cancelButtonBox 84 | * box size: height, marginTop, marginBottom 85 | */ 86 | _calculateHeight (props) { 87 | const styles = this.styles 88 | 89 | const getHeight = (name) => { 90 | const style = styles[name][styles[name].length - 1] 91 | let h = 0 92 | ;['height', 'marginTop', 'marginBottom'].forEach((attrName) => { 93 | if (typeof style[attrName] !== 'undefined') { 94 | h += style[attrName] 95 | } 96 | }) 97 | return h 98 | } 99 | 100 | let height = 0 101 | if (props.title) height += getHeight('titleBox') 102 | if (props.message) height += getHeight('messageBox') 103 | if (utils.isset(props.cancelButtonIndex)) { 104 | height += getHeight('cancelButtonBox') 105 | height += (props.options.length - 1) * getHeight('buttonBox') 106 | } else { 107 | height += props.options.length * getHeight('buttonBox') 108 | } 109 | 110 | if (height > MAX_HEIGHT) { 111 | this.scrollEnabled = true 112 | height = MAX_HEIGHT 113 | } else { 114 | this.scrollEnabled = false 115 | } 116 | 117 | return height 118 | } 119 | 120 | _renderTitle () { 121 | const { title } = this.props 122 | const styles = this.styles 123 | if (!title) return null 124 | return ( 125 | 126 | {React.isValidElement(title) ? title : ( 127 | {title} 128 | )} 129 | 130 | ) 131 | } 132 | 133 | _renderMessage () { 134 | const { message } = this.props 135 | const styles = this.styles 136 | if (!message) return null 137 | return ( 138 | 139 | {React.isValidElement(message) ? message : ( 140 | {message} 141 | )} 142 | 143 | ) 144 | } 145 | 146 | _renderCancelButton () { 147 | const { options, cancelButtonIndex } = this.props 148 | if (!utils.isset(cancelButtonIndex)) return null 149 | return this._createButton(options[cancelButtonIndex], cancelButtonIndex) 150 | } 151 | 152 | _createButton (title, index) { 153 | const styles = this.styles 154 | const { buttonUnderlayColor, cancelButtonIndex, destructiveButtonIndex, tintColor } = this.props 155 | const fontColor = destructiveButtonIndex === index ? WARN_COLOR : tintColor 156 | const buttonBoxStyle = cancelButtonIndex === index ? styles.cancelButtonBox : styles.buttonBox 157 | return ( 158 | this.hide(index)} 164 | > 165 | {React.isValidElement(title) ? title : ( 166 | {title} 167 | )} 168 | 169 | ) 170 | } 171 | 172 | _renderOptions () { 173 | const { cancelButtonIndex } = this.props 174 | return this.props.options.map((title, index) => { 175 | return cancelButtonIndex === index ? null : this._createButton(title, index) 176 | }) 177 | } 178 | 179 | render () { 180 | const styles = this.styles 181 | const { visible, sheetAnim } = this.state 182 | return ( 183 | 188 | 189 | 193 | 199 | {this._renderTitle()} 200 | {this._renderMessage()} 201 | {this._renderOptions()} 202 | {this._renderCancelButton()} 203 | 204 | 205 | 206 | ) 207 | } 208 | } 209 | 210 | export default ActionSheet 211 | -------------------------------------------------------------------------------- /example/app/lib/ActionSheetIOS.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Wrap ActionSheetIOS Api as a component. 3 | * @see http://facebook.github.io/react-native/docs/actionsheetios.html 4 | */ 5 | 6 | import React from 'react' 7 | import { ActionSheetIOS } from 'react-native' 8 | import optionsSchema from './options' 9 | 10 | class ActionSheet extends React.Component { 11 | // shold not update whenever, because nothing rendered 12 | shouldComponentUpdate () { 13 | return false 14 | } 15 | 16 | show () { 17 | const props = this.props 18 | const options = {} 19 | optionsSchema.forEach((name) => { 20 | const value = props[name] 21 | if (typeof value !== 'undefined') { 22 | options[name] = value 23 | } 24 | }) 25 | const callback = options.onPress 26 | delete options.onPress 27 | ActionSheetIOS.showActionSheetWithOptions(options, callback) 28 | } 29 | 30 | // need not render anything 31 | render () { 32 | return null 33 | } 34 | } 35 | 36 | export default ActionSheet 37 | -------------------------------------------------------------------------------- /example/app/lib/index.js: -------------------------------------------------------------------------------- 1 | import { Platform } from 'react-native' 2 | import _ActionSheetIOS from './ActionSheetIOS' 3 | import _ActionSheetCustom from './ActionSheetCustom' 4 | 5 | export const ActionSheetCustom = _ActionSheetCustom 6 | 7 | let ActionSheet 8 | 9 | if (Platform.OS === 'ios') { 10 | ActionSheet = _ActionSheetIOS 11 | } else { 12 | ActionSheet = _ActionSheetCustom 13 | } 14 | 15 | export default ActionSheet 16 | -------------------------------------------------------------------------------- /example/app/lib/options.js: -------------------------------------------------------------------------------- 1 | /** 2 | * define all valid options 3 | */ 4 | 5 | export default [ 6 | /** 7 | * a list of button titles (required) 8 | * @type string[] 9 | * @example 10 | * ['cancel', 'Apple', 'Banana'] 11 | */ 12 | 'options', 13 | 14 | /** 15 | * index of cancel button in options 16 | * @type int 17 | */ 18 | 'cancelButtonIndex', 19 | 20 | /** 21 | * index of destructive button in options 22 | * @type int 23 | */ 24 | 'destructiveButtonIndex', 25 | 26 | /** 27 | * a title to show above the action sheet 28 | * @type string 29 | */ 30 | 'title', 31 | 32 | /** 33 | * a message to show below the title 34 | * @type string 35 | */ 36 | 'message', 37 | 38 | /** 39 | * the color used for non-destructive button titles 40 | * @type string 41 | * @see http://facebook.github.io/react-native/docs/colors.html 42 | */ 43 | 'tintColor', 44 | 45 | /** 46 | * The 'callback' function takes one parameter, the zero-based index of the selected item 47 | * @type (buttonIndex) => void 48 | * @example 49 | * (buttonIndex) => if (buttonIndex === 1) { // do something } 50 | */ 51 | 'onPress' 52 | ] 53 | -------------------------------------------------------------------------------- /example/app/lib/styles.js: -------------------------------------------------------------------------------- 1 | import { StyleSheet } from 'react-native' 2 | export const hairlineWidth = StyleSheet.hairlineWidth 3 | export default { 4 | overlay: { 5 | position: 'absolute', 6 | top: 0, 7 | right: 0, 8 | bottom: 0, 9 | left: 0, 10 | opacity: 0.4, 11 | backgroundColor: '#000' 12 | }, 13 | wrapper: { 14 | flex: 1, 15 | flexDirection: 'row' 16 | }, 17 | body: { 18 | flex: 1, 19 | alignSelf: 'flex-end', 20 | backgroundColor: '#e5e5e5' 21 | }, 22 | titleBox: { 23 | height: 40, 24 | alignItems: 'center', 25 | justifyContent: 'center', 26 | backgroundColor: '#fff' 27 | }, 28 | titleText: { 29 | color: '#757575', 30 | fontSize: 14 31 | }, 32 | messageBox: { 33 | height: 30, 34 | paddingLeft: 10, 35 | paddingRight: 10, 36 | paddingBottom: 10, 37 | alignItems: 'center', 38 | justifyContent: 'center', 39 | backgroundColor: '#fff' 40 | }, 41 | messageText: { 42 | color: '#9a9a9a', 43 | fontSize: 12 44 | }, 45 | buttonBox: { 46 | height: 50, 47 | marginTop: hairlineWidth, 48 | alignItems: 'center', 49 | justifyContent: 'center', 50 | backgroundColor: '#fff' 51 | }, 52 | buttonText: { 53 | fontSize: 18 54 | }, 55 | cancelButtonBox: { 56 | height: 50, 57 | marginTop: 6, 58 | alignItems: 'center', 59 | justifyContent: 'center', 60 | backgroundColor: '#fff' 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /example/app/lib/utils.js: -------------------------------------------------------------------------------- 1 | export function isset (prop) { 2 | return typeof prop !== 'undefined' 3 | } 4 | 5 | export function merge (target, source) { 6 | Object.keys(source).forEach((key) => { 7 | if (Object.prototype.toString.call(source).slice(8, -1) === 'Object') { 8 | target[key] = merge(target[key] || {}, source[key]) 9 | } else { 10 | target[key] = source[key] 11 | } 12 | }) 13 | return target 14 | } 15 | -------------------------------------------------------------------------------- /example/app/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "app", 3 | "version": "0.1.0", 4 | "private": true, 5 | "devDependencies": { 6 | "react-native-scripts": "1.13.1", 7 | "jest-expo": "26.0.0", 8 | "react-test-renderer": "16.3.0-alpha.1" 9 | }, 10 | "main": "./node_modules/react-native-scripts/build/bin/crna-entry.js", 11 | "scripts": { 12 | "start": "react-native-scripts start", 13 | "eject": "react-native-scripts eject", 14 | "android": "react-native-scripts android", 15 | "ios": "react-native-scripts ios", 16 | "test": "jest" 17 | }, 18 | "jest": { 19 | "preset": "jest-expo" 20 | }, 21 | "dependencies": { 22 | "expo": "^26.0.0", 23 | "react": "16.3.0-alpha.1", 24 | "react-native": "0.54.0" 25 | } 26 | } -------------------------------------------------------------------------------- /lib/ActionSheetCustom.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { Text, View, Dimensions, Modal, TouchableHighlight, Animated, ScrollView, Easing, SafeAreaView, LayoutChangeEvent, Appearance, StyleSheet } from 'react-native' 3 | import * as utils from './utils' 4 | import styles2 from './styles' 5 | import BlurView from './BlurView' 6 | import { Platform } from 'react-native' 7 | 8 | const WARN_COLOR = '#FF3B30' 9 | 10 | const getMaxHeight = () => { 11 | return Math.round(Dimensions.get('window').height * 0.8) 12 | } 13 | 14 | class ActionSheet extends React.Component { 15 | static defaultProps = { 16 | disabledColor: '#A0A0A0', 17 | buttonUnderlayColor: '#A0A0A055', 18 | disabledIndexes: [], 19 | onPress: () => {}, 20 | styles: {}, 21 | useNativeDriver: true, 22 | statusBarTranslucent: false, 23 | } 24 | 25 | defaultOrientations = ["portrait", "landscape", "landscape-left", "landscape-right"] 26 | 27 | onLayout = (e: LayoutChangeEvent) => { 28 | let translateY = e.nativeEvent.layout.height 29 | const maxHeight = getMaxHeight() 30 | let scrollEnabled = translateY > maxHeight 31 | if (scrollEnabled) { 32 | translateY = maxHeight 33 | } 34 | this.setState({ 35 | translateY, 36 | scrollEnabled, 37 | }) 38 | } 39 | 40 | constructor (props) { 41 | super(props) 42 | 43 | this.state = { 44 | translateY: 0, 45 | scrollEnabled: false, 46 | visible: false, 47 | sheetAnim: new Animated.Value(1), 48 | sheetVisible: false, 49 | } 50 | } 51 | 52 | show = () => { 53 | this.setState({visible: true}, () => { 54 | setTimeout(() => { 55 | this._showSheet() 56 | }) 57 | }) 58 | } 59 | 60 | hide = (index) => { 61 | this._hideSheet(() => { 62 | this.setState({ 63 | visible: false, 64 | translateY: 0, 65 | scrollEnabled: false, 66 | }, () => { 67 | this.props.onPress(index) 68 | }) 69 | }) 70 | } 71 | 72 | _cancel = () => { 73 | const { cancelButtonIndex } = this.props 74 | // 保持和 ActionSheetIOS 一致, 75 | // 未设置 cancelButtonIndex 时,点击背景不隐藏 ActionSheet 76 | if (utils.isset(cancelButtonIndex)) { 77 | this.hide(cancelButtonIndex) 78 | } 79 | } 80 | 81 | _showSheet = () => { 82 | Animated.timing(this.state.sheetAnim, { 83 | toValue: 0, 84 | duration: 250, 85 | easing: Easing.out(Easing.ease), 86 | useNativeDriver: this.props.useNativeDriver 87 | }).start() 88 | } 89 | 90 | _hideSheet (callback) { 91 | Animated.timing(this.state.sheetAnim, { 92 | toValue: 1, 93 | duration: 200, 94 | useNativeDriver: this.props.useNativeDriver 95 | }).start(callback) 96 | } 97 | 98 | _textColor = (mergedStyles) => { 99 | return this._isDarkMode() ? mergedStyles.textDarkTheme : mergedStyles.textLightTheme 100 | } 101 | 102 | _renderTitle () { 103 | const { title, styles } = this.props 104 | const mergedStyles = getMergedStyles(styles) 105 | if (!title) return null 106 | return ( 107 | 108 | {React.isValidElement(title) ? title : ( 109 | {title} 110 | )} 111 | 112 | ) 113 | } 114 | 115 | _renderMessage () { 116 | const { message, styles } = this.props 117 | const mergedStyles = getMergedStyles(styles) 118 | if (!message) return null 119 | return ( 120 | 121 | {React.isValidElement(message) ? message : ( 122 | {message} 123 | )} 124 | 125 | ) 126 | } 127 | 128 | _renderCancelButton () { 129 | const { options, cancelButtonIndex } = this.props 130 | if (!utils.isset(cancelButtonIndex)) return null 131 | return this._createButton(options[cancelButtonIndex], cancelButtonIndex) 132 | } 133 | 134 | _getBorderTopStyle = () => { 135 | return { 136 | borderTopColor: this._isDarkMode() ? "#fff5" : "#0005", 137 | borderTopWidth: StyleSheet.hairlineWidth, 138 | } 139 | } 140 | 141 | _createButton (title, index) { 142 | const styles = getMergedStyles(this.props.styles) 143 | const { buttonUnderlayColor, cancelButtonIndex, destructiveButtonIndex, disabledIndexes, disabledColor } = this.props 144 | const tintColor = this.props.tintColor || (this._isDarkMode() ? "#4793FF" : "#007AFF") 145 | const fontColor = destructiveButtonIndex === index ? WARN_COLOR : tintColor 146 | const isCancel = cancelButtonIndex === index 147 | const buttonBoxStyle = isCancel ? styles.cancelButtonBox : styles.buttonBox 148 | const isDisabled = disabledIndexes.indexOf(index) !== -1 149 | if (isDisabled) { 150 | fontColor = disabledColor 151 | } 152 | return ( 153 | this.hide(index)} 159 | disabled={isDisabled} 160 | > 161 | {React.isValidElement(title) ? title : ( 162 | {title} 163 | )} 164 | 165 | ) 166 | } 167 | 168 | _renderOptions () { 169 | const { cancelButtonIndex } = this.props 170 | return this.props.options.map((title, index) => { 171 | return cancelButtonIndex === index ? null : this._createButton(title, index) 172 | }) 173 | } 174 | 175 | _isDarkMode = () => { 176 | return (this.props.userInterfaceStyle || Appearance.getColorScheme()) == "dark" 177 | } 178 | 179 | render () { 180 | const styles = getMergedStyles(this.props.styles) 181 | const iosStyle = this.props.theme ? this.props.theme == "ios" : Platform.OS == "ios" 182 | const boxStyle = iosStyle ? styles.roundedBox : {} 183 | const { visible, sheetAnim, scrollEnabled, translateY } = this.state 184 | const darkMode = this._isDarkMode() 185 | return ( 186 | 193 | 194 | 200 | 216 | 230 | {this._renderTitle()} 231 | {this._renderMessage()} 232 | 233 | {this._renderOptions()} 234 | 235 | 236 | {this._renderCancelButton()} 237 | 238 | 239 | 240 | 241 | ) 242 | } 243 | } 244 | 245 | function getMergedStyles(styles) { 246 | const obj = {} 247 | Object.keys(styles2).forEach((key) => { 248 | const arr = [styles2[key]] 249 | if (styles[key]) { 250 | arr.push(styles[key]) 251 | } 252 | obj[key] = arr 253 | }) 254 | return obj 255 | } 256 | 257 | export default ActionSheet 258 | -------------------------------------------------------------------------------- /lib/ActionSheetIOS.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Wrap ActionSheetIOS Api as a component. 3 | * @see http://facebook.github.io/react-native/docs/actionsheetios.html 4 | */ 5 | 6 | import React from 'react' 7 | import { ActionSheetIOS } from 'react-native' 8 | import optionsSchema from './options' 9 | 10 | class ActionSheet extends React.Component { 11 | // shold not update whenever, because nothing rendered 12 | shouldComponentUpdate () { 13 | return false 14 | } 15 | 16 | show () { 17 | const props = this.props 18 | const options = {} 19 | optionsSchema.forEach((name) => { 20 | const value = props[name] 21 | if (typeof value !== 'undefined') { 22 | options[name] = value 23 | } 24 | }) 25 | const callback = options.onPress 26 | delete options.onPress 27 | ActionSheetIOS.showActionSheetWithOptions(options, callback) 28 | } 29 | 30 | // need not render anything 31 | render () { 32 | return null 33 | } 34 | } 35 | 36 | export default ActionSheet 37 | -------------------------------------------------------------------------------- /lib/BlurView.ios.js: -------------------------------------------------------------------------------- 1 | 2 | import { BlurView } from '@react-native-community/blur' 3 | 4 | export default BlurView 5 | -------------------------------------------------------------------------------- /lib/BlurView.js: -------------------------------------------------------------------------------- 1 | import { View } from "react-native" 2 | 3 | const BlurView = View 4 | export default BlurView; 5 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | import { Platform } from 'react-native' 2 | import _ActionSheetIOS from './ActionSheetIOS' 3 | import _ActionSheetCustom from './ActionSheetCustom' 4 | 5 | export const ActionSheetCustom = _ActionSheetCustom 6 | 7 | let ActionSheet 8 | 9 | if (Platform.OS === 'ios') { 10 | ActionSheet = _ActionSheetIOS 11 | } else { 12 | ActionSheet = _ActionSheetCustom 13 | } 14 | 15 | export default ActionSheet 16 | -------------------------------------------------------------------------------- /lib/options.js: -------------------------------------------------------------------------------- 1 | /** 2 | * define all valid options 3 | */ 4 | 5 | export default [ 6 | /** 7 | * a list of button titles (required) 8 | * @type string[] 9 | * @example 10 | * ['cancel', 'Apple', 'Banana'] 11 | */ 12 | 'options', 13 | 14 | /** 15 | * index of cancel button in options 16 | * @type int 17 | */ 18 | 'cancelButtonIndex', 19 | 20 | /** 21 | * index of destructive button in options 22 | * @type int 23 | */ 24 | 'destructiveButtonIndex', 25 | 26 | /** 27 | * a title to show above the action sheet 28 | * @type string 29 | */ 30 | 'title', 31 | 32 | /** 33 | * a message to show below the title 34 | * @type string 35 | */ 36 | 'message', 37 | 38 | /** 39 | * the color used for non-destructive button titles 40 | * @type string 41 | * @see http://facebook.github.io/react-native/docs/colors.html 42 | */ 43 | 'tintColor', 44 | 45 | /** 46 | * iOS only, change default theme 47 | * @default system theme color 48 | */ 49 | 'userInterfaceStyle', 50 | 51 | /** 52 | * The 'callback' function takes one parameter, the zero-based index of the selected item 53 | * @type (buttonIndex) => void 54 | * @example 55 | * (buttonIndex) => if (buttonIndex === 1) { // do something } 56 | */ 57 | 'onPress', 58 | 59 | /** 60 | * Android only, set the Modal to be drawn under the (translucent) StatusBar. 61 | * Sets the property of the same name on the Modal. 62 | * @type boolean 63 | * @see https://reactnative.dev/docs/modal#statusbartranslucent-android 64 | */ 65 | 'statusBarTranslucent' 66 | ] 67 | -------------------------------------------------------------------------------- /lib/styles.js: -------------------------------------------------------------------------------- 1 | import { StyleSheet } from 'react-native' 2 | export const hairlineWidth = StyleSheet.hairlineWidth 3 | export default { 4 | overlay: { 5 | position: 'absolute', 6 | top: 0, 7 | right: 0, 8 | bottom: 0, 9 | left: 0, 10 | opacity: 0.4, 11 | backgroundColor: '#000' 12 | }, 13 | wrapper: { 14 | flex: 1, 15 | flexDirection: 'row' 16 | }, 17 | body: { 18 | flex: 1, 19 | alignSelf: 'flex-end', 20 | backgroundColor: '#e5e5e5' 21 | }, 22 | bodyDark: { 23 | flex: 1, 24 | alignSelf: 'flex-end', 25 | backgroundColor: '#404040', 26 | }, 27 | bodyIos: { 28 | flex: 1, 29 | alignSelf: 'flex-end', 30 | padding: 8, 31 | }, 32 | titleBox: { 33 | paddingTop: 15, 34 | paddingBottom: 5, 35 | alignItems: 'center', 36 | justifyContent: 'center', 37 | }, 38 | titleText: { 39 | fontSize: 14, 40 | fontWeight: "bold", 41 | textAlign: "center", 42 | }, 43 | messageBox: { 44 | paddingHorizontal: 15, 45 | paddingBottom: 20, 46 | alignItems: 'center', 47 | justifyContent: 'center', 48 | }, 49 | messageText: { 50 | fontSize: 13, 51 | textAlign: "center", 52 | }, 53 | textLightTheme: { 54 | color: "#7c7c7c", 55 | }, 56 | textDarkTheme: { 57 | color: "#a4a4A4", 58 | }, 59 | buttonBox: { 60 | height: 57, 61 | alignItems: 'center', 62 | justifyContent: 'center', 63 | }, 64 | buttonText: { 65 | fontSize: 20, 66 | }, 67 | cancelButtonBox: { 68 | height: 57, 69 | alignItems: 'center', 70 | justifyContent: 'center', 71 | }, 72 | cancelButtonText: { 73 | fontSize: 20, 74 | fontWeight: "500", 75 | }, 76 | roundedBox: { 77 | borderRadius: 13, 78 | overflow: "hidden", 79 | }, 80 | } -------------------------------------------------------------------------------- /lib/ts/index.d.ts: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { TextStyle, ViewStyle } from 'react-native'; 3 | 4 | type Props = { 5 | options: string[]; 6 | onPress: (index: number) => void; 7 | title?: string; 8 | message?: string; 9 | tintColor?: string; 10 | cancelButtonIndex?: number; 11 | destructiveButtonIndex?: number; 12 | /** 13 | * Only for Android or ActionSheetCustom 14 | */ 15 | styles?: { 16 | titleBox?: ViewStyle, 17 | titleText?: TextStyle, 18 | 19 | messageBox?: ViewStyle, 20 | messageText?: TextStyle, 21 | 22 | buttonText?: TextStyle, 23 | buttonBox?: ViewStyle, 24 | cancelButtonBox?: ViewStyle, 25 | 26 | overlay?: TextStyle, 27 | wrapper?: ViewStyle, 28 | body?: ViewStyle, 29 | }; 30 | /** 31 | * Only for Android 32 | */ 33 | statusBarTranslucent?: boolean; 34 | 35 | /** 36 | * Change theme color 37 | * @default system theme color 38 | */ 39 | userInterfaceStyle?: "light" | "dark" 40 | } 41 | 42 | type ActionSheetProps = Props & { 43 | /** 44 | * Android only. 45 | * **ios** theme is similar to the iOS ActionSheet with rounded boxes 46 | * @default flat 47 | */ 48 | theme?: "flat" | "ios" 49 | } 50 | 51 | export default class ActionSheet extends Component { 52 | public show: () => void; 53 | } 54 | 55 | type ActionSheetCustomProps = Props | { 56 | title?: string | React.ReactNode 57 | message?: string | React.ReactNode 58 | options: (string | React.ReactChild)[] 59 | 60 | /** 61 | * Edit supported orientations (https://reactnative.dev/docs/modal#supportedorientations-ios). 62 | * Use this prop to support portrait-upsite-down or to lock landscape orientation. 63 | * @default ["portrait", "landscape", "landscape-left", "landscape-right"] 64 | */ 65 | supportedOrientations?: string[] 66 | 67 | /** 68 | * Starting from v3.0.0 ActionSheetCustom uses a native-like theme build using react-native to allow React Components as options (or title or message) 69 | * 70 | * **flat** is the default option for Android (use theme="flat" to use it on iOS too) 71 | * 72 | * Use theme="ios" to use rounded boxes (like iOS theme) on Android 73 | * @default flat for Android and native-like for iOS 74 | */ 75 | theme?: "flat" | "ios" 76 | } 77 | 78 | export class ActionSheetCustom extends Component { 79 | public show: () => void; 80 | } -------------------------------------------------------------------------------- /lib/utils.js: -------------------------------------------------------------------------------- 1 | export function isset (prop) { 2 | return typeof prop !== 'undefined' 3 | } 4 | 5 | export function merge (target, source) { 6 | Object.keys(source).forEach((key) => { 7 | if (Object.prototype.toString.call(source).slice(8, -1) === 'Object') { 8 | target[key] = merge(target[key] || {}, source[key]) 9 | } else { 10 | target[key] = source[key] 11 | } 12 | }) 13 | return target 14 | } 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@alessiocancian/react-native-actionsheet", 3 | "version": "3.2.0", 4 | "description": "Cross platform ActionSheet. This component implements a custom ActionSheet and provides the same way to drawing it on the defferent platforms(iOS and Android). Actually, In order to keep the best effect, it still uses the ActionSheetIOS on iOS.", 5 | "main": "lib/index.js", 6 | "types": "lib/ts/index.d.ts", 7 | "scripts": { 8 | "lint": "standard lib/**/*.js", 9 | "test": "jest --no-cache" 10 | }, 11 | "jest": { 12 | "preset": "react-native", 13 | "modulePathIgnorePatterns": [ 14 | "/Examples" 15 | ] 16 | }, 17 | "files": [ 18 | "lib" 19 | ], 20 | "repository": { 21 | "type": "git", 22 | "url": "git+https://github.com/alessiocancian/react-native-actionsheet.git" 23 | }, 24 | "keywords": [ 25 | "actionsheet", 26 | "action sheet", 27 | "react-native", 28 | "react-native-action-sheet", 29 | "react-native-actionsheet", 30 | "custom-action-sheet" 31 | ], 32 | "author": { 33 | "name": "Alessio Cancian", 34 | "url": "https://github.com/alessiocancian" 35 | }, 36 | "license": "MIT", 37 | "bugs": { 38 | "url": "https://github.com/alessiocancian/react-native-actionsheet/issues" 39 | }, 40 | "homepage": "https://github.com/alessiocancian/react-native-actionsheet#readme", 41 | "peerDependencies": { 42 | "prop-types": ">=15.4.0", 43 | "react": ">=15.4.0", 44 | "react-native": "*" 45 | }, 46 | "devDependencies": { 47 | "babel-eslint": "^8.2.2", 48 | "babel-jest": "^21.2.0", 49 | "babel-preset-env": "^1.6.1", 50 | "babel-preset-react-native": "^4.0.0", 51 | "jest": "^21.2.1", 52 | "react-test-renderer": "15.4.0", 53 | "standard": "*" 54 | }, 55 | "standard": { 56 | "parser": "babel-eslint" 57 | }, 58 | "dependencies": { 59 | "@react-native-community/blur": "^4.2.0" 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /tests/ActionSheetCustom.test.js: -------------------------------------------------------------------------------- 1 | import 'react-native' 2 | import React from 'react' 3 | import renderer from 'react-test-renderer' 4 | 5 | import ExampleA from '../Examples/Basic/ExampleA' 6 | import ExampleB from '../Examples/Basic/ExampleB' 7 | 8 | test('ExampleA render correctly', () => { 9 | const tree = renderer.create( 10 | 11 | ).toJSON() 12 | expect(tree).toMatchSnapshot() 13 | }) 14 | 15 | test('ExampleB render correctly', () => { 16 | const tree = renderer.create( 17 | 18 | ).toJSON() 19 | expect(tree).toMatchSnapshot() 20 | }) 21 | -------------------------------------------------------------------------------- /tests/__snapshots__/ActionSheetCustom.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`ExampleA render correctly 1`] = ` 4 | 13 | 23 | I like 24 | 25 | 42 | Example A 43 | 44 | 45 | `; 46 | 47 | exports[`ExampleB render correctly 1`] = ` 48 | 57 | 67 | I like 68 | 69 | 86 | Example B 87 | 88 | 94 | 102 | 119 | 134 | 144 | 155 | Which one do you like? 156 | 157 | 158 | 169 | 180 | custom message custom message custom message custom message custom message custom message 181 | 182 | 183 | 187 | 219 | 234 | Apple 235 | 236 | 237 | 269 | 279 | Banana 280 | 281 | 282 | 314 | 329 | Watermelon 330 | 331 | 332 | 364 | 374 | Durian 375 | 376 | 377 | 378 | 412 | 428 | Cancel 429 | 430 | 431 | 432 | 433 | 434 | 435 | `; 436 | --------------------------------------------------------------------------------