Skip to content

Instantly share code, notes, and snippets.

@renestalder
Last active August 4, 2023 11:25
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save renestalder/1eedfe9b7e8ef775c8a83369fb8351f4 to your computer and use it in GitHub Desktop.
Save renestalder/1eedfe9b7e8ef775c8a83369fb8351f4 to your computer and use it in GitHub Desktop.
Storybook 6 Twig Setup
  1. Install twigjs-loader via npm.

    $ npm i -D twigjs-loader
  2. Extend Storybook's Webpack config to use the Twig loader.

    main.js:

    module.exports = {
     stories: [
       "../stories/**/*.stories.mdx",
       "../stories/**/*.stories.@(js|jsx|ts|tsx)",
     ],
     addons: [
       "@storybook/addon-links",
       "@storybook/addon-essentials"
     ],
     webpackFinal: async (config, { configType }) => {
       config.module.rules.push({
         test: /\.twig$/,
         use: "twigjs-loader",
       });
    
       return config;
     }};
  3. Write your stories by importing your Twig files. Example:

    button.twig:

    <button type="{{ type }}">{{ text }}</button>

    button.stories.js:

    import twigButton from "./button.twig";
    
     export default {
       title: "Button",
       argTypes: {
         type: {
           control: {
             type: "inline-radio",
             options: ["button", "submit"],
           },
         },
       },
     };
    
     const Template = ({ text, type }) =>
       twigButton({
         text,
         type,
       });
    
     export const DefaultButton = Template.bind({});
     DefaultButton.args = {
       text: "Button",
       type: "button",
     };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment