ui icon indicating copy to clipboard operation
ui copied to clipboard

Scroll in Tabs

Open vijaystroup opened this issue 1 year ago • 8 comments

https://ui.shadcn.com/docs/components/scroll-area https://ui.shadcn.com/docs/components/tabs

Looking to implement a elegant horizontal scroll in a Tabs component so I tried using a Scroll component around the Tabs Trigger components.

Outcome was not as expected: image

Code:

<Tabs defaultValue='account'>
  <div className='flex justify-center p-5'>
    <TabsList className=''>
      <ScrollArea orientation='horizontal'>
        <TabsTrigger value='account'>Account</TabsTrigger>
        <TabsTrigger value='password'>Password</TabsTrigger>
      </ScrollArea>
    </TabsList>
  </div>
  <TabsContent value='account'>
    Make changes to your account here.
  </TabsContent>
  <TabsContent value='password'>
    Change your password here.
  </TabsContent>
</Tabs>

Without <Scroll/> performs normally: image

Overflow of <TabsTrigger /> without <Scroll />: image

Interested in a solution like this in the event more items get added to TabsList that overflow the parent container.

vijaystroup avatar Jan 02 '24 23:01 vijaystroup

Would it be because you are using "justify-center" in the parent div?

sandercoffee avatar Jan 03 '24 12:01 sandercoffee

Would it be because you are using "justify-center" in the parent div?

Removing the wrapper produced the same overflow error as the screen shot; just starting at justify-start instead of justify-center. However, removing the parent div and introducing the <ScrollArea /> component again, there seems to be some type of wrapping going on: image

Couldn't get a fix with overflow scroll and display flex with no wrap though..

vijaystroup avatar Jan 03 '24 14:01 vijaystroup

The orientation="horizontal" should be a part of <ScrollBar /> I guess.

Consider this structure once:

<Tabs> <ScrollArea> <TabList /> // {either map or individually} <ScrollBar orientation="horizontal" /> </ScrollArea> <TabsContent> </TabsContent? </Tabs>

harsh-1923 avatar Jan 06 '24 05:01 harsh-1923

Produced the same result as wrapping above.

vijaystroup avatar Jan 07 '24 17:01 vijaystroup

@vijaystroup not sure why is @harsh-1923's solution working for you but here is a sandbox for your reference: LINK

That is odd. I am trying to reproduce with your code exactly within the ScrollArea but it is still overflowing without any scroll. I think it may be the div wrapper I have to center and pad the tab menu.

vijaystroup avatar Jan 15 '24 20:01 vijaystroup

@vijaystroup , did you find a solution?

HMoen avatar Jan 19 '24 00:01 HMoen

@vijaystroup , did you find a solution?

No unfortunately. Currently it is not a hindrance to my application, but it may be in the future, that is why I opened this issue now.

vijaystroup avatar Jan 19 '24 16:01 vijaystroup

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

shadcn avatar Feb 26 '24 23:02 shadcn

Please reopen it

mineraux avatar Mar 12 '24 10:03 mineraux

Reopen. Issue is not resolved.

BertVanHecke avatar Mar 12 '24 22:03 BertVanHecke

Reopen, I'm needed this

randysmachado avatar Mar 14 '24 10:03 randysmachado

Not solved yet. @shadcn

vijaystroup avatar Mar 22 '24 20:03 vijaystroup

Please, reopen!

sotirelisc avatar Mar 25 '24 07:03 sotirelisc

parking, look forward for this

marwanbukhori avatar Mar 27 '24 07:03 marwanbukhori

The orientation="horizontal" should be a part of <ScrollBar /> I guess.

Consider this structure once:

<Tabs> <ScrollArea> <TabList /> // {either map or individually} <ScrollBar orientation="horizontal" /> </ScrollArea> <TabsContent> </TabsContent? </Tabs>

work for me ❤️

SweydManaf avatar Mar 28 '24 01:03 SweydManaf

Looking for this feature as well.

Here's a workaround I did for now:

   <div className="relative rounded-sm overflow-x-scroll h-10 bg-muted">
      <TabsList className="absolute flex flex-row justify-stretch w-full">
        {tabs.map((value, index) => (
          <TabsTrigger
            className="w-full"
            key={`tab_trigger_${index}`}
            value={value}
          >
            {value}
          </TabsTrigger>
        ))}
      </TabsList>
    </div>

Not ideal, but I used "bg-muted" on the outer div to give an illusion of a continuous bg for the tab list

mrvfino avatar Apr 09 '24 11:04 mrvfino

Reopen pls

taii03 avatar Apr 20 '24 04:04 taii03

      <ScrollArea>
        <div className="w-full relative h-10">
          <TabsList className="flex absolute h-10">
            {tabsHeadList.map(({ id, value, label }) => (
              <TabsTrigger key={id} value={value}>
                {label}
              </TabsTrigger>
            ))}
          </TabsList>
        </div>
        <ScrollBar orientation="horizontal" />
      </ScrollArea>

gianicolajara avatar Apr 20 '24 22:04 gianicolajara

How to add tailwindcss scroll snap align?

burxtx avatar Apr 23 '24 09:04 burxtx

I get somewhat workable code as:

<TabsList className='flex items-center justify-start flex-wrap h-auto space-y-1'> </TabsList>

abubakerx1da49 avatar May 18 '24 04:05 abubakerx1da49

      <ScrollArea>
        <div className="w-full relative h-10">
          <TabsList className="flex absolute h-10">
            {tabsHeadList.map(({ id, value, label }) => (
              <TabsTrigger key={id} value={value}>
                {label}
              </TabsTrigger>
            ))}
          </TabsList>
        </div>
        <ScrollBar orientation="horizontal" />
      </ScrollArea>

Thank yo so much @gianicolajara - this is great and solves the issue.

I set the scroll area component to render a div as per this discussion and then added pb-2 to make the bar render beneath the tabs instead of quatting the bottom padding of the TabTriggers. Is there a way to detect if the tabs have overflown or not so I can conditionally apply that pb?

timreach avatar Jul 03 '24 14:07 timreach