Skip to content

Add lit and initial web components #2264

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

Merged
merged 4 commits into from
Jun 13, 2025
Merged

Add lit and initial web components #2264

merged 4 commits into from
Jun 13, 2025

Conversation

anselmbradford
Copy link
Member

@anselmbradford anselmbradford commented May 30, 2025

Additions

  • Add a new "elements" page with initial web components: cfpb-button and cfpb-file-upload.
  • Add lit dependency for web components.

Testing

  1. Check components > elements page for example web components. https://deploy-preview-2264--cfpb-design-system.netlify.app/design-system/components/elements

Screenshots

Screenshot 2025-05-30 at 7 27 23 PM

Copy link

netlify bot commented May 30, 2025

Thanks for the improvements! Browse a preview of your changes using the link below.

Name Link
🔨 Latest commit 09f0434
🔍 Latest deploy log https://app.netlify.com/projects/cfpb-design-system/deploys/684c44e275cd9a0008806914
😎 Deploy Preview https://deploy-preview-2264--cfpb-design-system.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@anselmbradford
Copy link
Member Author

Regular components /dist/ files are receiving uncompressed dupes of the web components code. Lemme know if you see why.

Copy link
Member

@wpears wpears left a comment

Choose a reason for hiding this comment

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

This looks directionally correct to me! I do wonder about trying to isolate scss from component code. That is, instead of having component scss with use statements that gets loaded as a text, what if we put the translation from scss-land into lit css template tags higher up, that is, in core, vars, etc. Then in component code we could simply import { vars } from './where/ever.js' and use them as normal in lit.

There's kinda a delicate dance, I'll admit, between maintaining scss for child consumers (mostly cf.gov) and making the lit components more idiomatic. Not sure what the best thing is here.

As to the practicalities of this PR, looks like there's some commented stuff in the file-upload index.js file we could trim.

@anselmbradford anselmbradford force-pushed the ans_wc_lit branch 2 times, most recently from 82faa19 to 2b7fde6 Compare June 11, 2025 21:29
@anselmbradford
Copy link
Member Author

This looks directionally correct to me! I do wonder about trying to isolate scss from component code. That is, instead of having component scss with use statements that gets loaded as a text, what if we put the translation from scss-land into lit css template tags higher up, that is, in core, vars, etc. Then in component code we could simply import { vars } from './where/ever.js' and use them as normal in lit.

One reason for the structure you see here is that I was trying to follow the USWDS structure somewhat (note though that they have an ?inline query on their scss, which is used by Vite) --> https://github.com/uswds/uswds/blob/develop/packages/usa-banner/src/usa-banner.component.js#L5

Another approach could be to have an intermediary JS file that serves as a bridge between the component JS and the SCSS file. Kind of like what you see on https://lit.dev/docs/components/styles/#sharing-styles, but in that example button-styles.js would have the import styles from './cfpb-button.component.scss' that you currently see in cfpb-button/index.js. This would create an extra JS file per component, so for simplicity I opted to add the SCSS import directly in the component code.

Copy link
Member

@wpears wpears left a comment

Choose a reason for hiding this comment

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

I'm happy to go with this and if we encounter any friction we can adapt as needed. Nice work!

@anselmbradford
Copy link
Member Author

Regular components /dist/ files are receiving uncompressed dupes of the web components code. Lemme know if you see why.

I figured this out. Internally within the DS packages, we had the convenience import of, e.g., import { BaseTransition, EventObserver } from '@cfpb/cfpb-design-system';. This imports from the main DS index file, which exports all public JS methods, including the web component code. This led to the web component code ending up in the other non-web component JS bundles. If we instead update the convenience imports to the actual locations of the JS methods, then the individual component bundles are scoped only to their immediate dependencies. See 09f0434

@anselmbradford
Copy link
Member Author

I'm going to merge this and open a separate PR that tweaks the optimizations. cssnano appears to provide slightly better compression than postcss-minify, and also might be able to handle autoprefixer as well. It would also be nice to remove whitespace from the web component markup in the JS file. It doesn't matter too much right now, because the markup is minimal, but could make a bigger difference in more complex components in the future.

@anselmbradford anselmbradford merged commit bd2c9c5 into main Jun 13, 2025
8 checks passed
@anselmbradford anselmbradford deleted the ans_wc_lit branch June 13, 2025 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

2 participants