styled-transform-proxy
A wrapper function for extending styled-components via a custom transform function. The
wrapper function is curried and takes two arguments - the transform function and the
original styled
function from styled-components
. The transform receives the original
strings & interpolations and is expected to return an array containing the strings and
interpolations with any transformations applied.
Install
# Yarn yarn add styled-transform-proxy # npm npm install --save styled-transform-proxy
Type Definitions
transform :: (Array<String> strings, ...* interpolations) -> [strings, ...interpolations]
styledTransformProxy :: transform -> styled -> styled
Examples
Transform strings
// src/styled.js;;; const transformStrings = ; const transform = ...interpolations; transform styled; // src/components/MyComponent.js; // Results in `.foo` below being transformed to `.bar`const MyComponent = styledspan` .foo { color: red; }`;
Transform interpolations
// src/styled.js;;; const transformInterpolations = ; const transform = strings ...; transform styled; // src/components/MyComponent.js; const MyComponent = styledspan` color: ; background-color: `; // The above is equivalent to:const MyComponent = styledspan` color: ; background-color: ;`;
Composing transforms
The wrapper function being curried means that composing multiple transformations together is simple:
// src/styled.js;;; const transformFoo = ...;const transformBar = ...; const applyTransforms = ; styled;
Third-party libraries
If you're creating a third-party package that utilizes styled-transform-proxy
you can simplify things a bit compared to the above examples. In addition to the wrapper function being curried, it takes the original styled
function as its last argument, meaning instead of this:
;
...you can simply do this:
transform;
Caveats
Currently there is no straightforward way to tap into the extend
method on a styled
component, so the transform function will not be applied when using extend
. So instead
of this:
const ChildComponent = ParentComponentextend`...`;
...you'll need to do this:
const ChildComponent = `...`;