Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Embed StencilJS components inside Storybook React App?

I'm trying to integrate Stencil and Storybook inside the same project. I've been following this set up guide and this one however one of the steps is to publish the library of components to NPM and that's not what I want.

I have this repo which I've configured with components library (src folder) and with the reviewer of those components with Storybook, which resides in the storybook folder.

The problem is that when I compile the components using Stencil and copy the dist folder inside the Storybook app and import the component nothing renders. Tweaking the configuration using custom head tags I was able to import it correctly however no styles where applied.

When I open the network panel there is some error when importing the component:

Error in console

And thus the component is present in the DOM but with visibility set to hidden, which I think it does when there is an error.

This is the component au-button:

import { Component } from '@stencil/core';

@Component({
  tag: 'au-button',
  styleUrl: 'button.css',
  shadow: true
})
export class Button {
  render() {
    return (
      <button class="test">Hello</button>
    );
  }
}

Here is the story my component:

import React from 'react';
import { storiesOf } from '@storybook/react';

import '../components/components.js'

storiesOf('Button', module)
  .add('with text', () => <au-button></au-button>)

These are the scripts inside the Storybook app:

"scripts": {
    "storybook": "start-storybook -p 9009",
    "build-storybook": "build-storybook",
    "copy": "cp -R ./../dist/* components"
  },

And the workflow is as follows:

  1. Launch storybook
  2. Make changes in the component
  3. Execute build command
  4. Execute copy command

Also, I would like to automate the developer experience, but after I solve this problem first.

Any ideas of what I could be doing wrong?

like image 367
César Alberca Avatar asked Mar 25 '18 17:03

César Alberca


2 Answers

Sample for this could be found in the repo https://github.com/shanmugapriyaEK/stencil-storybook. It autogenerates stories with knobs and notes. Also it has custom theme in it. Hope it helps.

like image 146
Shanmuga Priya EK Avatar answered Oct 26 '22 02:10

Shanmuga Priya EK


I'm using @storybook/polymer and it's working for me really well.

following your example:

import { Component } from '@stencil/core';

@Component({
  tag: 'au-button',
  styleUrl: 'button.css',
  shadow: true
})
export class Button {
  render() {
    return (
      <button class="test">Hello</button>
    );
  }
}

the story would be:

import { storiesOf } from '@storybook/polymer';

storiesOf('Button', module)
  .add('with text', () => <au-button></au-button>)

the scripts in the package.json:

"scripts": {
    "storybook": "start-storybook -p 9001 -c .storybook -s www"
  },

the storybook config file:

import { configure, addDecorator } from '@storybook/polymer';
const req = require.context('../src', true, /\.stories\.js$/);
function loadStories() {
  req.keys().forEach((filename) => req(filename))
}
configure(loadStories, module);

and storybook preview-head.html you have to add to the body the following:

<body>
    <div id="root"></div>
    <div id="error-message"></div>
    <div id="error-stack"></div>
</body>
like image 1
vpaulo Avatar answered Oct 26 '22 01:10

vpaulo