Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stripe.android.uicore.elements

import androidx.annotation.RestrictTo
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
Expand All @@ -11,23 +12,34 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.relocation.BringIntoViewRequester
import androidx.compose.foundation.relocation.bringIntoViewRequester
import androidx.compose.material.Card
import androidx.compose.material.Divider
import androidx.compose.material.LocalContentColor
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import com.stripe.android.uicore.R
import com.stripe.android.uicore.stripeColors
import com.stripe.android.uicore.text.annotatedStringResource

@OptIn(ExperimentalFoundationApi::class)
@Composable
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
fun InlineAddressPredictionsUI(
Expand All @@ -41,11 +53,33 @@ fun InlineAddressPredictionsUI(
return
}

// When predictions appear (or change), scroll the page up so the whole dropdown fits above
// the keyboard, plus about half a result box of extra space below the last result so it isn't
// flush against the keyboard. This pushes the fields above (e.g. card details) off-screen if
// needed so all predictions stay visible.
val bringIntoViewRequester = remember { BringIntoViewRequester() }
val extraBottomSpacePx = with(LocalDensity.current) { 32.dp.toPx() }
var dropdownSize by remember { mutableStateOf(IntSize.Zero) }
LaunchedEffect(state, dropdownSize) {
if (dropdownSize != IntSize.Zero) {
bringIntoViewRequester.bringIntoView(
Rect(
left = 0f,
top = 0f,
right = dropdownSize.width.toFloat(),
bottom = dropdownSize.height + extraBottomSpacePx,
)
)
}
}

Card(
elevation = 4.dp,
modifier = Modifier
.fillMaxWidth()
.padding(top = 4.dp),
.padding(top = 4.dp)
.onSizeChanged { dropdownSize = it }
.bringIntoViewRequester(bringIntoViewRequester),
) {
InlineAddressPredictionsContent(
state = state,
Expand Down
Loading