Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a gap/spacing between items #598

Open
rasgo-cc opened this issue Jan 8, 2025 · 5 comments
Open

Adding a gap/spacing between items #598

rasgo-cc opened this issue Jan 8, 2025 · 5 comments

Comments

@rasgo-cc
Copy link

rasgo-cc commented Jan 8, 2025

Is your feature request related to a problem? Please describe.
Not being able to add spacing (gap) between items.

Describe the solution you'd like
I'd like to have something like the gap prop in @tanstack/react-virtual so you can pass the exact amount of pixels that should be placed in between items. Passing that value should allow the virtualization code to account for that and thus calculate the item offsets correctly.

Describe alternatives you've considered
So far the only way I found to have spacing between items is to add a top and bottom margin (so it's vertically centered). This is inconvenient as it will also add a margin to the top and bottom of the list, not just in between the items.

Additional context
(none)

@aeharding
Copy link
Contributor

This could be done with a custom item component, have you tried that?

@rasgo-cc
Copy link
Author

rasgo-cc commented Jan 9, 2025

This could be done with a custom item component, have you tried that?

Thank you for suggesting @aeharding !

I tried this (see below) but this doesn't work as findStartIndex() gives me the index of the first visible item, not the first item in the virtualized list (visible + overscan items). When I scroll and index 1 becomes the first visible item, the index 0 is still in the virtualized list so it doesn't work as I intended.

  const ItemComponent = forwardRef(
    (props: CustomItemComponentProps, extRef: any) => {

      let offset = 0;
      if (componentRef.current) {
        offset = componentRef.current.findStartIndex();
      }
      const gap = 10;

      return (
        <div
          {...props}
          ref={extRef}
          style={{
            ...props.style,
            top: (props.style.top as number) + gap * (props.index - offset),
          }}
        />
      );
    }
  );

Any way for me to get the first index of the virtualized list?

@aeharding
Copy link
Contributor

Does props.index work? That should be the absolute index

@rasgo-cc
Copy link
Author

rasgo-cc commented Jan 9, 2025

It doesn't work. I'm sure I'm missing something in the logic though but I don't know what.
Just created a sandbox:
https://stackblitz.com/edit/react-starter-tailwindcss-la2clkwt?file=src%2FApp.tsx

You can see the fist items have a gap (the first virtualized window?) but then spacing gets messed up.
Any ideas?

@inokawa
Copy link
Owner

inokawa commented Jan 14, 2025

I agree with that @tanstack/react-virtual like gap prop is needed. However, there is no clear schedule for now.

You can do the similar thing by manually adding margin-top to the last item and margin-bottom to the others like:

<VList>
  {array.map((d, i) => <Component key={i} isLast={i === array.length - 1}>{d}</Component> )}
</VList>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants