fwouts / previewjs

Preview UI components in your IDE instantly
https://previewjs.com
Other
1.85k stars 45 forks source link

Different wrappers for different components #2107

Open rudolfbyker opened 1 year ago

rudolfbyker commented 1 year ago

I'm curious to hear more about your use case for different wrappers for different components though. What do you envision that looking like?

Originally posted by @fwouts in https://github.com/fwouts/previewjs/issues/2086#issuecomment-1750384724

Is your feature request related to a problem? Please describe.

Let's say I have ComponentA and ComponentB. ComponentA requires some things to be provided by the wrapper, and some globals to be mocked. ComponentB requires something totally different, but maybe with the same name. It's not possible to use the same wrapper for previewing both.

Describe the solution you'd like

I don't have strong opinions about how to implement this, but here are some ideas:

Some of these suggestions only allow a 1:1 relationship between wrapper and component. Others allow 1:N relationship between wrapper and components. Currently, we have a 1:all relationship (just one wrapper for all components in the project)

Describe alternatives you've considered

fwouts commented 1 year ago

Thanks for all this context @rudolfbyker!

One alternative you could already use today is to use Storybook decorators. They actually work with component previews too, as per the preview renderer code.

With Vue 3, this would look like the following block in your .vue file:

<script lang="ts">
export default {
  decorators: [
    (story: any) => ({
      components: { story },
      template: '<div style="margin: 3em;"><story /></div>',
    }),
  ]
};
</script>

I wonder if a solution like this would work for you? Happy to refine it based on your feedback—the fact that it works today is mostly an accident.

rudolfbyker commented 1 year ago

Thanks! I'll try that. I have not used Storybook before (my first time hearing about it was at ViteConf). ~Does it also work with <script setup lang="ts"> and/or composition API?~ nevermind, I read the storybook docs now, and I understand better.

But now that I have tried storybook, I don't understand why one would use PreviewJS with storybook. They seem to do similar things.