How to use Remix Link components in Storybook #6273
-
Hello. I am building UIs using the Link component provided by What I have tried is described below. Just adding Link.stories.tsx will result in throwing an error For the Link component of So I tried to use export const decorators: Decorator[] = [
(Story: StoryFn) => {
const route = [
{
path: '/*', element: <Story />, element: <Story />, element: <StoryFn
element: <Story />, }
}, }
]
const RemixStub = unstable_createRemixStub(route)
return <RemixStub initialEntries={['/']} />
}, }
] This way, something similar to Remix's initial error text is now displayed on the Storybook, but as for the useHref error text that is thrown, it is unchanged from the first time. Do you have any ideas? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 18 replies
-
Add a decorator to your storybook preview file
|
Beta Was this translation helpful? Give feedback.
-
I think there is room to argument that UI components such as those added to storybook should not depend on a framework like remix, but should use react-router. |
Beta Was this translation helpful? Give feedback.
-
I don't know if it will helps in any way but on my side I did it this way : // .storybook/preview.tsx
import { unstable_createRemixStub } from '@remix-run/testing';
import { Preview } from '@storybook/react';
import '../app/tailwind.css';
const preview: Preview = {
decorators: [
Story => {
const RemixStub = unstable_createRemixStub([
{
path: '/*',
element: <Story />,
},
]);
return <div><RemixStub initialEntries={ ['/']} /></div>;
},
],
};
export default preview; And then // Button.stories.tsx
import type { Meta, StoryObj } from '@storybook/react';
import Button from '../../../app/component/Button';
export default {
title: 'Composants/Button/Primary',
component: Button,
} as Meta;
type Story = StoryObj<typeof Button>;
export const Plain: Story = {
args: {
label: 'Button',
},
};
export const Loading: Story = {
args: {
...Plain.args,
loading: true,
},
};
👇 this one used to have the error as it's a <Link />
export const Link: Story = {
args: {
...Plain.args,
to: 'https://www.google.fr',
},
}; hope it will help ! |
Beta Was this translation helpful? Give feedback.
-
I attempted the above solutions but was getting all kinds of Typescript errors like this:
Not saying this is correct, but this is the only way I could satisfy the transpiler: import type { Meta, StoryObj } from "@storybook/react";
import { CollectionCard } from "../app/theme/components/CollectionCard/CollectionCard";
import { MOCK_COLLECTION } from "./mocks/mocks";
import { unstable_createRemixStub as createRemixStub } from "@remix-run/testing";
const meta = {
title: "Example/CollectionCard",
component: CollectionCard,
decorators: [
(story) => {
const remixStub = createRemixStub([
{
path: "/*",
action: () => ({ redirect: "/" }),
loader: () => ({ redirect: "/" }),
Component: () => story(),
},
]);
return remixStub({ initialEntries: ["/"] });
},
],
parameters: {
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/react/configure/story-layout
layout: "centered",
},
tags: ["autodocs"],
} satisfies Meta<typeof CollectionCard>; |
Beta Was this translation helpful? Give feedback.
-
I was having the same issue with |
Beta Was this translation helpful? Give feedback.
-
If you are using storybook 8, you need to migrate to using this package from storybook. Also, be sure to be on the same version of any package prefixed with @remix-run/ Here is an example:
Also, if you are adding withRouter to the decorator |
Beta Was this translation helpful? Give feedback.
I'm on Remix 1.16.0 and Storybook 7.0.11, the following code works for me.