question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular.js causes styled-components to break on IE 11

See original GitHub issue

Version

1.4.4 (maybe a lot older too)

Reproduction

Webpackbin doesn’t work on IE 11… So here’s a repo! https://github.com/dfrankland/styled-components-angular-ie-11-conflict

Steps to reproduce

Follow steps in linked repo.

Expected Behavior

Work like normal.

Actual Behavior

No CSS rules are applied to the style element.

Why?

Angular will recompile all the individual DOM elements including those in the head. Normally, on Chrome and other browsers this isn’t an issue, but on IE 11 this causes text nodes to lose their parentNode. This could occur due to anything though, it doesn’t have to be Angular. The more longwinded explanation:

  1. Style elements and text nodes get inserted in ComponentStyle

  2. Afterwards, anything could mutate the DOM, such as Angular, causing issues later

  3. A styled component is ready to render so generateAndInjectStyles inside of ComponentStyle is called

  4. appendRule inside the Glamor sheet tries to mutate a text node, but it has already been removed from the DOM, thus no styles are applied to the style tags created in step 1

Fix

I propose to move the appending of text nodes to the style element DOM only once the component is ready to render, rather than doing it preemptively.

https://github.com/styled-components/styled-components/blob/master/src/vendor/glamor/sheet.js#L125

const textNode = document.createTextNode(rule)
- last(this.tags).appendChild(textNode)
- insertedRule = { textNode, appendRule: newCss => textNode.appendData(newCss)}
+ insertedRule = {
+   textNode,
+   appendRule: newCss => textNode.appendData(newCss),
+   appendNode: () => last(this.tags).appendChild(textNode),
+ };

https://github.com/styled-components/styled-components/blob/master/src/models/ComponentStyle.js#L39

const selector = nameGenerator(hash)
inserted[hash] = selector
const root = parse(`.${selector} { ${flatCSS} }`)
postcssNested(root)
autoprefix(root)
this.insertedRule.appendRule(root.toResult().css)
+ this.insertedRule.appendNode()

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

8reactions
byrektcommented, Sep 18, 2017

Just a heads-up. If you use auto-bootstrapping with ng-app on the html tag of your index.html, you can try moving ng-app to the body tag. That resolved it for me.

2reactions
mxstbrcommented, Aug 28, 2017
- angular.bootstrap(document, ['blah'], {});
+ angular.bootstrap(document.body, ['blah'], {});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Work-around for Styled-component not working on Internet ...
I have styled-components on my web-app. They work great. Except on Internet Explorer 11. They completely break and the layout is just a...
Read more >
Releases - styled-components
Only major versions have the potential to introduce breaking changes (noted ... from emitting empty style tags, which would cause issues in IE11...
Read more >
Why We're Breaking Up with CSS-in-JS - DEV Community ‍ ‍
Styled-components are a major source of performance issues with re-renders and Js execution time especially in mobile devices. If your code is ...
Read more >
Stop Repeating Yourself in Angular: How to Create Abstract ...
I have been working on Angular (not AngularJS, not anymore) since it was ... we see fit for our holy cause, i.e. avoiding...
Read more >
Goober: A lightweight CSS-in-JS solution - LogRocket Blog
It is designed to work with vanilla JavaScript as well as its frontend libraries/frameworks like React, Vue.js, Angular, Svelte, etc. Support ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found