Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add units to config #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Type: `Object | Null`
Default:
```js
{
units: 'rem',
rootValue: 16,
unitPrecision: 5,
propList: ['font', 'font-size', 'line-height', 'letter-spacing'],
Expand All @@ -72,6 +73,7 @@ Default:
}
```

- `rem` (String) Convert target unit.
- `rootValue` (Number | Function) Represents the root element font size or returns the root element font size based on the [`input`](https://api.postcss.org/Input.html) parameter
- `unitPrecision` (Number) The decimal numbers to allow the REM units to grow to.
- `propList` (Array) The properties that can change from px to rem.
Expand Down
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const filterPropList = require("./lib/filter-prop-list");
const type = require("./lib/type");

const defaults = {
units: 'rem',
rootValue: 16,
unitPrecision: 5,
selectorBlackList: [],
Expand Down Expand Up @@ -43,13 +44,13 @@ function convertLegacyOptions(options) {
});
}

function createPxReplace(rootValue, unitPrecision, minPixelValue) {
function createPxReplace(rootValue, unitPrecision, minPixelValue, units) {
return (m, $1) => {
if (!$1) return m;
const pixels = parseFloat($1);
if (pixels < minPixelValue) return m;
const fixedVal = toFixed(pixels / rootValue, unitPrecision);
return fixedVal === 0 ? "0" : fixedVal + "rem";
return fixedVal === 0 ? "0" : fixedVal + units;
};
}

Expand Down Expand Up @@ -145,7 +146,8 @@ module.exports = (options = {}) => {
pxReplace = createPxReplace(
rootValue,
opts.unitPrecision,
opts.minPixelValue
opts.minPixelValue,
opts.units
);
},
Declaration(decl) {
Expand Down
13 changes: 13 additions & 0 deletions spec/pxtorem-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ describe("unitPrecision", function() {
});
});

describe("units", function() {
it("should replace with specified unit", function() {
var expected = ".rule { font-size: 0.938em }";
var options = {
unitPrecision: 3,
units: 'em'
};
var processed = postcss(pxtorem(options)).process(basicCSS).css;

expect(processed).toBe(expected);
});
});

describe("propWhiteList", function() {
// Deprecate
it("should only replace properties in the white list - legacy", function() {
Expand Down