Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
How to preview component with react portal using storybook.js

I implemented a Popper component using popper.js, and I used React's Portal to render popper element.

And now I want to preview my popper component, but It fails to create portal on storybook.js

Here's my approach.

.storybook/preview.js

import { ThemeProvider } from 'emotion-theming';
import { Global } from '@emotion/react';
import { theme, normalize } from 'custom-theme';

export const parameters = {
  actions: { argTypesRegex: '^on[A-Z].*' },
  controls: {
    matchers: {
      color: /(background|color)$/i,
      date: /Date$/,
    },
  },
};

export const decorators = [
  Story => (
    <>
      <ThemeProvider theme={theme}>
        <Global styles={normalize} />
        {Story()}
        <div id="popper-root"></div>
      </ThemeProvider>
    </>
  ),
];

Portal.tsx

import { ReactNode, useMemo } from 'react';
import { createPortal } from 'react-dom';

interface IPortal {
  children: ReactNode;
  portalId: string;
}

const Portal = ({ children, portalId }: IPortal) => {
  const portalElement = useMemo<HTMLElement | null>(
    () => document.getElementById(portalId),
    [portalId],
  );

  if (!portalElement) {
    throw new Error(`Undefined Portal Id : ${portalId}`);
  }

  return createPortal(children, portalElement);
};

export default Portal;

Capture of Error Message

Error Image

Is there any way to show component using react portal on storybook? If not, is there other way to preview it other than testing it on react project?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I was able to do a workaround on this by adding <div id="popper-root"></div> to my ComponentStory Template.

But One thing is if I render story with portal open, it cannot find popper-root.

about 4 years ago · Juan Pablo Isaza Relatório

0

I have got a root element id in storybook to use for the portal. And I have used it for the React/Next.js portal. Now it's working well.

  • Storybook root element <div id="docs-root"></div>
  • portal element selector document.getElementById('docs-root)
about 4 years ago · Juan Pablo Isaza Relatório

0

Add preview-body.html, next to preview.js, with this:

<div id="popper-root"></div>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda