UNPKG

8.21 kBMarkdownView Raw
1# react-native-action-sheet
2[![npm](https://img.shields.io/npm/v/@expo/react-native-action-sheet.svg?style=flat-square)](https://www.npmjs.com/package/@expo/react-native-action-sheet)
3[![License: MIT](https://img.shields.io/github/license/nd-02110114/goofi-mobile.svg)](https://opensource.org/licenses/MIT)
4[![Slack](https://slack.expo.io/badge.svg)](https://slack.expo.io)
5
6ActionSheet is a cross-platform React Native component that uses the native UIActionSheet on iOS and a JS implementation on Android. Almost a drop in replacement for [ActionSheetIOS](https://facebook.github.io/react-native/docs/actionsheetios.html) except it cannot be called statically.
7
8
9| iOS | Android | Web |
10|---------------------------|---------------------------|---------------------------|
11| <img src="https://raw.githubusercontent.com/expo/react-native-action-sheet/master/gif/ios.gif" width="200" height="400"/> | <img src="https://raw.githubusercontent.com/expo/react-native-action-sheet/master/gif/android.gif" width="200" height="400"/> | <img src="https://raw.githubusercontent.com/expo/react-native-action-sheet/master/gif/web.gif" width="400" height="400"/> |
12
13
14## Installation
15
16```
17$ npm install @expo/react-native-action-sheet -S
18```
19or
20```
21$ yarn add @expo/react-native-action-sheet
22```
23
24## A basic ActionSheet Setup
25
26### 1. Wrap your top-level component with `<ActionSheetProvider />`
27
28```es6
29
30import { ActionSheetProvider } from '@expo/react-native-action-sheet'
31
32class AppContainer extends React.Component {
33 render() {
34 return (
35 <ActionSheetProvider>
36 <ConnectedApp />
37 </ActionSheetProvider>
38 );
39 }
40}
41```
42
43### 2. Connect your component which uses showActionSheetWithOptions.
44```es6
45import { connectActionSheet } from '@expo/react-native-action-sheet'
46
47class App extends React.Component {
48 /* ... */
49}
50
51const ConnectedApp = connectActionSheet(App)
52
53export default ConnectedApp
54```
55
56`App` component can access the actionSheet method as `this.props.showActionSheetWithOptions`
57
58```es6
59_onOpenActionSheet = () => {
60 // Same interface as https://facebook.github.io/react-native/docs/actionsheetios.html
61 const options = ['Delete', 'Save', 'Cancel'];
62 const destructiveButtonIndex = 0;
63 const cancelButtonIndex = 2;
64
65 this.props.showActionSheetWithOptions(
66 {
67 options,
68 cancelButtonIndex,
69 destructiveButtonIndex,
70 },
71 buttonIndex => {
72 // Do something here depending on the button index selected
73 },
74 );
75};
76```
77
78You can use a hook instead of the higher order component if you are on React 16.8 or newer.
79
80```es6
81import { useActionSheet } from '@expo/react-native-action-sheet'
82
83export default function App () {
84 const { showActionSheetWithOptions } = useActionSheet();
85 /* ... */
86}
87```
88
89## Options
90
91The goal of this library is to mimic the native iOS and Android ActionSheets as closely as possible.
92
93This library can also be used in the browser with Expo for web.
94
95### Universal Props
96
97The same options available on React Native's [ActionSheetIOS](https://facebook.github.io/react-native/docs/actionsheetios.html#showactionsheetwithoptions) component exist for both iOS and Android in this library.
98
99### iOS Only Props
100
101| Name | Type | Required | Default |
102| -------------------| -------| -------- | ------- |
103| anchor | number | No | |
104| userInterfaceStyle | string | No | |
105
106#### `anchor` (optional)
107iPad only option that allows for docking the action sheet to a node. See [ShowActionSheetButton.tsx](/example/ShowActionSheetButton.tsx) for an example on how to implement this.
108
109#### `userInterfaceStyle` (optional)
110The interface style used for the action sheet, can be set to `light` or `dark`, otherwise the default system style will be used.
111
112### Android/Web-Only Props
113
114The below props allow modification of the Android ActionSheet. They have no effect on the look on iOS as the native iOS Action Sheet does not have options for modifying these options.
115
116
117| Name | Type | Required | Default |
118| -----------------| ----------------------------------| -------- | ------- |
119| icons | array of required images or icons | No | |
120| tintIcons | boolean | No | true |
121| textStyle | TextStyle | No | |
122| titleTextStyle | TextStyle | No | |
123| messageTextStyle | TextStyle | No | |
124| autoFocus | boolean | No | false |
125| showSeparators | boolean | No | false |
126| containerStyle | ViewStyle | No | |
127| separatorStyle | ViewStyle | No | |
128| useModal | boolean | No | false |
129| destructiveColor | string | No | #d32f2f |
130
131#### `icons` (optional)
132
133Show icons to go along with each option. If image source paths are provided via `require`, images will be rendered for you. Alternatively, you can provide an array of elements such as vector icons, pre-rendered Images, etc.
134
135#### `tintIcons` (optional)
136 Icons by default will be tinted to match the text color. When set to false, the icons will be the color of the source image. This is useful if you want to use multicolor icons. If you provide your own nodes/pre-rendered icons rather than required images in the `icons` array, you will need to tint them appropriately before providing them in the array of `icons`; `tintColor` will not be applied to icons unless they are images from a required source.
137
138#### `textStyle` (optional)
139Apply any text style props to the options. If the `tintColor` option is provided, it takes precedence over a color text style prop.
140
141#### `titleTextStyle` (optional)
142Apply any text style props to the title if present.
143
144#### `messageTextStyle` (optional)
145Apply any text style props to the message if present.
146
147#### `autoFocus`: (optional)
148If true, will give the first option screen reader focus automatically when the action sheet becomes visible.
149On iOS, this is the default behavior of the native action sheet.
150
151#### `showSeparators`: (optional)
152Show separators between items. On iOS, separators always show so this prop has no effect.
153
154#### `containerStyle`: (optional)
155Apply any view style props to the container rather than use the default look (e.g. dark mode).
156
157#### `separatorStyle`: (optional)
158Modify the look of the separators rather than use the default look.
159
160#### `useModal`: (optional)
161Wrap the ActionSheet with a Modal, in order to show in front of other Modals that were already opened ([issue reference](https://github.com/expo/react-native-action-sheet/issues/164)).
162
163#### `destructiveColor`: (optional)
164Modify color for text of destructive option.
165
166## ActionSheetProvider Props
167
168The following props can be set directly on the `ActionSheetProvider`
169
170#### `useNativeDriver` (optional)
171Windows only option that provides the option to disable the [native animation](https://reactnative.dev/docs/animated#using-the-native-driver) driver for React Native Windows projects targeting _Windows 10 Version-1809 ; Build-10.0.17763.0_ and earlier. `useNativeDriver` is [supported in Version-1903 and later](https://microsoft.github.io/react-native-windows/docs/win10-compat) so if your project is targeting that, you don't need to set this prop.
172
173
174## Try it out
175
176Try it in Expo: https://expo.io/@community/react-native-action-sheet-example
177
178## Example
179
180See the [example app](https://github.com/expo/react-native-action-sheet/tree/master/example).
181
182### Usage
183```
184$ cd example
185$ yarn
186
187// build simulator
188$ yarn ios
189$ yarn android
190
191// web
192$ yarn web
193```
194
195## Development
196
197### Setup
198```
199$ git clone git@github.com:expo/react-native-action-sheet.git
200$ cd react-native-action-sheet
201$ yarn
202```
203
204### Build
205We use [bob](https://github.com/react-native-community/bob).
206```
207$ yarn build
208```
209
210### Lint & Format
211```
212// tsc
213$ yarn type-check
214
215// ESLint
216$ yarn lint
217
218// prettier
219$ yarn fmt
220```