This module complements the react and styled-components libraries to create an API that removes the cognitive load and arduous boilerplate associated with responsive scenarios.
Setting a min-width
and max-width
on your elements is a great way to create fluid layouts that adhere to responsive methodologies.
Unfortunately min-*
and max-*
declarations are not supported natively in CSS for things like font-size
, padding
, margin
, etc. These properties would benefit greatly from this functionality to help make content more accessible to a wider range of device sizes.
This module addresses the scenarios where:
-
Users can experience jarring layout reflows as various breakpoints are triggered forcing abrupt changes to CSS values.
-
Percentage based values (referencing things like viewport or an element container) can encounter cases where values scale to exaggerated limits (both big and small) due to the absence of Minimum and Maximum thresholds.
This demo retro fits several Bootstrap components with the fluid resizing system this module offers. Resize the window to see the responsive measurement declarations scale up / down while staying within the limits of their thresholds.
Install the module from NPM .
npm install --save fish-tacos
Import the module into your project.
import ft from "fish-tacos";
The API is very simple. Specify the CSS property that you want to change and supply a Minimum and Maximum threshold to restrict scaling.
Because designers like to supply their measurements in pixel based units our API uses pixels as the base target and converts them into REM's in the final output. This gives styles an enhanced level of accessibility (with dynamic font scaling) while making design and development collaboration easier.
The result is pure, static CSS. This means the fluid scaling is based on native browser functionality and therefore performant.
ft("font-size", [20, 32]);
The above declaration will create the following vanilla CSS:
font-size: 1.25rem;
@media (min-width: 30rem) {
font-size: 4.166666666666667vw;
}
@media (min-width: 48rem) {
font-size: 2rem;
}
If you want to target a property that uses top
, right
, bottom
and left
references for more granularity you can use the more verbose permutation below.
ft("margin", { top: [30, 60], bottom: [10, 30] });
The above declaration will create the following vanilla CSS:
margin-top: 1.875rem;
@media (min-width: 30rem) {
margin-top: 6.25vw;
}
@media (min-width: 60rem) {
margin-top: 3.75rem;
}
margin-bottom: 0.625rem;
@media (min-width: 30rem) {
margin-bottom: 2.0833333333333335vw;
}
@media (min-width: 90rem) {
margin-bottom: 1.875rem;
}
As of release 1.2.0 you can now pass in a static number or string and have it be included as a non-fluid measurement 😃.
- String: Will be included verbatim.
- Number: Should be supplied as a pixel reference and will be converted to REM's.
ft("margin", { top: 50, right: "auto", bottom: 20, left: "auto" });
The above declaration will create the following vanilla CSS:
margin-top: 3.125rem;
margin-right: auto;
margin-bottom: 1.25rem;
margin-left: auto;
As of release 1.3.0 you can now consolidate verbose references that share the same values into a single declaration by comma separating their keys 👍.
ft("margin", { "top,bottom": [20, 50], "right,left": "auto" });
The above declaration will create the following vanilla CSS:
margin-top: 1.25rem;
@media (min-width: 30rem) {
margin-top: 4.166666666666667vw;
}
@media (min-width: 75rem) {
margin-top: 3.125rem;
}
margin-bottom: 1.25rem;
@media (min-width: 30rem) {
margin-bottom: 4.166666666666667vw;
}
@media (min-width: 75rem) {
margin-bottom: 3.125rem;
}
margin-right: auto;
margin-left: auto;
Integrating this module into your existing workflow is as easy as swapping out a standard CSS property / value declaration for the new API.
import React from "react";
import ReactDOM from "react-dom";
import styled from "styled-components";
import ft from "fish-tacos";
const Heading1 = styled.h1`
${ft("font-size", [30, 50])}
${ft("margin", { top: [30, 60], "right,left": "auto", bottom: 20 })};
`;
ReactDOM.render(
<Heading1>Hello World 👋</Heading1>,
document.getElementById("app")
);
MIT