Skip to content

Use CSS variables to set styles - #26

Merged
loichuder merged 1 commit into
mainfrom
css-vars
Aug 28, 2026
Merged

Use CSS variables to set styles#26
loichuder merged 1 commit into
mainfrom
css-vars

Conversation

@loichuder

@loichuder loichuder commented Aug 28, 2026

Copy link
Copy Markdown
Member

PR summary

In web projects, we usually keep color consistent by using CSS variables

See https://github.com/silx-kit/h5web/blob/828da78669af0fa56e6cb9a9dd54cbfc138c5357/packages/app/src/App.module.css#L12 for example.

It thought it would be good to do it for our produced SVG as well. We can then keep all colors in a single place so that it is easier to change the style conditionally. This is mainly for #22 but we could imagine providing different theming for the output later.

For this, I added a new root.css file containing the variables that is always included in the generated SVG.

AI Disclosure

  • No AI used

Comment thread CONTRIBUTING.md

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the liberty to also expand a bit the CONTRIBUTING file

root_element = generate_style_element("root.css")
if root_element is None:
return all_styles
return [root_element, *all_styles]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how I add the new root.css file to the other styles.

style = Element("style")
style.text = f"<![CDATA[\n{css_content}\n]]>"
return style
return generate_style_element(css_file_name=f"css_{self._css_class}.css")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored this slightly with the introduction of generate_style_element to be able to reuse the CSS loading for root.css

@loichuder
loichuder requested a review from LudoBroche August 28, 2026 11:04
@codecov

codecov Bot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/ewoksdraw/svg/svg_canvas.py 83.33% 1 Missing ⚠️
src/ewoksdraw/svg/utils.py 91.66% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Comment thread src/ewoksdraw/svg/utils.py Outdated


def generate_style_element(css_file_name: str) -> None | Element:
css_file_path = Path(f"src/ewoksdraw/css_styles/{css_file_name}")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was already the case before, but don't we risk having issues if the working directory changes?

canvas = SvgCanvas(10, 10)
canvas.add_element(background)
os.chdir('/somewhere')
canvas.draw(Path('/tmp/example.svg'))

We could use importlib

from importlib.resources import files

css_file = files("ewoksdraw").joinpath("css_styles", css_file_name)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I thought about it afterwards. We should instead rely on the directory of the package itself.

stroke-width: 0.2%;
fill: var(--transparent);
stroke: var(--stroke-color);
stroke-width: var(--stroke-width);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--stroke-width is now shared between css_task_box and css_task_line.
The values were different prior to the change (1 vs 0.2 %). Should we create a separate variable?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes. Well, it depends: do we want the box and the line to have both the same stroke width or not?

I'd think it would be more consistent to have the same but perhaps you had something else in mind when you designed it earlier?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep it like that for now.
Anyway, if we want to tone it down an easier way, it is to change the color.
image

@LudoBroche

Copy link
Copy Markdown
Member

I agree with you, and I wanted to do that in the first place, but I was struggling with HTML integration.

Do we have to support SVG strings directly embedded in HTML?
If yes, the use of :root for a particular SVG image will leak/overwrite the style of the entire page.

An example of an HTML page highlighting the issue: svg-leak.html>

This can be fixed by adding the style variable directly in the element.
Example
svg-clean.html

I'm 100% sure there is a better way to do that.

@loichuder

loichuder commented Aug 28, 2026

Copy link
Copy Markdown
Member Author

If yes, the use of :root for a particular SVG image will leak/overwrite the style of the entire page.

Only if the page uses the same CSS variables.

So there is two ways of fixing this:

  • We prefix our CSS variables to ensure there is no collision (e.g. ewoksdraw-stroke-width)
  • Or we do as you suggest and add the CSS variables only for the current SVG (adding a specific class to the svg element for example)

Perhaps the second option is cleaner. What do you think?

@LudoBroche

Copy link
Copy Markdown
Member

Yes, I'm in favor of solution 2, making the SVG block self-contained.

@loichuder
loichuder requested a review from LudoBroche August 28, 2026 13:18
@loichuder

Copy link
Copy Markdown
Member Author

Please have a second look @LudoBroche, I added two commits to handle the issues you pointed out

@LudoBroche LudoBroche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@loichuder
loichuder merged commit 160f8bd into main Aug 28, 2026
6 checks passed
@loichuder
loichuder deleted the css-vars branch August 28, 2026 14:25
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

Successfully merging this pull request may close these issues.

2 participants