What is @expo/react-native-action-sheet?
@expo/react-native-action-sheet is a library that provides a customizable ActionSheet component for React Native applications. It allows developers to present a list of options to the user in a modal dialog, which can be used for various purposes such as selecting an item, confirming an action, or displaying additional options.
What are @expo/react-native-action-sheet's main functionalities?
Displaying an ActionSheet
This feature allows you to display an ActionSheet with a list of options. The user can select an option, and the selected option's index is returned in the callback function.
import React from 'react';
import { View, Button } from 'react-native';
import { useActionSheet } from '@expo/react-native-action-sheet';
const App = () => {
const { showActionSheetWithOptions } = useActionSheet();
const onOpenActionSheet = () => {
const options = ['Option 1', 'Option 2', 'Cancel'];
const cancelButtonIndex = 2;
showActionSheetWithOptions(
{
options,
cancelButtonIndex,
},
(buttonIndex) => {
// Handle button press
console.log('Selected option:', buttonIndex);
}
);
};
return (
<View>
<Button title="Open ActionSheet" onPress={onOpenActionSheet} />
</View>
);
};
export default App;
Customizing ActionSheet Options
This feature allows you to customize the ActionSheet options, including setting a destructive button (e.g., for delete actions) and a cancel button.
import React from 'react';
import { View, Button } from 'react-native';
import { useActionSheet } from '@expo/react-native-action-sheet';
const App = () => {
const { showActionSheetWithOptions } = useActionSheet();
const onOpenActionSheet = () => {
const options = ['Delete', 'Save', 'Cancel'];
const destructiveButtonIndex = 0;
const cancelButtonIndex = 2;
showActionSheetWithOptions(
{
options,
cancelButtonIndex,
destructiveButtonIndex,
},
(buttonIndex) => {
// Handle button press
console.log('Selected option:', buttonIndex);
}
);
};
return (
<View>
<Button title="Open ActionSheet" onPress={onOpenActionSheet} />
</View>
);
};
export default App;
Other packages similar to @expo/react-native-action-sheet
react-native-action-sheet
react-native-action-sheet is a popular library for displaying action sheets in React Native applications. It provides similar functionality to @expo/react-native-action-sheet, allowing developers to present a list of options to the user. However, it may require additional setup and configuration compared to the Expo version.
react-native-modal
react-native-modal is a versatile library for creating modals in React Native applications. While it is not specifically designed for action sheets, it can be used to create custom modal dialogs with a list of options. It offers more flexibility and customization options compared to @expo/react-native-action-sheet but may require more effort to implement.
react-native-action-sheet
ActionSheet 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 except it cannot be called statically.
Installation
npm install @expo/react-native-action-sheet -S
or
yarn add @expo/react-native-action-sheet
A basic ActionSheet Setup
1. import connectActionSheet function and connect your component which uses showActionSheetWithOptions.
import { connectActionSheet } from '@expo/react-native-action-sheet';
class App extends React.Component {
/* ... */
}
const ConnectedApp = connectActionSheet(App)
export default ConnectedApp
App
component can access actionSheet method as this.props.showActionSheetWithOptions
_onOpenActionSheet = () => {
// Same interface as https://facebook.github.io/react-native/docs/actionsheetios.html
const options = ['Delete', 'Save', 'Cancel'];
const destructiveButtonIndex = 0;
const cancelButtonIndex = 2;
this.props.showActionSheetWithOptions(
{
options,
cancelButtonIndex,
destructiveButtonIndex,
},
buttonIndex => {
// Do something here depending on the button index selected
},
);
};
2. import ActionSheetProvider and wrap your top-level component with <ActionSheetProvider />
import { ActionSheetProvider } from '@expo/react-native-action-sheet';
class AppContainer extends React.Component {
render() {
return (
<ActionSheetProvider>
<ConnectedApp />
</ActionSheetProvider>
);
}
}
Web Setup
WIP
Options
The goal of this library is to mimic the native iOS and Android ActionSheets as closely as possible.
This library can also be used with on web with Expo.
Universal Props
The same options available on React Native's ActionSheetIOS component exist for both iOS and Android in this library.
Android/Web-Only Props
The 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.
Name | Type | Required | Default |
---|
icons | array of required images or icons | No | |
tintIcons | boolean | No | true |
textStyle | TextStyle | No | |
titleTextStyle | TextStyle | No | |
messageTextStyle | TextStyle | No | |
showSeparators | boolean | No | false |
separatorStyle | ViewStyle | No | |
icons
(optional)
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.
tintIcons
(optional)
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.
textStyle
(optional)
Apply any text style props to the options. If the tintColor
option is provided, it takes precedence over a color text style prop.
titleTextStyle
(optional)
Apply any text style props to the title if present.
messageTextStyle
(optional)
Apply any text style props to the message if present.
showSeparators
: (optional)
Show separators between items. On iOS, separators always show so this prop has no effect.
separatorStyle
: (optional)
Modify the look of the separators rather than use the default look.
Try it out
Try it in Expo: https://expo.io/@community/react-native-action-sheet-example
Example
See the example app for examples of how to apply different options.
Usage
$ cd exmaple
$ yarn
$ yarn ios