icon icon indicating copy to clipboard operation
icon copied to clipboard

How do we make nuxt-icon work in Storybook?

Open nasianss opened this issue 2 years ago β€’ 25 comments

Is it possible to make <Icon /> work in Storybook? Thank you

nasianss avatar May 10 '23 09:05 nasianss

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.

ricovandevin avatar May 12 '23 13:05 ricovandevin

Would be nice to have a repository / reproduction to help

atinux avatar May 19 '23 09:05 atinux

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

miclgael avatar May 31 '23 07:05 miclgael

Could you take a look at https://github.com/hirotaka/storybook-addon-nuxt and tell me if this works?

atinux avatar Jun 18 '23 09:06 atinux

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.

miclgael avatar Jun 19 '23 01:06 miclgael

@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 avatar Jun 20 '23 22:06 miclgael

@miclgael I'm getting the exact same error. Did you find a way to get this to work?

DenFin avatar Jul 31 '23 07:07 DenFin

@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 avatar Aug 02 '23 21:08 miclgael

@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

DenFin avatar Aug 03 '23 12:08 DenFin

@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-icon on modules, the issues starts, you don't even need to used on any story !!!

export default defineNuxtConfig({
    modules: ["@nuxtjs/storybook", "nuxt-icon"],
});

TiBianMod avatar Nov 29 '23 16:11 TiBianMod

Any updates please ?

zola33dsf avatar Jan 23 '24 14:01 zola33dsf

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.

memtech3 avatar Feb 23 '24 21:02 memtech3

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

memtech3 avatar Feb 23 '24 23:02 memtech3

@nuxt/icon is also NOT working in histoire. Did you found a way for storybook? maybe it also helps with histoire?

awacode21 avatar Jul 25 '24 13:07 awacode21

@Atinux Any updates on this or is there maybe a recommended workaround?

AdrianFahrbach avatar Aug 07 '24 12:08 AdrianFahrbach

Also would look forward for an update on this topic.

simon-kramer avatar Aug 09 '24 04:08 simon-kramer

same issue here, any update ?

QuanticPotatoes avatar Aug 14 '24 12:08 QuanticPotatoes

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.

AdrianFahrbach avatar Aug 14 '24 12:08 AdrianFahrbach

Thanks @AdrianFahrbach for the quick answer πŸ‘! I found a possible workaround as well :

  1. Create a .storybook/middleware.js file 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': '',
    },
  }))
}
  1. This middleware proxies requests from Storybook to your Nuxt server, resolving the 404 errors for icon requests.

image

  1. Make sure to install the http-proxy-middleware package:
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 avatar Aug 14 '24 12:08 QuanticPotatoes

@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,
            }),
          )
      },
    }
  },
}

tkjaergaard avatar Aug 15 '24 19:08 tkjaergaard