react-native-bottom-sheet icon indicating copy to clipboard operation
react-native-bottom-sheet copied to clipboard

Lock the Sheet at a Specific Index

Open calebpanza opened this issue 2 years ago • 2 comments

Feature Request

Why it is needed

One of the ways that I use the Bottom Sheet (specifically the Modal) is to keep it open all the time with gestures enabled. It's been awesome to get a really cool effect where as you drag up on the sheet, the UI inside of the sheet becomes visible (I know I'm not the first, it's basically what Apple Maps does. Lol)

One scenario that I recently came across, though, was that I needed to keep the modal locked in the 0 index based on what information was presented inside the sheet. And this got me thinking, could there be a way to lock/unlock the Bottom Sheet to a specific index from within a child of the Bottom Sheet?

Possible implementation

Using the useBottomSheet, I could see there being a new function passed in called lockToIndex(number|null).

Passing in a number would expand/collapse the sheet to the index specified and would not allow the user to expand/collapse outside of that index.

To unlock the sheet again, pass in null (or -1 if you wanted all numeric values)

Considerations ::

  • While I think that forceClose should always close the sheet regardless of the locked state, I don't think that collapse, expand nor close should function when the sheet is locked. Instead, a warning could be thrown while developing informing that You cannot collapse a sheet while it's locked and so on
  • If my sheet is currently opened to the 0 index and I call lockToIndex(2), the sheet should expand to the second index. In the same way, if I then call lockToIndex(0), the sheet would collapse to the 0 index and stay locked there
  • In order to allow for other components to respond to lock state, there might be a lockedIndex that also gets passed down through useBottomSheet so that side effects can run on sibling components.
  • The main <BottomSheet> component could also be rendered with an index locked in by passing in initialLockedIndex at render

Code sample

This is how I would imagine using it in a code example:

const ComponentRenderedInBottomSheet = () => {
    const { lockToIndex } = useBottomSheet()

    function lockSheetToOpen() {
        lockToIndex(1)
    }

    function unlockSheet() {
        lockToIndex(null)
    }

    return <View>
        <Button onPress={lockSheetToOpen}>Lock the User to an Index</Button>
        <Button onPress={unlockSheet}>Unlock the sheet</Button>
    </View>
}

calebpanza avatar Aug 10 '22 13:08 calebpanza

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[bot] avatar Sep 10 '22 09:09 github-actions[bot]

Hey I'm still really interested in this functionality! Just wanted to comment this so that it's not auto-closed

calebpanza avatar Sep 12 '22 18:09 calebpanza

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[bot] avatar Oct 13 '22 09:10 github-actions[bot]

...still interested! Lol. Any chance of this getting looked at?

calebpanza avatar Oct 13 '22 19:10 calebpanza

Have you tried snapToIndex?

zealmurapa avatar Oct 30 '22 13:10 zealmurapa

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[bot] avatar Nov 30 '22 09:11 github-actions[bot]

@zealmurapa yeah I've used snapToIndex, but the user still has the ability to jump to other indices.

calebpanza avatar Nov 30 '22 15:11 calebpanza

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[bot] avatar Jan 01 '23 09:01 github-actions[bot]

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

github-actions[bot] avatar Jan 06 '23 09:01 github-actions[bot]

would love this feature as well! 😍

mishkaloven avatar Jan 10 '23 21:01 mishkaloven

Any chance we could get this re-opened?

calebpanza avatar Jan 10 '23 21:01 calebpanza

I want this too

mhosman avatar Sep 15 '23 19:09 mhosman

I would like this as well

kepegram avatar Mar 28 '24 19:03 kepegram