question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow wrapping `TabPanel` components

See original GitHub issue

🚀 Feature request

🧱 Problem Statement / Justification

I’m using the Tabs components. If I want to wrap the TabPanel components in another component, the Tabs components break.

// Does not work
<Tabs>
  <TabList>
      <Tab>FIrst Tab</Tab>
      <Tab>Second Tab</Tab>
      <Tab>Third Tab</Tab>
  </TabList>
  <TabPanels>
      <div>
          <TabPanel>Tab panel 1</TabPanel>
      </div>
      <div>
          <TabPanel>Tab panel 2</TabPanel>
      </div>
      <div>
          <TabPanel>Tab panel 3</TabPanel>
      </div>
  </TabPanels>
</Tabs>

Interestingly, wrapping Tab components in custom components works just fine:

// Works just fine
<Tabs>
  <TabList>
    <div>
      <Tab>FIrst Tab</Tab>
    </div>
    <div>
      <Tab>Second Tab</Tab>
    </div>
    <div>
      <Tab>Third Tab</Tab>
    </div>
  </TabList>
  <TabPanels>
    <TabPanel>Tab panel 1</TabPanel>
    <TabPanel>Tab panel 2</TabPanel>
    <TabPanel>Tab panel 3</TabPanel>
  </TabPanels>
</Tabs>

✅ Proposed solution or API

The proposed solution is to remove the coupling between TabPanels and TabPanel components and don’t require the TabPanel component to be a direct child of the TabPanels components.

I’ve looked at the source code and figured out that while the TabList and Tab components use the “descendant context” to get the index in the Tab component, the TabPanels and TabPanel components use React.Children.map and React.cloneElement to pass the index onto the TabPanel component.

I believe that the issue could be solved by using the “descendant context” for the TabPanels and TabPanel components as well…

↩️ Alternatives

An alternative is to leave the necessity of TabPanel being a direct child of TabPanels as it is now.

📝 Additional Information

I could send a PR with the proposed changes if you’re interested in implementing this feature request.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
tomdohnalcommented, Mar 22, 2021

I still believe this could be both useful and quite straightforward to implement (happy to help with it myself) 🙂

4reactions
tomdohnalcommented, Jan 13, 2021

Generally creating custom TabPanel components does not work with this approach:


function FancyTabPanel({ children }) {
  return (
    <TabPanel>
      <Heading>This is a fancy tab panel</Heading>
      {children}
    </TabPanel>
  );
}

function App() {
  return (
    <Tabs>
      <TabList>
        <Tab>FIrst Tab</Tab>
        <Tab>Second Tab</Tab>
        <Tab>Third Tab</Tab>
      </TabList>
      <TabPanels>
        <FancyTabPanel>Tab panel 1</FancyTabPanel>
        <FancyTabPanel>Tab panel 2</FancyTabPanel>
        <FancyTabPanel>Tab panel 3</FancyTabPanel>
      </TabPanels>
    </Tabs>
  );
}

(full code on https://codesandbox.io/s/bold-resonance-7s2bx?file=/src/index.tsx)

You could actually solve this by passing on the props in the FancyTabPanel onto the TabPanel component like this:

function FancyTabPanel({ children, ...props }) {
  return (
    <TabPanel {...props}>
      <Heading>This is a fancy tab panel</Heading>
      {children}
    </TabPanel>
  );
}

But IMHO that’s too much coupling to how the things are wired up in Chakra UI under the hood…

Read more comments on GitHub >

github_iconTop Results From Across the Web

material-ui wrapping Tab component with Link - Stack Overflow
I need to wrap the Tab components with a react-router Link compoenent. but considering the following code, when clicking on tabs yields no ......
Read more >
React Tabs component - Material UI - MUI
Long labels will automatically wrap on tabs. If the label is too long for the tab, it will overflow, and the text will...
Read more >
Tabs - Reach UI
The Tab and TabPanel elements are associated by their order in the tree. None of the components are empty wrappers, each is associated...
Read more >
How to Use Tabs in Material UI - Coding Beauty
textColor only allows a value of primary , secondary , or inherit . This means we can only use the color of the...
Read more >
How to build a tab component in React - LogRocket Blog
Tabs are a prevalent UI component, and it is essential to understand ... Open the App.js file, delete everything wrapped inside the div...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found