|
1 | | ---- |
2 | | -id: actionsheetios |
3 | | -title: ActionSheetIOS |
4 | | ---- |
5 | | - |
6 | | -Displays native to iOS [Action Sheet](https://developer.apple.com/design/human-interface-guidelines/action-sheets) component. |
7 | | - |
8 | | -## Example |
9 | | - |
10 | | -```SnackPlayer name=ActionSheetIOS%20Example&supportedPlatforms=ios |
11 | | -import {useState} from 'react'; |
12 | | -import {ActionSheetIOS, Button, StyleSheet, Text} from 'react-native'; |
13 | | -import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context'; |
14 | | -
|
15 | | -const App = () => { |
16 | | - const [result, setResult] = useState('🔮'); |
17 | | -
|
18 | | - const onPress = () => |
19 | | - ActionSheetIOS.showActionSheetWithOptions( |
20 | | - { |
21 | | - options: ['Cancel', 'Generate number', 'Reset'], |
22 | | - destructiveButtonIndex: 2, |
23 | | - cancelButtonIndex: 0, |
24 | | - userInterfaceStyle: 'dark', |
25 | | - }, |
26 | | - buttonIndex => { |
27 | | - if (buttonIndex === 0) { |
28 | | - // cancel action |
29 | | - } else if (buttonIndex === 1) { |
30 | | - setResult(String(Math.floor(Math.random() * 100) + 1)); |
31 | | - } else if (buttonIndex === 2) { |
32 | | - setResult('🔮'); |
33 | | - } |
34 | | - }, |
35 | | - ); |
36 | | -
|
37 | | - return ( |
38 | | - <SafeAreaProvider> |
39 | | - <SafeAreaView style={styles.container}> |
40 | | - <Text style={styles.result}>{result}</Text> |
41 | | - <Button onPress={onPress} title="Show Action Sheet" /> |
42 | | - </SafeAreaView> |
43 | | - </SafeAreaProvider> |
44 | | - ); |
45 | | -}; |
46 | | -
|
47 | | -const styles = StyleSheet.create({ |
48 | | - container: { |
49 | | - flex: 1, |
50 | | - justifyContent: 'center', |
51 | | - }, |
52 | | - result: { |
53 | | - fontSize: 64, |
54 | | - textAlign: 'center', |
55 | | - }, |
56 | | -}); |
57 | | -
|
58 | | -export default App; |
59 | | -``` |
60 | | - |
61 | | -# Reference |
62 | | - |
63 | | -## Methods |
64 | | - |
65 | | -### `showActionSheetWithOptions()` |
66 | | - |
67 | | -```tsx |
68 | | -static showActionSheetWithOptions: ( |
69 | | - options: ActionSheetIOSOptions, |
70 | | - callback: (buttonIndex: number) => void, |
71 | | -); |
72 | | -``` |
73 | | - |
74 | | -Display an iOS action sheet. The `options` object must contain one or more of: |
75 | | - |
76 | | -- `options` (array of strings) - a list of button titles (required) |
77 | | -- `cancelButtonIndex` (int) - index of cancel button in `options` |
78 | | -- `cancelButtonTintColor` (string) - the [color](colors) used for the change the text color of the cancel button |
79 | | -- `destructiveButtonIndex` (int or array of ints) - indices of destructive buttons in `options` |
80 | | -- `title` (string) - a title to show above the action sheet |
81 | | -- `message` (string) - a message to show below the title |
82 | | -- `anchor` (number) - the node to which the action sheet should be anchored (used for iPad) |
83 | | -- `tintColor` (string) - the [color](colors) used for non-destructive button titles |
84 | | -- `disabledButtonIndices` (array of numbers) - a list of button indices which should be disabled |
85 | | -- `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 |
86 | | - |
87 | | -The 'callback' function takes one parameter, the zero-based index of the selected item. |
88 | | - |
89 | | -Minimal example: |
90 | | - |
91 | | -```tsx |
92 | | -ActionSheetIOS.showActionSheetWithOptions( |
93 | | - { |
94 | | - options: ['Cancel', 'Remove'], |
95 | | - destructiveButtonIndex: 1, |
96 | | - cancelButtonIndex: 0, |
97 | | - }, |
98 | | - buttonIndex => { |
99 | | - if (buttonIndex === 1) { |
100 | | - /* destructive action */ |
101 | | - } |
102 | | - }, |
103 | | -); |
104 | | -``` |
105 | | - |
106 | | ---- |
107 | | - |
108 | | -### `dismissActionSheet()` |
109 | | - |
110 | | -```tsx |
111 | | -static dismissActionSheet(); |
112 | | -``` |
113 | | - |
114 | | -Dismisses the most upper iOS action sheet presented, if no action sheet is present a warning is displayed. |
115 | | - |
116 | | ---- |
117 | | - |
118 | | -### `showShareActionSheetWithOptions()` |
119 | | - |
120 | | -```tsx |
121 | | -static showShareActionSheetWithOptions: ( |
122 | | - options: ShareActionSheetIOSOptions, |
123 | | - failureCallback: (error: Error) => void, |
124 | | - successCallback: (success: boolean, method: string) => void, |
125 | | -); |
126 | | -``` |
127 | | - |
128 | | -Display the iOS share sheet. The `options` object should contain one or both of `message` and `url` and can additionally have a `subject` or `excludedActivityTypes`: |
129 | | - |
130 | | -- `url` (string) - a URL to share |
131 | | -- `message` (string) - a message to share |
132 | | -- `subject` (string) - a subject for the message |
133 | | -- `excludedActivityTypes` (array) - the activities to exclude from the ActionSheet |
134 | | - |
135 | | -:::note |
136 | | -If `url` points to a local file, or is a base64-encoded uri, the file it points to will be loaded and shared directly. In this way, you can share images, videos, PDF files, etc. If `url` points to a remote file or address it must conform to URL format as described in [RFC 2396](https://www.ietf.org/rfc/rfc2396.txt). For example, a web URL without a proper protocol (HTTP/HTTPS) will not be shared. |
137 | | -::: |
138 | | - |
139 | | -The 'failureCallback' function takes one parameter, an error object. The only property defined on this object is an optional `stack` property of type `string`. |
140 | | - |
141 | | -The 'successCallback' function takes two parameters: |
142 | | - |
143 | | -- a boolean value signifying success or failure |
144 | | -- a string that, in the case of success, indicates the method of sharing |
| 1 | +--- |
| 2 | +id: actionsheetios |
| 3 | +title: ActionSheetIOS |
| 4 | +--- |
| 5 | + |
| 6 | +Displays native to iOS [Action Sheet](https://developer.apple.com/design/human-interface-guidelines/action-sheets) component. |
| 7 | + |
| 8 | +## Example |
| 9 | + |
| 10 | +```SnackPlayer name=ActionSheetIOS%20Example&supportedPlatforms=ios |
| 11 | +import {useState} from 'react'; |
| 12 | +import {ActionSheetIOS, Button, StyleSheet, Text} from 'react-native'; |
| 13 | +import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context'; |
| 14 | +
|
| 15 | +const App = () => { |
| 16 | + const [result, setResult] = useState('🔮'); |
| 17 | +
|
| 18 | + const onPress = () => |
| 19 | + ActionSheetIOS.showActionSheetWithOptions( |
| 20 | + { |
| 21 | + options: ['Cancel', 'Generate number', 'Reset'], |
| 22 | + destructiveButtonIndex: 2, |
| 23 | + cancelButtonIndex: 0, |
| 24 | + userInterfaceStyle: 'dark', |
| 25 | + }, |
| 26 | + buttonIndex => { |
| 27 | + if (buttonIndex === 0) { |
| 28 | + // cancel action |
| 29 | + } else if (buttonIndex === 1) { |
| 30 | + setResult(String(Math.floor(Math.random() * 100) + 1)); |
| 31 | + } else if (buttonIndex === 2) { |
| 32 | + setResult('🔮'); |
| 33 | + } |
| 34 | + }, |
| 35 | + ); |
| 36 | +
|
| 37 | + return ( |
| 38 | + <SafeAreaProvider> |
| 39 | + <SafeAreaView style={styles.container}> |
| 40 | + <Text style={styles.result}>{result}</Text> |
| 41 | + <Button onPress={onPress} title="Show Action Sheet" /> |
| 42 | + </SafeAreaView> |
| 43 | + </SafeAreaProvider> |
| 44 | + ); |
| 45 | +}; |
| 46 | +
|
| 47 | +const styles = StyleSheet.create({ |
| 48 | + container: { |
| 49 | + flex: 1, |
| 50 | + justifyContent: 'center', |
| 51 | + }, |
| 52 | + result: { |
| 53 | + fontSize: 64, |
| 54 | + textAlign: 'center', |
| 55 | + }, |
| 56 | +}); |
| 57 | +
|
| 58 | +export default App; |
| 59 | +``` |
| 60 | + |
| 61 | +# Reference |
| 62 | + |
| 63 | +## Methods |
| 64 | + |
| 65 | +### `showActionSheetWithOptions()` |
| 66 | + |
| 67 | +```tsx |
| 68 | +static showActionSheetWithOptions: ( |
| 69 | + options: ActionSheetIOSOptions, |
| 70 | + callback: (buttonIndex: number) => void, |
| 71 | +); |
| 72 | +``` |
| 73 | + |
| 74 | +Display an iOS action sheet. The `options` object must contain one or more of: |
| 75 | + |
| 76 | +- `options` (array of strings) - a list of button titles (required) |
| 77 | +- `cancelButtonIndex` (int) - index of cancel button in `options` |
| 78 | +- `cancelButtonTintColor` (string) - the [color](colors) used for the change the text color of the cancel button |
| 79 | +- `destructiveButtonIndex` (int or array of ints) - indices of destructive buttons in `options` |
| 80 | +- `title` (string) - a title to show above the action sheet |
| 81 | +- `message` (string) - a message to show below the title |
| 82 | +- `anchor` (number) - the node to which the action sheet should be anchored (used for iPad) |
| 83 | +- `tintColor` (string) - the [color](colors) used for non-destructive button titles |
| 84 | +- `disabledButtonIndices` (array of numbers) - a list of button indices which should be disabled |
| 85 | +- `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 |
| 86 | + |
| 87 | +The 'callback' function takes one parameter, the zero-based index of the selected item. |
| 88 | + |
| 89 | +Minimal example: |
| 90 | + |
| 91 | +```tsx |
| 92 | +ActionSheetIOS.showActionSheetWithOptions( |
| 93 | + { |
| 94 | + options: ['Cancel', 'Remove'], |
| 95 | + destructiveButtonIndex: 1, |
| 96 | + cancelButtonIndex: 0, |
| 97 | + }, |
| 98 | + buttonIndex => { |
| 99 | + if (buttonIndex === 1) { |
| 100 | + /* destructive action */ |
| 101 | + } |
| 102 | + }, |
| 103 | +); |
| 104 | +``` |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +### `dismissActionSheet()` |
| 109 | + |
| 110 | +```tsx |
| 111 | +static dismissActionSheet(); |
| 112 | +``` |
| 113 | + |
| 114 | +Dismisses the most upper iOS action sheet presented, if no action sheet is present a warning is displayed. |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +### `showShareActionSheetWithOptions()` |
| 119 | + |
| 120 | +```tsx |
| 121 | +static showShareActionSheetWithOptions: ( |
| 122 | + options: ShareActionSheetIOSOptions, |
| 123 | + failureCallback: (error: Error) => void, |
| 124 | + successCallback: (success: boolean, method: string) => void, |
| 125 | +); |
| 126 | +``` |
| 127 | + |
| 128 | +Display the iOS share sheet. The `options` object should contain one or both of `message` and `url` and can additionally have a `subject` or `excludedActivityTypes`: |
| 129 | + |
| 130 | +- `url` (string) - a URL to share |
| 131 | +- `message` (string) - a message to share |
| 132 | +- `subject` (string) - a subject for the message |
| 133 | +- `excludedActivityTypes` (array) - the activities to exclude from the ActionSheet |
| 134 | + |
| 135 | +:::note |
| 136 | +If `url` points to a local file, or is a base64-encoded uri, the file it points to will be loaded and shared directly. In this way, you can share images, videos, PDF files, etc. If `url` points to a remote file or address it must conform to URL format as described in [RFC 2396](https://www.ietf.org/rfc/rfc2396.txt). For example, a web URL without a proper protocol (HTTP/HTTPS) will not be shared. |
| 137 | +::: |
| 138 | + |
| 139 | +The 'failureCallback' function takes one parameter, an error object. The only property defined on this object is an optional `stack` property of type `string`. |
| 140 | + |
| 141 | +The 'successCallback' function takes two parameters: |
| 142 | + |
| 143 | +- a boolean value signifying success or failure |
| 144 | +- a string that, in the case of success, indicates the method of sharing |
0 commit comments