Skip to content

scrollToElement does not work at all (or as I expect it to work) #48

@icedemon72

Description

@icedemon72

When I try to use your new scrollToElement hook, I am getting only redirected to page (it seems to activate goToPage(x) only). Also I have really heavy logic with various selectors and stuff to do scrollToElement function, so it would save me lots of work if this was implemented natively (plus currently I need scrollToElement with centering the element in view logic).

Maybe I am doing something wrong but my solution sometimes redirects me to page 1 even though (for example) page 17 exists where my element (created using updateElement) is.

const scrollToRestriction = useCallback(
  async (key: string, pageHint: number) => {
    if (pageHint) goToPage(pageHint);

    await waitForPageRendered(pageHint);

    const el = restrictionRefs.current[key];
    if (!el) return;

    const pagesRoot = getPagesRoot();
    if (!pagesRoot) return;
    const container = getScrollableDescendant(pagesRoot) ?? pagesRoot;

    centerUntilSettled(container as HTMLElement, el, {
      tolerance: 8,
      stableFrames: 2,
      timeoutMs: 800,
      animDuration: 520
    });
  },
  [goToPage]
);

P.S. Perhaps we need access to the scrollable container inside the document? I currently retrieve it using this helper:

export function getScrollableDescendant(root: HTMLElement): HTMLElement | null {
  const isScrollable = (node: Element) => {
    const st = getComputedStyle(node as HTMLElement);
    return (
      /(auto|scroll)/.test(st.overflowY) && (node as HTMLElement).scrollHeight > (node as HTMLElement).clientHeight
    );
  };
  if (isScrollable(root)) return root;
  const q: Element[] = Array.from(root.children);
  while (q.length) {
    const node = q.shift()!;
    if (isScrollable(node)) return node as HTMLElement;
    q.push(...Array.from(node.children));
  }
  return null;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions