Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bitttttten/storybook-mock-date-decorator

Mock and freeze the dates in your stories.
https://github.com/bitttttten/storybook-mock-date-decorator

date datetime mock mockdate react storybook storybook-addon storybook-addons storybook-ui storybookjs

Last synced: 15 days ago
JSON representation

Mock and freeze the dates in your stories.

Awesome Lists containing this project

README

        

✨⏰πŸ₯Ά storybook-mock-date-decorator πŸ₯Άβ°βœ¨


to help freeze time or mock dates in your stories





[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![Github release version](https://img.shields.io/github/tag/bitttttten/storybook-mock-date-decorator.svg)](https://github.com/bitttttten/storybook-mock-date-decorator/releases)
[![Commits since release](https://img.shields.io/github/commits-since/bitttttten/storybook-mock-date-decorator/v2.0.5.svg)](https://github.com/bitttttten/storybook-mock-date-decorator/compare/v2.0.5...main)
[![npm release version](https://img.shields.io/npm/v/storybook-mock-date-decorator.svg)](https://www.npmjs.com/package/storybook-mock-date-decorator)

## Install

```sh
npm i storybook-mock-date-decorator
```

## API

Once the decorator has been added to your storybook, you can configure the date with the parameter name `date` inside your stories.

## Usage

The syntax here is valid for each respective Storybook version. You may want to check the [official docs](https://storybook.js.org/basics/writing-stories/) if you are on a different version or a new one has come out since the last update.

### Storybook 8

```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),
}
};
```

### Storybook 7

```js
// .storybook/preview.js
import { mockDateDecorator } from "storybook-mock-date-decorator/legacy";

export let decorators = [mockDateDecorator];
```

### Storybook 6

```js
// .storybook/preview.js
import { addDecorator } from "@storybook/react";
import { mockDateDecorator } from "storybook-mock-date-decorator/legacy";

addDecorator(mockDateDecorator);
```

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

hello world at {new Date().toLocaleString()}

}

export function WithMockedDate() {
return

hello world! with mocked date of March 10th at {new Date().toLocaleString()}

}
WithMockedDate.parameters = {
date: new Date("March 10, 2021 10:00:00"),
}

```