0.0.19 • Published 2 months ago

storybook-addon-contentful-preview v0.0.19

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Storybook Addon Contentful Preview

Preview your components with contentful data. Connect your storybook to contentful and live preview your components with real data.

Version Downloads/week Size Release License

screen-recording.gif

Installation

First, install the package.

npm install -D storybook-addon-contentful-preview

Then, register it as an addon in .storybook/main.{js|ts}.

export default {
  addons: ['storybook-addon-contentful-preview'],
};

Usage

Plugin

When registered, The plugin will automatically add the required decorators to your storybook. You can then use the contentfulPreview parameter to connect your storybook to contentful. The decorators will be ignored, if the contentfulPreview parameter is not set.

Manual

The decorators can also be used manually, if you want to have more control over the behavior.

Parameters

This addon contributes the following parameters to Storybook, under the contentfulPreview namespace:

NameTypeDescriptionRequired
spaceIdstringThe space id of your contentful spaceYes
accessTokenstringThe access token of your contentful spaceYes
entryIdstringThe entry id of the content you want to previewYes
localestringThe locale of the content you want to previewNo
hoststringThe host of the contentful api (defaults to api.contentful.comNo
livePreviewbooleanEnables the live preview sdkNo
argsMutatorfunctionMutator for the loaded entry data. By default, all fields will be populated as top-level args to the storyNo

Define Parameters

You can set the parameters globally, by adding the contentfulPreview parameter to the globals object of your .storybook/preview.js file (example).

import type {Preview} from "@storybook/react";

const preview: Preview = {
  parameters: {
    globals: {
      contentfulPreview: {
        space: "<space-id>",
        accessToken: "<access-token>",
        host: 'preview.contentful.com',
      }
    }
  },
};

export default preview;

You can also set the parameters per story, by adding the contentfulPreview parameter to the story (example)

export default {
  title: 'Button',
  parameters: {
    contentfulPreview: {
      spaceId: 'your-space',
    }
  }
}

The parameters set on the story level will override the global parameters.

Decorators

withContentful

When you want to preview your component with contentful data, you can use the withContentful decorator. This decorator will fetch the data from contentful and pass it to your component.

import {withContentful} from 'storybook-addon-contentful-preview';

export default {
  title: 'Button',
  decorators: [withContentful],
  parameters: {
    contentfulPreview: {
      spaceId: 'your-space',
      accessToken: 'your-access-token',
      entryId: 'your-entry-id',
    }
  }
};

withContentfulLivePreview

When you want to preview your component with contentful data, you can use the withContentfulLivePreview decorator to enable the live preview sdk. This decorator will fetch the data from contentful and pass it to your component when renderer inside the contentful UI.

import {withContentful, withLivePreview} from 'storybook-addon-contentful-preview';

export default {
  title: 'Button',
  decorators: [withLivePreview, withContentful],
  parameters: {
    contentfulPreview: {
      spaceId: 'your-space',
      accessToken: 'your-access-token',
      entryId: 'your-entry-id',
      livePreview: true,
    }
  }
};

Only tested in conjunction with the withContentful decorator

withEntryArgMutator

When you want to modify the loaded entry data, you can use the withEntryArgMutator decorator. The loaded the entry will be provided as args.contentful_entry. You can use this decorator to modify the loaded entry data before it is passed to your component to match your component props. WHen used as plugin, the default behavour is to populate all fields as top-level args to the story.

import {withContentful, withEntryArgMutator} from 'storybook-addon-contentful-preview';

export default {
  title: 'Button',
  decorators: [withEntryArgMutator, withContentful],
  parameters: {
    contentfulPreview: {
      spaceId: 'your-space',
      accessToken: 'your-access-token',
      entryId: 'your-entry-id',
      livePreview: true,
      // optionally, you can use the argsMutator param to modify the default behaviour
      argsMutator: (entry, args) => {
        return {
          ...args,
          // ...entry.fields  <-- default behaviour
          headline: entry.fields.title
        }
      }
    }
  }
};

Contentful Preview

To preview your components inside the contentful app, it's best to use the fullscreen mode of storybook.

  • Open the storybook in your browser
  • Open fullscreen mode
  • Copy URL
  • Open contentful
  • Got to settings for "Contentful Preview"
  • Paste URL into (add parameters)

contentful-preview-panel.png

The "Contentful Preview" panel will provide you the matching url for your components including all dynamic parameters.

Development

Development scripts

  • npm run start runs babel in watch mode and starts Storybook
  • npm run build build and package your addon code
0.0.19

2 months ago

0.0.18

3 months ago

0.0.10

3 months ago

0.0.11

3 months ago

0.0.12

3 months ago

0.0.13

3 months ago

0.0.14

3 months ago

0.0.15

3 months ago

0.0.9

3 months ago

0.0.16

3 months ago

0.0.17

3 months ago

0.0.8

3 months ago

0.0.7

3 months ago

0.0.6

3 months ago

0.0.5

3 months ago

0.0.4

3 months ago

0.0.3

3 months ago

0.0.2

3 months ago

0.0.1

3 months ago