GitXplorerGitXplorer
k

styled-proxy

public
1 stars
0 forks
5 issues

Commits

List of commits on branch master.
Verified
28b145d9af9cabb924ee596a3c8a265e1d5cf1f5

chore(ci): cache Yarn

kkinday committed 7 years ago
Verified
7e01f98a0b862cef2df420bb30bcf50bbc102f93

chore: check package size

kkinday committed 7 years ago
Verified
4456b2d9ab633a9d097afa5738bdd431e305b46c

chore: set up Semantic Release

kkinday committed 7 years ago
Verified
846fcce5c0670ec32eb68fcc99c16e6a00e8c3c4

1.0.1

kkinday committed 7 years ago
Verified
2037f1a2b1e5a31cd4a7806ed4a3ee2b0747aa28

fix: fix ES5 compatibility

kkinday committed 7 years ago
Verified
88cc43220cdb7882536cc2bd1db2b2d4d55a1c74

fix: restrict peer dependency to React 16

kkinday committed 7 years ago

README

The README file for this repository.

<StyledProxy>

Avoid unneeded DOM nodes while using CSS-in-JS components

npm Code style: prettier Build Status codecov

This small component allows to create visual primitives without excessive DOM pollution. Designed to be library-agnostic. Tested with styled-components.

Installation

npm Yarn
npm install styled-proxy --save yarn add styled-proxy

Usage

Pass it as base component to CSS-in-JS library of your choice. Then wrap any component with it to get it styled without extra DOM nodes.

// bubble.js
import StyledProxy from 'styled-proxy'
import styled from 'styled-components'

export default styled(StyledProxy)`
  padding: 1em 2em;
  border-radius: .3em;
  box-shadow: 0 .3em .5em rgba(0, 0, 0, .2);
`
// app.js
import Bubble from './bubble'

function App() {
  return (
    <main>
      <h1>Styled Proxy demo</h1>
      <Bubble><p>This will be a single tag</p></Bubble>
    </main>
  )
}
<!-- Resulting HTML -->
<main>
  <h1>Styled Proxy demo</h1>
  <p class="sc-hfu239h">This will be a single tag</p>
</main>

Important requirements are following:

  • library you use must accept components (styled(StyledProxy));
  • library you use must inject className into given component;
  • component passed as child must accept className.

API

children

Type: React elements.

The StyledProxy styles target, will get className produced by CSS-in-JS library unless component property is given (see below).

<SomeStyledProxy>
  <div>Foo</div>
</SomeStyledProxy>

Resulting HTML:

<div class="sc-3j03y55">Foo</div>

Multiple children will get the same class:

<SomeStyledProxy>
  <div>Foo</div>
  <div>Bar</div>
  <div>Baz</div>
</SomeStyledProxy>

Resulting HTML:

<div class="sc-3j03y55">Foo</div>
<div class="sc-3j03y55">Bar</div>
<div class="sc-3j03y55">Baz</div>

className

Type: string

Class to inject together with a generated one. Allows to pass additional classes and helps nesting StyledProxy instances.

<FirstStyledProxy>
  <SecondStyledProxy className="custom">
    <p>Example text</p>
  </SecondStyledProxy>
</FirstStyledProxy>

Resulting HTML:

<p class="sc-hfu239h sc-384hb3j custom">
  Example text
</p>

component

Type: React class, stateless component or string.

Render the StyledProxy element as regular component. Whatever passed to component prop will be used as element type. className will be attached to that element and not to children.

JSX:

<SomeStyledProxy component="blockquote">
  <p>Example text</p>
</SomeStyledProxy>

Resulting HTML:

<blockquote class="sc-384hb3j">
  <p>Example text</p>
</blockquote>

License

MIT © Leonard Kinday