styled-providers

0.0.8 • Public • Published

✨ Styled Providers

Build Status

Handy Providers for Emotion / Styled Components

Installation

npm install --save-dev styled-providers

Requirements

Usage

Emotion and Styled Components are supported.

Emotion

import React from "react";
import styled from "@emotion/styled";
import { ScopeProvider } from "styled-providers/emotion";
 
const View = styled.div`
    background: blue;
`;
 
const App = () => {
    return (
        <div id="#app">
            <ScopeProvider scope="#app">
                <View />
            </ScopeProvider>
        </div>
    );
};

Styled Components

import React from "react";
import styled from "styled-components";
import { ScopeProvider } from "styled-providers/styled-components";
 
const View = styled.div`
    background: blue;
`;
 
const App = () => {
    return (
        <div id="#app">
            <ScopeProvider scope="#app">
                <View />
            </ScopeProvider>
        </div>
    );
};

Components

FrameProvider

Provides the correct document.head to mount the <style> tag for either Emotion or Styled Components. This is necessary when rendering CSS-in-JS generated styles within elements like iFrames on runtime.

Example
import React from "react";
import styled from "@emotion/styled";
import { FrameProvider } from "styled-providers/emotion";
import Frame from "react-frame-component";
 
const View = styled.div`
    background: blue;
`;
 
const App = () => {
    return (
        <Frame>
            <View />
        </Frame>
    );
};

ScopeProvider

Provides a scope to prefix before generated classNames for either Emotion or Styled Components. Prefixing increases specificity, allowing for smoother integrations with pre-existing CSS rules.

Example
import React from "react";
import styled from "styled-components";
import { ScopeProvider } from "styled-providers/emotion";
 
const View = styled.div`
    background: blue;
`;
 
const App = () => {
    return (
        <div id="#app">
            <ScopeProvider scope="html #app">
                <View />
            </ScopeProvider>
        </div>
    );
};

In the above example, rendered selectors (e.g. .css-123jda) will be prefixed with html #app, resulting in selectors like html #app .css-123jda)

Package Sidebar

Install

npm i styled-providers

Weekly Downloads

1

Version

0.0.8

License

MIT

Unpacked Size

17.4 kB

Total Files

11

Last publish

Collaborators

  • itsjonq