Git Product home page Git Product logo

craft-storybook-starter's Introduction

Craft CMS

Craft Storybook Starter

Storybook is an open source tool for developing UI components in isolation.
It makes building stunning UIs organized and efficient.

๐ŸŽ‰ Storybook runs outside Craft in JavaScript and doesn't require a Craft plugin
๐ŸŽ‰ Storybook works with the same Twig files from your project with some differences

This starter sets up everything for you to be productive in Storybook.
Use Storybook to create a living styleguide from your existing site/app.
Take advantage of the excellent Webpack dev server to create solid components with mock data.

Storybook Features

This starter has many modern features including:

๐Ÿ‘ Hot module reloading
๐Ÿ‘ Automatic accessibility tests
๐Ÿ‘ Color accessibility tests
๐Ÿ‘ Fake data generator (Faker)
๐Ÿ‘ Sass support
๐Ÿ‘ Device previews
๐Ÿ‘ Extendable Webpack config
๐Ÿ‘ Static site generator

View the Storybook demo ๐Ÿ‘‰

Getting Started

Create a new project

Create a new project based on this template using degit:

npx degit ben-rogerson/craft-storybook-starter craft-storybook && cd $_ && npm install

Start Storybook with:

npm run storybook

Start Craft CMS setup:

composer install && ./craft setup

Bring into an existing project

Adding Storybook into your project is a quick process:

  1. Grab the repository:
    git clone https://github.com/ben-rogerson/craft-storybook-starter.git
  2. Copy these folders into your project:
    .storybook
    stories
    templates/components
    
  3. Install the Storybook devDependencies:
    npm i -D @babel/core @babel/preset-env @storybook/addon-a11y @storybook/addon-knobs @storybook/addon-viewport @storybook/html faker babel-loader css-loader node-sass sass-loader style-loader twig twigjs-loader babel-plugin-module-resolver webpack-cli
  4. Add these script definitions into your package.json:
    "scripts": {
        "storybook": "start-storybook",
        "build-storybook": "build-storybook -c .storybook -o build"
    },
  5. Start Storybook with: npm run storybook

Working with Storybook

  1. Create a normal Twig file somewhere in your templates folder:
    Eg: /templates/components/[component].twig
  2. Add a matching story in the stories folder:
    Eg: /stories/[component].stories.js
  3. Start Storybook with: npm run storybook

Hosting your Storybook

Storybook builds static file previews for your components. This means you can leverage easy (and free) hosting services like Netlify.

Configure it to run npm run build-storybook and serve from the /build folder.

You could also use Github pages to host your storybook.

A heads up: Storybook Twig != Craft Twig

Storybook uses a JavaScript implementation of Twig and you may come across some of its limitations.

  • Any Craft or Craft Plugin functions, or Twig tags or filters will throw an error
  • String interpolation isnโ€™t supported
  • Importing components within components isn't supported (perhaps fixable with Webpack adjustments)
    Importing now works correctly

While I agree that this isnโ€™t ideal, there is an upside to it. It forces small and basic components. You see this same technique constantly in modern JavaScript apps and itโ€™s a good thing! Itโ€™s a technique you can also bring to many of your Craft Components. If youโ€™re interested in the concept check out Atomic design.

If youโ€™re bringing components into Storybook and youโ€™re having compatibility issues you may need to break it up into a presentational and logic component. The presentational component would have the basic component html and wouldnโ€™t contain the incompatible Twig code. This would be the component youโ€™d display in Storybook.

Links

craft-storybook-starter's People

Contributors

angrybrad avatar ben-rogerson avatar benjamindavid avatar brandonkelly avatar carlcs avatar cromwellryan avatar dependabot-preview[bot] avatar geenious avatar jasonmccallister avatar jnowland avatar mildlygeeky avatar mtnorthrop avatar tam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

craft-storybook-starter's Issues

Twig 'include' in variable throws "Unable to find template file"

Unable to find template file

I came across this error while experimenting with your awesome craft/storybook setup. I already found a workaround: I use Twig macro.

Craft uses /templates as base dir for twig-template-files so I use . as base path for my twig-files in /templates/**/*.twig. But I guess webpack does not think like that so we got a problem here...

If you have any further questions, just comment below, thanks in advance!

After git clone I added the following files:

  • stories/a.stories.js
  • stories/b.stories.js
  • templates/components/a.twig
  • templates/components/b.twig

Works with Storybook but not with Craft

stories/a.stories.js

// Import: Twig
import component from 'components/a.twig';

// Component name
export default { title: 'Test/A' };

export const Simple = () => {
  return component();
};

stories/b.stories.js

// Import: Twig
import component from 'components/b.twig';

// Component name
export default { title: 'Test/B' };

export const Simple = () => {
  return component();
};

templates/components/a.twig

A

{% set content %}
{% include "templates/components/b.twig" %}
{% endset %}

{{ content }}

templates/components/b.twig

B

Output in Storybook:

A B

Works with Craft but not with Storybook

templates/components/a.twig

A

{% set content %}
{% include "./b.twig" %}
{% endset %}

{{ content }}

Output in Storybook:

Unable to find template file ./b.twig

Workaround with macro

templates/components/a.twig

A

{% import "./b.twig" as b %}

{% set content %}
{{ b.render() }}
{% endset %}

{{ content }}

templates/components/b.twig

{% macro render() %}
  B
{% endmacro %}

Output in Storybook:

A B

Suggestion - Instructions for implementing in existing projects

Hey Ben!

This looks absolutely rad. Going to start playing with this later today.

Do you think it would be worthwhile to include instructions on how to integrate this into an existing Craft project, instead of how to spin it up with a new install? I have a few ongoing projects I'd love to drop this into, but I'm a little fuzzy about what files I need and where.

Thanks again for this awesome project.

Is it possible to create stories of a matrix

This repo is a lifesaver, thanks!

I have a template that includes a matrix, which works fine on my site, but I can't figure out how to organize the matrix data for the story.

Here's a dumbed down version of the twig file:

<section>
    {% set charactersByDay = castInOrder.charactersByDay.all() %}
    {% if charactersByDay|length %}
        {% for character in charactersByDay %}
            {% if character.characterName is not empty %}
                {{character.characterName}}
            {% endif %}
        {% endfor %}
    {% endif %}
</section>

This is the story, which doesn't work. :)

export const withCharacters = () => container(
  component({
    castInOrder: {
      charactersByDay: [{
        characterName: 'asdf'
      }],
    }
  })
)

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.