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

Support container queries in editor CSS #49915

Merged
merged 7 commits into from
Aug 10, 2023
Merged
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
31 changes: 31 additions & 0 deletions packages/block-editor/src/utils/transform-styles/ast/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,36 @@ export default function ( css, options ) {
} );
}

/**
* Parse container.
*/

function atcontainer() {
const pos = position();
const m = match( /^@container *([^{]+)/ );

if ( ! m ) {
return;
}
const container = trim( m[ 1 ] );

if ( ! open() ) {
return error( "@container missing '{'" );
}

const style = comments().concat( rules() );

if ( ! close() ) {
return error( "@container missing '}'" );
}

return pos( {
type: 'container',
container,
rules: style,
} );
}

/**
* Parse custom-media.
*/
Expand Down Expand Up @@ -624,6 +654,7 @@ export default function ( css, options ) {
return (
atkeyframes() ||
atmedia() ||
atcontainer() ||
atcustommedia() ||
atsupports() ||
atimport() ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ Compiler.prototype.media = function ( node ) {
);
};

/**
* Visit container node.
*/

Compiler.prototype.container = function ( node ) {
return (
this.emit( '@container ' + node.container, node.position ) +
this.emit( '{' ) +
this.mapVisit( node.rules ) +
this.emit( '}' )
);
};

/**
* Visit document node.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ Compiler.prototype.media = function ( node ) {
);
};

/**
* Visit container node.
*/

Compiler.prototype.container = function ( node ) {
return (
this.emit( '@container ' + node.container, node.position ) +
this.emit( ' {\n' + this.indent( 1 ) ) +
this.mapVisit( node.rules, '\n\n' ) +
this.emit( this.indent( -1 ) + '\n}' )
);
};

/**
* Visit document node.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ color: red;
}"
`;

exports[`CSS selector wrap should wrap selectors inside container queries 1`] = `
"@container (width > 400px) {
.my-namespace h1 {
color: red;
}
}"
`;

exports[`CSS selector wrap should replace :root selectors 1`] = `
".my-namespace {
--my-color: #ff0000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ describe( 'CSS selector wrap', () => {
expect( output ).toMatchSnapshot();
} );

it( 'should wrap selectors inside container queries', () => {
const callback = wrap( '.my-namespace' );
const input = `
@container (width > 400px) {
h1 { color: red; }
}`;
const output = traverse( input, callback );

expect( output ).toMatchSnapshot();
} );

it( 'should ignore font-face selectors', () => {
const callback = wrap( '.my-namespace' );
const input = `
Expand Down
Loading