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

How to use with Storybook? #175

Open
jdahdah opened this issue Mar 18, 2021 · 3 comments
Open

How to use with Storybook? #175

jdahdah opened this issue Mar 18, 2021 · 3 comments

Comments

@jdahdah
Copy link

jdahdah commented Mar 18, 2021

Does anyone by chance have a working example of how to use this with Storybook? Some of my components use <FormattedMessage /> and <Link /> from gatsby-plugin-intl, and I'm not sure how to get those working within the Storybook context.

I tried adding context providers like <IntlProvider /> and <IntlContextProvider /> to a story, but they rely on pageContext, which is not available outside of the gatsby runtime environment.

@bud-mo
Copy link

bud-mo commented Sep 10, 2021

Hi @jdahdah I got the <FormattedMessage /> and <Link /> components working.

file: .storybook/wrap-with-provider.js

import React from 'react';
import { IntlProvider, IntlContextProvider} from 'gatsby-plugin-intl';
import messages from '../src/intl/en.json';
import { createIntl, createIntlCache } from 'react-intl';

const intlCache = createIntlCache();
const intl = createIntl(
  {
    defaultLanguage: 'en',
    language: 'en',
    messages: messages,
  },
  intlCache,
);

export default function WrapWithProvider(Story) {
  return (
    <IntlProvider
      locale={intl.language}
      defaultLocale={intl.defaultLanguage}
      messages={intl.messages}
    >
      <IntlContextProvider
        value={intl}
      >
          <Story />
      </IntlContextProvider>
    </IntlProvider>
  );
}

File: .storybook/preview.js

import WrapWithProvider from './wrap-with-provider';

export const decorators = [
  WrapWithProvider,
];

@bud-mo
Copy link

bud-mo commented Sep 10, 2021

@jdahdah More or less, the wrapPage function from gatsby-plugin-intl does the same thing.

@jdahdah
Copy link
Author

jdahdah commented Sep 10, 2021

@bud-mo Amazing, I will give this a shot next week and let you know how it goes. Thank you for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants