Giter Site home page Giter Site logo

bitttttten / storybook-mock-date-decorator Goto Github PK

View Code? Open in Web Editor NEW
19.0 2.0 3.0 70 KB

Mock and freeze the dates in your stories.

Home Page: https://www.npmjs.com/package/storybook-mock-date-decorator

JavaScript 100.00%
storybook mock react storybook-addon storybook-addons storybook-ui storybookjs mockdate date datetime

storybook-mock-date-decorator's Introduction

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

to help freeze time or mock dates in your stories


styled with prettier Github release version Commits since release npm release version

Install

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 if you are on a different version or a new one has come out since the last update.

Storybook 8

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;
// 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

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

export let decorators = [mockDateDecorator];

Storybook 6

// .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:

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:

import { Meta } from "@storybook/react"
import { YourComponent } from "./your-component"

export default {
	title: "YourComponent",
	component: YourComponent,
} as Meta

export function Default() {
    return <div>hello world at {new Date().toLocaleString()}</div>
}

export function WithMockedDate() {
    return <div>hello world! with mocked date of March 10th at {new Date().toLocaleString()}</div>
}
WithMockedDate.parameters = {
    date: new Date("March 10, 2021 10:00:00"),
}

storybook-mock-date-decorator's People

Contributors

bitttttten avatar fernandopasik avatar jipis avatar kwintijn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

storybook-mock-date-decorator's Issues

@storybook/preview-api not found

If you are running into this issue, please try and import from the legacy export:

Storybook 7

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

export let decorators = [mockDateDecorator];

Storybook 6

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

addDecorator(mockDateDecorator);

@storybook/react has no member addDecorator

Hi there,

In you documentation, it says to import the react decorator: import { addDecorator } from '@storybook/react';. I get the error @storybook/react has no member addDecorator. I am on storybook version 7.

Thanks!

Types not found

error TS7016: Could not find a declaration file for module 'storybook-mock-date-decorator'. '/some/path/to/app/node_modules/storybook-mock-date-decorator/index.js' implicitly has an 'any' type.
Try npm i --save-dev @types/storybook-mock-date-decorator if it exists or add a new declaration (.d.ts) file containing declare module 'storybook-mock-date-decorator';

Maybe it needs a types declaration in package.json?

Incorrect export name in decorator.js

index.js tries to import { decorator } from "./decorator";

but in decorator.js, export const generator = ...

Which is probably why 2.0.1 crashes and burns when you try to run a story using it.

Depends on old version of @storybook/addons

"@storybook/addons": "^6.4.19",

While attempting an upgrade to Storybook 7.x, the automigrate/upgrade helper complains:

β”‚β”‚   Critical: The following dependencies are duplicated and WILL cause unexpected behavior:                                                  
β”‚β”‚ 
β”‚β”‚   @storybook/core-events:                                                                                                                   
β”‚β”‚   7.6.9, 6.5.16                                                                                                                             
||                                                                                                                                   
β”‚β”‚   Attention: The following dependencies are duplicated which might cause unexpected behavior:                                               
β”‚β”‚                                                                                                                                            
β”‚β”‚   @storybook/channels:                                                                                                                      
β”‚β”‚   7.6.9, 6.5.16

Running yarn why indicates this is because of the dependency in this package:

=> Found "@storybook/addons#@storybook/[email protected]"
info This module exists because "_project_#project-name#storybook-mock-date-decorator#@storybook#addons" depends on it.

=> Found "@storybook/addons#@storybook/[email protected]"
info This module exists because "_project_#project-name#storybook-mock-date-decorator#@storybook#addons" depends on it.

Maybe it should instead be a peer-dependency?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.