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

Addon-docs: Define children for DocsContainer #19437

Merged
merged 2 commits into from
Oct 18, 2022

Conversation

mihkeleidast
Copy link
Contributor

Issue: N/A

What I did

Explicitly defined children prop for DocsContainer props. In React 18 types, children is not automatically available in FunctionComponent, instead it needs to be explictly added. When trying to override DocsContainer with a component written in TypeScript, with React 18 types installed, it currently throws an error because the internal DocsContainer does not allow children.

Example code that should work without those @ts-expect-error comments:

import { DocsContainer as BaseContainer, DocsContainerProps } from '@storybook/addon-docs';
import { themes } from '@storybook/theming';
import React from 'react';

import { ThemeProvider } from './theme_provider';
import { useStoredTheme } from './use_stored_theme';

/* @ts-expect-error BaseContainer does not allow children */
export const DocsContainer: React.FunctionComponent<DocsContainerProps> = ({ children, context }) => {
  const { storyById } = context;
  const [currentTheme] = useStoredTheme();

  return (
    <ThemeProvider>
      {/* @ts-expect-error BaseContainer does not allow children */}
      <BaseContainer
        context={{
          ...context,
          storyById: (id) => {
            const storyContext = storyById(id);
            return {
              ...storyContext,
              parameters: {
                ...storyContext?.parameters,
                docs: {
                  ...storyContext?.parameters?.docs,
                  theme: currentTheme.includes('dark') ? themes.dark : themes.light,
                },
              },
            };
          },
        }}>
        {children}
      </BaseContainer>
    </ThemeProvider>
  );
};

Would be nice if this got backported to SB 6.5 as well.

How to test

  • Is this testable with Jest or Chromatic screenshots?
  • Does this need a new example in the kitchen sink apps?
  • Does this need an update to the documentation?

If your answer is yes to any of these, please make sure to include it in your PR.

@shilman shilman changed the title fix: define children for DocsContainer Addon-docs: Define children for DocsContainer Oct 17, 2022
@shilman shilman added typescript maintenance User-facing maintenance tasks patch:yes Bugfix & documentation PR that need to be picked to main branch and removed feature request labels Oct 17, 2022
Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!! Thanks so much @mihkeleidast 🙏

@shilman shilman merged commit 90c58e7 into storybookjs:next Oct 18, 2022
@shilman shilman added the patch:done Patch/release PRs already cherry-picked to main/release branch label Jun 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addon: docs maintenance User-facing maintenance tasks patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants