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: Support arguments in CSS components #88

Closed
tonilogbo opened this issue May 15, 2023 · 5 comments · Fixed by #484
Closed

css: Support arguments in CSS components #88

tonilogbo opened this issue May 15, 2023 · 5 comments · Fixed by #484
Labels
css enhancement New feature or request NeedsFix Needs implementing

Comments

@tonilogbo
Copy link

I am attempting build dynamic pie charts using CSS where the percentage would be controlled by a CSS variable calculated on the server. I'm not sure if this can currently be achieved in templ because style attributes do not allow for templ expressions.

@joerdav
Copy link
Collaborator

joerdav commented May 15, 2023

There is some flexibility in styles at the moment: https://templ.guide/syntax-and-usage/css-style-management/#css-expressions

However, I think what you are asking for is for css functions to allow the passing of parameters?

Currently this is invalid:

package main

css myStyle(color string) {
	color: {color};
}

templ myComponent() {
	<p class={myStyle("blue")}>Hello, blue.</p>
	<p class={myStyle("red")}>Hello, red.</p>
}

@tantalic
Copy link
Contributor

Allowing CSS expressions to take parameters would be extremely helpful in creating components which maximize re-use/flexibility. As a matter of fact, I found this discussion after trying code using the exact syntax @joerdav shows above and working my way back from the error to the cssExpressionParser that currently returns an error when parameters are included.

Would the maintainers be open to such a feature?

@joerdav
Copy link
Collaborator

joerdav commented Sep 26, 2023

I think it's a valuable feature, seems ergonomic for css functions to support this. I think we just haven't got round to it yes as it's not high up on the priority list :)

@lukpank
Copy link

lukpank commented Dec 4, 2023

For me most use cases would be covered if css components
would interpolate integers (as opposed to strings in templ components)
(there could be special handling for specific css properties, such
as background-image, see issue #261).

If having different interpolation meaning between templ and css components would be a problem, maybe one could introduce some prefix such as {# x} to mean integer interpolation (as opposed to string interpolation) and in css components only allow integer interpolation and leave string interpolation for later decision. (I think integer interpolation could be considered safe).

Example usage:

css square(size int) {
    width: { size }px;
    height: { size }px;
}

templ useAsClass() {
    <div class={ square(100) }/>
}

should render to something like

<style type="text/css">
 .className_f179{width:100px;height:100px}
</style>
<div class="className_f179"></div>

And having parameterized css components it would also be useful to
allow using css components interpolated as value for style attribute

templ useAsStyle() {
    <div style={ square(100) }/>
}

which should render as

<div style="width:100px;height:100px"></div>

@joerdav joerdav changed the title Allow for CSS variables to be set dynamically in style attributes css: Support arguments in CSS components Jan 31, 2024
@joerdav joerdav added enhancement New feature or request NeedsFix Needs implementing css labels Jan 31, 2024
@joerdav joerdav mentioned this issue Feb 1, 2024
@a-h a-h closed this as completed in #484 Feb 1, 2024
a-h pushed a commit that referenced this issue Feb 1, 2024
@a-h
Copy link
Owner

a-h commented Feb 1, 2024

@joerdav just implemented CSS arguments. There's a few good improvements in the main branch for the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
css enhancement New feature or request NeedsFix Needs implementing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants