Superficial
npm install superficial
A library for managing responsive inline styles on React components.
Superficial allows you to define CSS rules at explicit widths. It automatically generates the values in-between!
Usage
Superficial allows you to define "looks" that are applied to parts of your
components. These looks behave like CSS blocks, except you can specify values
for explicit widths. Wrap your component with superficial()
before you export
it, and pass in a width
prop. Superficial will calculate the right CSS
attributes for whatever width you provide!
import React from 'react';import superficial from 'superficial'; Component { return <h1 => Be Superficial! </h1> ; } MyComponentlooks = header: // We want the font-size to be 12px when the component is 200 pixels wide, // but 20px when the component is 400 pixels wide. fontSize: 200: '12px' 400: '20px' base: // You can also group properties by specifying the width first 200: // Use your favorite shorthand properties. Superficial will interpolate // the values individually margin: '0px auto' padding: '16px 12px 4px' 400: margin: '16px auto' padding: '8px 18px 14px' ; MyComponent;
In this example, we specified CSS properties for when the component is 200px wide and 400px wide. But if we pass it an intermediary value, it computes the values for us!
<MyComponent = />
The resulting HTML looks like this:
Let's Be Superficial!
Where does the width come from?
Superficial's strength comes from the ability to express your component's look as a function of its own width (rather than the document width, as with media queries). Where this really shines is in the ability to determine the width value of child components, as in the following example:
{ return <MyComponent = /> <MyComponent = /> ;}
Superficial also provides a small <DocumentListener />
component that will
automatically pass down the browser width to its children (and listen for any
resizing). You can use it like this:
ReactDOM;
Now your App
component will have a width property that matches the
clientWidth
of the browser page.
Special cases
There are a few cases where the Superficial library makes some assumptions
about what you intend, where otherwise things might be ambiguous. In the
examples below, we show what the resulting value is exactly between two
breakpoints. Note that the between
function does not exist; it is provided
solely to illustrate the library's behavior in certain cases.
CSS shorthand expressions
// '10px auto' // 'rgb(25, 75, 125)' // ERROR! // '2.5px 10px 0'
Superficial will interpolate all the values in your string, allowing you to use
shorthand CSS values. The library is agnostic to what format you use, and will
simply pull out the values from each breakpoint. The format, however, needs to
be the consistent for a given CSS property. Mixing different types of shortand
(margin: { 100: '15px auto', 400: '20px 5px 10px' }
) is not supported. The
only exception is that 0
can be applied against an expression of any length.
Single-Breakpoint expressions
height: 500: '100px' 0: 0 ; // Standard definitionheight: 500: '100px' ; // Syntactic shortcut
Frequently, it may be valuable to express a single breakpoint, assuming a
linear responsiveness for values below. If a property is specified for a given
width, it can be assumed that it should scale proportionally below that width.
If a single breakpoint is provided, Superficial will treat the expression as
though there were an additional breakpoint at 0
width for which the property
value is 0.
Mixing unit and unitless values
// '7.5px' // '12.5em'
If you use a value without a unit (for example, border: 0
), and at a
different width, provide a value with a unit, Superficial will assume it
should use the unit throughout that range. So if the nearest neighbor is
border: '15px'
, pixel values will be used in-between.
Mixing different units
// 'calc(7.5px + (10%))' // 'calc(12.5vw + (21rem))'
If you mix units, Superficial will try to handle this gracefully using the CSS calc() function. Note that this may not be supported in legacy browsers, so we recommend you use the same units across breakpoints.
Using "static" values
// '7.5px auto' // 'inherit' // 'none'
Values like auto
, inherit
, and none
don't really make much sense as part
of a range of values. If you choose to use them, they will take precedence over
their counterparts.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/gisikw/superficial
License
The library is available as open source under the terms of the MIT License.