Skip to content

Commit

Permalink
Focus on page title when returning from viewing item
Browse files Browse the repository at this point in the history
  • Loading branch information
davidc-gds committed Dec 12, 2024
1 parent 016bf1d commit 53945eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
21 changes: 4 additions & 17 deletions design/src/main/kotlin/uk/govuk/app/design/ui/component/Header.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.govuk.app.design.ui.component

import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
Expand All @@ -14,8 +13,6 @@ import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
Expand Down Expand Up @@ -49,8 +46,7 @@ fun ChildPageHeader(
text: String? = null,
onBack: (() -> Unit)? = null,
onAction: (() -> Unit)? = null,
actionText: String? = null,
focusRequester: FocusRequester? = null
actionText: String? = null
) {
Column(modifier) {
if (onBack != null || onAction != null) {
Expand Down Expand Up @@ -89,20 +85,11 @@ fun ChildPageHeader(
}

if (text != null) {
var titleModifier =
Modifier
.fillMaxWidth()
.padding(horizontal = GovUkTheme.spacing.medium)
focusRequester?.let {
titleModifier = titleModifier.then(
Modifier
.focusRequester(it)
.focusable()
)
}
LargeTitleBoldLabel(
text = text,
modifier = titleModifier
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = GovUkTheme.spacing.medium)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
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.focus.FocusRequester
Expand All @@ -31,6 +33,7 @@ import uk.govuk.app.design.ui.component.BodyRegularLabel
import uk.govuk.app.design.ui.component.ChildPageHeader
import uk.govuk.app.design.ui.component.ExternalLinkListItem
import uk.govuk.app.design.ui.component.ExtraLargeVerticalSpacer
import uk.govuk.app.design.ui.component.LargeTitleBoldLabel
import uk.govuk.app.design.ui.component.LargeVerticalSpacer
import uk.govuk.app.design.ui.component.ListHeadingLabel
import uk.govuk.app.design.ui.component.SmallVerticalSpacer
Expand Down Expand Up @@ -86,16 +89,23 @@ private fun VisitedScreen(
val lifecycleState by lifecycleOwner.lifecycle.currentStateFlow.collectAsState()
val focusManager = LocalFocusManager.current
val focusRequester = remember { FocusRequester() }
var isViewingItem by remember { mutableStateOf(false) }

Column(modifier) {
val onAction = if (!visitedItems.isNullOrEmpty()) onEditClick else null

ChildPageHeader(
text = title,
onBack = onBack,
onAction = onAction,
actionText = editText,
focusRequester = focusRequester
)
LargeTitleBoldLabel(
text = title,
modifier = Modifier
.fillMaxWidth()
.focusRequester(focusRequester)
.focusable()
.padding(horizontal = GovUkTheme.spacing.medium)
)
LazyColumn(
Modifier
Expand All @@ -106,7 +116,10 @@ private fun VisitedScreen(
if (visitedItems.isNullOrEmpty()) {
NoVisitedItems(modifier)
} else {
ShowVisitedItems(visitedItems, onClick)
ShowVisitedItems(visitedItems) { title, url ->
isViewingItem = true
onClick(title, url)
}
}
}
}
Expand All @@ -115,9 +128,12 @@ private fun VisitedScreen(
LaunchedEffect(lifecycleState) {
when (lifecycleState) {
Lifecycle.State.RESUMED -> {
focusManager.clearFocus(true)
delay(500)
focusRequester.requestFocus()
if (isViewingItem) {
focusManager.clearFocus(true)
delay(500)
focusRequester.requestFocus()
isViewingItem = false
}
}
else -> { /* Do nothing */ }
}
Expand Down

0 comments on commit 53945eb

Please sign in to comment.