Docs
Storybook Docs

Viewport

Watch a video tutorial

The Viewport toolbar item allows you to adjust the dimensions of the iframe your story is rendered in. It makes it easy to develop responsive UIs.

Configuration

Out of the box, the Viewport addon offers you a standard set of viewports that you can use.

If you want to change the default set of viewports, you can set the global parameters.viewport parameter in your .storybook/preview.js:

.storybook/preview.js
export const parameters = {
  viewport: {
    viewports: newViewports, // newViewports would be an ViewportMap. (see below for examples)
    defaultViewport: 'someDefault',
  },
}

The viewport global can take an object with the following keys:

FieldTypeDescriptionDefault Value
defaultViewportStringSets the default viewport'responsive'
disableBooleanDisables the viewportN/A
viewportsObjectThe configuration object for the viewport{}

The viewports object needs the following keys:

FieldTypeDescriptionExample values
nameStringName for the viewport'Responsive'
stylesObjectSets Inline styles to be applied to the story{width:0,height:0}
typeStringType of the device (e.g. desktop, mobile, or tablet)desktop

Use a detailed set of devices

By default, Storybook uses a minimal set of viewports to get you started. But you're not restricted to these. The addon offers a more granular list of devices that you can use.

Change your .storybook/preview.js to the following:

.storybook/preview.js
import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport';
 
export const parameters = {
  viewport: {
    viewports: INITIAL_VIEWPORTS,
  },
};

Start your Storybook, and you'll see the new devices that you can use.

See here the complete list of devices and their configurations.

Add new devices

If you have either a specific viewport or a list of viewports that you need to use, you can modify your .storybook/preview.js file and include them:

.storybook/preview.js
const customViewports = {
  kindleFire2: {
    name: 'Kindle Fire 2',
    styles: {
      width: '600px',
      height: '963px',
    },
  },
  kindleFireHD: {
    name: 'Kindle Fire HD',
    styles: {
      width: '533px',
      height: '801px',
    },
  },
};

Make the following change to use them in your Storybook:

.storybook/preview.js
export const parameters = {
  viewport: { viewports: customViewports },
};

Once you start Storybook, you'll see your new viewports and devices.

If you need, you can also add these two to another list of viewports.

For instance, if you want to use these two with the minimal set of viewports, you can do it like so:

.storybook/preview.js
import { MINIMAL_VIEWPORTS} from '@storybook/addon-viewport';
 
const customViewports = {
  kindleFire2: {
    name: 'Kindle Fire 2',
    styles: {
      width: '600px',
      height: '963px',
    },
  },
  kindleFireHD: {
    name: 'Kindle Fire HD',
    styles: {
      width: '533px',
      height: '801px',
    },
  },
};
 
export const parameters = {
  viewport: {
    viewports: {
       ...MINIMAL_VIEWPORTS,
      ...customViewports,
    },
  },
};

Both viewports (Kindle Fire 2 and Kindle Fire HD) will feature in the list of devices by merging them into the MINIMAL_VIEWPORTS.

Configuring per component or story

There are cases where it's not practical for you to use a specific visual viewport on a global scale, and you need it to adjust it to an individual story.

Update your story through parameters to include your viewports at a component level or for a specific story:

MyComponent.stories.ts|tsx
import React from 'react';
 
import { ComponentStory, ComponentMeta } from '@storybook/react';
 
import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport';
 
import { MyComponent } from './MyComponent';
 
export default {
  /* ๐Ÿ‘‡ The title prop is optional.
  * See https://storybook.js.org/docs/6/configure#configure-story-loading
  * to learn how to generate automatic titles
  */
  title: 'MyComponent',
  component: MyComponent,
  parameters: {
    //๐Ÿ‘‡ The viewports object from the Essentials addon
    viewport: {
      //๐Ÿ‘‡ The viewports you want to use
      viewports: INITIAL_VIEWPORTS,
      //๐Ÿ‘‡ Your own default viewport
      defaultViewport: 'iphone6',
    },
  },
} as ComponentMeta<typeof MyComponent>;
 
export const MyStory: ComponentStory<typeof MyComponent> = () => <MyComponent />;
MyStory.parameters = {
  viewport: {
    defaultViewport: 'iphonex',
  },
};

Keyboard shortcuts

  • Previous viewport: shift + v
  • Next viewport: v
  • Reset viewport: alt + v

If you need, you can edit them on the shortcuts page.