kobaltedev / kobalte

A UI toolkit for building accessible web apps and design systems with SolidJS.
https://kobalte.dev
MIT License
1.2k stars 61 forks source link

[Tabs] Tab indicator is broken when resizing the window #273

Open cs-clarence opened 10 months ago

cs-clarence commented 10 months ago

Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior:

  1. Go to https://kobalte.dev/docs/core/components/tabs
  2. Scroll down to dynamic tabs section
  3. Add 10 tabs (or more)
  4. Click a tab somewhere in the middle of the list
  5. Resize the window
  6. The tab indicator stays its size and position despite the tab items getting downsized or upsized

Expected behavior The tab indicator should follow the tab item above it when resizing the window

cs-clarence commented 10 months ago

Workaround:

// get the ref of your Tabs.Root
let root: HTMLDivElement;
onMount(() => {
  const list = root!.querySelector<HTMLElement>(
    ".tabs__list",
  )!;
  const indicator = root!.querySelector<HTMLElement>(
    ".tabs__indicator",
  )!;

  const handleResize: ResizeObserverCallback = debounce(() => {
    const selected = root!.querySelector<HTMLElement>(
      `.tabs__item[data-selected]`,
    )!;

    indicator.style.width = `${selected.clientWidth}px`;
    indicator.style.transform = `translateX(${selected.offsetLeft}px)`;
  }, 100);

  const ro = new ResizeObserver(handleResize);
  ro.observe(list);

  return () => {
    ro.unobserve(list);
  };
});