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.

support remove styles of `injectGlobal`

See original GitHub issue

The problem

I am using emotion in sevelte components, where I can’t use @emotion/react, and instead I could use @emotion/css.

I want to inject some global styles when component mount and remove them when component unmount. With injectGlobal I have no way to undo the injection.

Proposed solution

Alternative solutions

Additional context

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
Andaristcommented, Nov 24, 2020

This is an interesting use case but it’s also quite problematic to handle right now and supporting this out of the box in the current injectGlobal could have been a breaking change.

You can implement your own version of injectGlobal under 20 LOC that would support this:

import { serializeStyles } from "@emotion/serialize";
import { StyleSheet } from "@emotion/sheet";
import { serialize, compile, middleware, rulesheet, stringify } from "stylis";

function injectGlobal(...args) {
  const { name, styles } = serializeStyles(...args);
  const sheet = new StyleSheet({
    key: `global-${name}`,
    container: document.head
  });
  const stylis = (styles) =>
    serialize(
      compile(styles),
      middleware([
        stringify,
        rulesheet((rule) => {
          sheet.insert(rule);
        })
      ])
    );
  stylis(styles);
  return () => sheet.flush();
}

You can check out the demo here. It’s using React for rendering as that was just convenient for me but doesn’t use any React-specific APIs for styling.

0reactions
gregjacobscommented, Nov 25, 2020

@Andarist Interesting. Ok, thanks for that, and the quick response. Removal of rules has been on the back of my mind for large portal systems which load many apps as the user is using it, but maybe it’s not necessary after all.

Best, Greg

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to update or remove global styles in Emotion
Learn how to change globally injected styles in Emotion without breaking the rest of your app's styles.
Read more >
FAQs - styled-components
When using global styling APIs like createGlobalStyle or the former injectGlobal, adding and removing certain styles from the DOM like @font-face definitions ...
Read more >
Angular - How to remove <style> element from <head> after ...
So the service should compile sass files and inject global styles? – Hrvoje Golcic. May 13, 2018 at 13:39. No, ...
Read more >
Migrating to Recipe 10
In order to support the full set of CSS selectors with emotion, the Recipe ... First, find instance of injectGlobal in your app,...
Read more >
styled-components | Yarn - Package Manager
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress. react, css, css-in-js,...
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