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

RFC: CSS Modules #410

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft

RFC: CSS Modules #410

wants to merge 6 commits into from

Conversation

johnnyomair
Copy link
Collaborator

@johnnyomair johnnyomair commented Oct 24, 2024

We want to replace styled-components with another styling solution to support Server Components. This PR demonstrates how some components, which have dynamic styles, could be implemented using CSS Modules.

Advantages of CSS Modules over CSS-in-JS

  • Reduced Bundle Size
  • Works without JS
  • Works with Server Components out-of-the-box
  • No runtime required

Disadvantages

  • Inferior developer experience due to missing types (maybe this could be fixed)
  • Dynamic styles are more complicated

color: var(--palette-primary-main);
border: 1px solid var(--palette-primary-main);

&:hover {
Copy link
Member

Choose a reason for hiding this comment

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

who or what does make this working? Or is this valid css in 2024?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes: https://developer.mozilla.org/en-US/docs/Web/CSS/Nesting_selector

However, it doesn't meet our support requirements on Can I use... to be honest, I haven't thought about this too much.

Copy link
Member

Choose a reason for hiding this comment

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

we could add postcss....

is something like autoprefixer needed nowadays? how does styled-components handle this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we could add postcss....

Yes, if we decide to switch, we should invest some time into improving the developer experience.

is something like autoprefixer needed nowadays? how does styled-components handle this?

I don't know. styled-components does it out-of-the-box: https://styled-components.com/docs/basics#motivation

Copy link
Contributor

Choose a reason for hiding this comment

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

Importing scss files instead of css files seems to work out of the box, this would allow nested selectors independent of browser support.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Switched to Sass: 78a44b4

@nsams
Copy link
Member

nsams commented Oct 28, 2024

Interesting would also be an example where a component has multiple elements, not just a root

@johnnyomair
Copy link
Collaborator Author

Interesting would also be an example where a component has multiple elements, not just a root

Here you go: 98158fd

@johnnyomair johnnyomair changed the title RFC: Implement Button with CSS Modules RFC: CSS Modules Oct 28, 2024
}
}

@media (min-width: 900px) {
Copy link
Member

Choose a reason for hiding this comment

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

I guess there is not way to avoid hardcoding the breakpoints

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried using CSS variables, but that didn't work

Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately css variables are not supported in media queries.
Using this PostCSS plugin would allow defining custom media queries though that can be used as variables: https://www.npmjs.com/package/postcss-custom-media

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I used Sass variables: e347a2b

However, the need to import the partial Sass file isn't great.

Comment on lines 2 to 9
--palette-primary-main: #1e88e5;
--palette-primary-dark: #155fa0;
--palette-primary-contrast-text: #ffffff;

--palette-gray-50: #fafafa;
--palette-gray-200: #eeeeee;
--palette-gray-300: #e0e0e0;
--palette-gray-400: #bdbdbd;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd omit the palette- prefix, --primary-main or --gray-50 es precise enough and easier to write imo.

Copy link
Contributor

@jamesricky jamesricky left a comment

Choose a reason for hiding this comment

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

I like this 👍🏼

@thomasdax98 thomasdax98 removed their request for review November 14, 2024 12:58
@dkarnutsch dkarnutsch removed their request for review December 2, 2024 11:40
@johnnyomair johnnyomair marked this pull request as draft December 19, 2024 13:22
@johnnyomair
Copy link
Collaborator Author

johnnyomair commented Jan 9, 2025

Inferior developer experience due to missing types (maybe this could be fixed)

Added types with a small util here: 5378f9d

image image

Copy link

sonarqubecloud bot commented Jan 9, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

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.

3 participants