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

refactor: consolidate CSS stylesheets #114

Merged

Conversation

huyenltnguyen
Copy link
Member

@huyenltnguyen huyenltnguyen commented May 3, 2024

Checklist:

We currently have multiple stylesheets in the package, and they contain CSS rules that overlap / override each other. This PR is an attempt to consolidate them into a single stylesheet, housing the default styles of our design system.

I'll explain the changes with PR comments, but the high-level approach is:

  • Enable Tailwind preflight
  • Review the styles in global-element-styles.css and normalize.css and pick out the styles we want to keep
  • Remove global-element-styles.css and normalize.css
  • Add additional global styles to the base.css stylesheet (focus ring, link underline position, etc.)
  • Check and fix UI regression

Closes #61

Testing

I opened the production site (https://opensource.freecodecamp.org/ui) and compared the local version against it.

*/
border-width: 0;
html {
@apply text-md;
Copy link
Member Author

Choose a reason for hiding this comment

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

There are two font sizes specified in global-element-styles (the latter takes precedence):

And since we want to use the standard styles for html text:

/* Element styles rewrites from global.css that could be moved to presets*/
/* typography should be handled in tailwind config */

I applied the text-md style to the element.

The text-md style is defined as follows:

md: ["18px", "1.42857143"],


Also, we don't need to specify font family here.

In the Tailwind preflight stylesheet:

sans and mono fonts are specified here:

ui/tailwind.config.js

Lines 90 to 93 in 7f9fc26

fontFamily: {
sans: ["Lato", "sans-serif"],
mono: ["Hack-ZeroSlash", "monospace"],
},

/* Override the browser default border width in order to style individual border sides
* Ref: https://stackoverflow.com/a/76961084
*/
border-width: 0;
Copy link
Member Author

Choose a reason for hiding this comment

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

We needed to add this rule because we didn't use Tailwind preflight. It can be removed now that we enable the preflight.

Ref: https://github.com/tailwindlabs/tailwindcss/blob/3.4/src/css/preflight.css#L10

Comment on lines +12 to +14
:focus-visible {
@apply outline outline-3 outline-focus-outline-color outline-offset-0;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a custom rule.

I make it global so that we don't have to manually add it into each component. This also ensures that the focus outline display is consistent across components.

Comment on lines +29 to +34
code {
@apply bg-background-tertiary text-foreground-tertiary;
}
:not(pre) > code {
@apply border-1 border-gray-450;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

When I was checking the Tabs component on Storybook (/?path=/story/components-tabs--default), I noticed that the text color of the code element is red:

Screenshot 2024-05-07 at 14 57 11

This comes from:

code {
padding: 2px 4px;
font-size: 90%;
color: #c7254e;
background-color: #f9f2f4;
border-radius: 4px;
}

#c7254e isn't in our color system, and we also don't use this color for code in /learn, so I went ahead and copied the code styles from /learn:

https://github.com/freeCodeCamp/freeCodeCamp/blob/5694b0837c7bad757777d15b9f196a1d8d3c8dae/client/src/components/layouts/global.css#L458-L465


code element after the changes:

Light Dark
Screenshot 2024-05-07 at 15 07 18 Screenshot 2024-05-07 at 15 08 41

/* Override Tailwind's default `-webkit-tap-highlight-color` rule. */
/* https://github.com/tailwindlabs/tailwindcss/discussions/2984 */
button {
-webkit-tap-highlight-color: transparent;
Copy link
Member Author

Choose a reason for hiding this comment

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

Tailwind didn't have this rule in In earlier versions, but the rule has been added: https://github.com/tailwindlabs/tailwindcss/blob/3.4/src/css/preflight.css#L39.

Comment on lines -21 to -24
// Focus state
"focus:outline-none", // Hide the default browser outline
"focus-visible:ring",
"focus-visible:ring-focus-outline-color",
Copy link
Member Author

Choose a reason for hiding this comment

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

These are redundant now that the focus outline styles have been moved to the global stylesheet (base.css).

Comment on lines -28 to -30
"focus:outline-none", // Hide the default browser outline
"focus-visible:ring",
"focus-visible:ring-focus-outline-color",
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed as the focus outline styles are moved to the global stylesheet (base.css).

Comment on lines -11 to -13
"focus:outline-none", // Hide the default browser outline
"focus:ring",
"focus:ring-focus-outline-color",
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed as the focus outline styles are moved to the global stylesheet (base.css).

const classes = responsive
? [className, "block max-w-full h-auto"].join(" ")
Copy link
Member Author

Choose a reason for hiding this comment

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

Orginally, the img element doesn't have a max width, which allows it to expand outside of its content.

We actually do have that rule set, but I'm not sure why it doesn't take effect:

img {
max-width: 100% !important;
}

And in order to make the image responsive, we need to add in a couple of CSS styles:

  • display: block
  • max-width: 100%
  • height: auto

Now, with Tailwind preflight enabled, the above rules are set by default: https://github.com/tailwindlabs/tailwindcss/blob/3.4/src/css/preflight.css#L360-L380.

And this means we have to unset the default rules (specifically the width constraint) to make the image not responsive.

(Side note: We might want to revisit the component and see if we should just make Image always responsive. I can't think of a scenario where we want the image to scale freely.)

Comment on lines -5 to -7
corePlugins: {
preflight: false,
},
Copy link
Member Author

Choose a reason for hiding this comment

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

Enable Tailwind preflight.

(We can also just flip the boolean, but the preflight is enabled by default so I thought it's cleaner to just get rid of the code.)

Comment on lines +20 to +26
/* This is required in order to improve text readability in Arabic */
text-underline-position: under;
}
@supports not (text-underline-position: under) {
a {
text-underline-offset: 0.1em;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Pulled from normalize.css

ui/src/normalize.css

Lines 43 to 50 in 7f9fc26

/* This is required in order to improve text readability in Arabic */
text-underline-position: under;
}
@supports not (text-underline-position: under) {
a {
text-underline-offset: 1em;
}
}

The rule is needed to ensure there is an appropriate space between the text and the underline.

Ref: freeCodeCamp/freeCodeCamp#52366.

@huyenltnguyen huyenltnguyen force-pushed the fix/consolidate-css-stylesheets branch from 17a118f to b13b276 Compare May 8, 2024 06:42
/* https://github.com/tailwindlabs/tailwindcss/blob/3.4/src/css/preflight.css#L335 */
input::placeholder,
textarea::placeholder {
@apply text-foreground-quaternary opacity-70;
Copy link
Member Author

Choose a reason for hiding this comment

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

Tailwind gives the placeholder of input and textarea the following rules:

  • opacity: 1
  • color: theme('colors.gray.400', #9ca3af)

We're using foreground-quaternary for placeholder text, which overrides the Tailwind's color:

"outline-0 block w-full py-1.5 px-2.5 text-md text-foreground-primary placeholder:text-foreground-quaternary bg-background-primary bg-none rounded-none border-1 border-solid border-background-quaternary shadow-none transition ease-in-out duration-150 focus:border-foreground-tertiary";

And the color is strong enough that with opacity: 1, the placeholder text can be mistaken with an actual input value:

Screenshot 2024-05-08 at 12 14 32

To fix the issue, I had to override Tailwind color and opacity values. I also moved the placeholder rules from FormControl to base.css, just so that the rules can be applied globally.

(Tailwind is kind enough to set the text color for us, but we don't have gray-400 in our color list. And while the #9ca3af fallback works, the color isn't in our design system and the contrast ratio is also insufficient, so I kept using foreground-quaternary instead.)

Theme Screenshot Contrast ratio
Light Screenshot 2024-05-08 at 13 51 37 4.53:1 (contrast checker)
Dark Screenshot 2024-05-08 at 13 45 28 6.55:1 (contrast checker)

@huyenltnguyen huyenltnguyen marked this pull request as ready for review May 8, 2024 06:56
@huyenltnguyen huyenltnguyen requested a review from a team as a code owner May 8, 2024 06:56
@ahmaxed
Copy link
Member

ahmaxed commented May 18, 2024

Thanks for putting this pr together.
Once bootstrap is removed, we can gradually remove some of our customizations in favor of best practices or defaults.

@ahmaxed ahmaxed merged commit fc7654f into freeCodeCamp:main May 18, 2024
5 checks passed
@huyenltnguyen huyenltnguyen deleted the fix/consolidate-css-stylesheets branch May 20, 2024 05:42
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.

Consolidate CSS stylesheets
2 participants