Storybook MockDate decorator
npm install storybook-mock-date-decorator



``sh`
npm i storybook-mock-date-decorator@3 -D
`sh`
npm i storybook-mock-date-decorator@2 -D
Note: If you're using Storybook 6, 7, or 8, you must use the /legacy import path. Please refer to the v2.x README for complete usage instructions.
Once the decorator has been added to your storybook, you can configure the date with the parameter name date inside your stories.
`js
import { mockDateDecorator } from "storybook-mock-date-decorator";
/* @type { import('@storybook/react').Preview } /
const preview = {
decorators: [mockDateDecorator],
parameters: {
controls: {
matchers: {
color: /(background|color)$/i,
},
},
},
};
export default preview;
`
`js
// stories/Button.stories.js
export default {
title: 'Example/Button',
component: Button,
parameters: {
date: new Date(1999, 10, 24),
},
};
export const Primary = {
args: {
primary: true,
label: 'Button',
},
parameters: {
date: new Date(2021, 1, 1),
}
};
`
Then inside your storybook, you can use the following code to mock/freeze the date for all stories of a component:
`js
import { Meta } from "@storybook/react"
import { YourComponent } from "./your-component"
export default {
title: "YourComponent",
component: YourComponent,
parameters: {
date: new Date("March 10, 2021 10:00:00"),
},
} as Meta
`
Or you can mock/freeze the date for a specific story:
`js
import { Meta } from "@storybook/react"
import { YourComponent } from "./your-component"
export default {
title: "YourComponent",
component: YourComponent,
} as Meta
export function Default() {
return
export function WithMockedDate() {
return
`
| Storybook Version | Package Version | Import Path | Documentation |
|-------------------|-----------------|-------------|---------------|
| 9.0+ | @3 | storybook-mock-date-decorator | This README |@2
| 8.x | | storybook-mock-date-decorator/legacy | v2.x README |@2
| 6.x, 7.x | | storybook-mock-date-decorator/legacy | v2.x README |
Important: For Storybook 6, 7, and 8, you must use the /legacy` import path and follow the setup instructions in the v2.x documentation.