Intro to Storybook
  • Get started
  • Simple component
  • Composite component
  • Data
  • Screens
  • Deploy
  • Testing
  • Addons
  • Conclusion
  • Contribute
ReactReact NativeVueAngularSvelteEmber

Construct a screen

Construct a screen out of components
This community translation has not been updated to the latest version of Storybook yet. Help us update it by applying the changes in the English guide to this translation. Pull requests are welcome.

We've concentrated on building UIs from the bottom up, starting small and adding complexity. Doing so has allowed us to develop each component in isolation, figure out its data needs, and play with it in Storybook. All without needing to stand up a server or build out screens!

In this chapter, we continue to increase the sophistication by combining components in a screen and developing that screen in Storybook.

Nested container components

As our app is straightforward, the screen we’ll build is pretty trivial, simply wrapping the TaskList component (which supplies its own data via Svelte Store) in some layout and pulling a top-level error field out of the store (let's assume we'll set that field if we have some problem connecting to our server).

Let's start by updating our Svelte store (in src/store.js) to include our new error field we want:

import { writable } from 'svelte/store';

const TaskBox = () => {
  // Creates a new writable store populated with some initial data
  const { subscribe, update } = writable([
    { id: '1', title: 'Something', state: 'TASK_INBOX' },
    { id: '2', title: 'Something more', state: 'TASK_INBOX' },
    { id: '3', title: 'Something else', state: 'TASK_INBOX' },
    { id: '4', title: 'Something again', state: 'TASK_INBOX' },

  return {
    // Method to archive a task, think of a action with redux or Vuex
    archiveTask: id =>
      update(tasks => => ( === id ? { ...task, state: 'TASK_ARCHIVED' } : task))
    // Method to archive a task, think of a action with redux or Vuex
    pinTask: id =>
      update(tasks => => ( === id ? { ...task, state: 'TASK_PINNED' } : task))
export const taskStore = TaskBox();

+ // Store to handle the app state
+ const AppState = () => {
+  const { subscribe, update } = writable(false);
+  return {
+    subscribe,
+    error: () => update(error => !error),
+  };
+ };

+ export const AppStore = AppState();

Now that we have the store updated with the new field. Let's create InboxScreen.svelte in your components directory:

  import TaskList from './TaskList.svelte';
  export let error = false;

  {#if error}
    <div class="page lists-show">
      <div class="wrapper-message">
        <span class="icon-face-sad" />
        <div class="title-message">Oh no!</div>
        <div class="subtitle-message">Something went wrong</div>
    <div class="page lists-show">
        <h1 class="title-page">
          <span class="title-wrapper">Taskbox</span>
      <TaskList />

We also need to change the App component to render the InboxScreen (eventually, we would use a router to choose the correct screen, but let's not worry about that here):

  import './index.css'
  import { AppStore } from './store';
  import InboxScreen from './components/InboxScreen.svelte';

<InboxScreen error="{$AppStore}" />
💡 Don't forget to update the TaskList component and your unit tests to reflect the changes that were introduced.

However, where things get interesting is in rendering the story in Storybook.

As we saw previously, the TaskList component is a container that renders the PureTaskList presentational component. By definition, container components cannot be simply rendered in isolation; they expect to be passed some context or connected to a service. What this means is that to render a container in Storybook, we must mock (i.e., provide a pretend version) the context or service it requires.

When placing the TaskList into Storybook, we were able to dodge this issue by simply rendering the PureTaskList and avoiding the container. We'll do something similar and render the InboxScreen in Storybook also.

So when we set up our stories in InboxScreen.stories.js:

import InboxScreen from './InboxScreen.svelte';

export default {
  component: InboxScreen,
  title: 'InboxScreen',

const Template = args => ({
  Component: InboxScreen,
  props: args,

export const Default = Template.bind({});

export const Error = Template.bind({});
Error.args = {
  error: true,

We see that both the error and standard stories work just fine. (But you will encounter some problems when trying to test the InboxScreen with a unit test if no data is supplied as we did with TaskList).

💡 As an aside, passing data down the hierarchy is a legitimate approach, especially when using GraphQL. It’s how we have built Chromatic alongside 800+ stories.

Cycling through states in Storybook makes it easy to test we’ve done this correctly:

Interactive stories

So far, we've been able to build a fully functional application from the ground up, starting from a simple component up to a screen and continuously testing each change using our stories. But each new story also requires a manual check on all the other stories to ensure the UI doesn't break. That's a lot of extra work.

Can't we automate this workflow and interact with our components automatically?

Storybook's play function allows us to do just that. A play function includes small snippets of code that are run after the story renders.

The play function helps us verify what happens to the UI when tasks are updated. It uses framework-agnostic DOM APIs, that means we can write stories with the play function to interact with the UI and simulate human behavior no matter the frontend framework.

Let's see it in action! Update your newly created PureInboxScreen story, and set up component interactions by adding the following:

+ import { fireEvent, within } from '@storybook/testing-library';
import InboxScreen from './InboxScreen.svelte';

export default {
  component: InboxScreen,
  title: 'InboxScreen',

const Template = args => ({
  Component: InboxScreen,
  props: args,

export const Default = Template.bind({});

export const Error = Template.bind({});
Error.args = {
  error: true,

+ export const WithInteractions = Template.bind({});
+ = async ({ canvasElement }) => {
+   const canvas = within(canvasElement);
+   // Simulates pinning the first task
+   await"pinTask-1"));
+   // Simulates pinning the third task
+   await"pinTask-3"));
+ };

Check your newly created story. Click the Interactions panel to see the list of interactions inside the story's play function.

The play function allows us to interact with our UI and quickly check how it responds if we update our tasks. That keeps the UI consistent at no extra manual effort. All without needing to spin up a testing environment or add additional packages.

Component-Driven Development

We started from the bottom with Task, then progressed to TaskList, now we’re here with a whole screen UI. Our InboxScreen accommodates a nested container component and includes accompanying stories.

Component-Driven Development allows you to gradually expand complexity as you move up the component hierarchy. Among the benefits are a more focused development process and increased coverage of all possible UI permutations. In short, CDD helps you build higher-quality and more complex user interfaces.

We’re not done yet - the job doesn't end when the UI is built. We also need to ensure that it remains durable over time.

💡 Don't forget to commit your changes with git!
Is this free guide helping you? Tweet to give kudos and help other devs find it.
Next Chapter
Learn how to deploy Storybook online
✍️ Edit on GitHub – PRs welcome!
Add Storybook to your project in less than a minute to build components faster and easier.
Learn Storybook with in-depth tutorials that teaches Storybook best practices. Follow along with code samples.
Learn Storybook now
The MIT License (MIT). Website design by @domyen and the awesome Storybook community.
StorybookShowcaseDocsTutorialsAddonsBlogReleasesGet involvedUse casesSupportTelemetryTeam
Community GitHub Twitter Discord chat Youtube Component Driven UIs
Get news, free tutorials, and Storybook tips emailed to you.

Maintained by
Continuous integration by
Hosting by