Add auto-focus to search toolbar (#725)

This commit is contained in:
David Perez 2024-01-22 21:42:16 -06:00 committed by Álison Fernandes
parent 112d181394
commit 150fd159c3

View file

@ -12,7 +12,11 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.TopAppBarScrollBehavior import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
@ -33,7 +37,9 @@ fun BitwardenSearchTopAppBar(
scrollBehavior: TopAppBarScrollBehavior, scrollBehavior: TopAppBarScrollBehavior,
navigationIcon: NavigationIcon?, navigationIcon: NavigationIcon?,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
autoFocus: Boolean = true,
) { ) {
val focusRequester = remember { FocusRequester() }
TopAppBar( TopAppBar(
modifier = modifier, modifier = modifier,
colors = TopAppBarDefaults.topAppBarColors( colors = TopAppBarDefaults.topAppBarColors(
@ -78,8 +84,12 @@ fun BitwardenSearchTopAppBar(
} }
}, },
modifier = Modifier modifier = Modifier
.focusRequester(focusRequester)
.fillMaxWidth(), .fillMaxWidth(),
) )
}, },
) )
if (autoFocus) {
LaunchedEffect(Unit) { focusRequester.requestFocus() }
}
} }