Skip to content

Custom media handling seems incorrect #286

@romainmenke

Description

@romainmenke

I am working on the PostCSS plugin for @custom-media and found it to be wildly incorrect.
When comparing with lightningcss I think I also found some instances where lightningcss has bugs.

@custom-media --not-width not (min-width: 300px);

@media screen and ((prefers-color-scheme: dark) or (--not-width)) {
	.foo {
		order: 6;
	}
}

becomes

@media screen and ((prefers-color-scheme: dark) or not (min-width: 300px)) {
  .foo {
    order: 6;
  }
}

This is not a valid media query.

Equivalent could be :

@media screen {
	@media (prefers-color-scheme: dark) {
		.foo {
			order: 6;
		}
	}

	@media not (min-width: 300px) {
		.foo {
			order: 6;
		}
	}
}

For the PostCSS plugin I am now considering only allowing <media-feature> https://drafts.csswg.org/mediaqueries-5/#mq-syntax

/* supported */
@custom-media --mq (min-width: 300px);
/* not supported */
@custom-media --mq-with-type screen and (min-width: 300px);
@custom-media --mq-with-modifier not (min-width: 300px);
@custom-media --mq-list (min-width: 300px), (prefers-color-scheme: dark);

I think technically lists would be possible but at this time I prefer to scale back the scope of the plugin as much as possible to something that is known to be correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions