I want to write different stories, one for each @media query in my CSS.
For example, I hide certain elements for @media print. What I would like to achieve is something like this:
export const PrintFoo: Story = {
render: (args) => (
<MediaWrapper media="print">
<Foo .../>
</List>
),
};
This should be a common task in Storybook (for testing different screen sizes) but I couldn't find any documentation how to do this.
I found JavaScript which modifies the style sheet (replaces print with screen: https://github.com/RRMoelker/print-css-toggle/blob/master/src/index.js) but for this, I would need a "post rendering hook but before compare" where I can call this from a story.
Or do I have to write an addon for this?
Here is a wrapper element (React with TypeScript) which can simulate the print mode. The wrapper collects all media rules of the CSS stylesheets attached to the document. If you switch to
@media print, it will change all@media screenrules to@media disabled(effectively rendering them uselss) and replaceprintwithscreenin all@media printrules, making them the default.If you have a component that uses
@media printlike this one:then you can test this in two stories: