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

feat: css modules #2318

Closed
wants to merge 7 commits into from
Closed

feat: css modules #2318

wants to merge 7 commits into from

Conversation

eirikbacker
Copy link
Contributor

@eirikbacker eirikbacker commented Aug 25, 2024

  • Try implementing CSS modules to enable multiple versions of the designsystem on the same page #2277
  • Since most bundlers do tree shaking very well these days, maybe we should not have @digdir/designsystemet-react and @digdir/designsystemet-css, (...and @digdir/designsystemet-js and @digdir/designsystemet-svelte etc.), but rather just @digdir/designsystemet (or @digdir/designsystemet-components) as CSS and React/JS/* must be kept in sync anyway?
    This makes it easier for us to maintain, and easier for the consumer, as they get one package but can use different flavours:
import `@digdir/designsystemet/styles.module.css` // To load the hashed styles
import Button from `@digdir/designsystemet/react`

or:

import `@digdir/designsystemet/styles.css` // To load un-hashed styles for those who prefer no versioning

or:

import styles from `@digdir/designsystemet/button.module.css` // To extend or use in HTML/other framework

and:

.my-fancy-button {
  composes: button from '@digdir/designsystemet/button.module.css'; // To extend using pure CSS
  …
}

and:

.my-fancy-react-button { … }
+
<Button className="my-fancy-react-button"> // To extend using React

@eirikbacker eirikbacker self-assigned this Aug 25, 2024
Copy link

changeset-bot bot commented Aug 25, 2024

🦋 Changeset detected

Latest commit: 014641a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@digdir/designsystemet-css Patch
@digdir/designsystemet-react Patch
@digdir/designsystemet Patch
@digdir/designsystemet-theme Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Aug 25, 2024

Preview deployments for this pull request:

📖 Storybook 5. Sep 2024 - 10:38 (Norwegian time)

🖥 Storefront 5. Sep 2024 - 10:39 (Norwegian time)

See all deployments at https://dev.designsystemet.no

@Barsnes
Copy link
Member

Barsnes commented Aug 26, 2024

I am not sure if having one package makes it easier for the consumer.

  • If they want to import, they have to activley make sure it comes from the right library in a package
    • for example: I start typiong <Alert>, and then I press ctrl+. for it to give me suggestions. Since there are multiple exports of the same name, I have to stop and look to make sure it comes for the right library
  • It will just get bigger and bigger, making downloading the package take longer and longer. Especially relevant for people having to download the package on a remote server every time they deploy

Having one package makes it easier to update, but if we keep all packages in sync, this is not a problem.

Comment on lines 5 to 10
/* TODO: Can this be moved to the related components? */
[class^='ds-'],
[class^='ds-']::before,
[class^='ds-']::after {
box-sizing: border-box;
}
Copy link
Member

Choose a reason for hiding this comment

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

This is relevant for all components, it's the same as doing

* { box-sizing: border-box } 

So I think this is fine to have in this file 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not if moving to CSS modules or allowing people to extend our classes like:

.my-fancy-button {
  composes: button from '@digdir/designsystemet/button.module.css'; // To extend using pure CSS
  …
}

Also, this targets only if elements class names starts with ds- so class="ds-button my-button" works, but class="my-button ds-button" does not. I think it is safer and more explicit to just add box-sizing: border-box into the relevant classes in the relevant files :)

Copy link
Member

@Barsnes Barsnes Aug 26, 2024

Choose a reason for hiding this comment

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

Ah yeah, but this should be [class~="ds-"] 😄

[class^='ds-'],
[class^='ds-']::before,
[class^='ds-']::after {
box-sizing: border-box;
}

/* Inherit fonts for inputs and buttons */

/* TODO: Can this be moved to the related form element CSS files? */
Copy link
Member

Choose a reason for hiding this comment

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

Yeah I think this is a better candididate for moving to to their relevant components.
However, it gives us another point of failure

Copy link
Contributor Author

@eirikbacker eirikbacker Aug 26, 2024

Choose a reason for hiding this comment

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

See comment above regarding targeting start of classnames :)
And I suggest forgetting to add styling should be done in reviews, not by an "invisible" dependency relation :)

@@ -1,3 +1,4 @@
/* TODO: Could these styles be placed in css root like the other files? */
Copy link
Member

Choose a reason for hiding this comment

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

Agreed

@Barsnes
Copy link
Member

Barsnes commented Aug 26, 2024

We would also need to add a script for this, to fix hashes, prefixes and support extracting pure classes without a hash

@eirikbacker
Copy link
Contributor Author

We would also need to add a script for this, to fix hashes, prefixes and support extracting pure classes without a hash

Unsure what you mean here - Vite does this? :) and the version without hashes is just making a file style.css:

@import url('style.module.css');

... since style.css does not have .module. in its name, it will not be processed :)
Or maybe I misunderstand the comment? :)

@Barsnes
Copy link
Member

Barsnes commented Aug 26, 2024

We would also need to add a script for this, to fix hashes, prefixes and support extracting pure classes without a hash

Unsure what you mean here - Vite does this? :) and the version without hashes is just making a file style.css:

@import url('style.module.css');

... since style.css does not have .module. in its name, it will not be processed :) Or maybe I misunderstand the comment? :)

I'll elaborate on my thoughs:
Our module files should be processed out as normal css files they can use, without hashes - otherwise we would need two files right? one module and one pure.

I am not sure how vite handles hashing, but at the same time we don't know if our consumers use vite.
Some people might use rollup + postcss, like we did before. We probably don't need to do anything with the hashes ourselves, but we need to tell our users how to use this. If the hash does not change., we have not solves anything. I am also guessing that if anyone decides to use the module files, they should know how to set this up 🤔

Edit: If we want to use features like composes, we would need to process the files for our users right?

@mimarz
Copy link
Collaborator

mimarz commented Aug 26, 2024

To summaries our meeting on 26.08.24 with @Barsnes @Thuneer @eirikbacker:

We decided to introduce css-modules for the CSS package so that our users can choose to use it if needed, be it for versioning different version or composing with their own CSS.

This also has the benefit internally of giving us typesafety for our classes atleast and composing typography to reduce number classNames to get desired result, while keeping themable typography.

  • Added a changset.

@mimarz
Copy link
Collaborator

mimarz commented Aug 28, 2024

One thing, won't this remove the ability for our React components to be headless (since we have an import statement for css files inside components).

@Barsnes
Copy link
Member

Barsnes commented Aug 28, 2024

One thing, won't this remove the ability for our React components to be headless (since we have an import statement for css files inside components).

This would indeed be correct 🤔

@Barsnes
Copy link
Member

Barsnes commented Aug 28, 2024

One thing, won't this remove the ability for our React components to be headless (since we have an import statement for css files inside components).

This would indeed be correct 🤔

It also makes it hard for users that target our css classes without a buildstep for css modules, since it would change for every version

@eirikbacker
Copy link
Contributor Author

@mimarz regarding headless; as @Barsnes points out; headless it not possible today either. A solution if we want to provide headless in the future is adding a <UnstyledProvider> an outer wrapper. This is a convention used by for instance Storybook, to provide the ability to opt out of the base classes being set.

@Barsnes Targeting our classnames would always be a bad idea, as we can change the classnames regardless. We should encourage people using the React package to always add their own className to extend styling ☺️

@eirikbacker eirikbacker assigned Barsnes and eirikbacker and unassigned eirikbacker Sep 2, 2024
Copy link
Contributor

github-actions bot commented Sep 5, 2024

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 65.3% 4909 / 7517
🔵 Statements 65.3% 4909 / 7517
🔵 Functions 83.22% 129 / 155
🔵 Branches 75.68% 610 / 806
File Coverage
File Stmts % Branch % Funcs % Lines Uncovered Lines
Changed Files
packages/react/src/components/Box/Box.tsx 100% 100% 100% 100%
packages/react/src/components/Breadcrumbs/BreadcrumbsItem.tsx 100% 100% 100% 100%
packages/react/src/components/Breadcrumbs/BreadcrumbsLink.tsx 100% 100% 100% 100%
packages/react/src/components/Breadcrumbs/BreadcrumbsList.tsx 100% 75% 100% 100%
packages/react/src/components/Breadcrumbs/BreadcrumbsNav.tsx 100% 100% 100% 100%
packages/react/src/components/Breadcrumbs/BreadcrumbsRoot.tsx 100% 100% 100% 100%
packages/react/src/components/Typography/Paragraph/Paragraph.tsx 100% 80% 100% 100%
Generated in workflow #80

Copy link
Contributor

github-actions bot commented Sep 5, 2024

Preview deployments

Theme 5. Sep 2024 - 10:39 (Norwegian time)

@mimarz
Copy link
Collaborator

mimarz commented Sep 6, 2024

This will be done again in #2385

@mimarz mimarz closed this Sep 6, 2024
@Barsnes Barsnes deleted the feat/css-modules branch October 22, 2024 12:53
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