Scroll to top after changing the page?
Is there a way to scroll to the top whenever the page is changed, i.e. the
Thank you for any help!
We don't have anything built in to help, or any examples, but this was asked once before. Perhaps one of the suggestions posted will help you out: https://github.com/elastic/search-ui/issues/500
For the record, this could be achieved by configuring onSearch as follows:
onSearch: async (requestState, queryConfig, next) => {
const response = await next(requestState, queryConfig);
// Scroll to top on response
window.scrollTo({
top: 0,
behavior: 'smooth'
});
return response;
}
Where you could do some additional checks with window.scrollY or requestState.current (comparing it to a previous value) for example. The example above acts on every search/response like filtering, sorting, paging, etc.
See
- https://docs.elastic.co/search-ui/api/react/search-provider
- https://docs.elastic.co/search-ui/api/core/configuration#using-middleware-in-connector-handlers
The solution by @mauricesnip perfect, in case you need to add the correct types to a .tsx file
onSearch: async (state: RequestState,
queryConfig: QueryConfig,
next: (
state: RequestState,
queryConfig: QueryConfig
) => Promise<ResponseState>
) => {
const response = await next(state, queryConfig);
window.scrollTo({
top: 0,
behavior: "smooth",
});
return response;
},