<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Joaquin Senosiain, Jr</title>
    <description>The latest articles on DEV Community by Joaquin Senosiain, Jr (@jsenosiain).</description>
    <link>https://dev.to/jsenosiain</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F204208%2Fd223fd69-fc0e-4628-a01d-5fdb1e0bfc21.jpeg</url>
      <title>DEV Community: Joaquin Senosiain, Jr</title>
      <link>https://dev.to/jsenosiain</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jsenosiain"/>
    <language>en</language>
    <item>
      <title>Setting up Stencil 4 with Storybook 8</title>
      <dc:creator>Joaquin Senosiain, Jr</dc:creator>
      <pubDate>Fri, 31 Jan 2025 18:25:21 +0000</pubDate>
      <link>https://dev.to/jsenosiain/setting-up-stencil-4-with-storybook-8-499c</link>
      <guid>https://dev.to/jsenosiain/setting-up-stencil-4-with-storybook-8-499c</guid>
      <description>&lt;p&gt;I had a lot of trouble getting this setup and working properly, but I finally managed to get it working. I hope this saves someone the same anguish I went through.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Install Stencil
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init stencil
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the prompt select &lt;em&gt;&lt;strong&gt;components&lt;/strong&gt;&lt;/em&gt; then name your project and confirm.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Navigate to Project and Install Dependencies
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd &amp;lt;your-project-name&amp;gt;
npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Integrate Storybook
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx sb init --type web_components
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Select &lt;strong&gt;&lt;em&gt;Vite&lt;/em&gt;&lt;/strong&gt; at the prompt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4. Update compilerOptions in tsconfig.json
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"compilerOptions": {
  ...,
  "skipLibCheck": true
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5. Configure Storybook
&lt;/h2&gt;

&lt;p&gt;Open &lt;em&gt;.storybook/preview.js&lt;/em&gt; and add the following code to the top of the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {defineCustomElements} from '../loader';
defineCustomElements();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, open &lt;em&gt;.storybook/main.ts&lt;/em&gt; (you may have to update the js file to ts) and copy the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import type { StorybookConfig } from '@storybook/web-components-vite';

const config: StorybookConfig = {
  stories: ['../src/components', '../src/styleguide', '../src/stories'],
  addons: ['@storybook/addon-essentials'],
  //staticDirs: ['../dist/lib-components'],
  docs: {
    autodocs: true
  },
  async viteFinal(config, { configType }) {
    const { mergeConfig } = await import('vite')

    if (configType !== 'DEVELOPMENT') {
      return config
    }

    return mergeConfig(config, {
      build: {
        // this is set to 'dist' by default which causes hot-reloading for stencil components to break
        // see: https://vitejs.dev/config/server-options.html#server-watch
        // setting it to anything other than dist fixes the issue
        outDir: 'dist-vite'
      }
    })
  },
  core: {
    disableTelemetry: true
  },
  framework: '@storybook/web-components-vite'
}

export default config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may have to install &lt;strong&gt;&lt;em&gt;vite&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D vite --legacy-peer-deps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The --legacy-peer-deps flag was need as of this writing.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6. Setup first Story
&lt;/h2&gt;

&lt;p&gt;Delete &lt;em&gt;src/stories&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In the &lt;em&gt;src/components/my-component&lt;/em&gt; folder, create &lt;em&gt;my-component.stories.tsx&lt;/em&gt; and add the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default {
  // this creates a 'Components' folder and a 'MyComponent' subfolder
  title: 'Components/MyComponent',
};

const Template = (args) =&amp;gt; `&amp;lt;my-component first="${args.first}" middle="${args.middle}" last="${args.last}"&amp;gt;&amp;lt;/my-component&amp;gt;`;

export const Example = Template.bind({});
Example.args = {
  first: 'Alan',
  middle: 'Dean',
  last: 'Foster'
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final Step: Run Project
&lt;/h2&gt;

&lt;p&gt;Open terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run build -- --watch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open a separate terminal and run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run storybook
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your project should be up and running and any changes you make in you &lt;em&gt;my-component.tsx&lt;/em&gt; file will immediately be reflected in Storybook.&lt;/p&gt;

&lt;p&gt;I hope this helps someone out there.&lt;/p&gt;

</description>
      <category>stenciljs</category>
      <category>storybook</category>
      <category>react</category>
    </item>
  </channel>
</rss>
