Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,26 @@ bun run test:android:instrumented

Instrumentation tests belong in `android/src/androidTest`. Prefer JVM or
Robolectric coverage when Android framework behavior can be reproduced there.

## Native bottom-anchored slot

Run the Android host test against the generated Java delegate and Robolectric
after preparing the example project:

```sh
./example/android/gradlew --project-dir example/android \
:swmansion_react-native-bottom-sheet:testDebugUnitTest --tests '*BottomSheetHostViewAnchorTest'
```

On an Apple silicon Mac with Xcode, run the UIKit host test through Mac Catalyst
without starting a simulator:

```sh
test_dir=$(mktemp -d)
sdk=$(xcrun --sdk macosx --show-sdk-path)
swiftc -sdk "$sdk" -target arm64-apple-ios16.0-macabi \
-F "$sdk/System/iOSSupport/System/Library/Frameworks" \
-I "$sdk/System/iOSSupport/usr/include" \
ios/HostingView/*.swift tests/ios/BottomSheetHostSettleTests.swift -o "$test_dir/host-test"
"$test_dir/host-test"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.swmansion.reactnativebottomsheet

import android.graphics.Rect
import android.view.View
import android.widget.FrameLayout
import com.facebook.react.uimanager.PointerEvents
import com.facebook.react.uimanager.ReactPointerEventsView

/** Native viewport for the anchored React child. Its bounds gate drawing and hit testing. */
internal class BottomSheetAnchorClipLayout(val content: View) :
FrameLayout(content.context), ReactPointerEventsView {
override val pointerEvents = PointerEvents.BOX_NONE
private val viewport = Rect()

init {
addView(content)
}

// Card lays the child out at its Fabric-measured size, independently of the viewport.
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) = Unit

fun place(width: Int, top: Int, bottom: Int) {
if (this.width == width && this.top == top && this.bottom == bottom) return
layout(0, top, width, bottom)
if (viewport.right != width || viewport.bottom != bottom - top) {
viewport.set(0, 0, width, bottom - top)
clipBounds = viewport
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.swmansion.reactnativebottomsheet;

import androidx.annotation.Nullable;
import com.facebook.react.uimanager.PointerEvents;
import com.facebook.react.viewmanagers.BottomSheetContentWrapperViewManagerDelegate;
import com.facebook.react.views.view.ReactViewGroup;

// Match Fabric's generated Java delegate, including RN's Java compatibility bridge.
final class BottomSheetContentPointerEventsDelegate
extends BottomSheetContentWrapperViewManagerDelegate<
ReactViewGroup, BottomSheetContentWrapperViewManager> {
BottomSheetContentPointerEventsDelegate(BottomSheetContentWrapperViewManager manager) {
super(manager);
}

@Override
public void setProperty(ReactViewGroup view, String propName, @Nullable Object value) {
if ("pointerEvents".equals(propName)) {
view.setPointerEvents(PointerEvents.parsePointerEvents((String) value));
} else {
super.setProperty(view, propName, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.uimanager.ViewManagerDelegate
import com.facebook.react.viewmanagers.BottomSheetContentWrapperViewManagerDelegate
import com.facebook.react.viewmanagers.BottomSheetContentWrapperViewManagerInterface
import com.facebook.react.views.view.ReactViewGroup

Expand All @@ -22,7 +21,7 @@ class BottomSheetContentWrapperViewManager :
const val NAME = "BottomSheetContentWrapperView"
}

private val delegate = BottomSheetContentWrapperViewManagerDelegate(this)
private val delegate = BottomSheetContentPointerEventsDelegate(this)

override fun getDelegate(): ViewManagerDelegate<ReactViewGroup> = delegate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.view.ViewGroup
import android.view.WindowInsets
import androidx.core.view.NestedScrollingParent3
import androidx.core.view.ViewCompat
import com.facebook.react.R
import com.facebook.react.uimanager.PointerEvents
import com.facebook.react.uimanager.StateWrapper
import com.facebook.react.views.view.ReactViewGroup
Expand Down Expand Up @@ -133,6 +134,7 @@ class BottomSheetHostView(context: Context) : ReactViewGroup(context), NestedScr
private val detents: Detents =
Detents(
state,
card,
clipContainer,
scrim,
contentRegion,
Expand Down Expand Up @@ -199,15 +201,26 @@ class BottomSheetHostView(context: Context) : ReactViewGroup(context), NestedScr
val sheetChildCount: Int
get() = clipContainer.childCount

fun getSheetChildAt(index: Int): View? = clipContainer.getChildAt(index)
fun getSheetChildAt(index: Int): View? {
val child = clipContainer.getChildAt(index)
return if (child is BottomSheetAnchorClipLayout) child.content else child
}

fun addSheetChild(child: View, index: Int) {
clipContainer.addView(child, index)
val hostChild =
if (child.getTag(R.id.view_tag_native_id) == BOTTOM_ANCHORED_CONTENT_NATIVE_ID) {
BottomSheetAnchorClipLayout(child)
} else {
child
}
clipContainer.addView(hostChild, index)
detents.refreshContentHeightMarker()
}

fun removeSheetChildAt(index: Int) {
clipContainer.getChildAt(index)?.let(card::resetContentTransform)
getSheetChildAt(index)?.let(card::resetContentTransform)
val child = clipContainer.getChildAt(index)
if (child is BottomSheetAnchorClipLayout) child.removeView(child.content)
clipContainer.removeViewAt(index)
detents.refreshContentHeightMarker()
}
Expand All @@ -218,24 +231,22 @@ class BottomSheetHostView(context: Context) : ReactViewGroup(context), NestedScr
if (child === sheetContainer || child === cardTouchSensor || child === scrimDismissView) {
super.addView(child, index, params)
} else {
clipContainer.addView(child, index, params)
child.layoutParams = params
addSheetChild(child, index)
}
}

override fun removeView(view: View) {
if (view === sheetContainer) {
super.removeView(view)
} else {
card.resetContentTransform(view)
clipContainer.removeView(view)
detents.refreshContentHeightMarker()
val index = clipContainer.indexOfChild((view.parent as? BottomSheetAnchorClipLayout) ?: view)
if (index >= 0) removeSheetChildAt(index)
}
}

override fun removeViewAt(index: Int) {
clipContainer.getChildAt(index)?.let(card::resetContentTransform)
clipContainer.removeViewAt(index)
detents.refreshContentHeightMarker()
removeSheetChildAt(index)
}

// MARK: - Layout
Expand Down Expand Up @@ -644,10 +655,9 @@ class BottomSheetHostView(context: Context) : ReactViewGroup(context), NestedScr
scrim.clearScrimProgress()
state.suppressScrimForClosingTarget = false
state.scrimPinnedFull = false
for (i in 0 until clipContainer.childCount) {
card.resetContentTransform(clipContainer.getChildAt(i))
for (i in clipContainer.childCount - 1 downTo 0) {
removeSheetChildAt(i)
}
clipContainer.removeAllViews()
stateWrapper = null
contentRegion.clearShadowOffset()
card.clearShadowContentTransform()
Expand Down Expand Up @@ -688,6 +698,8 @@ class BottomSheetHostView(context: Context) : ReactViewGroup(context), NestedScr
}

companion object {
internal const val BOTTOM_ANCHORED_CONTENT_NATIVE_ID =
"swm-bottom-sheet-bottom-anchored-content"
// The `nativeID` the JS side gives the zero-height marker it appends to
// the content wrapper. Mirrored in `bottomSheetUtils.ts` and
// `BottomSheetHostingView.swift`; keep the three in sync.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ class BottomSheetViewManager :
"insetRight" to inset("insetRight"),
"insetBottom" to inset("insetBottom"),
"widestCard" to (map.hasKey("widestCard") && map.getBoolean("widestCard")),
"contentClipTopInset" to inset("contentClipTopInset"),
"maxWidth" to inset("maxWidth"),
"placementBias" to
(if (map.hasKey("placementBias")) map.getDouble("placementBias") else 0.5),
"scrimOpacity" to
(if (map.hasKey("scrimOpacity")) map.getDouble("scrimOpacity") else 1.0),
),
"scrimOpacity" to (if (map.hasKey("scrimOpacity")) map.getDouble("scrimOpacity") else 1.0),
)
)
}
view.setDetents(list)
Expand Down Expand Up @@ -211,7 +211,7 @@ class BottomSheetViewManager :
blurRadius = shadow.getDouble("blurRadius").toFloat(),
spreadDistance = shadow.getDouble("spreadDistance").toFloat(),
color = shadowColor(view, shadow),
),
)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.swmansion.reactnativebottomsheet.host

import android.view.View
import com.swmansion.reactnativebottomsheet.BottomSheetAnchorClipLayout
import com.swmansion.reactnativebottomsheet.BottomSheetCardClipLayout
import com.swmansion.reactnativebottomsheet.BottomSheetCardShadowView
import com.swmansion.reactnativebottomsheet.CardShadow
import kotlin.math.round
import kotlin.math.roundToInt

private val NO_INSETS = DetentInsets()

Expand All @@ -23,6 +25,11 @@ internal class Card(
val shadowViews = listOf(shadowTop, shadowBottom)
var shadowContentOffsetX = 0f
var shadowContentScale = 1f
var shadowAnchorCardLeft = 0f
private set

var shadowAnchorCardHeight = 0f
private set

/** Physical corner radii of the card in dp: top-left, top-right, bottom-right, bottom-left. */
fun setSurfaceCornerRadii(values: List<Float>) {
Expand All @@ -34,14 +41,15 @@ internal class Card(

/** Outset card shadows, with lengths in dp. */
fun setSurfaceShadows(values: List<CardShadow>) {
val shadows = values.map {
it.copy(
offsetX = it.offsetX * state.density,
offsetY = it.offsetY * state.density,
blurRadius = it.blurRadius * state.density,
spreadDistance = it.spreadDistance * state.density,
)
}
val shadows =
values.map {
it.copy(
offsetX = it.offsetX * state.density,
offsetY = it.offsetY * state.density,
blurRadius = it.blurRadius * state.density,
spreadDistance = it.spreadDistance * state.density,
)
}
for (shadowView in shadowViews) shadowView.setShadows(shadows)
updateCardGeometry()
}
Expand Down Expand Up @@ -92,7 +100,7 @@ internal class Card(
val right = left + span.width
val bottom = cardBottomAt(position, insets)
clipContainer.setCard(left, 0f, right, bottom)
updateContentTransform(left, right)
updateContentTransform(left, right, bottom, position)
updateCardShadows(left, right, bottom)
}

Expand All @@ -115,32 +123,76 @@ internal class Card(
return if (scale > 0f) height / scale else height
}

/** Scales the surface and content to the card's width and pins them to its top-left corner. */
private fun updateContentTransform(cardLeft: Float, cardRight: Float) {
/** Fits the content to the card's width and places it against its chosen edge. */
private fun updateContentTransform(
cardLeft: Float,
cardRight: Float,
cardBottom: Float,
position: Float,
) {
val widest = state.placeCard(state.widestCardInsets, state.sheetContainer.width.toFloat()).width
val scale = if (widest > 0f) (cardRight - cardLeft) / widest else 1f
shadowAnchorCardLeft = 0f
shadowAnchorCardHeight = 0f
for (i in 0 until clipContainer.childCount) {
val child = clipContainer.getChildAt(i)
val hostChild = clipContainer.getChildAt(i)
val anchor = hostChild as? BottomSheetAnchorClipLayout
val child = anchor?.content ?: hostChild
child.pivotX = 0f
child.pivotY = 0f
child.scaleX = scale
child.scaleY = scale
child.translationX = cardLeft - child.left
if (anchor != null) {
val bottom = cardBottom.roundToInt()
val top = (anchorTopInset(position) * scale).roundToInt().coerceIn(0, bottom)
anchor.place(clipContainer.width, top, bottom)
child.translationY = cardBottom - top - child.height * scale
shadowAnchorCardLeft = cardLeft
shadowAnchorCardHeight = cardBottom
}
}
shadowContentScale = scale
// The shadow tree scales and shifts the children from where they are laid out, so `measure`
// follows what is drawn even before a new layout catches up with a resize.
val first = clipContainer.getChildAt(0)
val contentLeft =
if (clipContainer.childCount > 0) clipContainer.getChildAt(0).left.toFloat()
else state.contentLeft(widest)
((first as? BottomSheetAnchorClipLayout)?.content ?: first)?.left?.toFloat()
?: state.contentLeft(widest)
shadowContentOffsetX = cardLeft - scale * contentLeft
}

private var anchorInsets: List<Float> = emptyList()

fun setAnchorInsets(values: List<Float>): Boolean {
val insets = if (values.any { it > 0f }) values else emptyList()
if (insets == anchorInsets) return false
anchorInsets = insets
return true
}

private fun anchorTopInset(position: Float): Float {
val specs = state.detentSpecs
if (anchorInsets.isEmpty() || anchorInsets.size != specs.size) return 0f
val upperIndex = specs.indexOfFirst { position < it.position }
if (upperIndex < 0) return anchorInsets.last()
val upper = specs[upperIndex]
var end = upperIndex
while (end < specs.lastIndex && specs[end + 1].position == upper.position) end++
val target = anchorInsets[end]
if (upperIndex == 0) return target
val lower = specs[upperIndex - 1]
val progress = (position - lower.position) / (upper.position - lower.position)
val start = anchorInsets[upperIndex - 1]
return start + (target - start) * progress
}

// Fabric recycles the view, which must not keep the card's scale.
fun resetContentTransform(view: View) {
view.scaleX = 1f
view.scaleY = 1f
view.translationX = 0f
if (view.parent is BottomSheetAnchorClipLayout) view.translationY = 0f
}

private fun updateCardShadows(cardLeft: Float, cardRight: Float, cardBottom: Float) {
Expand Down Expand Up @@ -206,14 +258,18 @@ internal class Card(
clipContainer.layout(0, 0, containerWidth, containerHeight)
// Fabric only measures the children, so the host applies the content's Yoga position.
for (i in 0 until clipContainer.childCount) {
val child = clipContainer.getChildAt(i)
val hostChild = clipContainer.getChildAt(i)
val child = (hostChild as? BottomSheetAnchorClipLayout)?.content ?: hostChild
val contentLeft = state.contentLeft(child.measuredWidth.toFloat()).toInt()
child.layout(contentLeft, 0, contentLeft + child.measuredWidth, child.measuredHeight)
}
}

fun clearShadowContentTransform() {
anchorInsets = emptyList()
shadowContentOffsetX = 0f
shadowContentScale = 1f
shadowAnchorCardLeft = 0f
shadowAnchorCardHeight = 0f
}
}
Loading
Loading