Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Knobs for slotted content #1

Open
bkjohnson opened this issue Jul 27, 2019 · 3 comments
Open

Knobs for slotted content #1

bkjohnson opened this issue Jul 27, 2019 · 3 comments

Comments

@bkjohnson
Copy link

This starting config is fantastic!

So far my only question is if it would be possible to have a knob or some kind of way of passing in HTML/JSX to a <slot /> in a component. Do you know if storybook would allow this?

@DesignByOnyx
Copy link
Owner

DesignByOnyx commented Jul 30, 2019

Hmmm, could you describe your use case with a little more detail so I can visualize it better. The short answer is probably "yes" - but I need to see what you're trying to do so I'm not guessing. Thanks!

@bkjohnson
Copy link
Author

Sure. Let's say I redefined the default <my-component> so that the render function looked like this:

   render() {
-    return <div>Hello, World! I'm {this.getText()}</div>;
+    return (
+      <div>
+        Hello, World! I'm <slot />, nice to meet you!
+      </div>
+    );
   }
 }

In the index.html I can do something like this and expect to see it in the shadow DOM, along with any styles I've given to slotted content.

<my-component>
  <span>a component</span>
</my-component>

However, the more I look at knobs, the more it seems like it would be cumbersome to have a knob for slot content. Instead, maybe it would be easier to allow the story to export slotted content as part of the states array, and then make it a part of componentEl here.

@DesignByOnyx
Copy link
Owner

Ah, I see what you mean. I will ponder this as I can see how that would be useful. Though one of my goals is to keep a minimal API.

There's another feature for this thing which I haven't documented yet, but your individual story files can export a function instead of the object I used in the example. If we detect a function, it will be called with the story context and reference to KNOBS so that it can render itself (see code here). It would look something like this:

/* my-component.story.js */
export default function(stories, knobs) {
  const mainEl = document.createElement('div'); // do this OUTSIDE the render function below

  stories.add('my-component', () => {
    mainEl.innerHTML = '';
    /** 
    * Append things to the mainEl here. You can insert knob values
    * anywhere you want.
    */
    return mainEl;
  });
}

My plan with this is to eventually provide the function with all the utilities it needs to efficiently render a custom story without having to rewrite all the code I wrote to render a story. This would be similar to the idea of render props.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants