Skip to content

Instantly share code, notes, and snippets.

@miclgael
Last active December 2, 2023 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miclgael/73ce0297321338d4092258d99240710f to your computer and use it in GitHub Desktop.
Save miclgael/73ce0297321338d4092258d99240710f to your computer and use it in GitHub Desktop.
Storybook 7 - global style leak fix
// ==UserScript==
// @name Storybook global style fix
// @namespace Violentmonkey Scripts
// @match http://localhost:6006/
// @grant none
// @version 1.0
// @author Michael Gale <https://www.michaelgale.dev>
// @description Fixes global style leaking into your components in Storybook 7 by harshly removing the offending style tag from the iframe.
// ==/UserScript==
window.onload = function () {
const findAndRemove = () => {
const iframe = document?.querySelector('#storybook-preview-iframe')
const innerDoc = iframe?.contentDocument || iframe?.contentWindow?.document;
const innerStyles = [...innerDoc?.querySelectorAll('style')]
const badStyles = innerStyles?.find(style => {
return style?.innerHTML?.includes('@property')
})
badStyles?.remove()
}
findAndRemove()
setInterval(findAndRemove, 1000)
window.addEventListener('resize', findAndRemove)
}
@miclgael
Copy link
Author

miclgael commented Dec 2, 2023

An overly hectic way of fixing this: storybookjs/storybook#17533

(Requires Violent Monkey )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment