Socket
Socket
Sign inDemoInstall

@emotion/use-insertion-effect-with-fallbacks

Package Overview
Dependencies
Maintainers
4
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@emotion/use-insertion-effect-with-fallbacks

A wrapper package that uses `useInsertionEffect` or a fallback for it


Version published
Weekly downloads
8.7M
decreased by-2.25%
Maintainers
4
Weekly downloads
 
Created

What is @emotion/use-insertion-effect-with-fallbacks?

@emotion/use-insertion-effect-with-fallbacks is a utility package designed to handle CSS insertion in React applications. It leverages the `useInsertionEffect` hook when available, and falls back to other methods when it is not, ensuring compatibility across different React versions.

What are @emotion/use-insertion-effect-with-fallbacks's main functionalities?

CSS Insertion with useInsertionEffect

This feature allows you to insert CSS into the document head using the `useInsertionEffect` hook when available. It ensures that styles are applied correctly and cleaned up when the component unmounts.

import { useInsertionEffectWithFallbacks } from '@emotion/use-insertion-effect-with-fallbacks';

const useStyles = () => {
  useInsertionEffectWithFallbacks(() => {
    const style = document.createElement('style');
    style.textContent = '.my-class { color: red; }';
    document.head.appendChild(style);
    return () => {
      document.head.removeChild(style);
    };
  }, []);
};

const MyComponent = () => {
  useStyles();
  return <div className="my-class">Hello World</div>;
};

Fallback to useLayoutEffect

If `useInsertionEffect` is not available, the package falls back to using `useLayoutEffect`. This ensures compatibility with older versions of React that do not support `useInsertionEffect`.

import { useInsertionEffectWithFallbacks } from '@emotion/use-insertion-effect-with-fallbacks';

const useStyles = () => {
  useInsertionEffectWithFallbacks(() => {
    const style = document.createElement('style');
    style.textContent = '.my-class { color: blue; }';
    document.head.appendChild(style);
    return () => {
      document.head.removeChild(style);
    };
  }, []);
};

const MyComponent = () => {
  useStyles();
  return <div className="my-class">Hello World</div>;
};

Other packages similar to @emotion/use-insertion-effect-with-fallbacks

FAQs

Package last updated on 20 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc