Comments (3)
@philwolstenholme and whoever else this helps:
You can work around the Promise issue in Storybook HTML by using Storybook's (experimental) loaders feature. A story itself cannot be an async function, but you can set it to render the result of an async loader.
The pattern I'm using looks like this:
import button from './button.twig';
import { ButtonSpan } from './ButtonSpan.stories';
export default { title: 'Button' };
// never call this directly (only gets called by Storybook);
// EVERY Twig-story should be a function that looks like this.
export const Button = (_, { loaded: { component } }) => component;
// THIS is what you call directly if there's need
Button.render = async args => {
// Rendering logic goes here.
// If, instead of `include` or `embed`, you'd like to
// inject another Twig-story directly (e.g., if you have
// `{{ buttonSpan }}` in `button.twig`), `await` its `render()` method:
const buttonSpan = await ButtonSpan.render({ text: args.text });
// Same goes for any `.twig` imports used directly:
const renderedButton = await button({ buttonSpan });
return renderedButton;
};
Button.args = {
text: 'some button text'
};
This works in conjunction with the following global loader, set in preview.js
:
export const loaders = [
async ({ args, originalStoryFn }) => {
if (originalStoryFn.render) {
const component = await originalStoryFn.render(args);
return { component };
}
}
];
from twing-loader.
Thinking about it a bit more, the issue here is probably that I am using Storybook HTML, and if I were to use Storybook with a JS flavour then the async nature would probably be fine.
from twing-loader.
If I switch to using
"twing": "2.3.7",
"twing-loader": "2.0.2"
Then this works fine:
const template = require('./button.twig');
export default {
title: 'Components/Button',
};
export const Default = () =>
template({
message: 'bar',
});
But it feels unprofessional to use 2 major versions older version of a dependency on a new project…
from twing-loader.
Related Issues (20)
- Error when resolving environmentModulePath HOT 3
- Possible to use namespaces in browser? HOT 11
- Windows compatibility HOT 5
- Migrate to Twing@3
- Webpack do not reload page when Twig template is edited HOT 4
- twing-loader is not compatible with Twing@4 HOT 2
- How to use with html-webpack-plugin HOT 2
- Update variables in "renderContext"
- npm prepare script should be used instead of prepack
- Catch Twing errors and emit them with Webpack API HOT 2
- Warnings with webpack 5 and HtmlWebpackPlugin
- Webpack 5 support HOT 1
- Feature request: option to render templates surrounded by HTML comments indicating Twig file's path
- Loading templates from string HOT 2
- How to force new TwingEnvironment when loading through webpack? HOT 1
- interpolation in include statements
- `source` function is not supported
- twing-loader is not compatible with Twing@6 HOT 2
- Improve the README
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from twing-loader.