startijenn-rem
JavaScript function to convert CSS rem units. Used by postcss-rem.
Usage
Install with npm i startijenn-rem
.
import rem, { em, px, convert } from "startijenn-rem";
const unitless = rem(24);
const simple = rem("24px");
const multipleValues = rem("5px -10px 1.5rem");
const multipleMixedValues = rem("1px solid black");
const commaSeparatedValues = rem("0 0 2px #ccc, inset 0 0 5px #eee");
const variable = "5px";
const withVariable = rem(`${variable} 10px`);
const array = rem([24, "24px", "5px -10px 1.5rem"]);
const object = rem({
fontSize: 24,
margin: "24px",
padding: "5px -10px 1.5rem",
});
const changingBaseline = rem("24px", { baseline: 10 });
const changingPrecision = rem("16px", { baseline: 12, precision: 3 });
const convertToPx = px("1.5rem 24px");
const convertFunction = convert("24px", "em", { baseline: 12 });
const StyledComponent = styled.h1`
font-size: ${(fontSize) => rem(fontSize)};
padding: ${(fontSize) => em(12, fontSize)};
margin: ${rem("8px 16px")};
`;
const h1 = (text) => <StyledComponent fontSize={24}>{text}</StyledComponent>;
You can change the default options of the functions by doing your own aliases.
import { convert } from "startijenn-rem";
export const rem = (value) => convert(value, "rem", { baseline: 10 });
export const em = (value, baseline = 10) => convert(value, "em", { baseline });
export default rem;
import rem, { em } from "./utils/rem";
const unitless = rem(24);
const convertToEm = em("12px", "24px");
const object = rem({
fontSize: 24,
margin: "24px",
padding: "5px -10px 1.5rem",
});
const StyledComponent = styled.h1`
font-size: ${(fontSize) => rem(fontSize)};
padding: ${(fontSize) => em("6px 12px", fontSize)};
`;
const h1 = (text) => <StyledComponent fontSize={24}>{text}</StyledComponent>;