Storybook: Nunjucks template?

Created on 7 Feb 2019  路  4Comments  路  Source: storybookjs/storybook

I am currently using storybook html and got everything I need going except one thing, that is the support for importing a nunjucks partial. Is there a way to do that? If so what鈥檚 the best approach?

I see some people using storybook set up with vue template option but I feel like hack approach.

Let me know what you think.

Thank you!

Most helpful comment

main.js

module.exports = {
    stories: [
        '../src/**/*.stories.mdx',
        '../src/**/*.stories.@(js|jsx|ts|tsx)'
    ],
    addons: [
        '@storybook/addon-links',
        '@storybook/addon-essentials',
        "@storybook/preset-scss"
    ],
    webpackFinal: async (config, { configType }) => {
        // `configType` has a value of 'DEVELOPMENT' or 'PRODUCTION'
        // You can change the configuration based on that.
        // 'PRODUCTION' is used when building the static version of storybook.

        // Make whatever fine-grained changes you need
        config.module.rules.push({
            test: /\.njk$/,
            use: [
                {
                    loader: 'simple-nunjucks-loader',
                    options: {}
                }
            ]
        });

        // Return the altered config
        return config;
    }
};

test.stories.js

import { action } from '@storybook/addon-actions';

import Button from  'Button.njk';

export default {
    title: 'Design System/Atoms/Button',
    argTypes: {
        label: { control: 'text' }
    }
};

const Template = ({ onClick, label }) => {
    return Button({ label });
};

export const Text = Template.bind({});
Text.args = {
    label: 'Button',
    onClick: action('onClick')
};

Button.njk

{% macro Button(label='Button') %}
<button type="button" class="lta-btn">{{ label }}</button>
{% endmacro %}

{{ Button(label) }}

And that is it, this works.

All 4 comments

Decided to move away form storyboard since I found a different solution.

Sorry for the hijack, but I stumbled upon this issue, what alternative solution did you found?

could you add nunjucks-loader to your webpack config?

https://github.com/at0g/nunjucks-loader

main.js

module.exports = {
    stories: [
        '../src/**/*.stories.mdx',
        '../src/**/*.stories.@(js|jsx|ts|tsx)'
    ],
    addons: [
        '@storybook/addon-links',
        '@storybook/addon-essentials',
        "@storybook/preset-scss"
    ],
    webpackFinal: async (config, { configType }) => {
        // `configType` has a value of 'DEVELOPMENT' or 'PRODUCTION'
        // You can change the configuration based on that.
        // 'PRODUCTION' is used when building the static version of storybook.

        // Make whatever fine-grained changes you need
        config.module.rules.push({
            test: /\.njk$/,
            use: [
                {
                    loader: 'simple-nunjucks-loader',
                    options: {}
                }
            ]
        });

        // Return the altered config
        return config;
    }
};

test.stories.js

import { action } from '@storybook/addon-actions';

import Button from  'Button.njk';

export default {
    title: 'Design System/Atoms/Button',
    argTypes: {
        label: { control: 'text' }
    }
};

const Template = ({ onClick, label }) => {
    return Button({ label });
};

export const Text = Template.bind({});
Text.args = {
    label: 'Button',
    onClick: action('onClick')
};

Button.njk

{% macro Button(label='Button') %}
<button type="button" class="lta-btn">{{ label }}</button>
{% endmacro %}

{{ Button(label) }}

And that is it, this works.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zvictor picture zvictor  路  3Comments

MrOrz picture MrOrz  路  3Comments

miljan-aleksic picture miljan-aleksic  路  3Comments

wahengchang picture wahengchang  路  3Comments

sakulstra picture sakulstra  路  3Comments