Open thibaudcolas opened 4 years ago
We’ve reviewed this together with @bcdickinson and @William-Blackie today – here are things to be addressed / to think of further,
extends
, include
, include_block
)And clear improvements that this brings:
The takeaway from our conversation is that this comes with clear improvements on multiple fronts (UI, workflow, constraints on file structure), and doesn’t make other areas of the pattern library worse, beyond the initial setup.
There’s more for us to research (cc @bcdickinson) on whether this would make it easier for us to address the pattern library’s limitation to basic data structures (as opposed to Python-specific types / Django objects), and whether it could help address the limitation of having to hard-code a lot of mock data (integration with factories).
@bcdickinson I think this would be a good place to share your findings once you’ve had the time to look into this more.
I’ve experimented with this a bit more since writing the last comment and found something that seems to work quite well – automatically generating stories based on the project’s YAML files.
There is a barebones example of this at https://github.com/torchbox/storybook-django/blob/master/demo/storybook/legacy.stories.js, and here is a more advanced example:
This has a number of advantages:
{% include %}
pattern for the component.Doing this, I also realised a few shortcomings of the prototype:
Here’s a quick GIF demo of this setup:
I've added this to BCUK (although have not made an MR based on this - I can do if that would be useful) and have had a good look around. My findings:
Good 👍
Observations 👀
patterns/molecules/accordion/accordion.html
) component will toggle open/closed when viewing in DPL but just reloads the page in Storybook Django. Could be related the JS for the component (static_src/javascript/components/accordion.js
) itself. Thoughts 🤔
Thank you @chris-lawton 🤘 This is very useful feedback! I’ve removed links straight to the project’s code from your comment. I don’t think this is particularly problematic but since this is all public I’d rather be extra cautious. Might be worth raising with the sysadmin whether this kind of caution is warranted or not.
For JS – I suspect this is just because JS for each component isn’t loaded in the Storybook version. This is something I didn’t implement with the sample code I sent you. The vanilla pattern library renders each component as if they were inside the project’s base
template: https://github.com/torchbox/django-pattern-library/tree/master/docs#customising-the-patterns-surroundings
…which (for most builds) includes the project’s stylesheet, JS, SVG icons. For the Storybook version, by default patterns are rendered directly without any base template, so we need to load the CSS & JS manually, as well as any other component dependencies. Inside the Storybook preview.js
:
import { configure, addDecorator, addParameters } from '@storybook/react';
import { withA11y } from '@storybook/addon-a11y';
import iconSprite from '../../project_styleguide/templates/patterns/atoms/sprites/sprites.html';
configure(() => {
const hasIcons = document.querySelector('[data-storybook-svg-icons]');
if (!hasIcons) {
const icons = document.createElement('div');
icons.setAttribute('data-storybook-svg-icons', true);
icons.innerHTML = iconSprite;
document.body.appendChild(icons);
}
// eslint-disable-next-line global-require
require('../sass/main.scss');
}, module);
When I get back to this I’ll check what would be the best way to load JS, whether it’s as simple as require('../js/main.js');
, or whether we’d want to do something else.
Here is a short update on this prototype.
It seems to be working very well. I personally feel more productive with it than with DPL’s YAML files.
Compared with django-pattern-library re the above comments,
import { Pattern } from 'storybook-django/src/react';
export default {};
export const Base = () => <Pattern filename={__filename} />;
This is the equivalent of not having a .yaml
file with DPL. So 3 lines is still more than 0 line 0 file, but not as big as it used to be.
There is only one I’m aware of compared to django-pattern-library – the current API to render a template only supports passing context and tags for the currently-rendered template, not any of its dependencies. For vanilla DPL this is only an issue in a few cases (#138, #8).
I’m interested in trying out a version of DPL with the Storybook UI but without the runtime dependencies. This is dependent on the release of Storybook v7, as I want to make sure to not rely on any internal or deprecated APIs that might get removed (storiesOf
API vs. CSF).
The prototype has been further trialed by @stevedya – we ran into #138, #193, and found an unrelated django-pattern-library bug: #199.
We tried this with Storybook v6.5, which is much lighter than previous releases. The next release, v7.0, promises to be even lighter, which might alleviate some of the concerns with using npm dependencies for this in addition to the Python django-pattern-library.
This was done on the Wagtail project, which uses storybook-django. Here is a PR showing the work involved: https://github.com/wagtail/wagtail/pull/8665.
Tracking issue for conversations relating to storybook-django, a prototype Storybook integration of the pattern library.
Related issues
All of the issues tagged
storybook
are related to this integration – either because the Storybook UI addresses them, or because using the pattern library through such an integration would drastically change the implementation.Addressed by Storybook
Here are open issues that would no longer be relevant when using the pattern library with Storybook:
87 – Add support for pattern variants
102 – Add a way to customise the iframe dimensions by manually dragging
101 – Add option to customise breakpoints per-project
99 – Make pattern library UI WCAG 2.1 AA compliant
98 – Show the pattern name rather than the file path
97 – Hide directory structure for single patterns
96 – List patterns in alphabetical order
95 – Add ability to persist menu state
86 – Docs: Describe the approach to the base template which should include CSS and JS
92 – Add ability to view html/css/js for each pattern within the pattern library
And issues for which the implementation would be drastically simpler:
17 – Export static version of the pattern library hostable as a static site (Storybook already supports this, we would just need to implement this for the patterns’ rendering)
18 – Add a command to checks whether each template renders correctly (Storyshots addon + part of #17)
91 – Add support for pattern status (In progress, In review, Signed off etc) (Storybook probably has an addon for this)
100 – Add view showing multiple screen sizes next to each other at the same time (Storybook probably has an addon for this)