Join live session: Test your components in browser with Storybook Test
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.

globals API

This addon has been updated to use the globals API when the viewportStoryGlobals feature flag is enabled. With globals, when you specify a viewport for a story, it cannot be overridden from the toolbar, which ensures a consistent experience while navigating between stories. This will be the default behavior and API in Storybook 9.

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 configure your own viewports with the viewport parameter in your .storybook/preview.js.

You can define the available viewports using the viewports property and set the initial viewport using the defaultViewport property:

.storybook/preview.ts
// Replace your-renderer with the renderer you are using (e.g., react, vue3, angular, etc.)
import { Preview } from '@storybook/your-renderer';
import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport';
 
const preview: Preview = {
  parameters: {
    viewport: {
      viewports: INITIAL_VIEWPORTS,
      defaultViewport: 'ipad',
    },
  },
};
 
export default preview;

With the globals API

When using the globals API, you must define the available viewports using the options property. The initial viewport can be set using the initialGlobals property, which accepts the same object properties as the globals for this addon.

.storybook/preview.ts
// Replace your-renderer with the renderer you are using (e.g., react, vue3, angular, etc.)
import { Preview } from '@storybook/your-renderer';
 
const preview: Preview = {
  parameters: {
    viewport: {
      options: INITIAL_VIEWPORTS,
    },
  },
  initialGlobals: {
    viewport: { value: 'ipad', isRotated: false },
  },
};
 
export default preview;

Use a detailed set of devices

By default, the Viewport addon will use a minimal set of viewports, which enables you to test your UI in common responsive scenarios. These are also available in the MINIMAL_VIEWPORTS export and include the following devices:

KeyDescriptionDimensions
(wร—h, px)
mobile1Small mobile320 ร— 568
mobile2Large mobile414 ร— 896
tabletTablet834 ร— 1112

If you need a more detailed set of devices, you can use the INITIAL_VIEWPORTS export, which includes the following devices:

KeyDescriptionDimensions
(wร—h, px)
iphone5iPhone 5320 ร— 568
iphone6iPhone 6375 ร— 667
iphone6piPhone 6 Plus414 ร— 736
iphone8piPhone 8 Plus414 ร— 736
iphonexiPhone X375 ร— 812
iphonexriPhone XR414 ร— 896
iphonexsmaxiPhone XS Max414 ร— 896
iphonese2iPhone SE (2nd generation)375 ร— 667
iphone12miniiPhone 12 mini375 ร— 812
iphone12iPhone 12390 ร— 844
iphone12promaxiPhone 12 Pro Max428 ร— 926
iphoneSE3iPhone SE 3rd generation375 ร— 667
iphone13iPhone 13390 ร— 844
iphone13proiPhone 13 Pro390 ร— 844
iphone13promaxiPhone 13 Pro Max428 ร— 926
iphone14iPhone 14390 ร— 844
iphone14proiPhone 14 Pro393 ร— 852
iphone14promaxiPhone 14 Pro Max430 ร— 932
galaxys5Galaxy S5360 ร— 640
galaxys9Galaxy S9360 ร— 740
nexus5xNexus 5X412 ร— 668
nexus6pNexus 6P412 ร— 732
pixelPixel540 ร— 960
pixelxlPixel XL720 ร— 1280
mobile1Small mobile
(also in MINIMAL_VIEWPORTS)
320 ร— 568
mobile2Large mobile
(also in MINIMAL_VIEWPORTS)
414 ร— 896
ipadiPad768 ร— 1024
ipad10piPad Pro 10.5-in834 ร— 112
ipad11piPad Pro 11-in834 ร— 1194
ipad12piPad Pro 12.9-in1024 ร— 1366
tabletTablet
(also in MINIMAL_VIEWPORTS)
834 ร— 1112

To use the detailed set of devices, you can replace the viewports property in your configuration with the INITIAL_VIEWPORTS export:

.storybook/preview.ts
// Replace your-renderer with the renderer you are using (e.g., react, vue3, angular, etc.)
import { Preview } from '@storybook/your-renderer';
import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport';
 
const preview: Preview = {
  parameters: {
    viewport: {
      viewports: INITIAL_VIEWPORTS,
      defaultViewport: 'ipad',
    },
  },
};
 
export default preview;

With the globals API

When using the globals API, the available viewports are defined using the options property.

.storybook/preview.ts
// Replace your-renderer with the renderer you are using (e.g., react, vue3, angular, etc.)
import { Preview } from '@storybook/your-renderer';
 
const preview: Preview = {
  parameters: {
    viewport: {
      options: INITIAL_VIEWPORTS,
    },
  },
  initialGlobals: {
    viewport: { value: 'ipad', isRotated: false },
  },
};
 
export default preview;

Add new devices

If the predefined viewports don't meet your needs, you can add new devices to the list of viewports. For example, let's add two Kindle devices to the default set of minimal viewports:

With the globals API

When using the globals API, note that available viewports are defined using the options property.

.storybook/preview.ts
// Replace your-renderer with the renderer you are using (e.g., react, vue3, angular, etc.)
import { Preview } from '@storybook/your-renderer';
import { MINIMAL_VIEWPORTS } from '@storybook/addon-viewport';
 
const kindleViewports = {
  kindleFire2: {
    name: 'Kindle Fire 2',
    styles: {
      width: '600px',
      height: '963px',
    },
  },
  kindleFireHD: {
    name: 'Kindle Fire HD',
    styles: {
      width: '533px',
      height: '801px',
    },
  },
};
 
const preview: Preview = {
  parameters: {
    viewport: {
      viewports: {
        ...MINIMAL_VIEWPORTS,
        ...kindleViewports,
      },
    },
  },
};
 
export default preview;

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 or set of stories for a component.

Parameters can be applied at the project, component, and story levels, which allows you to specify the configuration where needed. For example, you can set the available viewports for all of the stories for a component like so:

MyComponent.stories.ts|tsx
import type { Meta, StoryObj } from '@storybook/react';
import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport';
 
import { MyComponent } from './MyComponent';
 
const meta: Meta<typeof MyComponent> = {
  component: MyComponent,
  parameters: {
    viewport: {
      //๐Ÿ‘‡ Set available viewports for every story in the file
      viewports: INITIAL_VIEWPORTS,
    },
  },
};
 
export default meta;

Defining the viewport for a story

The Viewport addon enables you to change the viewport applied to a story by selecting from the list of predefined viewports in the toolbar. If needed, you can set a story to default to a specific viewport, by using the parameters.viewport.default parameter:

Button.stories.ts|tsx
// Replace your-framework with the name of your framework
import type { Meta, StoryObj } from '@storybook/your-framework';
 
import { Button } from './Button';
 
const meta: Meta<typeof Button> = {
  component: Button,
  parameters: {
    // ๐Ÿ‘‡ Set default viewport for all component stories
    viewport: { defaultViewport: 'tablet' },
  },
};
 
export default meta;
type Story = StoryObj<typeof Button>;
 
export const OnPhone: Story = {
  parameters: {
    // ๐Ÿ‘‡ Override default viewport for this story
    viewport: { defaultViewport: 'mobile1' },
  },
};

As implied by the name, this method only sets the default viewport for a story. You can still change the viewport using the toolbar when viewing the story.

With the globals API

When you specify a viewport for a story (or a component's stories) using globals, the viewport is applied and cannot be changed using the toolbar. This is useful when you want to ensure that a story is always rendered on a specific viewport.

Button.stories.ts|tsx
// Replace your-renderer with the renderer you are using (e.g., react, vue3, angular, etc.)
import type { Meta, StoryObj } from '@storybook/your-renderer';
 
import { Button } from './Button';
 
const meta: Meta<typeof Button> = {
  component: Button,
  globals: {
    // ๐Ÿ‘‡ Set viewport for all component stories
    viewport: { value: 'tablet', isRotated: false },
  },
};
 
export default meta;
type Story = StoryObj<typeof Button>;
 
export const OnPhone: Story = {
  globals: {
    // ๐Ÿ‘‡ Override viewport for this story
    viewport: { value: 'mobile1', isRotated: false },
  },
};

API

Keyboard shortcuts

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

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

With the globals API

Globals

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

value

Type: string

When set, the viewport is applied and cannot be changed using the toolbar. Must match the key of one of the available viewports.

isRotated

Type: boolean

When true the viewport applied will be rotated 90ยฐ, e.g. it will rotate from portrait to landscape orientation.

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. Only available if you haven't enabled the globals API.

defaultViewport

Type: string

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

disable

With the globals API

Note that the property has been renamed to disabled.

Type: boolean

Turn off this addon's behavior. If you wish to turn off 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 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. See usage example above. The width and height values must include the unit, e.g. '320px'.

With the globals API

options

Type:

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

Replaces: viewports

Specify the available viewports. 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 full set of initial viewports provided by the Viewport addon, listed above.

MINIMAL_VIEWPORTS

Type: object

A minimal set of viewports provided by the Viewport addon listed above. These are used by default.