Storybook Docs

Viewport

Watch a video tutorial on the Storybook channel

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:

Code Snippets
Oh no! We could not find the code you are looking for.
It would be great if you could report an issue on Github if you see that message.

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

| Field | Type | Description | Default Value | | ---------------------- | ------- | --------------------------------------------------------- | -------------- | | defaultViewport | String | Sets the default viewport | 'responsive' | | defaultOrientation | String | Sets the default orientation (e.g. portrait or landscape) | 'portrait' | | disable | Boolean | Disables the viewport | N/A | | viewports | Object | The configuration objects for the viewport | {} |

The viewports object needs the following keys:

| Field | Type | Description | Example values | | ---------- | ------ | ----------------------------------------------------- | ----------------------- | | name | String | Name for the viewport | 'Responsive' | | styles | Object | Sets Inline styles to be applied to the story | { width:0, height:0 } | | type | String | Type of the device (e.g., desktop, mobile, or tablet) | desktop |

Use a detailed set of devices

The Viewport addon includes a selection of devices that you can use to test your components. Listed below are the available devices and examples of how to use them.

| Device | Description | Dimensions
(w×h, px) | | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | | iPhone 5 | Configures the iPhone 5 as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphone5', }}, | 320 × 568 | | iPhone 6 | Enables the iPhone 6 to be used with the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphone6', }}, | 375 × 667 | | iPhone 6 Plus | Includes the iPhone 6 Plus as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphone6p', }}, | 414 × 736 | | iPhone 8 Plus | Sets the iPhone 8 Plus as a device to be used by the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphone8p', }}, | 414 × 736 | | iPhone X | Configures the iPhone X as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphonex', }}, | 375 × 812 | | iPhone XR | Includes the iPhone XR as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphonexr', }}, | 414 × 896 | | iPhone XS Max | Sets the iPhone XS Max as a device to be used by the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphonexsmax', }}, | 414 × 896 | | iPhone SE (2nd generation) | Configures the iPhone SE (2nd generation) as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphonese2', }}, | 375 × 667 | | iPhone 12 mini | Enables the iPhone 12 Mini to be used with the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphone12mini', }}, | 375 × 812 | | iPhone 12 | Includes the iPhone 12 as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphone12', }}, | 390 × 844 | | iPhone 12 Pro Max | Configures the iPhone 12 Pro Max as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphone12promax', }}, | 428 × 926 | | iPhone SE 3rd generation | Enables the iPhone SE (3rd generation) to be used with the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphoneSE3', }}, | 375 × 667 | | iPhone 13 | Includes the iPhone 13 as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphone13', }}, | 390 × 844 | | iPhone 13 Pro | Enables the iPhone 13 Pro to be used with the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphone13pro', }}, | 390 × 844 | | iPhone 13 Pro Max | Configures the iPhone 13 Pro Max as a device to be used by the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphone13promax', }}, | 428 × 926 | | iPhone 14 | Enables the iPhone 14 to be used with the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphone14', }}, | 390 × 844 | | iPhone 14 Pro | Includes the iPhone 14 Pro as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphone14pro', }}, | 393 × 852 | | iPhone 14 Pro Max | Sets the iPhone 14 Pro Max as a device to be used by the Viewport addon.
parameters: { viewport: { defaultViewport: 'iphone14promax', }}, | 430 × 932 | | Galaxy S5 | Configures the Galaxy S5 as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'galaxys5', }}, | 360 × 640 | | Galaxy S9 | Enables the Galaxy S9 to be used with the Viewport addon.
parameters: { viewport: { defaultViewport: 'galaxys9', }}, | 360 × 740 | | Nexus 5X | Includes the Nexus 5x as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'nexus5x', }}, | 412 × 668 | | Nexus 6P | Sets the Nexus 6P as a device to be used by the Viewport addon.
parameters: { viewport: { defaultViewport: 'nexus6p', }}, | 412 × 732 | | Pixel | Configures the Pixel as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'pixel', }}, | 540 × 960 | | Pixel XL | Enables the Pixel XL to be used with the Viewport addon.
parameters: { viewport: { defaultViewport: 'pixelxl', }}, | 720 × 1280 | | Small mobile | Enabled by default.
Configures a small form factor generic mobile device to be used by the Viewport addon.
parameters: { viewport: { defaultViewport: 'mobile1', }}, | 320 × 568 | | Large mobile | Enabled by default.
Configures a large form factor mobile device to be used by the Viewport addon.
parameters: { viewport: { defaultViewport: 'mobile2', }}, | 414 × 896 | | iPad | Includes the iPad as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'ipad', }}, | 768 × 1024 | | iPad Pro 10.5-in | Enables the iPad Pro (10.5-inch) to be used with the Viewport addon.
parameters: { viewport: { defaultViewport: 'ipad10p', }}, | 834 × 112 | | iPad Pro 11-in | Configures the iPad Pro (11-inch) as a device for the Viewport addon.
parameters: { viewport: { defaultViewport: 'ipad11p', }}, | 834 × 1194 | | iPad Pro 12.9-in | Sets the iPad Pro (12.9-inch) as a device to be used by the Viewport addon.
parameters: { viewport: { defaultViewport: 'ipad12p', }}, | 1024 × 1366 | | Tablet | Enabled by default.
Configures a standard form factor tablet device to be used by the Viewport addon.
parameters: { viewport: { defaultViewport: 'tablet', }}, | 834 × 1112 |

Code Snippets
Oh no! We could not find the code you are looking for.
It would be great if you could report an issue on Github if you see that message.

The generic devices are enabled by default as they are the most common for testing responsive UIs. You don't need to include them in your configuration if you want to use them. For the implementation details, see the source code for the Viewport addon.

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:

Code Snippets
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 changes to use them in your Storybook:

Code Snippets
Oh no! We could not find the code you are looking for.
It would be great if you could report an issue on Github if you see that message.

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

You can add these two to another list of viewports if needed.

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

Code Snippets
Oh no! We could not find the code you are looking for.
It would be great if you could report an issue on Github if you see that message.

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

In some cases, it's not practical for you to use a specific visual viewport on a global scale, and you need 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:

Code Snippets
Oh no! We could not find the code you are looking for.
It would be great if you could report an issue on Github if you see that message.

Keyboard shortcuts

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

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

API

Parameters

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

defaultOrientation

Type: 'portrait' | 'landscape'

Default: 'portrait'

Specifies the default orientation used when viewing a story.

defaultViewport

Type: string

Specifies the default viewport used when viewing a story. Must match a key in the viewports object.

disable

Type: boolean

Disable this addon's behavior. If you wish to disable this addon for the entire Storybook, you should do so when registering addon-essentials. See the essential addon's docs for more information.

This parameter is most useful to allow overriding at more specific levels. For example, if this parameter is set to true at the project level, it could then be re-enabled by setting it to false at the meta (component) or story level.

viewports

Type:

{
  [key: string]: {
    name: string,
    styles: { height: string, width: string },
    type: 'desktop' | 'mobile' | 'tablet',
  },
};

Specify the available viewports. The key is used to reference the viewport and the value is an object to define it. See usage example, above. The width and height values must include the unit, e.g. '320px'.

Exports

This addon contributes the following exports to Storybook:

import { INITIAL_VIEWPORTS, MINIMAL_VIEWPORTS } from '@storybook/addon-viewport';

INITIAL_VIEWPORTS

Type: object

The default set of viewports provided by the Viewport addon, listed above.

MINIMAL_VIEWPORTS

Type: object

A minimal set of viewports provided by the Viewport addon, listed below:

| Device | Description | Dimensions
(w×h, px) | | ------------ | -------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | | Small mobile | A small mobile device size
parameters: { viewport: { defaultViewport: 'mobile1', }}, | 320 × 568 | | Large mobile | A large mobile device size
parameters: { viewport: { defaultViewport: 'mobile2', }}, | 414 × 896 | | Tablet | A tablet device size
parameters: { viewport: { defaultViewport: 'tablet', }}, | 834 × 1112 |

Join the community

6,378 developers and counting
Open source software
Maintained by
Chromatic
Special thanks to Netlify and CircleCi