Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onRequestClose prop #1318

Closed
Stenbaek opened this issue Mar 9, 2023 · 6 comments
Closed

onRequestClose prop #1318

Stenbaek opened this issue Mar 9, 2023 · 6 comments
Labels
enhancement New feature or request no-issue-activity

Comments

@Stenbaek
Copy link

Stenbaek commented Mar 9, 2023

Feature Request

An onRequestClose prop on the BottomSheet. This prop should accept a function returning a Promise, which should be run between user dismissal/closing interaction and the sheet actually closing (to index -1). The return value of the promise should in turn decide whether to proceed with closing the sheet.

Why it is needed

In certain situations, it would be very useful to be able to interrupt any dismissal/closing of the sheet, eg. if there is unsaved changes in a form, or if some process is running. This would eg. allow you to prompt the user with an alert to confirm ending whatever workflow they are currently in.

Possible implementation

Almost as described above. I actually made it work by adding the onRequestClose prop to the sheet, and injecting that function in the handleClose handler. But this only works for dismissal of the sheet by either pressing the backdrop or running the .close() ref handler, and not when closing with the pan down to close gesture. I would love to make a PR, but I think I need help or guidance to make it work with the panning gesture as well.

Code sample

I imagine the use of onRequestClose prop something like:

const App = () => {
  return (
     <BottomSheet
        onRequestClose={async () => await AsyncAlert()}
        {...otherProps}
      />
  )
}

const AsyncAlert = async (): Promise<boolean> =>
    new Promise(resolve => {
        Alert.alert(
            'Some info',
            'Do you want to close?',
            [
                {
                    text: 'Close',
                    onPress: () => resolve(true),
                },
                {
                    text: 'Cancel',
                    onPress: () => resolve(false),
                },
            ],
        );
    });
@Stenbaek Stenbaek added the enhancement New feature or request label Mar 9, 2023
@Stenbaek Stenbaek changed the title onRequestClose props onRequestClose prop Mar 9, 2023
@Stenbaek
Copy link
Author

Stenbaek commented Mar 10, 2023

@gorhom I made a PR that add's the functionality, and it is working as intended. #1320

@RohovDmytro
Copy link

Can you solve your usecase by listening to index = -1 via onChange prop and then do your thing?

@RohovDmytro
Copy link

The other way is to use the onPress (and other) props of the backdrop component.

@Stenbaek
Copy link
Author

Can you solve your usecase by listening to index = -1 via onChange prop and then do your thing?

I've tried both, but it doesn't work as intended. Especially not since you want the onClose handler to run after the close happens (in this case, after the actual close), and BottomSheet listens for the animated change to happen and fires both the onClose and the onChange when the sheet animates to -1. I think it would be a nice addition to the API, but I understand that it could fall outside of the author's scope.

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions
Copy link

This issue was closed because it has been stalled for 5 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request no-issue-activity
Projects
None yet
Development

No branches or pull requests

2 participants