-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Marpit PostCSS container query plugin
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** @module */ | ||
import cssesc from 'cssesc' | ||
import postcssPlugin from '../helpers/postcss_plugin' | ||
|
||
const reservedNames = [ | ||
'none', | ||
'inherit', | ||
'initial', | ||
'revert', | ||
'revert-layer', | ||
'unset', | ||
] | ||
|
||
/** | ||
* Marpit PostCSS container query plugin. | ||
* | ||
* Add support of container queries for child elements of the `section` element. | ||
* (`@container` at-rule, and `cqw` `cqh` `cqi` `cqb` `cqmin` `cqmax` units) | ||
* | ||
* @function meta | ||
* @param {string|string[]} [containerName=undefined] Container name | ||
* @param {boolean} [escape=true] Set whether to escape container name | ||
*/ | ||
export const containerQuery = postcssPlugin( | ||
'marpit-postcss-container-query', | ||
(containerName = undefined, escape = true) => | ||
(css) => { | ||
const containerNames = ( | ||
Array.isArray(containerName) ? containerName : [containerName] | ||
).filter((name) => name && !reservedNames.includes(name)) | ||
|
||
const containerNameDeclaration = | ||
containerNames.length > 0 | ||
? `\n container-name: ${containerNames | ||
.map((name) => | ||
escape | ||
? cssesc(name.toString(), { isIdentifier: true }) | ||
: name.toString(), | ||
) | ||
.join(' ')};` | ||
: '' | ||
|
||
css.first.before( | ||
` | ||
:where(section) { | ||
container-type: size;${containerNameDeclaration} | ||
} | ||
`.trim(), | ||
) | ||
}, | ||
) | ||
|
||
export default containerQuery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters