Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@storybook/addon-viewport
Advanced tools
The @storybook/addon-viewport package allows developers to customize the viewport size to simulate different screen sizes in Storybook's UI. This is particularly useful for ensuring that components and layouts are responsive and look good on various devices, from mobile phones to desktop monitors.
Custom Viewport Configuration
This code configures Storybook to use a set of predefined viewports. The INITIAL_VIEWPORTS constant provides a list of common device resolutions that can be used to test the responsiveness of components.
import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport';
export const parameters = {
viewport: {
viewports: INITIAL_VIEWPORTS,
},
};
Adding New Viewports
Developers can add custom viewports to the Storybook configuration. This code sample demonstrates how to add new devices, such as Kindle Fire 2 and Kindle Fire HD, with specific resolutions to the viewport addon.
export const parameters = {
viewport: {
viewports: {
kindleFire2: {
name: 'Kindle Fire 2',
styles: {
width: '600px',
height: '963px',
},
},
kindleFireHD: {
name: 'Kindle Fire HD',
styles: {
width: '533px',
height: '801px',
},
},
},
},
};
This package provides device detection for React applications. It offers hooks and components to conditionally render UI elements based on the device. Unlike @storybook/addon-viewport, it does not integrate with Storybook but can be used within the app's codebase to adapt the UI for different devices.
React-responsive is a package that allows the creation of media queries in React components. It is similar to @storybook/addon-viewport in that it helps in building responsive UIs, but it does so within the context of the app rather than in a Storybook environment.
Storybook Viewport Addon allows your stories to be displayed in different sizes and layouts in Storybook. This helps build responsive components inside of Storybook.
Install the following npm module:
npm i --save-dev @storybook/addon-viewport
or with yarn:
yarn add -D @storybook/addon-viewport
Then, add following content to .storybook/addons.js
import '@storybook/addon-viewport/register';
Import and use the configureViewport
function in your config.js
file.
import { configureViewport } from '@storybook/addon-viewport';
Setting this property to, let say iphone6
, will make iPhone 6
the default device/viewport for all stories. Default is 'responsive'
which fills 100% of the preview area.
A key-value pair of viewport's key and properties (see Viewport
definition below) for all viewports to be displayed. Default is INITIAL_VIEWPORTS
{
/**
* name to display in the dropdown
* @type {String}
*/
name: 'Responsive',
/**
* Inline styles to be applied to the story (iframe).
* styles is an object whose key is the camelCased version of the style name, and whose
* value is the style’s value, usually a string
* @type {Object}
*/
styles: {
width: '100%',
height: '100%',
},
/**
* type of the device (e.g. desktop, mobile, or tablet)
* @type {String}
*/
type: 'desktop',
}
Sometimes you want to show collection of mobile stories, and you know those stories look horible on desktop (responsive
), so you think you need to change the default viewport only for those?
Here is the answer, with withViewport
decorator, you can change the default viewport of single, multiple, or all stories.
withViewport
accepts either
String
, which represents the default viewport, orObject
, which looks like{
name: 'iphone6', // default viewport
onViewportChange({ viewport }) { // called whenever different viewport is selected from the dropdown
}
}
Simply import the Storybook Viewport Addon in the addons.js
file in your .storybook
directory.
import '@storybook/addon-viewport/register'
This will register the Viewport Addon to Storybook and will show up in the action area.
This will replace all previous devices with Kindle Fire 2
and Kindle Fire HD
by simply calling configureViewport
with the two devices as viewports
in config.js
file in your .storybook
directory.
import { configureViewport } from '@storybook/addon-viewport';
const newViewports = {
kindleFire2: {
name: 'Kindle Fire 2',
styles: {
width: '600px',
height: '963px'
}
},
kindleFireHD: {
name: 'Kindle Fire HD',
styles: {
width: '533px',
height: '801px'
}
}
};
configureViewport({
viewports: newViewports
});
This will add both Kindle Fire 2
and Kindle Fire HD
to the list of devices. This is acheived by making use of the exported INITIAL_VIEWPORTS
property, by merging it with the new viewports and pass the result as viewports
to configureViewport
function
import { configureViewport, INITIAL_VIEWPORTS } from '@storybook/addon-viewport';
const newViewports = {
kindleFire2: {
name: 'Kindle Fire 2',
styles: {
width: '600px',
height: '963px'
}
},
kindleFireHD: {
name: 'Kindle Fire HD',
styles: {
width: '533px',
height: '801px'
}
}
};
configureViewport({
viewports: {
...INITIAL_VIEWPORTS,
...newViewports
}
});
This will make iPhone 6
the default viewport for all stories.
import { configureViewport } from '@storybook/addon-viewport';
configureViewport({
defaultViewport: 'iphone6'
});
Change the default viewport for single/multiple/global stories, or listen to viewport selection changes
import React from 'react';
import { storiesOf, addDecorator } from '@storybook/react';
import { withViewport } from '@storybook/addon-viewport';
// Globablly
addDecorator(withViewport('iphone5'));
// Collection
storiesOf('Decorator with string', module)
.addDecorator(withViewport('iphone6'))
.add('iPhone 6', () => (
<h1>
Do I look good on <b>iPhone 6</b>?
</h1>
));
// Single
storiesOf('Parameterized story', module)
.addDecorator(withViewport())
.add(
'iPad',
() => (
<h1>
Do I look good on <b>iPad</b>?
</h1>
),
{ viewport: 'ipad' }
);
storiesOf('Decorator with object', module)
.addDecorator(
withViewport({
onViewportChange({ viewport }) {
console.log(`Viewport changed: ${viewport.name} (${viewport.type})`); // e.g. Viewport changed: iphone6 (mobile)
},
})
)
.add('onViewportChange', () => <MobileFirstComponent />);
FAQs
Build responsive components by adjusting Storybook’s viewport size and orientation
We found that @storybook/addon-viewport demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 11 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.