templ icon indicating copy to clipboard operation
templ copied to clipboard

css: CSS Media Query Support

Open wedow opened this issue 1 year ago • 10 comments

Is there any convenient support for media queries which target classes from CSS expressions?

Given some component like

css nav() { ... }

templ Nav() {
	<nav class={ nav }>
		{ children... }
	</nav>
}

I'd like to write a media query which changes some properties on that nav class. There doesn't seem to be a way to reference the ClassName() of the templ.ComponentCSSClass from inside a <style> tag so I'm currently resorting a plain func() string to build a style tag with fmt.Sprintf which isn't very ergonomic.

Is there a better way to accomplish this at this time?

Ideally, something like Emotion's media query support would be available so we could write something like

css nav() {
	display: flex;
	flex-direction: column;

	@media (min-width: 768px) {
		flex-direction: row;
	}
}

which would generate CSS like

<style type="text/css">
	.className_f179 {
		display: flex;
		flex-direction: column;
	}

	@media (min-width: 768px) {
		.className_f179 {
			flex-direction: row;
		}
	}
</style>

wedow avatar Aug 28 '23 05:08 wedow

Currently no there isn't a good way to accomplish this, definitely a good feature for us to have though!

joerdav avatar Sep 06 '23 15:09 joerdav

Hello! Might attempt to add this in a PR.

headblockhead avatar Oct 05 '23 16:10 headblockhead

Hi @headblockhead, I was thinking of picking this one up this week, are you still thinking of PRing for this one? Don't want to duplicate work if you're almost there with it!

joerdav avatar Oct 16 '23 08:10 joerdav

@joerdav Feel free to make a PR, I haven’t been able to start on adding media queries yet, maybe I was a bit too eager with my comment(!)

headblockhead avatar Oct 16 '23 14:10 headblockhead

Started work here: https://github.com/joerdav/templ/tree/media-queries

joerdav avatar Oct 17 '23 08:10 joerdav

Figured it would be worth discussing the design of it before I go ahead and implement the generator.

joerdav avatar Oct 17 '23 08:10 joerdav

So far the generator side of this has been more complex than expected, I'm thinking of parking for now since there is bigger fish to fry! And this can be worked around at the moment by just using a normal old style tag rather than templ css.

joerdav avatar Oct 19 '23 08:10 joerdav

Any update on this?

nftbolt avatar Jan 02 '24 12:01 nftbolt

@nftbolt not yet. For now I'd recommend using <style> tags in your template, or css files.

joerdav avatar Jan 02 '24 13:01 joerdav

I have a proof-of-concept started that uses the tdewolff CSS parser as a base, and layers in some additional parsing for Go expressions.

The idea would be to use standard <style> tags, but support {{ go.variable }} within values. I've got a few tests here - https://github.com/a-h/templ-css/blob/main/parse_test.go - obviously, the test suite would need to be massively widened out.

Note that most are of type CSSToken, but then there's also a few of type GoToken, which is where the variables are inserted. At the moment, there's no concept of property name to property value, which is what the CSS sanitization uses to determine what (if any) sanitization to support.

Before I jump to anything, the plan is to have an online meeting where everyone that's interested in templ CSS development can get together kick around ideas, and hopefully build consensus on a direction.

a-h avatar Apr 30 '24 09:04 a-h