search-ui icon indicating copy to clipboard operation
search-ui copied to clipboard

Scroll to top after changing the page?

Open 684efs3 opened this issue 4 years ago • 3 comments

Is there a way to scroll to the top whenever the page is changed, i.e. the component is clicked? I have tried to get this to work, but haven't figured out how. I think it is bothersome to readers to have to scroll back to the top every time they click to go to the next page.

Thank you for any help!

684efs3 avatar Nov 08 '22 19:11 684efs3

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

JasonStoltz avatar Nov 14 '22 22:11 JasonStoltz

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

  1. https://docs.elastic.co/search-ui/api/react/search-provider
  2. https://docs.elastic.co/search-ui/api/core/configuration#using-middleware-in-connector-handlers

mauricesnip avatar Jul 05 '23 08:07 mauricesnip

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;
  },

SumiSastri avatar Jul 22 '24 15:07 SumiSastri