Skip to content

Commit fe90b3f

Browse files
authored
Merge pull request #12 from factorial-io/fix!/custom-property-output
fix!/custom property output
2 parents 7aa0eb6 + 3d77e04 commit fe90b3f

File tree

4 files changed

+39
-46
lines changed

4 files changed

+39
-46
lines changed

README.md

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -100,46 +100,22 @@ theme_name.lg:
100100
@custom-media --Themename-small-mediaQuery (only screen and (max-width: 47.9375rem));
101101
@custom-media --Themename-small-resolution (resolution: 2x);
102102
@custom-media --Themename-small-maxWidth (max-width: 47.9375rem);
103-
@custom-media --Themename-medium-mediaQuery (only screen and (min-width: 48rem) and (max-width: 63.9375rem));
104-
@custom-media --Themename-medium-resolution (resolution: 2x);
105-
@custom-media --Themename-medium-minWidth (min-width: 48rem);
106-
@custom-media --Themename-medium-maxWidth (max-width: 63.9375rem);
107-
@custom-media --Themename-large-mediaQuery (only screen and (min-width: 64rem) and (max-width: 89.9375rem));
108-
@custom-media --Themename-large-resolution (resolution: 2x);
109-
@custom-media --Themename-large-minWidth (min-width: 64rem);
110-
@custom-media --Themename-large-maxWidth (max-width: 89.9375rem);
111103

112104
:root {
113105
--ThemeName-small-mediaQuery: "only screen and (max-width: 47.9375rem)";
114-
--ThemeName-small-resolution: "resolution: 2x";
115-
--ThemeName-small-maxWidth: "max-width: 47.9375rem";
116-
--ThemeName-medium-mediaQuery: "only screen and (min-width: 48rem) and (max-width: 63.9375rem)";
117-
--ThemeName-medium-resolution: "resolution: 2x";
118-
--ThemeName-medium-minWidth: "min-width: 48rem";
119-
--ThemeName-medium-maxWidth: "max-width: 63.9375rem";
120-
--ThemeName-Group-large-mediaQuery: "only screen and (min-width: 64rem) and (max-width: 89.9375rem)";
121-
--ThemeName-Group-large-resolution: "resolution: 2x";
122-
--ThemeName-Group-large-minWidth: "min-width: 64rem";
123-
--ThemeName-Group-large-maxWidth: "max-width: 89.9375rem";
106+
--ThemeName-small-resolution: "2x";
107+
--ThemeName-small-maxWidth: "47.9375rem";
124108
}
125109
```
126110

127111
```js
128112
// Generated JS file
129113
const BREAKPOINTS = {
130-
"ThemeName-small-mediaQuery": "only screen and (max-width: 47.9375rem)",
114+
"ThemeName-small-mediaQuery--raw": "only screen and (max-width: 47.9375rem)",
115+
"ThemeName-small-resolution--raw": "2x",
131116
"ThemeName-small-resolution": "resolution: 2x",
117+
"ThemeName-small-maxWidth--raw": "47.9375rem",
132118
"ThemeName-small-maxWidth": "max-width: 47.9375rem",
133-
"ThemeName-medium-mediaQuery":
134-
"only screen and (min-width: 48rem) and (max-width: 63.9375rem)",
135-
"ThemeName-medium-resolution": "resolution: 2x",
136-
"ThemeName-medium-minWidth": "min-width: 48rem",
137-
"ThemeName-medium-maxWidth": "max-width: 63.9375rem",
138-
"ThemeName-Group-large-mediaQuery":
139-
"only screen and (min-width: 64rem) and (max-width: 89.9375rem)",
140-
"ThemeName-Group-large-resolution": "resolution: 2x",
141-
"ThemeName-Group-large-minWidth": "min-width: 64rem",
142-
"ThemeName-Group-large-maxWidth": "max-width: 89.9375rem",
143119
};
144120
export default BREAKPOINTS;
145121
```

lib/index.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ module.exports = class Breakpoints {
185185
.toLowerCase()}`;
186186
if (this.#config.options.mediaQuery) {
187187
this.#customProperties.push({
188-
identifier: `${baseName}-mediaQuery`,
188+
name: `${baseName}-mediaQuery`,
189189
value: option.mediaQuery,
190190
});
191191
}
@@ -194,19 +194,22 @@ module.exports = class Breakpoints {
194194
Breakpoints.getResolutions(option)
195195
) {
196196
this.#customProperties.push({
197-
identifier: `${baseName}-resolution`,
197+
mediaFeature: "resolution",
198+
name: `${baseName}-resolution`,
198199
value: Breakpoints.getResolutions(option),
199200
});
200201
}
201202
if (this.#config.options.minWidth && Breakpoints.getMinWidths(option)) {
202203
this.#customProperties.push({
203-
identifier: `${baseName}-minWidth`,
204+
mediaFeature: "minWidth",
205+
name: `${baseName}-minWidth`,
204206
value: Breakpoints.getMinWidths(option),
205207
});
206208
}
207209
if (this.#config.options.maxWidth && Breakpoints.getMaxWidths(option)) {
208210
this.#customProperties.push({
209-
identifier: `${baseName}-maxWidth`,
211+
mediaFeature: "maxWidth",
212+
name: `${baseName}-maxWidth`,
210213
value: Breakpoints.getMaxWidths(option),
211214
});
212215
}
@@ -236,16 +239,19 @@ module.exports = class Breakpoints {
236239
let string = "";
237240
if (this.#config.css.customMedia) {
238241
this.#customProperties.forEach((customProperty) => {
242+
const mediaFeature = customProperty.mediaFeature
243+
? `${customProperty.mediaFeature}:`
244+
: "";
239245
// For syntax see postcss-custom-media plugin or W3C draft
240246
// Ref: https://www.npmjs.com/package/postcss-custom-media
241247
// Ref: https://www.w3.org/TR/mediaqueries-5/#at-ruledef-custom-media
242-
string += `@custom-media --${customProperty.identifier} (${customProperty.value});`;
248+
string += `@custom-media --${customProperty.name} (${mediaFeature} ${customProperty.value});`;
243249
});
244250
}
245251
if (this.#config.css.customProperty) {
246252
string += `${this.#config.css.element} {`;
247253
this.#customProperties.forEach((customProperty) => {
248-
string += `--${customProperty.identifier}: "${customProperty.value}";`;
254+
string += `--${customProperty.name}: "${customProperty.value}";`;
249255
});
250256
string += `}`;
251257
}
@@ -262,10 +268,20 @@ module.exports = class Breakpoints {
262268
*/
263269
async #generateAndWriteJS() {
264270
const filePath = path.resolve(process.cwd(), this.#config.js.path);
265-
const strings = this.#customProperties.map(
266-
(customProperty) =>
267-
`"${customProperty.identifier}": "${customProperty.value}"`
268-
);
271+
const stringArray = [];
272+
this.#customProperties.forEach((customProperty) => {
273+
const mediaFeature = customProperty.mediaFeature
274+
? `${customProperty.mediaFeature}:`
275+
: "";
276+
if (mediaFeature) {
277+
stringArray.push(
278+
`"${customProperty.name}": "(${mediaFeature} ${customProperty.value})"`
279+
);
280+
}
281+
stringArray.push(
282+
`"${customProperty.name}--raw": "${customProperty.value}"`
283+
);
284+
});
269285
const prettierConfig = {
270286
...(this.#config.prettier?.configPath
271287
? await Breakpoints.getPrettierConfig(this.#config.prettier.configPath)
@@ -285,7 +301,7 @@ module.exports = class Breakpoints {
285301
await Breakpoints.writeFile(
286302
filePath,
287303
prettier.format(
288-
`const BREAKPOINTS = {${strings.join(
304+
`const BREAKPOINTS = {${stringArray.join(
289305
","
290306
)}}; export default BREAKPOINTS;`,
291307
prettierConfig
@@ -300,7 +316,7 @@ module.exports = class Breakpoints {
300316
await Breakpoints.writeFile(
301317
filePath,
302318
prettier.format(
303-
`module.exports = {${strings.join(",")}};`,
319+
`module.exports = {${stringArray.join(",")}};`,
304320
prettierConfig
305321
)
306322
);
@@ -339,7 +355,7 @@ module.exports = class Breakpoints {
339355
if (!Number.isFinite(width[2]) && !UNITS.length.includes(width[4])) {
340356
throw new RangeError(MESSAGES.widthRangeError(option));
341357
}
342-
return `min-width: ${width[2]}${width[4]}`;
358+
return width[2] + width[4];
343359
}
344360

345361
/**
@@ -360,7 +376,7 @@ module.exports = class Breakpoints {
360376
if (!Number.isFinite(width[2]) && !UNITS.length.includes(width[4])) {
361377
throw new RangeError(MESSAGES.widthRangeError(option));
362378
}
363-
return `max-width: ${width[2]}${width[4]}`;
379+
return width[2] + width[4];
364380
}
365381

366382
/**
@@ -389,7 +405,7 @@ module.exports = class Breakpoints {
389405
) {
390406
throw new RangeError(MESSAGES.resolutionRangeError(option));
391407
}
392-
return `resolution: ${resolution[1]}${resolution[2]}`;
408+
return resolution[1] + resolution[2];
393409
}
394410

395411
/**

lib/regexp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const UNITS = require("./units");
22

33
module.exports = {
4-
maxWidth: `min-width:\\s*((\\d+(\\.\\d+)?)\\s*(${UNITS.length.join("|")}))`,
4+
maxWidth: `max-width:\\s*((\\d+(\\.\\d+)?)\\s*(${UNITS.length.join("|")}))`,
55
minWidth: `min-width:\\s*((\\d+(\\.\\d+)?)\\s*(${UNITS.length.join("|")}))`,
66
resolution: `(\\d+)\\s*(${UNITS.resolution.join("|")})`,
77
};

lib/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export interface Breakpoint {
6464
}
6565

6666
export interface customProperty {
67-
identifier: string;
67+
mediaFeature?: "minWidth" | "maxWidth" | "resolution";
68+
name: string;
6869
value: string;
6970
}
7071

0 commit comments

Comments
 (0)