Skip to content

Commit 7f6824c

Browse files
authored
Merge pull request #10 from factorial-io/feature(custom-media)/custom-media-export-option
feature: custom media export option
2 parents 7302e88 + 2bd71e0 commit 7f6824c

File tree

6 files changed

+54
-5
lines changed

6 files changed

+54
-5
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export interface UserConfig {
4343
enabled?: boolean;
4444
path?: string;
4545
element?: string;
46+
customMedia?: boolean;
47+
customProperty?: boolean;
4648
};
4749
options?: {
4850
mediaQuery?: boolean;
@@ -95,6 +97,18 @@ theme_name.lg:
9597
9698
```css
9799
/* Generated CSS file */
100+
@custom-media --Themename-small-mediaQuery (only screen and (max-width: 47.9375rem));
101+
@custom-media --Themename-small-resolution (2x);
102+
@custom-media --Themename-small-maxWidth (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 (2x);
105+
@custom-media --Themename-medium-minWidth (48rem);
106+
@custom-media --Themename-medium-maxWidth (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 (2x);
109+
@custom-media --Themename-large-minWidth (64rem);
110+
@custom-media --Themename-large-maxWidth (89.9375rem);
111+
98112
:root {
99113
--ThemeName-small-mediaQuery: "only screen and (max-width: 47.9375rem)";
100114
--ThemeName-small-resolution: "2x";

lib/defaultConfig.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module.exports = {
1212
enabled: true,
1313
path: "./breakpoints.css",
1414
element: ":root",
15+
customMedia: true,
16+
customProperty: true,
1517
},
1618
options: {
1719
mediaQuery: true,

lib/index.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ module.exports = class Breakpoints {
110110
if (!this.#config.drupal?.themeName) {
111111
console.warn(MESSAGES.defaultThemeName());
112112
}
113+
if (!this.#config.css.customMedia && !this.#config.css.customProperty) {
114+
console.warn(MESSAGES.noCSSOutput());
115+
}
113116
return true;
114117
}
115118

@@ -217,20 +220,38 @@ module.exports = class Breakpoints {
217220
* @async
218221
*/
219222
async #generateAndWriteCSS() {
223+
if (!this.#config.css.enabled) {
224+
return;
225+
}
226+
if (!this.#config.css.customMedia && !this.#config.css.customProperty) {
227+
return;
228+
}
220229
const filePath = path.resolve(process.cwd(), this.#config.css.path);
221-
let string = "";
222-
this.#customProperties.forEach((customProperty) => {
223-
string += `--${customProperty.identifier}: "${customProperty.value}";`;
224-
});
225230
const prettierConfig = {
226231
...(this.#config.prettier?.configPath
227232
? await Breakpoints.getPrettierConfig(this.#config.prettier.configPath)
228233
: {}),
229234
parser: "css",
230235
};
236+
let string = "";
237+
if (this.#config.css.customMedia) {
238+
this.#customProperties.forEach((customProperty) => {
239+
// For syntax see postcss-custom-media plugin or W3C draft
240+
// Ref: https://www.npmjs.com/package/postcss-custom-media
241+
// Ref: https://www.w3.org/TR/mediaqueries-5/#at-ruledef-custom-media
242+
string += `@custom-media --${customProperty.identifier} (${customProperty.value});`;
243+
});
244+
}
245+
if (this.#config.css.customProperty) {
246+
string += `${this.#config.css.element} {`;
247+
this.#customProperties.forEach((customProperty) => {
248+
string += `--${customProperty.identifier}: "${customProperty.value}";`;
249+
});
250+
string += `}`;
251+
}
231252
await Breakpoints.writeFile(
232253
filePath,
233-
prettier.format(`${this.#config.css.element} {${string}}`, prettierConfig)
254+
prettier.format(string, prettierConfig)
234255
);
235256
}
236257

lib/messages.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module.exports = {
44
defaultThemeName: () => `Missing "userConfig.drupal.themeName" value.`,
55
noOutput: (config) =>
66
`No output with this configuration:\n${JSON.stringify(config)}`,
7+
noCSSOutput: () =>
8+
`No css output if "customMedia" and "customProperty" options are set to false.`,
79
prettierConfig: (path) => `Couldn't read prettier config from ${path}!`,
810
readFile: (path) => `Couldn't read from ${path} file!`,
911
resolutionRangeError: (option) =>

lib/types.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export interface Config {
1515
enabled: boolean;
1616
path: string;
1717
element: string;
18+
customMedia: boolean;
19+
customProperty: boolean;
1820
};
1921
options: {
2022
mediaQuery: boolean;
@@ -41,6 +43,8 @@ export interface UserConfig {
4143
enabled?: boolean;
4244
path?: string;
4345
element?: string;
46+
customMedia?: boolean;
47+
customProperty?: boolean;
4448
};
4549
options?: {
4650
mediaQuery?: boolean;

schema/drupal-breakpoints-css.json

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@
7272
},
7373
"element": {
7474
"type": "string"
75+
},
76+
"customMedia": {
77+
"type": "boolean"
78+
},
79+
"customProeprty": {
80+
"type": "boolean"
7581
}
7682
}
7783
},

0 commit comments

Comments
 (0)