Skip to content

Cannot extend Tabs and TabItem components #11672

Closed

Description

@Ethan-Arrowood

Have you read the Contributing Guidelines on issues?

Prerequisites

Description

I'm using a common set of Tabs and TabItem instances over-and-over again throughout multiple mdx pages. Instead of constantly writing out the groupId, value, and label properties over-and-over again, I tried create custom React components like:

import React, { ReactNode } from 'react';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

export const InstallationGroup = ({ children }: { children: ReactNode }) => (
  <Tabs groupId="installation">
    {children}
  </Tabs>
);

export const Local = ({ children }: { children: ReactNode }) => (
  <TabItem value="local" label="Local Installation">
    {children}
  </TabItem>
);

export const PaaS = ({ children }: { children: ReactNode }) => (
  <TabItem value="paas" label="Platform as a Service">
    {children}
  </TabItem>
);

And then in my MDX pages:

import { InstallationGroup, Local, PaaS } from '../../src/components/installation-tabs';

<InstallationGroup>
	<Local>

Local install content.

	</Local>
	<PaaS>

PaaS install content.

	</PaaS>
</InstallationGroup>

This builds fine, but when I navigate to the page in my browser I get this error:

Docusaurus error: Bad <Tabs> child <Local>: all children of the <Tabs> component should be <TabItem>, and every <TabItem> should have a unique "value" prop.

I feel like what I'm doing should work fine, but if not, is there another way I can achieve this?

Reproducible demo

https://codesandbox.io/p/devbox/clever-carlos-ztj56n

Steps to reproduce

I tried getting this to work in codesandbox but its stuck trying to bring up the localhost. I recommend just copying the files I shared above to demo the issue.

Expected behavior

I expect to be able to extend these react components for reusability.

Actual behavior

Error as described.

Your environment

Self-service

  • I'd be willing to fix this bug myself.

Activity

  1. added
    bug
    status: needs triage
    on Jan 16, 2026
  2. Patilpradnesh commented on Jan 19, 2026

    @Patilpradnesh

    Hi, I’m a first-time contributor and I’ve been working on a small improvement for this issue.

    I updated the Tabs error message to clarify that wrapper/custom components around <TabItem> are not supported, and updated the related test.

    Before opening a PR, I just wanted to confirm that this approach is acceptable.

    Thank you!

  3. Ethan-Arrowood commented on Jan 19, 2026

    @Ethan-Arrowood
    Author

    Why isn't it possible?

  4. Patilpradnesh commented on Jan 20, 2026

    @Patilpradnesh

    Thank you for the question — I’m still learning the internals here, so please correct me if my understanding is wrong.
    From what I observed in the current implementation, <Tabs> validates its children by checking the direct React element type (child.type) to ensure it is a <TabItem>.
    When a wrapper component like <Local> is used, the direct child is <Local>, not <TabItem>, even though <Local> returns <TabItem> when rendered. Because of this, the current validation logic seems to reject it by design.
    So my understanding is that this is not a limitation of React itself, but of how <Tabs> currently inspects its children.

    That’s why I thought improving the error message and documentation might be a safe first step — but I’d really appreciate your guidance if you think a better approach is possible here.

  5. Ethan-Arrowood commented on Jan 20, 2026

    @Ethan-Arrowood
    Author

    I'm not a maintainer so it's not my call, but from my understanding of react this check may be too aggressive and this pattern should be fine.

    Am I wrong and is this validation correct and necessary?

    Or is there better way to validate that would enable this pattern?

    --

    Aside, there's no harm in opening a PR (or multiple) and iterating there. At least it demonstrates some sort of solutions. You should always feel empowered to open a PR even if it doesn't get approved and merged.

  6. Patilpradnesh commented on Jan 20, 2026

    @Patilpradnesh

    Thanks for the thoughtful input — that’s really helpful.

    I agree the validation may be too strict, and I appreciate the perspective. I’ve gone ahead and opened a PR so we can iterate and discuss the best approach there.

    Thanks again for the encouragement!

  7. lab596 commented on Feb 2, 2026

    @lab596

    To the best of my understanding I think this is in place to enable quick Static Site Generation? An explicit check like this enables understanding of which tab is active during rendering isntead of a deep render which would require an execute of every child component to verify the metadata. Not exactly sure but on intial inspection that what it looks like?

  8. slorber commented on Feb 5, 2026

    @slorber
    Collaborator

    I agree that the check is too aggressive, and wrapping our components should be allowed.

    This historical check (#5674) should be reworked so that it allows custom tabs.

    // A very rough duck type, but good enough to guard against mistakes while
    // allowing customization
    function isTabItem(
      comp: ReactElement<unknown>,
    ): comp is ReactElement<TabItemProps> {
      const {props} = comp;
      return !!props && typeof props === 'object' && 'value' in props;
    }

    I think this heuristic here is wrong and fails for your case because your tab wrappers do not forward a value prop. Otherwise it would work.


    Ultimately, what we want to do here is to prevent typo errors and misusage.

    Unfortunately, TypeScript doesn't have "render types" like Flow and it's hard to prevent mistakes at compile time in MDX anyway (these files are not typechecked), so we implemented a poor man's runtime type check.

    If someone want to implement a PR that makes the above TabItem wrapper work, but at the same time still prevent usage mistakes, please do so (we have unit tests to cover that component, by the way, and new tests must be added to better support this valid use case).

    If no proper solution can be found, I'd rather remove the error entirely so that it doesn't prevent such custom composition/wrapper patterns

  9. removed
    status: needs triage
    on Feb 5, 2026
  10. slorber commented on Feb 12, 2026

    @slorber
    Collaborator

    The problem is more complex to solve than I initially thought, and would require a quite important refactor of the tabs component.

    The error being thrown is one thing, but the main problem is that the Tabss implementation assumes that TabItem React elements all have tabItem.props.value accessible, which is not the case with wrappers.

    I tried removing the failfast error check, and it still fails later in the process because the assumptions we rely on break.


    The problem with our API implementation is that we have a single TabItem component that is used to provide both the tab item button and the tab item panel/content. Somehow, the parent tab component has to extract all the tab item buttons from its children, which doesn't work if items are wrapped

  11. Josh-Cena commented on Feb 12, 2026

    @Josh-Cena
    Collaborator

    The children list of Tabs is really just a convenience alternative to

    <Tabs
      items={[
        {
          value: "",
          label: "",
          content: <div>...</div>,
        },
        // ...
      ]}
    />

    i.e. you shouldn't see each TabItem as anything more than sugar for an object literal. If you want to wrap TabItem, you also have to wrap Tabs to tell Tabs how to extract these values.

    function InstallationTabs({ children }) {
      return (
        <Tabs>
          <TabItem value="local" label="Local Installation">
            {children[0].props.children}
          </TabItem>
          <TabItem value="paas" label="Platform as a Service">
            {children[1].props.children}
          </TabItem>
        </Tabs>
      );
    }

    I don't think we can do significantly better here; TabItem doesn't actually render anything by itself. It's just a data carrier of its props.

  12. slorber commented on Feb 12, 2026

    @slorber
    Collaborator

    @Ethan-Arrowood, in #11733

    I'm adding a React context to solve the above limitation.

    Maybe not your ideal API, but the following will work if you provide an explicit list of <Tab values>.

    export function Local() {
      return (
        <TabItem value="local" {...props}>
          Local content
        </TabItem>
      );
    }
    
    export function PaaS() {
      return (
        <TabItem value="paas" >
          PaaS content
        </TabItem>
      );
    }
    
    export function InstallationTabs() {
      return (
        <Tabs
          groupId="installation"
          // ⚠️ you must provide that prop
          values={[
            {value: 'local', label: 'Local Installation'},
            {value: 'paas', label: 'Platform as a Service'},
          ]}>
          <Local />
          <PaaS />
        </Tabs>
      );
    }
    
    <InstallationTabs />

    You either have to:

    • Use children with values that we can extract
    • Pass the values explicitly as props
  13. slorber commented on Feb 12, 2026

    @slorber
    Collaborator
    function InstallationTabs({ children }) {
      return (
        <Tabs>
          <TabItem value="local" label="Local Installation">
            {children[0].props.children}
          </TabItem>
          <TabItem value="paas" label="Platform as a Service">
            {children[1].props.children}
          </TabItem>
        </Tabs>
      );
    }

    Agree that this solution also works. You can also pass the "paas" and "local" tabs as distinct props to make things clearer and less order-dependent.

    I'm still going to land the PR because using context still makes this a little bit more flexible, and I also took the opportunity to cleanup the code.

  14. Ethan-Arrowood commented on Feb 17, 2026

    @Ethan-Arrowood
    Author

    Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions