Skip to content

Commit

Permalink
Don't attempt to use attachShadow if the browser doesn't support it
Browse files Browse the repository at this point in the history
  • Loading branch information
ghengeveld committed Jan 21, 2021
1 parent cc85229 commit c7513a7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/withPseudoState.js
Expand Up @@ -52,13 +52,15 @@ function initPseudoStyles(shadowRoot) {
// Reinitialize CSS enhancements every time the story changes
addons.getChannel().on(STORY_RENDERED, () => initPseudoStyles())

// Monkeypatch the attachShadow method so we can handle pseudo styles inside shadow DOM
Element.prototype._attachShadow = Element.prototype.attachShadow
Element.prototype.attachShadow = function attachShadow(init) {
if (!this._attachShadow) return // IE doesn't support shadow DOM
const shadowRoot = this._attachShadow({ ...init, mode: "open" })
setTimeout(() => initPseudoStyles(shadowRoot))
return shadowRoot
// IE doesn't support shadow DOM
if (Element.prototype.attachShadow) {
// Monkeypatch the attachShadow method so we can handle pseudo styles inside shadow DOM
Element.prototype._attachShadow = Element.prototype.attachShadow
Element.prototype.attachShadow = function attachShadow(init) {
const shadowRoot = this._attachShadow({ ...init, mode: "open" })
setTimeout(() => initPseudoStyles(shadowRoot))
return shadowRoot
}
}

export const withPseudoState = (StoryFn) => {
Expand Down
1 change: 1 addition & 0 deletions stories/ShadowRoot.js
Expand Up @@ -4,6 +4,7 @@ export const ShadowRoot = () => {
const ref = React.useRef()

React.useEffect(() => {
if (!ref.current.attachShadow) return
ref.current.attachShadow({ mode: "closed" })
ref.current.shadowRoot.innerHTML = `
<style>
Expand Down

0 comments on commit c7513a7

Please sign in to comment.