Skip to content

Commit 7720a8a

Browse files
committed
cmin / cmax unit support
1 parent c6174a5 commit 7720a8a

8 files changed

+113
-13
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 1.1.0 (2017-02-27)
4+
5+
- cmin / cmax unit support
6+
37
## 1.0.4 (2017-02-27)
48

59
- Fixed the aspect-ratio query

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zeecoder/container-query",
3-
"version": "1.0.4",
3+
"version": "1.1.0",
44
"description": "A PostCSS plugin and Javascript runtime combination, which allows you to write @container queries in your CSS the same way you would write @media queries.",
55
"main": "index.js",
66
"author": "Viktor Hubert <[email protected]>",

src/constants.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export const HEIGHT_UNIT = 'ch';
22
export const WIDTH_UNIT = 'cw';
3+
export const MIN_UNIT = 'cmin';
4+
export const MAX_UNIT = 'cmax';
35
export const DEFINE_CONTAINER_NAME = 'define-container';

src/postcss/isValueUsingContainerUnits.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
22
HEIGHT_UNIT,
33
WIDTH_UNIT,
4+
MIN_UNIT,
5+
MAX_UNIT,
46
} from '../constants';
57

68
/**
@@ -13,6 +15,7 @@ export default function isValueUsingContainerUnits (value) {
1315
return false;
1416
}
1517

18+
// Matching numbers followed by alphanumeric characters and %
1619
const match = value.toLowerCase().match(/(\d+(\.\d+)?)([a-z%]+)/i);
1720

1821
if (match === null) {
@@ -24,9 +27,13 @@ export default function isValueUsingContainerUnits (value) {
2427
return (
2528
unit !== HEIGHT_UNIT &&
2629
unit !== WIDTH_UNIT &&
30+
unit !== MIN_UNIT &&
31+
unit !== MAX_UNIT &&
2732
(
2833
unit.indexOf(HEIGHT_UNIT) === 0 ||
29-
unit.indexOf(WIDTH_UNIT) === 0
34+
unit.indexOf(WIDTH_UNIT) === 0 ||
35+
unit.indexOf(MIN_UNIT) === 0 ||
36+
unit.indexOf(MAX_UNIT) === 0
3037
)
3138
);
3239
}

src/postcss/isValueUsingContainerUnits.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
22
HEIGHT_UNIT,
33
WIDTH_UNIT,
4+
MIN_UNIT,
5+
MAX_UNIT,
46
} from '../constants';
57
import isValueUsingContainerUnits from './isValueUsingContainerUnits';
68

@@ -18,3 +20,9 @@ test('should report true for values using either or both container units', () =>
1820
expect(isValueUsingContainerUnits(`42${WIDTH_UNIT}px`)).toBe(true);
1921
expect(isValueUsingContainerUnits(`42${WIDTH_UNIT}px 42${HEIGHT_UNIT}px 42${WIDTH_UNIT}px`)).toBe(true);
2022
});
23+
24+
test('should report cmin cmax units too', () => {
25+
expect(isValueUsingContainerUnits(`42${MIN_UNIT}px`)).toBe(true);
26+
expect(isValueUsingContainerUnits(`42${MAX_UNIT}px`)).toBe(true);
27+
expect(isValueUsingContainerUnits(`42${MIN_UNIT}px 42${MAX_UNIT}px 42${MIN_UNIT}px`)).toBe(true);
28+
});

src/runtime/adjustValueObjectByContainerDimensions.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import convertCompositValuesToPixel from './convertCompositValue';
1+
import convertCompositValue from './convertCompositValue';
22

33
/**
44
* @param {ContainerDimensions} containerDimensions
@@ -20,7 +20,7 @@ export default function adjustValueObjectByContainerDimensions (containerDimensi
2020
let values = Object.assign({}, valueDefinition);
2121

2222
for (let cssRule in values) {
23-
values[cssRule] = convertCompositValuesToPixel(containerDimensions, values[cssRule]);
23+
values[cssRule] = convertCompositValue(containerDimensions, values[cssRule]);
2424
}
2525

2626
return values;

src/runtime/convertSingleValue.js

+43-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import {
22
HEIGHT_UNIT,
33
WIDTH_UNIT,
4+
MIN_UNIT,
5+
MAX_UNIT,
46
} from '../constants';
57

68
/**
7-
* Normalise unit by removing height or width container unit from the
8-
* beginning of the string.
9+
* Normalise unit by removing the container unit from the beginning of the
10+
* string.
11+
* Ex: "chpx" => "px", "cwem" => "em", etc.
912
*
1013
* @param {string} unit
1114
*/
@@ -18,6 +21,14 @@ function normaliseUnit (unit) {
1821
return unit.substr(WIDTH_UNIT.length);
1922
}
2023

24+
if (unit.indexOf(MIN_UNIT) === 0) {
25+
return unit.substr(MIN_UNIT.length);
26+
}
27+
28+
if (unit.indexOf(MAX_UNIT) === 0) {
29+
return unit.substr(MAX_UNIT.length);
30+
}
31+
2132
return unit;
2233
}
2334

@@ -39,20 +50,43 @@ export default function convertSingleValue (dimensions, value) {
3950
const num = match[1];
4051
const unit = match[3];
4152

42-
if (unit === HEIGHT_UNIT || unit === WIDTH_UNIT) {
53+
if (
54+
unit === HEIGHT_UNIT ||
55+
unit === WIDTH_UNIT ||
56+
unit === MIN_UNIT ||
57+
unit === MAX_UNIT
58+
) {
4359
return value;
4460
}
4561

46-
const relativeToHeight = unit.indexOf(HEIGHT_UNIT) === 0;
4762
const normalisedUnit = normaliseUnit(unit);
4863

64+
const relativeToHeight = (
65+
unit.indexOf(HEIGHT_UNIT) === 0 ||
66+
(
67+
unit.indexOf(MIN_UNIT) === 0 &&
68+
dimensions.height < dimensions.width
69+
) ||
70+
(
71+
unit.indexOf(MAX_UNIT) === 0 &&
72+
dimensions.height > dimensions.width
73+
)
74+
);
75+
const relativeToWidth = (
76+
unit.indexOf(WIDTH_UNIT) === 0 ||
77+
(
78+
unit.indexOf(MIN_UNIT) === 0 &&
79+
dimensions.height >= dimensions.width
80+
) ||
81+
(
82+
unit.indexOf(MAX_UNIT) === 0 &&
83+
dimensions.height <= dimensions.width
84+
)
85+
);
86+
4987
if (relativeToHeight) {
5088
return (dimensions.height * parseFloat(num) / 100) + normalisedUnit;
51-
}
52-
53-
const relativeToWidth = unit.indexOf(WIDTH_UNIT) === 0;
54-
55-
if (relativeToWidth) {
89+
} else if (relativeToWidth) {
5690
return (dimensions.width * parseFloat(num) / 100) + normalisedUnit;
5791
}
5892

src/runtime/convertSingleValue.spec.js

+45
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,49 @@ test('single container value should be properly converted to px', () => {
119119
{ width: 1000 },
120120
`55.5px`
121121
)).toBe('55.5px');
122+
123+
expect(convertSingleValue(
124+
{ width: 1200, height: 100 },
125+
`1cminpx`
126+
)).toBe('1px');
127+
128+
expect(convertSingleValue(
129+
{ width: 1200, height: 120 },
130+
`1cminpx`
131+
)).toBe('1.2px');
132+
133+
expect(convertSingleValue(
134+
{ width: 1200, height: 1200 },
135+
`1cminpx`
136+
)).toBe('12px');
137+
138+
expect(convertSingleValue(
139+
{ width: 900, height: 1200 },
140+
`1cminpx`
141+
)).toBe('9px');
142+
143+
expect(convertSingleValue(
144+
{ width: 900, height: 1200 },
145+
`5cminem`
146+
)).toBe('45em');
147+
148+
expect(convertSingleValue(
149+
{ width: 900, height: 1200 },
150+
`1cmaxpx`
151+
)).toBe('12px');
152+
153+
expect(convertSingleValue(
154+
{ width: 900, height: 1200 },
155+
`2cmaxpx`
156+
)).toBe('24px');
157+
158+
expect(convertSingleValue(
159+
{ width: 100, height: 99 },
160+
`1cmaxem`
161+
)).toBe('1em');
162+
163+
expect(convertSingleValue(
164+
{ width: 120, height: 99 },
165+
`5cmaxem`
166+
)).toBe('6em');
122167
});

0 commit comments

Comments
 (0)