
Security News
Package Maintainers Call for Improvements to GitHub’s New npm Security Plan
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
storybook-addon-useragent
Advanced tools
Storybook Version | Installation Command | Status |
---|---|---|
v8 | npm i -D storybook-addon-useragent@8 | Current major version, actively maintained |
v7 | npm i -D storybook-addon-useragent@7 | Legacy support |
v6 | npm i -D storybook-addon-useragent@6 | Legacy support |
You can check for updates and upgrade to the latest version:
npm outdated
npm install storybook-addon-useragent@latest
Add the addon to your Storybook configuration in .storybook/main.js
(or .storybook/main.ts
):
export default {
stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|mjs|ts|tsx)"],
addons: [
// Other addons...
"storybook-addon-useragent",
],
};
Once installed, the UserAgent addon will appear in your Storybook UI. You can select from a list of predefined user agents to simulate different browsers and devices.
You can customize the list of user agents available in the addon by creating a configuration file:
.storybook/userAgent.js
:
export const customUserAgents = [
{
name: "Windows_7-IE_11",
userAgent:
"Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
},
// Add more custom user agents as needed
];
Then import and use it in your .storybook/preview.js
file:
import { customUserAgents } from "./userAgent";
export const parameters = {
// Other parameters...
userAgent: customUserAgents,
};
You can set a default UserAgent for individual stories:
import React from "react";
import { UserAgentExample } from "./UserAgentExample";
export default {
title: "Example/UserAgentExample",
component: UserAgentExample,
argTypes: { useragent: { control: "text" } },
};
const Template = (args) => <UserAgentExample {...args} />;
export const IOS = Template.bind({});
IOS.args = {
useragent:
"Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Mobile/15E148 Safari/604.1",
};
This addon supports the User-Agent Client Hints API, which is a more modern and privacy-preserving way to get browser and device information than the traditional user agent string.
The addon automatically uses Client Hints in modern browsers while falling back to traditional User-Agent string parsing in older browsers.
This addon uses modern browser APIs to simulate user agents:
Browser Category | Support Level | Browsers | Notes |
---|---|---|---|
Full Support | ✅ | Chrome 89+, Edge 89+, Opera 75+ | All features work as expected |
Good Support | ⚠️ | Firefox 90+, Safari 16.4+ | Most features work, some limitations |
Limited | ⚠️ | Safari 15-16.3, Firefox 78-89 | UserAgent string works, Client Hints limited |
Minimal | ⚠️ | IE11, Legacy Edge | Basic functionality only |
In browsers with limited support, the addon falls back to basic functionality while showing appropriate warnings.
The UserAgent addon has minimal impact on Storybook performance:
In rare cases, changing UserAgent in rapid succession may cause temporary UI flickering. We recommend:
This example shows how to test a responsive component across multiple devices:
// ResponsiveComponent.stories.js
import { ResponsiveComponent } from "./ResponsiveComponent";
export default {
title: "Components/ResponsiveComponent",
component: ResponsiveComponent,
};
// Basic template
const Template = (args) => <ResponsiveComponent {...args} />;
// Create stories for different device types
export const Desktop = Template.bind({});
Desktop.args = {
// Default desktop user agent is used
};
export const iPhone = Template.bind({});
iPhone.args = {
useragent:
"Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Mobile/15E148 Safari/604.1",
};
export const Android = Template.bind({});
Android.args = {
useragent:
"Mozilla/5.0 (Linux; Android 12; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.104 Mobile Safari/537.36",
};
You can then create a testing matrix to ensure your components work across browsers:
This addon modifies the navigator
object to simulate different user agents. While we've implemented safeguards:
Issue | Possible Cause | Solution |
---|---|---|
UserAgent not changing | Browser security restrictions | Try a different browser (Chrome works best) |
UI components not responding to UserAgent | Component not using UA detection | Ensure component checks navigator.userAgent |
Stories show error when changing UA | Timing or cleanup issues | Reload the story or check for errors in console |
Inconsistent behavior | Caching or stale state | Clear browser cache and restart Storybook |
If you encounter issues with the UserAgent addon:
console.log(window.navigator.userAgent);
Test how your components adapt to different device characteristics beyond just screen size:
// DeviceResponsiveMenu.jsx
import React from "react";
import { useUserAgent } from "./useUserAgent";
export function DeviceResponsiveMenu({ items }) {
const { isMobile, isTablet, isMacOS } = useUserAgent();
// Adapt menu based on device type
return (
<nav className={`menu ${isMobile ? "mobile-menu" : "desktop-menu"}`}>
{items.map((item) => (
<MenuItem
key={item.id}
item={item}
useShortText={isMobile}
useTouchOptimized={isMobile || isTablet}
useNativeControls={isMacOS}
/>
))}
</nav>
);
}
Conditionally enable features based on browser capability:
function BrowserOptimizedVideo({ src }) {
const userAgent = window.navigator.userAgent;
const isSafari = /^((?!chrome|android).)*safari/i.test(userAgent);
return (
<video
src={src}
controls
playsInline={isSafari} // Safari-specific attribute
format={isSafari ? "hls" : "dash"} // Different format based on browser
/>
);
}
We maintain a detailed changelog of all versions and updates:
Contributions are welcome! Please feel free to submit a Pull Request.
If you encounter compatibility issues:
FAQs
storybook-addon-useragent
The npm package storybook-addon-useragent receives a total of 690 weekly downloads. As such, storybook-addon-useragent popularity was classified as not popular.
We found that storybook-addon-useragent demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.