How do we make nuxt-icon work in Storybook?
Is it possible to make <Icon /> work in Storybook? Thank you
I'm interested in this too. A fair amount of Googling and trying some potential solutions did not give me a way to accomplish this.
Would be nice to have a repository / reproduction to help
Is the issue that it causes an error / warning?
If its anything like my experience getting nuxt-link to work, you may need to make a mock component.
https://github.com/miclgael/chia/blob/main/.storybook/mock-components/NuxtLink.js
Could you take a look at https://github.com/hirotaka/storybook-addon-nuxt and tell me if this works?
For me, storybook-addon-nuxt will throw an error when trying to use icon.
Interested to hear from you on this @nasianss π
I will try to make a reproduction as well.
@Atinux My reproduction with storybook-addon-nuxt yields [Vue warn]: Failed to resolve component: icon If this is a native custom element, make sure to exclude it from component
https://stackblitz.com/edit/nuxt-starter-nivfnx?file=stories%2FIconTester.vue,stories%2FIconTester.stories.js
npm run storybook
I think this is a common issue - where every module in the nuxt ecosystem requires some sort of patch or workaround to work in Storybook.
e.g. https://github.com/hirotaka/storybook-addon-nuxt/issues/5
@miclgael I'm getting the exact same error. Did you find a way to get this to work?
@miclgael I'm getting the exact same error. Did you find a way to get this to work?
@DenFin Itβs not ideal, but I think you would have to mock it for now. :(
@miclgael Thanks for your reply.
I solved it by adding the Material Symbols icon font directly in my project and use it in a stylesheet
@Atinux here you have a reproduction repo for testing
use main branche first to see that Storybook & Nuxt with Local components and Nuxt components is working !!!
and then switch to nuxt-icon branch and enjoy π
from the moment you add
nuxt-iconon modules, the issues starts, you don't even need to used on any story !!!
export default defineNuxtConfig({
modules: ["@nuxtjs/storybook", "nuxt-icon"],
});
Any updates please ?
I'm also experiencing this issue. I've glanced at IconCss.vue and nothing looks blaringly wrong there. Could this be a problem with all of the module.* files? They seem to be written sort of differently than nuxtUI writes theirs.
When I comment out the parts of module.mjs that import IconCSS.vue, (lines 91-95 in the npm package) storybook complains about template/script tags in Icon.vue. When I remove Icon.vue from module.mjs storybook stops complaining. Hopefully this is helpful diagnostic info
duplicate of #128
@nuxt/icon is also NOT working in histoire. Did you found a way for storybook? maybe it also helps with histoire?
@Atinux Any updates on this or is there maybe a recommended workaround?
Also would look forward for an update on this topic.
same issue here, any update ?
As a workaround you can use your own mock component as a replacement by adding this to your Storybooks preview.ts:
import NuxtIcon from './foo/bar/YourMockNuxtIcon.vue'
setup((app) => { app.component('NuxtIcon', NuxtIcon) })
This doesn't really solve the issue, but may work for some people. I guess Storybook just doesn't work great with Nuxt Modules in general (as mentioned above). So if you need to use Storybook then it is probably better to avoid them or build your own mock components for each.
Thanks @AdrianFahrbach for the quick answer π! I found a possible workaround as well :
- Create a
.storybook/middleware.jsfile in your Nuxt project with the following content:
const proxy = require('http-proxy-middleware')
module.exports = (router) => {
router.use('/api/_nuxt_icon', proxy.createProxyMiddleware({
target: 'http://localhost:3000/api/_nuxt_icon',
changeOrigin: true,
pathRewrite: {
'^/api/_nuxt_icon': '',
},
}))
}
- This middleware proxies requests from Storybook to your Nuxt server, resolving the 404 errors for icon requests.
- Make sure to install the
http-proxy-middlewarepackage:
npm install http-proxy-middleware --save-dev
This solution worked for me by redirecting the /api/_nuxt_icon requests from Storybook to the Nuxt server running on port 3000. It should resolve the 404 errors for icon requests, allowing @nuxt/icon to work properly in Storybook.
Let me know if you need any clarification or have any questions about this approach!
@QuanticPotatoes Thank you, it worked.
I had to wrap my story with a suspense as well to get it working.
import type { Meta, StoryObj } from '@storybook/vue3'
import { h, Suspense } from 'vue'
import Button from './Button.vue'
const meta = {
title: 'Features/Custom Components',
component: Button,
tags: ['autodocs'],
} satisfies Meta<typeof Button>
export default meta
type Story = StoryObj<typeof meta>
export const Default: Story = {
args: {
label: 'My label',
icon: 'material-symbols:arrow-right-alt',
},
render(args) {
return {
setup() {
return () =>
h(
Suspense,
h(Button, {
...args,
}),
)
},
}
},
}