Skip to content

Commit 029fd23

Browse files
Merge pull request #974 from oneblink/AP-5821
AP-5821 # added scrollToTopOfPage to OneBlinkFormBaseProps
2 parents c22f26b + 0243483 commit 029fd23

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- added `scrollToTopOfPage` to `OneBlinkFormBaseProps`
13+
1014
## [8.12.2] - 2025-10-28
1115

1216
### Fixed

src/OneBlinkFormBase.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ export type OneBlinkFormBaseProps = OneBlinkReadOnlyFormProps & {
190190
*/
191191
scrollableContainerId?: string
192192
}
193+
/**
194+
* Whether to scroll to the top of the page when the user navigates to a new
195+
* page. If false the window will scroll to the top of the page stepper.
196+
* Defaults to false.
197+
*/
198+
scrollToTopOfPage?: boolean
193199
}
194200

195201
export type OneBlinkFormUncontrolledProps = {
@@ -244,6 +250,7 @@ function OneBlinkFormBase({
244250
navigableValidationErrorsNotificationSettings,
245251
replaceInjectablesOverrides,
246252
sectionState,
253+
scrollToTopOfPage,
247254
}: Props) {
248255
const isOffline = useIsOffline()
249256
const { isUsingFormsKey } = useAuth()
@@ -526,6 +533,7 @@ function OneBlinkFormBase({
526533
pages,
527534
formElementsValidation,
528535
formElementsConditionallyShown,
536+
scrollToTopOfPage,
529537
})
530538

531539
// #endregion

src/hooks/usePages.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ export default function usePages({
1515
formElementsValidation,
1616
formElementsConditionallyShown,
1717
hasAttemptedSubmit,
18+
scrollToTopOfPage,
1819
}: {
1920
pages: FormTypes.PageElement[]
2021
formElementsValidation: FormElementsValidation | undefined
2122
formElementsConditionallyShown: FormElementsConditionallyShown
2223
hasAttemptedSubmit: boolean
24+
scrollToTopOfPage?: boolean
2325
}) {
2426
const scrollToTopOfPageHTMLElementRef = React.useRef<HTMLDivElement>(null)
2527
const [visitedPageIds, setVisitedPageIds] = React.useState<string[]>([])
@@ -80,31 +82,41 @@ export default function usePages({
8082
const scrollToTopOfPageHTMLElement =
8183
scrollToTopOfPageHTMLElementRef.current
8284
if (isShowingMultiplePages && scrollToTopOfPageHTMLElement) {
83-
if (scrollToTopOfPageHTMLElement) {
85+
if (scrollToTopOfPage) {
8486
window.requestAnimationFrame(() => {
85-
scrollToTopOfPageHTMLElement.scrollIntoView({
86-
block: 'start',
87-
behavior: 'smooth',
88-
})
87+
window.scrollTo({ top: 0, behavior: 'smooth' })
8988
})
90-
}
91-
const stepItemHTMLElement = document.getElementById(
92-
`steps-navigation-step-${pageId}`,
93-
)
94-
if (stepItemHTMLElement) {
89+
} else if (scrollToTopOfPageHTMLElement) {
9590
window.requestAnimationFrame(() => {
96-
stepItemHTMLElement.scrollIntoView({
91+
scrollToTopOfPageHTMLElement.scrollIntoView({
9792
block: 'start',
9893
behavior: 'smooth',
9994
})
10095
})
96+
const stepItemHTMLElement = document.getElementById(
97+
`steps-navigation-step-${pageId}`,
98+
)
99+
if (stepItemHTMLElement) {
100+
window.requestAnimationFrame(() => {
101+
stepItemHTMLElement.scrollIntoView({
102+
block: 'start',
103+
behavior: 'smooth',
104+
})
105+
})
106+
}
101107
}
108+
102109
//blur prev/next buttons after they've been clicked
103110
const activeElement = document?.activeElement as HTMLElement
104111
activeElement.blur()
105112
}
106113
},
107-
[closeStepsNavigation, currentPageId, isShowingMultiplePages],
114+
[
115+
closeStepsNavigation,
116+
currentPageId,
117+
isShowingMultiplePages,
118+
scrollToTopOfPage,
119+
],
108120
)
109121

110122
const goToNextPage = React.useCallback(() => {

0 commit comments

Comments
 (0)