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

css: CSS Media Query Support #132

Open
wedow opened this issue Aug 28, 2023 · 10 comments
Open

css: CSS Media Query Support #132

wedow opened this issue Aug 28, 2023 · 10 comments

Comments

@wedow
Copy link

wedow commented Aug 28, 2023

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>
@joerdav
Copy link
Collaborator

joerdav commented Sep 6, 2023

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

@headblockhead
Copy link
Contributor

Hello! Might attempt to add this in a PR.

@joerdav
Copy link
Collaborator

joerdav commented Oct 16, 2023

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!

@headblockhead
Copy link
Contributor

@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(!)

@joerdav
Copy link
Collaborator

joerdav commented Oct 17, 2023

@joerdav
Copy link
Collaborator

joerdav commented Oct 17, 2023

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

@joerdav
Copy link
Collaborator

joerdav commented Oct 19, 2023

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.

@nftbolt
Copy link

nftbolt commented Jan 2, 2024

Any update on this?

@joerdav
Copy link
Collaborator

joerdav commented Jan 2, 2024

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

@joerdav joerdav changed the title CSS Media Query Support css: CSS Media Query Support Jan 31, 2024
@joerdav joerdav added enhancement New feature or request NeedsFix Needs implementing css labels Jan 31, 2024
@a-h
Copy link
Owner

a-h commented Apr 30, 2024

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.

@linear linear bot added Migrated and removed enhancement New feature or request Migrated NeedsFix Needs implementing css labels Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants