UNPKG

16.3 kBMarkdownView Raw
1# @expo/react-native-action-sheet
2
3[![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)
4[![License: MIT](https://img.shields.io/github/license/nd-02110114/goofi-mobile.svg)](https://opensource.org/licenses/MIT)
5[![Discord](https://img.shields.io/badge/discord-expo-green?style=flat-square&logo=discord)](https://discord.gg/4gtbPAdpaE)
6
7React Native Action Sheet is a cross-platform React Native component that uses the native UIActionSheet on iOS and a pure JS implementation on Android.
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## [Check out the example snack here!](https://snack.expo.dev/@expo-action-sheet/example)
14
15## Installation
16
17```
18npm install @expo/react-native-action-sheet
19```
20
21or
22
23```
24yarn add @expo/react-native-action-sheet
25```
26
27## A basic ActionSheet Setup
28
29### 1. Wrap your top-level component with `<ActionSheetProvider />`
30
31ReactNativeActionSheet uses React context to allow your components to invoke the menu. This means your app needs to be wrapped with the `ActionSheetProvider` component first.
32
33```jsx
34import { ActionSheetProvider } from '@expo/react-native-action-sheet';
35
36export default function AppContainer() {
37 return (
38 <ActionSheetProvider>
39 <App />
40 </ActionSheetProvider>
41 );
42}
43```
44
45### 2. Call the `showActionSheetWithOptions` method with a hook or a higher order component.
46
47```tsx
48// Using the provided hook
49import { useActionSheet } from '@expo/react-native-action-sheet';
50
51export default Menu() {
52 const { showActionSheetWithOptions } = useActionSheet();
53
54 const onPress = () => {
55 const options = ['Delete', 'Save', 'Cancel'];
56 const destructiveButtonIndex = 0;
57 const cancelButtonIndex = 2;
58
59 showActionSheetWithOptions({
60 options,
61 cancelButtonIndex,
62 destructiveButtonIndex
63 }, (selectedIndex: number) => {
64 switch (selectedIndex) {
65 case 1:
66 // Save
67 break;
68
69 case destructiveButtonIndex:
70 // Delete
71 break;
72
73 case cancelButtonIndex:
74 // Canceled
75 }});
76 }
77
78 return (
79 <Button title="Menu" onPress={onPress}/>
80 )
81};
82```
83
84Alternatively, any component can use the higher order component to access the context and pass the `showActionSheetWithOptions` as a prop.
85
86```tsx
87// Using a Higher Order Component to wrap your component
88import { connectActionSheet } from '@expo/react-native-action-sheet';
89
90function Menu({ showActionSheetWithOptions }) {
91 /* ... */
92}
93
94export default connectActionSheet(Menu);
95```
96
97`Menu` component can now access the actionSheet prop as `showActionSheetWithOptions`.
98
99## Options
100
101The goal of this library is to mimic the native iOS and Android ActionSheets as closely as possible.
102
103This library can also be used in the browser with Expo for web.
104
105### Universal Props
106
107| Name | Type | Description |
108| ------------------------ | -------------------------- | ------------------------------------------------------------- |
109| `options` | array of strings | A list of button titles **(required)** |
110| `cancelButtonIndex` | number | Index of cancel button in options |
111| `cancelButtonTintColor` | string | Color used for the change the text color of the cancel button |
112| `destructiveButtonIndex` | number or array of numbers | Indices of destructive buttons in options |
113| `title` | string | Title to show above the action sheet |
114| `message` | string | Message to show below the title |
115| `tintColor` | string | Color used for non-destructive button titles |
116| `disabledButtonIndices` | array of numbers | Indices of disabled buttons in options |
117
118### iOS Only Props
119
120| Name | Type | Description |
121| -------------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
122| `anchor` | number | iPad 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. |
123| `userInterfaceStyle` | string | The interface style used for the action sheet, can be set to `light` or `dark`, otherwise the default system style will be used. |
124
125### Custom Action Sheet Only (Android/Web) Props
126
127The 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.
128
129| Name | Type | Description |
130| ------------------ | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
131| `icons` | array of required images or icons | Show 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. |
132| `tintIcons` | boolean | 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. |
133| `textStyle` | TextStyle | Apply any text style props to the options. If the `tintColor` option is provided, it takes precedence over a color text style prop. |
134| `titleTextStyle` | TextStyle | Apply any text style props to the title if present. |
135| `messageTextStyle` | TextStyle | Apply any text style props to the message if present. |
136| `autoFocus` | boolean | If `true`, this will give the first option screen reader focus automatically when the action sheet becomes visible. On iOS, this is the default behavior of the native action sheet. |
137| `showSeparators` | boolean | Show separators between items. On iOS, separators always show so this prop has no effect. |
138| `containerStyle` | ViewStyle | Apply any view style props to the container rather than use the default look (e.g. dark mode). |
139| `separatorStyle` | ViewStyle | Modify the look of the separators rather than use the default look. |
140| `useModal` | boolean | Defaults to `false` (`true` if autoFocus is also `true`) Wraps 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)). |
141| `destructiveColor` | string | Modify color for text of destructive option. Defaults to `#d32f2f`. |
142
143## ActionSheetProvider Props
144
145The following props can be set directly on the `ActionSheetProvider`
146
147| Name | Type | Description |
148| ---------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
149| `useCustomActionSheet` | boolean | iOS only prop that uses the custom pure JS action sheet (Android/Web version) instead of the native ActionSheetIOS component. Defaults to `false`. |
150| `useNativeDriver` | boolean | Windows 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. |
151
152```jsx
153// example of using useCustomActionSheet on iOS
154export default function AppContainer() {
155 return (
156 <ActionSheetProvider useCustomActionSheet={true}>
157 <App />
158 </ActionSheetProvider>
159 );
160}
161```
162
163## Callback
164
165The second parameter of the `showActionSheetWithOptions` function is a callback for when a button is selected. The callback takes a single argument which will be the zero-based index of the pressed option. You can check the value against your `cancelButtonIndex` to determine if the action was cancelled or not.
166
167```tsx
168function onButtonPress(selectedIndex: number) {
169 // handle it!
170}
171```
172
173## Try it out
174
175Try it in Expo Snack: https://snack.expo.dev/@expo-action-sheet/example.
176
177## Example
178
179See the [example app](https://github.com/expo/react-native-action-sheet/tree/master/example).
180
181### Usage
182
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```
200$ git clone git@github.com:expo/react-native-action-sheet.git
201$ cd react-native-action-sheet
202$ yarn
203```
204
205### Build
206
207We use [bob](https://github.com/react-native-community/bob).
208
209```
210$ yarn build
211```
212
213### Lint & Format
214
215```
216// tsc
217$ yarn type-check
218
219// ESLint + Prettier
220$ yarn lint
221```