mirror of
https://github.com/expo/react-native-action-sheet.git
synced 2026-11-14 19:11:42 -10:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1cda479ae1 |
No files matched your search
@@ -1,30 +0,0 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
pull_request:
|
||||
branches: [master]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: 🗂️ Checkout
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: ⚙️ Setup Node
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: '14'
|
||||
cache: 'yarn'
|
||||
|
||||
- name: 🧱 Build
|
||||
run: yarn
|
||||
|
||||
- name: 🚧 Type Check
|
||||
run: npm run type-check
|
||||
|
||||
- name: 🛟 Lint Check
|
||||
run: npm run lint-check
|
||||
@@ -1,39 +0,0 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
|
||||
jobs:
|
||||
release-please:
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
release-created: ${{ steps.release.outputs.release_created }}
|
||||
steps:
|
||||
- uses: google-github-actions/release-please-action@v3
|
||||
id: release
|
||||
with:
|
||||
release-type: node
|
||||
package-name: '@expo/react-native-action-sheet'
|
||||
|
||||
deploy:
|
||||
needs: [release-please]
|
||||
if: needs.release-please.outputs.release-created == 'true'
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: 🗂️ Checkout
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: ⚙️ Setup Node
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: '14'
|
||||
cache: 'yarn'
|
||||
|
||||
- name: 🧱 Build
|
||||
run: yarn
|
||||
|
||||
- name: 🚀 Deploy
|
||||
uses: JS-DevTools/npm-publish@v2
|
||||
with:
|
||||
token: ${{ secrets.NPM_TOKEN }}
|
||||
@@ -1,63 +1,5 @@
|
||||
# Changelog
|
||||
|
||||
## [4.0.1](https://github.com/expo/react-native-action-sheet/compare/v4.0.0...v4.0.1) (2022-11-04)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* expose ref on ActionSheetProvider for statically invoking ([#283](https://github.com/expo/react-native-action-sheet/issues/283)) ([b2f5f43](https://github.com/expo/react-native-action-sheet/commit/b2f5f433ff2070c77fdd62b273d2d80e636efd3a))
|
||||
|
||||
# [4.0.0](https://github.com/expo/react-native-action-sheet/compare/v3.14.0...v4.0.0) (2022-10-26)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* iOS use custom action sheet ([#281](https://github.com/expo/react-native-action-sheet/issues/281)) ([372dc80](https://github.com/expo/react-native-action-sheet/commit/372dc8065006a4da510ef5f31c27225d14a71ff3))
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* This implementation is backwards compatible, but just to be safe I'm marking this as breaking so that it bumps the major version.
|
||||
|
||||
# [3.14.0](https://github.com/expo/react-native-action-sheet/compare/v3.13.1...v3.14.0) (2022-10-03)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* support cancelButtonTintColor ([#278](https://github.com/expo/react-native-action-sheet/issues/278)) ([2dcd54c](https://github.com/expo/react-native-action-sheet/commit/2dcd54c56f35a64c66f8d9a7d09a9475801cf037))
|
||||
|
||||
## [3.13.1](https://github.com/expo/react-native-action-sheet/compare/v3.13.0...v3.13.1) (2022-10-03)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* add return type to connectActionSheet ([#273](https://github.com/expo/react-native-action-sheet/issues/273)) ([f46a5cf](https://github.com/expo/react-native-action-sheet/commit/f46a5cfc473807478c558a733c8adb1440941017))
|
||||
|
||||
# [3.13.0](https://github.com/expo/react-native-action-sheet/compare/v3.12.0...v3.13.0) (2022-01-10)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* defer calling onSelect until after animation ([#248](https://github.com/expo/react-native-action-sheet/issues/248)) ([1fb3411](https://github.com/expo/react-native-action-sheet/commit/1fb3411f575b4fa1cb762a804fc38cf3e2a5c73b)), closes [#203](https://github.com/expo/react-native-action-sheet/issues/203)
|
||||
* stabilize the identity of the context provider value ([#251](https://github.com/expo/react-native-action-sheet/issues/251)) ([55005ba](https://github.com/expo/react-native-action-sheet/commit/55005ba0fb4bc6aff546390cf324619bf2e0ed2a))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* support closing actionsheet with Esc key on web ([#247](https://github.com/expo/react-native-action-sheet/issues/247)) ([c4c3f8b](https://github.com/expo/react-native-action-sheet/commit/c4c3f8b873d76ff40daf8d2061d9f43346a3577f)), closes [#194](https://github.com/expo/react-native-action-sheet/issues/194)
|
||||
|
||||
# [3.12.0](https://github.com/expo/react-native-action-sheet/compare/v3.11.0...v3.12.0) (2021-10-14)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* use correct callback type allowing for undefined `i` ([#239](https://github.com/expo/react-native-action-sheet/issues/239)) ([b54b9f1](https://github.com/expo/react-native-action-sheet/commit/b54b9f16c251cb2ca5f1bd41ca7cc41b1393c6b3))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* adds support for disabledButtonIndices ([#246](https://github.com/expo/react-native-action-sheet/issues/246)) ([feed1f7](https://github.com/expo/react-native-action-sheet/commit/feed1f78b4c85855d48c1742fe9f8a67c0604609)), closes [#224](https://github.com/expo/react-native-action-sheet/issues/224)
|
||||
|
||||
# [3.11.0](https://github.com/expo/react-native-action-sheet/compare/v3.10.0...v3.11.0) (2021-09-07)
|
||||
|
||||
|
||||
|
||||
@@ -1,100 +1,90 @@
|
||||
# @expo/react-native-action-sheet
|
||||
|
||||
# react-native-action-sheet
|
||||
[](https://www.npmjs.com/package/@expo/react-native-action-sheet)
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
[](https://discord.gg/4gtbPAdpaE)
|
||||
|
||||
React Native Action Sheet is a cross-platform React Native component that uses the native UIActionSheet on iOS and a pure JS implementation on Android.
|
||||
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](https://facebook.github.io/react-native/docs/actionsheetios.html) except it cannot be called statically.
|
||||
|
||||
| iOS | Android | Web |
|
||||
| -------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------- |
|
||||
|
||||
| iOS | Android | Web |
|
||||
|---------------------------|---------------------------|---------------------------|
|
||||
| <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"/> |
|
||||
|
||||
## [Check out the example snack here!](https://snack.expo.dev/@expo-action-sheet/example)
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
npm install @expo/react-native-action-sheet
|
||||
$ npm install @expo/react-native-action-sheet -S
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```
|
||||
yarn add @expo/react-native-action-sheet
|
||||
$ yarn add @expo/react-native-action-sheet
|
||||
```
|
||||
|
||||
## A basic ActionSheet Setup
|
||||
|
||||
### 1. Wrap your top-level component with `<ActionSheetProvider />`
|
||||
|
||||
ReactNativeActionSheet uses React context to allow your components to invoke the menu. This means your app needs to be wrapped with the `ActionSheetProvider` component first.
|
||||
```es6
|
||||
|
||||
```jsx
|
||||
import { ActionSheetProvider } from '@expo/react-native-action-sheet';
|
||||
import { ActionSheetProvider } from '@expo/react-native-action-sheet'
|
||||
|
||||
export default function AppContainer() {
|
||||
return (
|
||||
<ActionSheetProvider>
|
||||
<App />
|
||||
</ActionSheetProvider>
|
||||
);
|
||||
class AppContainer extends React.Component {
|
||||
render() {
|
||||
return (
|
||||
<ActionSheetProvider>
|
||||
<ConnectedApp />
|
||||
</ActionSheetProvider>
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Call the `showActionSheetWithOptions` method with a hook or a higher order component.
|
||||
### 2. Connect your component which uses showActionSheetWithOptions.
|
||||
```es6
|
||||
import { connectActionSheet } from '@expo/react-native-action-sheet'
|
||||
|
||||
```tsx
|
||||
// Using the provided hook
|
||||
import { useActionSheet } from '@expo/react-native-action-sheet';
|
||||
|
||||
export default Menu() {
|
||||
const { showActionSheetWithOptions } = useActionSheet();
|
||||
|
||||
const onPress = () => {
|
||||
const options = ['Delete', 'Save', 'Cancel'];
|
||||
const destructiveButtonIndex = 0;
|
||||
const cancelButtonIndex = 2;
|
||||
|
||||
showActionSheetWithOptions({
|
||||
options,
|
||||
cancelButtonIndex,
|
||||
destructiveButtonIndex
|
||||
}, (selectedIndex: number) => {
|
||||
switch (selectedIndex) {
|
||||
case 1:
|
||||
// Save
|
||||
break;
|
||||
|
||||
case destructiveButtonIndex:
|
||||
// Delete
|
||||
break;
|
||||
|
||||
case cancelButtonIndex:
|
||||
// Canceled
|
||||
}});
|
||||
}
|
||||
|
||||
return (
|
||||
<Button title="Menu" onPress={onPress}/>
|
||||
)
|
||||
};
|
||||
```
|
||||
|
||||
Alternatively, any component can use the higher order component to access the context and pass the `showActionSheetWithOptions` as a prop.
|
||||
|
||||
```tsx
|
||||
// Using a Higher Order Component to wrap your component
|
||||
import { connectActionSheet } from '@expo/react-native-action-sheet';
|
||||
|
||||
function Menu({ showActionSheetWithOptions }) {
|
||||
class App extends React.Component {
|
||||
/* ... */
|
||||
}
|
||||
|
||||
export default connectActionSheet(Menu);
|
||||
const ConnectedApp = connectActionSheet(App)
|
||||
|
||||
export default ConnectedApp
|
||||
```
|
||||
|
||||
`Menu` component can now access the actionSheet prop as `showActionSheetWithOptions`.
|
||||
`App` component can access the actionSheet method as `this.props.showActionSheetWithOptions`
|
||||
|
||||
```es6
|
||||
_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
|
||||
},
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
You can use a hook instead of the higher order component if you are on React 16.8 or newer.
|
||||
|
||||
```es6
|
||||
import { useActionSheet } from '@expo/react-native-action-sheet'
|
||||
|
||||
export default function App () {
|
||||
const { showActionSheetWithOptions } = useActionSheet();
|
||||
/* ... */
|
||||
}
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
@@ -104,82 +94,94 @@ This library can also be used in the browser with Expo for web.
|
||||
|
||||
### Universal Props
|
||||
|
||||
| Name | Type | Description |
|
||||
| ------------------------ | -------------------------- | ------------------------------------------------------------- |
|
||||
| `options` | array of strings | A list of button titles **(required)** |
|
||||
| `cancelButtonIndex` | number | Index of cancel button in options |
|
||||
| `cancelButtonTintColor` | string | Color used for the change the text color of the cancel button |
|
||||
| `destructiveButtonIndex` | number or array of numbers | Indices of destructive buttons in options |
|
||||
| `title` | string | Title to show above the action sheet |
|
||||
| `message` | string | Message to show below the title |
|
||||
| `tintColor` | string | Color used for non-destructive button titles |
|
||||
| `disabledButtonIndices` | array of numbers | Indices of disabled buttons in options |
|
||||
The 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.
|
||||
|
||||
### iOS Only Props
|
||||
|
||||
| Name | Type | Description |
|
||||
| -------------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `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. |
|
||||
| `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. |
|
||||
| Name | Type | Required | Default |
|
||||
| -------------------| -------| -------- | ------- |
|
||||
| anchor | number | No | |
|
||||
| userInterfaceStyle | string | No | |
|
||||
|
||||
### Custom Action Sheet Only (Android/Web) Props
|
||||
#### `anchor` (optional)
|
||||
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.
|
||||
|
||||
#### `userInterfaceStyle` (optional)
|
||||
The interface style used for the action sheet, can be set to `light` or `dark`, otherwise the default system style will be used.
|
||||
|
||||
### 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 | Description |
|
||||
| ------------------ | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `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. |
|
||||
| `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. |
|
||||
| `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. |
|
||||
| `titleTextStyle` | TextStyle | Apply any text style props to the title if present. |
|
||||
| `messageTextStyle` | TextStyle | Apply any text style props to the message if present. |
|
||||
| `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. |
|
||||
| `showSeparators` | boolean | Show separators between items. On iOS, separators always show so this prop has no effect. |
|
||||
| `containerStyle` | ViewStyle | Apply any view style props to the container rather than use the default look (e.g. dark mode). |
|
||||
| `separatorStyle` | ViewStyle | Modify the look of the separators rather than use the default look. |
|
||||
| `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)). |
|
||||
| `destructiveColor` | string | Modify color for text of destructive option. Defaults to `#d32f2f`. |
|
||||
**Regarding Cancelling**: Unlike iOS, Android and Web allow you to render an action sheet _without_ a dedicated cancel button. The action sheet can then be closed by tapping in the background, or using the hardware back button. When the screen is closed in this way, your `buttonIndex` param will be `undefined`. Note that this will not work in iOS and the screen will be "stuck", forcing the user to select one of the 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 | |
|
||||
| autoFocus | boolean | No | false |
|
||||
| showSeparators | boolean | No | false |
|
||||
| containerStyle | ViewStyle | No | |
|
||||
| separatorStyle | ViewStyle | No | |
|
||||
| useModal | boolean | No | false (true if autoFocus is true) |
|
||||
| destructiveColor | string | No | #d32f2f |
|
||||
|
||||
#### `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.
|
||||
|
||||
#### `autoFocus`: (optional)
|
||||
If true, 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.
|
||||
|
||||
#### `showSeparators`: (optional)
|
||||
Show separators between items. On iOS, separators always show so this prop has no effect.
|
||||
|
||||
#### `containerStyle`: (optional)
|
||||
Apply any view style props to the container rather than use the default look (e.g. dark mode).
|
||||
|
||||
#### `separatorStyle`: (optional)
|
||||
Modify the look of the separators rather than use the default look.
|
||||
|
||||
#### `useModal`: (optional)
|
||||
Wrap 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)).
|
||||
|
||||
#### `destructiveColor`: (optional)
|
||||
Modify color for text of destructive option.
|
||||
|
||||
## ActionSheetProvider Props
|
||||
|
||||
The following props can be set directly on the `ActionSheetProvider`
|
||||
|
||||
| Name | Type | Description |
|
||||
| ---------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `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`. |
|
||||
| `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. |
|
||||
#### `useNativeDriver` (optional)
|
||||
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.
|
||||
|
||||
```jsx
|
||||
// example of using useCustomActionSheet on iOS
|
||||
export default function AppContainer() {
|
||||
return (
|
||||
<ActionSheetProvider useCustomActionSheet={true}>
|
||||
<App />
|
||||
</ActionSheetProvider>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## Callback
|
||||
|
||||
The 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.
|
||||
|
||||
```tsx
|
||||
function onButtonPress(selectedIndex: number) {
|
||||
// handle it!
|
||||
}
|
||||
```
|
||||
|
||||
## Try it out
|
||||
|
||||
Try it in Expo Snack: https://snack.expo.dev/@expo-action-sheet/example.
|
||||
Try it in Expo: https://expo.io/@community/react-native-action-sheet-example
|
||||
|
||||
## Example
|
||||
|
||||
See the [example app](https://github.com/expo/react-native-action-sheet/tree/master/example).
|
||||
|
||||
### Usage
|
||||
|
||||
```
|
||||
$ cd example
|
||||
$ yarn
|
||||
@@ -195,7 +197,6 @@ $ yarn web
|
||||
## Development
|
||||
|
||||
### Setup
|
||||
|
||||
```
|
||||
$ git clone [email protected]:expo/react-native-action-sheet.git
|
||||
$ cd react-native-action-sheet
|
||||
@@ -203,15 +204,12 @@ $ yarn
|
||||
```
|
||||
|
||||
### Build
|
||||
|
||||
We use [bob](https://github.com/react-native-community/bob).
|
||||
|
||||
```
|
||||
$ yarn build
|
||||
```
|
||||
|
||||
### Lint & Format
|
||||
|
||||
```
|
||||
// tsc
|
||||
$ yarn type-check
|
||||
|
||||
+15
-80
@@ -3,9 +3,7 @@ import {
|
||||
connectActionSheet,
|
||||
ActionSheetProps,
|
||||
} from '@expo/react-native-action-sheet';
|
||||
import { Entypo } from '@expo/vector-icons';
|
||||
import * as React from 'react';
|
||||
import { useState } from 'react';
|
||||
import {
|
||||
Modal,
|
||||
StyleSheet,
|
||||
@@ -14,16 +12,11 @@ import {
|
||||
ScrollView,
|
||||
SafeAreaView,
|
||||
TouchableOpacity,
|
||||
Share,
|
||||
Platform,
|
||||
} from 'react-native';
|
||||
|
||||
import ShowActionSheetButton from './ShowActionSheetButton';
|
||||
|
||||
type Props = ActionSheetProps & {
|
||||
useCustomActionSheet: boolean;
|
||||
setUseCustomActionSheet: (next: boolean) => void;
|
||||
};
|
||||
type Props = ActionSheetProps;
|
||||
|
||||
interface State {
|
||||
selectedIndex?: number | null;
|
||||
@@ -59,23 +52,6 @@ class App extends React.Component<Props, State> {
|
||||
this.setState((prevState) => ({ isModalOpen: !prevState.isModalOpen }));
|
||||
};
|
||||
|
||||
async _onShare() {
|
||||
try {
|
||||
const result = await Share.share({
|
||||
message: 'React Native | A framework for building native apps using React',
|
||||
});
|
||||
if (result.action === Share.sharedAction) {
|
||||
if (result.activityType) {
|
||||
// shared with activity type of result.activityType
|
||||
} else {
|
||||
// shared
|
||||
}
|
||||
} else if (result.action === Share.dismissedAction) {
|
||||
// dismissed
|
||||
}
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
_renderButtons() {
|
||||
const { showActionSheetWithOptions } = this.props;
|
||||
return (
|
||||
@@ -83,35 +59,6 @@ class App extends React.Component<Props, State> {
|
||||
style={{
|
||||
alignItems: 'center',
|
||||
}}>
|
||||
<View
|
||||
style={{
|
||||
alignItems: 'center',
|
||||
display: Platform.OS === 'ios' ? 'flex' : 'none',
|
||||
}}>
|
||||
{this._renderSectionHeader('Use Custom Action Sheet')}
|
||||
<Text style={{ marginBottom: 10 }}>
|
||||
On iOS the default action sheet will be the native UI. However, you can optionally
|
||||
enable the custom JS action sheet by setting the useCustomActionSheet prop on the
|
||||
provider.
|
||||
</Text>
|
||||
<Entypo.Button
|
||||
backgroundColor={this.props.useCustomActionSheet ? '#3e3e3e' : 'white'}
|
||||
name={this.props.useCustomActionSheet ? 'check' : 'circle'}
|
||||
color={this.props.useCustomActionSheet ? '#fff' : '#3e3e3e'}
|
||||
style={{
|
||||
borderColor: '#3e3e3e',
|
||||
borderWidth: 2,
|
||||
}}
|
||||
onPress={() => this.props.setUseCustomActionSheet(!this.props.useCustomActionSheet)}>
|
||||
<Text
|
||||
style={{
|
||||
fontSize: 15,
|
||||
color: this.props.useCustomActionSheet ? '#fff' : '#3e3e3e',
|
||||
}}>
|
||||
Use Custom Action Sheet
|
||||
</Text>
|
||||
</Entypo.Button>
|
||||
</View>
|
||||
{this._renderSectionHeader('Universal Options')}
|
||||
<ShowActionSheetButton
|
||||
title="Options Only"
|
||||
@@ -131,12 +78,6 @@ class App extends React.Component<Props, State> {
|
||||
onSelection={this._updateSelectionText}
|
||||
showActionSheetWithOptions={showActionSheetWithOptions}
|
||||
/>
|
||||
<ShowActionSheetButton
|
||||
title="Cancel Button Tint Color"
|
||||
withCancelButtonTintColor
|
||||
onSelection={this._updateSelectionText}
|
||||
showActionSheetWithOptions={showActionSheetWithOptions}
|
||||
/>
|
||||
<ShowActionSheetButton
|
||||
title="iPad Anchor"
|
||||
withAnchor
|
||||
@@ -147,7 +88,7 @@ class App extends React.Component<Props, State> {
|
||||
<ShowActionSheetButton
|
||||
title="Nested Action Sheets"
|
||||
onSelection={(index) => {
|
||||
if (!index || index < 3) {
|
||||
if (index && index < 3) {
|
||||
showActionSheetWithOptions(
|
||||
{
|
||||
title: 'Sub Action Sheet',
|
||||
@@ -161,17 +102,14 @@ class App extends React.Component<Props, State> {
|
||||
showActionSheetWithOptions={showActionSheetWithOptions}
|
||||
/>
|
||||
<ShowActionSheetButton
|
||||
title="Share Menu"
|
||||
title="No Cancel Button"
|
||||
onSelection={this._updateSelectionText}
|
||||
showActionSheetWithOptions={() =>
|
||||
showActionSheetWithOptions(
|
||||
{
|
||||
title: 'Share Menu',
|
||||
options: ['Share', 'Cancel'],
|
||||
cancelButtonIndex: 1,
|
||||
options: ['Option 1', 'Option 2'],
|
||||
},
|
||||
(i) => {
|
||||
i === 0 && this._onShare();
|
||||
}
|
||||
this._updateSelectionText
|
||||
)
|
||||
}
|
||||
/>
|
||||
@@ -252,19 +190,16 @@ class App extends React.Component<Props, State> {
|
||||
}
|
||||
}
|
||||
|
||||
const ConnectedApp = connectActionSheet<any>(App);
|
||||
const ConnectedApp = connectActionSheet<object>(App);
|
||||
|
||||
export default function WrappedApp() {
|
||||
const [useCustomActionSheet, setUseCustomActionSheet] = useState(false);
|
||||
|
||||
return (
|
||||
<ActionSheetProvider useCustomActionSheet={useCustomActionSheet}>
|
||||
<ConnectedApp
|
||||
useCustomActionSheet={useCustomActionSheet}
|
||||
setUseCustomActionSheet={setUseCustomActionSheet}
|
||||
/>
|
||||
</ActionSheetProvider>
|
||||
);
|
||||
export default class AppContainer extends React.Component {
|
||||
render() {
|
||||
return (
|
||||
<ActionSheetProvider>
|
||||
<ConnectedApp />
|
||||
</ActionSheetProvider>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { ActionSheetOptions } from '@expo/react-native-action-sheet';
|
||||
import { MaterialIcons, Entypo } from '@expo/vector-icons';
|
||||
import * as React from 'react';
|
||||
import { Text, View, TextStyle, ViewStyle, findNodeHandle } from 'react-native';
|
||||
import { Text, View, TextStyle, ViewStyle, findNodeHandle, Button } from 'react-native';
|
||||
|
||||
const icon = (name: React.ComponentProps<typeof MaterialIcons>['name']) => (
|
||||
<MaterialIcons key={name} name={name} size={24} />
|
||||
);
|
||||
const icon = (name: string) => <MaterialIcons key={name} name={name} size={24} />;
|
||||
|
||||
interface Props {
|
||||
title: string;
|
||||
@@ -19,7 +17,6 @@ interface Props {
|
||||
withIcons?: boolean;
|
||||
withSeparators?: boolean;
|
||||
withCustomStyles?: boolean;
|
||||
withCancelButtonTintColor?: boolean;
|
||||
withAnchor?: boolean;
|
||||
useModal?: boolean;
|
||||
}
|
||||
@@ -33,12 +30,11 @@ export default class ShowActionSheetButton extends React.PureComponent<Props> {
|
||||
withSeparators: false,
|
||||
withCustomStyles: false,
|
||||
withAnchor: false,
|
||||
withCancelButtonTintColor: false,
|
||||
onSelection: null,
|
||||
useModal: false,
|
||||
};
|
||||
|
||||
_anchorRef = React.createRef<any>();
|
||||
_anchorRef = React.createRef<Button>();
|
||||
|
||||
_showActionSheet = () => {
|
||||
const {
|
||||
@@ -48,14 +44,13 @@ export default class ShowActionSheetButton extends React.PureComponent<Props> {
|
||||
withIcons,
|
||||
withSeparators,
|
||||
withCustomStyles,
|
||||
withCancelButtonTintColor,
|
||||
onSelection,
|
||||
showActionSheetWithOptions,
|
||||
useModal,
|
||||
} = this.props;
|
||||
|
||||
// Same interface as https://facebook.github.io/react-native/docs/actionsheetios.html
|
||||
const options = ['Delete', 'Disabled', 'Save', 'Cancel'];
|
||||
const options = ['Delete', 'Save', 'Share', 'Cancel'];
|
||||
const icons = withIcons
|
||||
? [icon('delete'), icon('save'), icon('share'), icon('cancel')]
|
||||
: undefined;
|
||||
@@ -64,7 +59,6 @@ export default class ShowActionSheetButton extends React.PureComponent<Props> {
|
||||
? 'This library tries to mimic the native share sheets as close as possible.'
|
||||
: undefined;
|
||||
const destructiveButtonIndex = 0;
|
||||
const disabledButtonIndices = [1];
|
||||
const cancelButtonIndex = 3;
|
||||
const textStyle: TextStyle | undefined = withCustomStyles
|
||||
? {
|
||||
@@ -101,9 +95,7 @@ export default class ShowActionSheetButton extends React.PureComponent<Props> {
|
||||
{
|
||||
options,
|
||||
cancelButtonIndex,
|
||||
cancelButtonTintColor: withCancelButtonTintColor ? '#D93F0B' : undefined,
|
||||
destructiveButtonIndex,
|
||||
disabledButtonIndices,
|
||||
title,
|
||||
message,
|
||||
icons,
|
||||
|
||||
+10
-10
@@ -13,18 +13,18 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@expo/react-native-action-sheet": "../src",
|
||||
"@expo/vector-icons": "^13.0.0",
|
||||
"expo": "^48.0.0",
|
||||
"@expo/vector-icons": "^10.0.0",
|
||||
"expo": "^39.0.0",
|
||||
"hoist-non-react-statics": "^3.3.0",
|
||||
"react": "18.2.0",
|
||||
"react-dom": "18.2.0",
|
||||
"react-native": "0.71.3",
|
||||
"react-native-web": "~0.18.11"
|
||||
"react": "16.13.1",
|
||||
"react-dom": "16.13.1",
|
||||
"react-native": "https://github.com/expo/react-native/archive/sdk-39.0.3.tar.gz",
|
||||
"react-native-web": "~0.13.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "~18.2.0",
|
||||
"@types/react-native": "~0.71.3",
|
||||
"babel-preset-expo": "^9.3.0",
|
||||
"typescript": "^4.9.4"
|
||||
"@types/react": "~16.9.35",
|
||||
"@types/react-native": "~0.63.2",
|
||||
"babel-preset-expo": "^8.3.0",
|
||||
"typescript": "~3.9.2"
|
||||
}
|
||||
}
|
||||
+3
-10
@@ -2,21 +2,14 @@
|
||||
"compilerOptions": {
|
||||
"esModuleInterop": true,
|
||||
"jsx": "react-native",
|
||||
"lib": [
|
||||
"esnext"
|
||||
],
|
||||
"lib": ["esnext"],
|
||||
"moduleResolution": "node",
|
||||
"noEmit": true,
|
||||
"resolveJsonModule": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"target": "esnext",
|
||||
"typeRoots": [
|
||||
"./node_modules/@types"
|
||||
]
|
||||
"typeRoots": ["./node_modules/@types"],
|
||||
},
|
||||
"include": [
|
||||
"./**.tsx"
|
||||
],
|
||||
"extends": "expo/tsconfig.base"
|
||||
"include": ["./**.tsx"],
|
||||
}
|
||||
+2653
-4787
File diff suppressed because it is too large.
Load diff
+2
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@expo/react-native-action-sheet",
|
||||
"version": "4.0.1",
|
||||
"version": "3.11.0",
|
||||
"description": "A cross-platform ActionSheet for React Native",
|
||||
"react-native": "lib/module/index.js",
|
||||
"types": "lib/typescript/index.d.ts",
|
||||
@@ -14,7 +14,6 @@
|
||||
"build": "bob build",
|
||||
"type-check": "tsc --noEmit",
|
||||
"lint": "eslint . --fix",
|
||||
"lint-check": "eslint",
|
||||
"semantic-release": "semantic-release"
|
||||
},
|
||||
"repository": {
|
||||
@@ -45,6 +44,7 @@
|
||||
"prettier": "^2.3.2",
|
||||
"react": "^16.8.6",
|
||||
"react-native": "https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz",
|
||||
"semantic-release": "^17.4.4",
|
||||
"typescript": "^3.5.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
||||
@@ -55,10 +55,6 @@ const isIndexDestructive = (index: number, destructiveIndex?: number | number[])
|
||||
return index === destructiveIndex;
|
||||
};
|
||||
|
||||
const isIndexDisabled = (index: number, disabledButtonIndices: number[] = []) => {
|
||||
return disabledButtonIndices.includes(index);
|
||||
};
|
||||
|
||||
export default class ActionGroup extends React.Component<Props> {
|
||||
static defaultProps = {
|
||||
title: null,
|
||||
@@ -119,10 +115,7 @@ export default class ActionGroup extends React.Component<Props> {
|
||||
const {
|
||||
options,
|
||||
icons,
|
||||
cancelButtonIndex,
|
||||
cancelButtonTintColor,
|
||||
destructiveButtonIndex,
|
||||
disabledButtonIndices,
|
||||
destructiveColor = DESTRUCTIVE_COLOR,
|
||||
onSelect,
|
||||
startIndex,
|
||||
@@ -142,13 +135,7 @@ export default class ActionGroup extends React.Component<Props> {
|
||||
const defaultColor = tintColor
|
||||
? tintColor
|
||||
: (textStyle || {}).color || BLACK_87PC_TRANSPARENT;
|
||||
const disabled = isIndexDisabled(i, disabledButtonIndices);
|
||||
const isCancelButton = i === cancelButtonIndex;
|
||||
const color = isIndexDestructive(i, destructiveButtonIndex)
|
||||
? destructiveColor
|
||||
: isCancelButton
|
||||
? cancelButtonTintColor || defaultColor
|
||||
: defaultColor;
|
||||
const color = isIndexDestructive(i, destructiveButtonIndex) ? destructiveColor : defaultColor;
|
||||
const iconSource = icons != null ? icons[i] : null;
|
||||
|
||||
optionViews.push(
|
||||
@@ -157,9 +144,8 @@ export default class ActionGroup extends React.Component<Props> {
|
||||
key={i}
|
||||
pressInDelay={0}
|
||||
background={nativeFeedbackBackground}
|
||||
disabled={disabled}
|
||||
onPress={() => onSelect(i)}
|
||||
style={[styles.button, disabled && styles.disabledButton]}
|
||||
style={styles.button}
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel={options[i]}>
|
||||
{this._renderIconElement(iconSource, color)}
|
||||
@@ -184,9 +170,6 @@ const styles = StyleSheet.create({
|
||||
height: 56,
|
||||
paddingHorizontal: 16,
|
||||
},
|
||||
disabledButton: {
|
||||
opacity: 0.5,
|
||||
},
|
||||
groupContainer: {
|
||||
backgroundColor: '#ffffff',
|
||||
overflow: 'hidden',
|
||||
|
||||
@@ -1,333 +0,0 @@
|
||||
import * as React from 'react';
|
||||
import {
|
||||
Animated,
|
||||
BackHandler,
|
||||
Easing,
|
||||
Modal,
|
||||
Platform,
|
||||
StyleSheet,
|
||||
TouchableWithoutFeedback,
|
||||
View,
|
||||
ViewProps,
|
||||
} from 'react-native';
|
||||
|
||||
import { ActionSheetOptions } from '../types';
|
||||
import ActionGroup from './ActionGroup';
|
||||
|
||||
interface State {
|
||||
isVisible: boolean;
|
||||
isAnimating: boolean;
|
||||
options: ActionSheetOptions | null;
|
||||
onSelect: ((i: number) => void) | null;
|
||||
overlayOpacity: any;
|
||||
sheetOpacity: any;
|
||||
}
|
||||
|
||||
export interface Props {
|
||||
readonly children: React.ReactNode;
|
||||
readonly useNativeDriver?: boolean;
|
||||
readonly pointerEvents?: ViewProps['pointerEvents'];
|
||||
}
|
||||
|
||||
const OPACITY_ANIMATION_IN_TIME = 225;
|
||||
const OPACITY_ANIMATION_OUT_TIME = 195;
|
||||
const EASING_OUT = Easing.bezier(0.25, 0.46, 0.45, 0.94);
|
||||
const EASING_IN = Easing.out(EASING_OUT);
|
||||
const ESCAPE_KEY = 'Escape';
|
||||
|
||||
// Has same API as https://facebook.github.io/react-native/docs/actionsheetios.html
|
||||
export default class CustomActionSheet extends React.Component<Props, State> {
|
||||
_actionSheetHeight = 360;
|
||||
|
||||
state: State = {
|
||||
isVisible: false,
|
||||
isAnimating: false,
|
||||
options: null,
|
||||
onSelect: null,
|
||||
overlayOpacity: new Animated.Value(0),
|
||||
sheetOpacity: new Animated.Value(0),
|
||||
};
|
||||
|
||||
_deferAfterAnimation?: () => void = undefined;
|
||||
|
||||
componentDidMount() {
|
||||
if (Platform.OS === 'web') {
|
||||
document.addEventListener('keydown', this._handleWebKeyDown);
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
if (Platform.OS === 'web') {
|
||||
document.removeEventListener('keydown', this._handleWebKeyDown);
|
||||
}
|
||||
}
|
||||
|
||||
_handleWebKeyDown = (event: KeyboardEvent): void => {
|
||||
if (event.key === ESCAPE_KEY && this.state.isVisible) {
|
||||
event.preventDefault();
|
||||
this._selectCancelButton();
|
||||
}
|
||||
};
|
||||
|
||||
_setActionSheetHeight = ({ nativeEvent }: any) =>
|
||||
(this._actionSheetHeight = nativeEvent.layout.height);
|
||||
|
||||
render() {
|
||||
const { isVisible, overlayOpacity, options } = this.state;
|
||||
const useModal = options ? options.autoFocus || options.useModal === true : false;
|
||||
const overlay = isVisible ? (
|
||||
<Animated.View
|
||||
style={[
|
||||
styles.overlay,
|
||||
{
|
||||
opacity: overlayOpacity,
|
||||
},
|
||||
]}
|
||||
/>
|
||||
) : null;
|
||||
|
||||
// While the sheet is visible, hide the rest of the app's content from screen readers.
|
||||
const appContent = (
|
||||
<View
|
||||
style={styles.flexContainer}
|
||||
importantForAccessibility={isVisible ? 'no-hide-descendants' : 'auto'}>
|
||||
{React.Children.only(this.props.children)}
|
||||
</View>
|
||||
);
|
||||
|
||||
return (
|
||||
<View pointerEvents={this.props.pointerEvents} style={styles.flexContainer}>
|
||||
{appContent}
|
||||
{isVisible && !useModal && (
|
||||
<>
|
||||
{overlay}
|
||||
{this._renderSheet()}
|
||||
</>
|
||||
)}
|
||||
{isVisible && useModal && (
|
||||
<Modal animationType="none" transparent onRequestClose={this._selectCancelButton}>
|
||||
{overlay}
|
||||
{this._renderSheet()}
|
||||
</Modal>
|
||||
)}
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
_renderSheet() {
|
||||
const { options, isAnimating, sheetOpacity } = this.state;
|
||||
|
||||
if (!options) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const {
|
||||
options: optionsArray,
|
||||
icons,
|
||||
tintIcons,
|
||||
destructiveButtonIndex,
|
||||
disabledButtonIndices,
|
||||
destructiveColor,
|
||||
textStyle,
|
||||
tintColor,
|
||||
title,
|
||||
titleTextStyle,
|
||||
message,
|
||||
messageTextStyle,
|
||||
autoFocus,
|
||||
showSeparators,
|
||||
containerStyle,
|
||||
separatorStyle,
|
||||
cancelButtonIndex,
|
||||
cancelButtonTintColor,
|
||||
} = options;
|
||||
return (
|
||||
<TouchableWithoutFeedback importantForAccessibility="yes" onPress={this._selectCancelButton}>
|
||||
<Animated.View
|
||||
needsOffscreenAlphaCompositing={isAnimating}
|
||||
style={[
|
||||
styles.sheetContainer,
|
||||
{
|
||||
opacity: sheetOpacity,
|
||||
transform: [
|
||||
{
|
||||
translateY: sheetOpacity.interpolate({
|
||||
inputRange: [0, 1],
|
||||
outputRange: [this._actionSheetHeight, 0],
|
||||
}),
|
||||
},
|
||||
],
|
||||
},
|
||||
]}>
|
||||
<View style={styles.sheet} onLayout={this._setActionSheetHeight}>
|
||||
<ActionGroup
|
||||
options={optionsArray}
|
||||
icons={icons}
|
||||
tintIcons={tintIcons === undefined ? true : tintIcons}
|
||||
cancelButtonIndex={cancelButtonIndex}
|
||||
cancelButtonTintColor={cancelButtonTintColor}
|
||||
destructiveButtonIndex={destructiveButtonIndex}
|
||||
destructiveColor={destructiveColor}
|
||||
disabledButtonIndices={disabledButtonIndices}
|
||||
onSelect={this._onSelect}
|
||||
startIndex={0}
|
||||
length={optionsArray.length}
|
||||
textStyle={textStyle || {}}
|
||||
tintColor={tintColor}
|
||||
title={title || undefined}
|
||||
titleTextStyle={titleTextStyle}
|
||||
message={message || undefined}
|
||||
messageTextStyle={messageTextStyle}
|
||||
autoFocus={autoFocus}
|
||||
showSeparators={showSeparators}
|
||||
containerStyle={containerStyle}
|
||||
separatorStyle={separatorStyle}
|
||||
/>
|
||||
</View>
|
||||
</Animated.View>
|
||||
</TouchableWithoutFeedback>
|
||||
);
|
||||
}
|
||||
|
||||
showActionSheetWithOptions = (options: ActionSheetOptions, onSelect: (i: number) => void) => {
|
||||
const { isVisible, overlayOpacity, sheetOpacity } = this.state;
|
||||
const { useNativeDriver = true } = this.props;
|
||||
|
||||
if (isVisible) {
|
||||
this._deferAfterAnimation = this.showActionSheetWithOptions.bind(this, options, onSelect);
|
||||
return;
|
||||
}
|
||||
|
||||
this.setState({
|
||||
options,
|
||||
onSelect,
|
||||
isVisible: true,
|
||||
isAnimating: true,
|
||||
});
|
||||
overlayOpacity.setValue(0);
|
||||
sheetOpacity.setValue(0);
|
||||
Animated.parallel([
|
||||
Animated.timing(overlayOpacity, {
|
||||
toValue: 0.32,
|
||||
easing: EASING_OUT,
|
||||
duration: OPACITY_ANIMATION_IN_TIME,
|
||||
useNativeDriver,
|
||||
}),
|
||||
Animated.timing(sheetOpacity, {
|
||||
toValue: 1,
|
||||
easing: EASING_OUT,
|
||||
duration: OPACITY_ANIMATION_IN_TIME,
|
||||
useNativeDriver,
|
||||
}),
|
||||
]).start((result) => {
|
||||
if (result.finished) {
|
||||
this.setState({
|
||||
isAnimating: false,
|
||||
});
|
||||
this._deferAfterAnimation = undefined;
|
||||
}
|
||||
});
|
||||
// @ts-ignore: Argument of type '"actionSheetHardwareBackPress"' is not assignable to parameter of type '"hardwareBackPress"'
|
||||
BackHandler.addEventListener('actionSheetHardwareBackPress', this._selectCancelButton);
|
||||
};
|
||||
|
||||
_selectCancelButton = () => {
|
||||
const { options } = this.state;
|
||||
|
||||
if (!options) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeof options.cancelButtonIndex === 'undefined') {
|
||||
return false;
|
||||
} else if (typeof options.cancelButtonIndex === 'number') {
|
||||
return this._onSelect(options.cancelButtonIndex);
|
||||
} else {
|
||||
return this._animateOut();
|
||||
}
|
||||
};
|
||||
|
||||
_onSelect = (index: number): boolean => {
|
||||
const { isAnimating, onSelect } = this.state;
|
||||
|
||||
if (isAnimating) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (onSelect) {
|
||||
this._deferAfterAnimation = onSelect.bind(this, index);
|
||||
}
|
||||
|
||||
return this._animateOut();
|
||||
};
|
||||
|
||||
_animateOut = (): boolean => {
|
||||
const { isAnimating, overlayOpacity, sheetOpacity } = this.state;
|
||||
const { useNativeDriver = true } = this.props;
|
||||
|
||||
if (isAnimating) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// @ts-ignore: Argument of type '"actionSheetHardwareBackPress"' is not assignable to parameter of type '"hardwareBackPress"'
|
||||
BackHandler.removeEventListener('actionSheetHardwareBackPress', this._selectCancelButton);
|
||||
this.setState({
|
||||
isAnimating: true,
|
||||
});
|
||||
Animated.parallel([
|
||||
Animated.timing(overlayOpacity, {
|
||||
toValue: 0,
|
||||
easing: EASING_IN,
|
||||
duration: OPACITY_ANIMATION_OUT_TIME,
|
||||
useNativeDriver,
|
||||
}),
|
||||
Animated.timing(sheetOpacity, {
|
||||
toValue: 0,
|
||||
easing: EASING_IN,
|
||||
duration: OPACITY_ANIMATION_OUT_TIME,
|
||||
useNativeDriver,
|
||||
}),
|
||||
]).start((result) => {
|
||||
if (result.finished) {
|
||||
this.setState({
|
||||
isVisible: false,
|
||||
isAnimating: false,
|
||||
});
|
||||
|
||||
if (this._deferAfterAnimation) {
|
||||
this._deferAfterAnimation();
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
flexContainer: {
|
||||
flex: 1,
|
||||
},
|
||||
overlay: {
|
||||
position: 'absolute',
|
||||
top: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
backgroundColor: 'black',
|
||||
},
|
||||
sheetContainer: {
|
||||
position: 'absolute',
|
||||
left: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
top: 0,
|
||||
backgroundColor: 'transparent',
|
||||
alignItems: 'flex-end',
|
||||
justifyContent: 'center',
|
||||
flexDirection: 'row',
|
||||
},
|
||||
sheet: {
|
||||
flex: 1,
|
||||
backgroundColor: 'transparent',
|
||||
},
|
||||
});
|
||||
@@ -21,21 +21,12 @@ export default class ActionSheet extends React.Component<Props> {
|
||||
|
||||
showActionSheetWithOptions(dataOptions: ActionSheetIOSOptions, onSelect: onSelect) {
|
||||
// ...dataOptions include other keys which use in android and web, thats why `Android-Only options` Crash on IOS
|
||||
const {
|
||||
cancelButtonIndex,
|
||||
destructiveButtonIndex,
|
||||
options,
|
||||
tintColor,
|
||||
cancelButtonTintColor,
|
||||
disabledButtonIndices,
|
||||
} = dataOptions;
|
||||
const { cancelButtonIndex, destructiveButtonIndex, options, tintColor } = dataOptions;
|
||||
const iosOptions: ActionSheetIOSOptions = {
|
||||
cancelButtonIndex,
|
||||
destructiveButtonIndex,
|
||||
options,
|
||||
tintColor,
|
||||
cancelButtonTintColor,
|
||||
disabledButtonIndices,
|
||||
// A null title or message on iOS causes a crash
|
||||
title: dataOptions.title || undefined,
|
||||
message: dataOptions.message || undefined,
|
||||
|
||||
+295
-2
@@ -1,2 +1,295 @@
|
||||
import CustomActionSheet from './CustomActionSheet';
|
||||
export default CustomActionSheet;
|
||||
import * as React from 'react';
|
||||
import {
|
||||
Animated,
|
||||
BackHandler,
|
||||
Easing,
|
||||
Modal,
|
||||
StyleSheet,
|
||||
TouchableWithoutFeedback,
|
||||
View,
|
||||
ViewProps,
|
||||
} from 'react-native';
|
||||
|
||||
import { ActionSheetOptions } from '../types';
|
||||
import ActionGroup from './ActionGroup';
|
||||
|
||||
interface State {
|
||||
isVisible: boolean;
|
||||
isAnimating: boolean;
|
||||
options: ActionSheetOptions | null;
|
||||
onSelect: ((i?: number) => void) | null;
|
||||
overlayOpacity: any;
|
||||
sheetOpacity: any;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
readonly useNativeDriver: boolean | undefined;
|
||||
readonly pointerEvents?: ViewProps['pointerEvents'];
|
||||
}
|
||||
|
||||
const OPACITY_ANIMATION_IN_TIME = 225;
|
||||
const OPACITY_ANIMATION_OUT_TIME = 195;
|
||||
const EASING_OUT = Easing.bezier(0.25, 0.46, 0.45, 0.94);
|
||||
const EASING_IN = Easing.out(EASING_OUT);
|
||||
|
||||
// Has same API as https://facebook.github.io/react-native/docs/actionsheetios.html
|
||||
export default class ActionSheet extends React.Component<Props, State> {
|
||||
static defaultProps = {
|
||||
useNativeDriver: true,
|
||||
};
|
||||
|
||||
_actionSheetHeight = 360;
|
||||
|
||||
state: State = {
|
||||
isVisible: false,
|
||||
isAnimating: false,
|
||||
options: null,
|
||||
onSelect: null,
|
||||
overlayOpacity: new Animated.Value(0),
|
||||
sheetOpacity: new Animated.Value(0),
|
||||
};
|
||||
|
||||
_deferNextShow?: () => void = undefined;
|
||||
|
||||
_setActionSheetHeight = ({ nativeEvent }: any) =>
|
||||
(this._actionSheetHeight = nativeEvent.layout.height);
|
||||
|
||||
render() {
|
||||
const { isVisible, overlayOpacity, options } = this.state;
|
||||
const useModal = options ? options.autoFocus || options.useModal === true : false;
|
||||
const overlay = isVisible ? (
|
||||
<Animated.View
|
||||
style={[
|
||||
styles.overlay,
|
||||
{
|
||||
opacity: overlayOpacity,
|
||||
},
|
||||
]}
|
||||
/>
|
||||
) : null;
|
||||
|
||||
// While the sheet is visible, hide the rest of the app's content from screen readers.
|
||||
const appContent = (
|
||||
<View
|
||||
style={styles.flexContainer}
|
||||
importantForAccessibility={isVisible ? 'no-hide-descendants' : 'auto'}>
|
||||
{React.Children.only(this.props.children)}
|
||||
</View>
|
||||
);
|
||||
|
||||
return (
|
||||
<View pointerEvents={this.props.pointerEvents} style={styles.flexContainer}>
|
||||
{appContent}
|
||||
{isVisible && !useModal && (
|
||||
<>
|
||||
{overlay}
|
||||
{this._renderSheet()}
|
||||
</>
|
||||
)}
|
||||
{isVisible && useModal && (
|
||||
<Modal animationType="none" transparent onRequestClose={this._selectCancelButton}>
|
||||
{overlay}
|
||||
{this._renderSheet()}
|
||||
</Modal>
|
||||
)}
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
_renderSheet() {
|
||||
const { options, isAnimating, sheetOpacity } = this.state;
|
||||
|
||||
if (!options) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const {
|
||||
options: optionsArray,
|
||||
icons,
|
||||
tintIcons,
|
||||
destructiveButtonIndex,
|
||||
destructiveColor,
|
||||
textStyle,
|
||||
tintColor,
|
||||
title,
|
||||
titleTextStyle,
|
||||
message,
|
||||
messageTextStyle,
|
||||
autoFocus,
|
||||
showSeparators,
|
||||
containerStyle,
|
||||
separatorStyle,
|
||||
} = options;
|
||||
return (
|
||||
<TouchableWithoutFeedback importantForAccessibility="yes" onPress={this._selectCancelButton}>
|
||||
<Animated.View
|
||||
needsOffscreenAlphaCompositing={isAnimating}
|
||||
style={[
|
||||
styles.sheetContainer,
|
||||
{
|
||||
opacity: sheetOpacity,
|
||||
transform: [
|
||||
{
|
||||
translateY: sheetOpacity.interpolate({
|
||||
inputRange: [0, 1],
|
||||
outputRange: [this._actionSheetHeight, 0],
|
||||
}),
|
||||
},
|
||||
],
|
||||
},
|
||||
]}>
|
||||
<View style={styles.sheet} onLayout={this._setActionSheetHeight}>
|
||||
<ActionGroup
|
||||
options={optionsArray}
|
||||
icons={icons}
|
||||
tintIcons={tintIcons === undefined ? true : tintIcons}
|
||||
destructiveButtonIndex={destructiveButtonIndex}
|
||||
destructiveColor={destructiveColor}
|
||||
onSelect={this._onSelect}
|
||||
startIndex={0}
|
||||
length={optionsArray.length}
|
||||
textStyle={textStyle || {}}
|
||||
tintColor={tintColor}
|
||||
title={title || undefined}
|
||||
titleTextStyle={titleTextStyle}
|
||||
message={message || undefined}
|
||||
messageTextStyle={messageTextStyle}
|
||||
autoFocus={autoFocus}
|
||||
showSeparators={showSeparators}
|
||||
containerStyle={containerStyle}
|
||||
separatorStyle={separatorStyle}
|
||||
/>
|
||||
</View>
|
||||
</Animated.View>
|
||||
</TouchableWithoutFeedback>
|
||||
);
|
||||
}
|
||||
|
||||
showActionSheetWithOptions = (options: ActionSheetOptions, onSelect: (i?: number) => void) => {
|
||||
const { isVisible, overlayOpacity, sheetOpacity } = this.state;
|
||||
|
||||
if (isVisible) {
|
||||
this._deferNextShow = this.showActionSheetWithOptions.bind(this, options, onSelect);
|
||||
return;
|
||||
}
|
||||
|
||||
this.setState({
|
||||
options,
|
||||
onSelect,
|
||||
isVisible: true,
|
||||
isAnimating: true,
|
||||
});
|
||||
overlayOpacity.setValue(0);
|
||||
sheetOpacity.setValue(0);
|
||||
Animated.parallel([
|
||||
Animated.timing(overlayOpacity, {
|
||||
toValue: 0.32,
|
||||
easing: EASING_OUT,
|
||||
duration: OPACITY_ANIMATION_IN_TIME,
|
||||
useNativeDriver: this.props.useNativeDriver,
|
||||
}),
|
||||
Animated.timing(sheetOpacity, {
|
||||
toValue: 1,
|
||||
easing: EASING_OUT,
|
||||
duration: OPACITY_ANIMATION_IN_TIME,
|
||||
useNativeDriver: this.props.useNativeDriver,
|
||||
}),
|
||||
]).start((result) => {
|
||||
if (result.finished) {
|
||||
this.setState({
|
||||
isAnimating: false,
|
||||
});
|
||||
this._deferNextShow = undefined;
|
||||
}
|
||||
});
|
||||
// @ts-ignore: Argument of type '"actionSheetHardwareBackPress"' is not assignable to parameter of type '"hardwareBackPress"'
|
||||
BackHandler.addEventListener('actionSheetHardwareBackPress', this._selectCancelButton);
|
||||
};
|
||||
|
||||
_selectCancelButton = () => {
|
||||
const { options } = this.state;
|
||||
const cancelButtonIndex = options === null ? undefined : options.cancelButtonIndex;
|
||||
|
||||
return this._onSelect(cancelButtonIndex);
|
||||
};
|
||||
|
||||
_onSelect = (index?: number): boolean => {
|
||||
const { isAnimating, onSelect } = this.state;
|
||||
|
||||
if (isAnimating) {
|
||||
return false;
|
||||
}
|
||||
|
||||
onSelect && onSelect(index);
|
||||
return this._animateOut();
|
||||
};
|
||||
|
||||
_animateOut = (): boolean => {
|
||||
const { isAnimating, overlayOpacity, sheetOpacity } = this.state;
|
||||
|
||||
if (isAnimating) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// @ts-ignore: Argument of type '"actionSheetHardwareBackPress"' is not assignable to parameter of type '"hardwareBackPress"'
|
||||
BackHandler.removeEventListener('actionSheetHardwareBackPress', this._selectCancelButton);
|
||||
this.setState({
|
||||
isAnimating: true,
|
||||
});
|
||||
Animated.parallel([
|
||||
Animated.timing(overlayOpacity, {
|
||||
toValue: 0,
|
||||
easing: EASING_IN,
|
||||
duration: OPACITY_ANIMATION_OUT_TIME,
|
||||
useNativeDriver: this.props.useNativeDriver,
|
||||
}),
|
||||
Animated.timing(sheetOpacity, {
|
||||
toValue: 0,
|
||||
easing: EASING_IN,
|
||||
duration: OPACITY_ANIMATION_OUT_TIME,
|
||||
useNativeDriver: this.props.useNativeDriver,
|
||||
}),
|
||||
]).start((result) => {
|
||||
if (result.finished) {
|
||||
this.setState({
|
||||
isVisible: false,
|
||||
isAnimating: false,
|
||||
});
|
||||
|
||||
if (this._deferNextShow) {
|
||||
this._deferNextShow();
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
flexContainer: {
|
||||
flex: 1,
|
||||
},
|
||||
overlay: {
|
||||
position: 'absolute',
|
||||
top: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
backgroundColor: 'black',
|
||||
},
|
||||
sheetContainer: {
|
||||
position: 'absolute',
|
||||
left: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
top: 0,
|
||||
backgroundColor: 'transparent',
|
||||
alignItems: 'flex-end',
|
||||
justifyContent: 'center',
|
||||
flexDirection: 'row',
|
||||
},
|
||||
sheet: {
|
||||
flex: 1,
|
||||
backgroundColor: 'transparent',
|
||||
},
|
||||
});
|
||||
+25
-41
@@ -1,54 +1,38 @@
|
||||
import * as React from 'react';
|
||||
|
||||
// NativeActionSheet will always be custom when on Android/web
|
||||
import NativeActionSheet from './ActionSheet';
|
||||
import CustomActionSheet from './ActionSheet/CustomActionSheet';
|
||||
import ActionSheet from './ActionSheet';
|
||||
import { Provider } from './context';
|
||||
import { ActionSheetOptions, ActionSheetProviderRef } from './types';
|
||||
import { ActionSheetOptions } from './types';
|
||||
|
||||
interface Props {
|
||||
children: React.ReactNode;
|
||||
useNativeDriver?: boolean;
|
||||
useCustomActionSheet?: boolean;
|
||||
}
|
||||
|
||||
export default React.forwardRef<ActionSheetProviderRef, Props>(function ActionSheetProvider(
|
||||
{ children, useNativeDriver, useCustomActionSheet = false },
|
||||
ref
|
||||
) {
|
||||
const actionSheetRef = React.useRef<NativeActionSheet>(null);
|
||||
export default class ActionSheetProvider extends React.Component<Props> {
|
||||
_actionSheetRef: React.RefObject<ActionSheet>;
|
||||
|
||||
const context = React.useMemo(
|
||||
() => ({
|
||||
constructor(props: Props) {
|
||||
super(props);
|
||||
this._actionSheetRef = React.createRef();
|
||||
}
|
||||
|
||||
getContext = () => {
|
||||
return {
|
||||
showActionSheetWithOptions: (options: ActionSheetOptions, callback: (i: number) => void) => {
|
||||
if (actionSheetRef.current) {
|
||||
actionSheetRef.current.showActionSheetWithOptions(options, callback);
|
||||
}
|
||||
this._actionSheetRef.current !== null &&
|
||||
this._actionSheetRef.current.showActionSheetWithOptions(options, callback);
|
||||
},
|
||||
}),
|
||||
[actionSheetRef]
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
React.useImperativeHandle(
|
||||
ref,
|
||||
() => ({
|
||||
// backwards compatible with 13.x before context was being passed right on the ref
|
||||
getContext: () => context,
|
||||
showActionSheetWithOptions: context.showActionSheetWithOptions,
|
||||
}),
|
||||
[context]
|
||||
);
|
||||
|
||||
const ActionSheet = React.useMemo(
|
||||
() => (useCustomActionSheet ? CustomActionSheet : NativeActionSheet),
|
||||
[useCustomActionSheet]
|
||||
);
|
||||
|
||||
return (
|
||||
<Provider value={context}>
|
||||
<ActionSheet ref={actionSheetRef} useNativeDriver={useNativeDriver}>
|
||||
{React.Children.only(children)}
|
||||
</ActionSheet>
|
||||
</Provider>
|
||||
);
|
||||
});
|
||||
render() {
|
||||
return (
|
||||
<Provider value={this.getContext()}>
|
||||
<ActionSheet ref={this._actionSheetRef} useNativeDriver={this.props.useNativeDriver}>
|
||||
{React.Children.only(this.props.children)}
|
||||
</ActionSheet>
|
||||
</Provider>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import { ActionSheetProps } from './types';
|
||||
|
||||
export default function connectActionSheet<OwnProps = any>(
|
||||
WrappedComponent: React.ComponentType<OwnProps & ActionSheetProps>
|
||||
): React.FunctionComponent<OwnProps & ActionSheetProps> {
|
||||
) {
|
||||
const ConnectedActionSheet = (props: OwnProps) => {
|
||||
return (
|
||||
<Consumer>
|
||||
|
||||
@@ -8,13 +8,6 @@ export interface ActionSheetProps {
|
||||
) => void;
|
||||
}
|
||||
|
||||
export interface ActionSheetProviderRef extends ActionSheetProps {
|
||||
/**
|
||||
* @deprecated Simply call `showActionSheetWithOptions()` directly from the ref now
|
||||
*/
|
||||
getContext: () => ActionSheetProps;
|
||||
}
|
||||
|
||||
// for iOS
|
||||
export interface ActionSheetIOSOptions {
|
||||
options: string[];
|
||||
@@ -22,11 +15,9 @@ export interface ActionSheetIOSOptions {
|
||||
message?: string;
|
||||
tintColor?: string;
|
||||
cancelButtonIndex?: number;
|
||||
cancelButtonTintColor?: string;
|
||||
destructiveButtonIndex?: number | number[];
|
||||
anchor?: number;
|
||||
userInterfaceStyle?: 'light' | 'dark';
|
||||
disabledButtonIndices?: number[];
|
||||
}
|
||||
|
||||
// for Android or Web
|
||||
|
||||
+2
-3
@@ -5,10 +5,9 @@
|
||||
"module": "commonjs",
|
||||
"strict": true,
|
||||
"moduleResolution": "node",
|
||||
"lib": ["dom", "es2015", "es2016", "esnext"],
|
||||
"lib": ["es2015", "es2016", "esnext"],
|
||||
"jsx": "react-native",
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true
|
||||
"esModuleInterop": true
|
||||
},
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in new issue
Block a user