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

[v4] BottomSheetBackdrop - appearsOnIndex of 0 fails to render backdrop (has workaround) #779

Closed
1 task done
jakobo opened this issue Dec 16, 2021 · 5 comments
Closed
1 task done
Labels
bug Something isn't working v4 Written in Reanimated v2

Comments

@jakobo
Copy link
Contributor

jakobo commented Dec 16, 2021

Bug

  • I'm willing to provide a pull request for this issue

As of 4.1.5, the BottomSheetBackdrop does not support a value of appearsOnIndex={0}. The root cause seems to be coercion to a default of 1 from a falsey 0 value.

Environment info

Library Version
@gorhom/bottom-sheet 4.1.5
react-native 0.64.3
react-native-reanimated 2.2.4
react-native-gesture-handler 1.10.3

Steps To Reproduce

  1. Create a bottom sheet with a custom backdrop using the renderBackdrop from the example
  2. Provide the properties appearsOnIndex={0} and disappearsOnIndex={0}

Describe what you expected to happen:

  1. The backdrop should be visible on index 0

Workaround Providing a snapIndex such as snapIndex={[1, "40%"]} will work although technically the modal will be visible at the bottom of the screen. Alternatively, you can copy/paste the BottomSheetBackdrop into a new component with either new defaults or fixed coercion.

Reproducible sample code

Reproducible Snack: https://snack.expo.dev/@jakobo/bottomsheetbackdrop---appearsonindex-bug

@jakobo jakobo added the bug Something isn't working label Dec 16, 2021
@jakobo
Copy link
Contributor Author

jakobo commented Dec 16, 2021

In case someone gets to this before I can put a PR together, the issue is here and the signature probably needs to be rewritten to something like:

const BottomSheetBackdropComponent = ({
  animatedIndex,
  opacity,
  appearsOnIndex,
  disappearsOnIndex,
  enableTouchThrough = DEFAULT_ENABLE_TOUCH_THROUGH,
  pressBehavior = DEFAULT_PRESS_BEHAVIOR,
  style,
  children,
}: BottomSheetDefaultBackdropProps) => {
  //#region defaults
  opacity = opacity ?? DEFAULT_OPACITY;
  appearsOnIndex = appearsOnIndex ?? DEFAULT_APPEARS_ON_INDEX;
  disappearsOnIndex = disappearsOnIndex ?? DEFAULT_DISAPPEARS_ON_INDEX;
  //#endregion

It's only an issue because the initial value of 0 will get forced to their non-zero defaults. This problem technically exits with setting default opacity as well, but it's more noticeable with appearsOnIndex since we're no longer setting our first snap point of 0. Alternatively, we can use a non ?? operator like either of the below.

// conditional operator for undefined value
opacity = (typeof opacity !== "undefined") ? opacity : DEFAULT_OPACITY;

// Allow explicit zero, otherwise coerce
opacity = opacity === 0 ? 0 : opacity || DEFAULT_OPACITY;

jakobo added a commit to jakobo/react-native-bottom-sheet that referenced this issue Dec 23, 2021
Coercion of 0 and false result in defaults being accidentally assigned
to the BottomSheetBackdrop's props. This fix uses the null coalesce
operator ?? to use either a user supplied value or fall back to the
built-in default.

Resolves gorhom#779
@pnogier
Copy link

pnogier commented Jan 13, 2022

Hi, any update on this issue ? 🙏

@jakobo
Copy link
Contributor Author

jakobo commented Jan 13, 2022

PR is in #793. Hopefully it gets addressed soon. In the meantime, you can create your own backdrop with the changes, which is what we did.

jakobo added a commit to jakobo/react-native-bottom-sheet that referenced this issue Jan 25, 2022
Coercion of 0 and false result in defaults being accidentally assigned
to the BottomSheetBackdrop's props. This fix uses the null coalesce
operator ?? to use either a user supplied value or fall back to the
built-in default.

Resolves gorhom#779
@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.

@IngyuTae
Copy link

You could write a custom backdrop component until the PR merged

import { BottomSheetBackdropProps } from '@gorhom/bottom-sheet'
import React, { useMemo } from 'react'
import Animated, {
  Extrapolate,
  interpolate,
  useAnimatedStyle,
} from 'react-native-reanimated'

const CustomBackdrop = ({ animatedIndex, style }: BottomSheetBackdropProps) => {
  const containerAnimatedStyle = useAnimatedStyle(() => ({
    opacity: interpolate(
      animatedIndex.value,
      [-1, 0],
      [0, 0.5],
      Extrapolate.CLAMP
    ),
  }))

  const containerStyle = useMemo(
    () => [style, { backgroundColor: '#a8b5eb' }, containerAnimatedStyle],
    [style, containerAnimatedStyle]
  )

  return <Animated.View style={containerStyle} />
}

export default CustomBackdrop

@gorhom gorhom added the v4 Written in Reanimated v2 label Apr 21, 2022
@gorhom gorhom closed this as completed in 7e00dd2 Apr 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working v4 Written in Reanimated v2
Projects
None yet
Development

No branches or pull requests

4 participants