Upgrade to Pro — share decks privately, control downloads, hide ads and more …

๐ŸŒ The Geek Conf 2024

๐ŸŒ The Geek Confย 2024

It's 2024 and I Can Finally Talk About Atomic CSS

Libraries like Tailwind became quite popular and their utility-firstโ€”aka atomic classesโ€”approach was an interesting paradigm shift in CSS. Many developers love it, and it's understandable why.

However, we tend to forget that the core of this technique isn't new. Way before Bootstrap, we all had our small CSS snippets copied from project to project with similar classes.

In this session, we'll discuss the evolution of scalable CSS, walk through the limitations and drawbacks of Atomic CSS, and figure out where this concept can be beneficial.

Matheus Albuquerque

July 18, 2024
Tweet

More Decks by Matheus Albuquerque

Other Decks in Programming

Transcript

  1. Hello, The Geek Conf 2024! ๐Ÿ‘‹ IT'S 2024 AND I

    CAN FINALLY TALK ABOUT ATOMIC CSS โ€ข JULY 18, 2024.
  2. This talk presentsโ€ฆ โ† The Bad (takes) โ† The Good

    โ† The Bad โ† The Ugly โ† The Good (Ways) โ† Closing Thoughts
  3. #definition ๐Ÿง Atomic CSS is the approach to CSS architecture

    that favors small, single- purpose classes with names based on visual function.
  4. #question ๐Ÿค” If you were to summarize your experience with

    atomic CSS, what would you say? e.g. easy to reuse, hard to read, love it, an anti-pattern, enforced consistency, BEM is enoughโ€ฆ
  5. โ€œWhat Aboutโ€ฆ Inline Styles?โ€ โ† SURPRISINGLY COSTLY FOR THE BROWSER

    โ† CAN'T HANDLE MEDIA QUERIES OR PSEUDO SELECTORS โ† YOU DONโ€™T HAVE TO FOLLOW ANY PRE-EXISTING DEFINITION โ† UTILITY CLASSES EXPOSE A WELL-DEFINED API AND A SINGLE SOURCE OF TRUTH
  6. โ€œWhat Aboutโ€ฆ the HTML Bloat?โ€ โ† THE DEFLATE ALGORITHM (GZIP)

    CAN HANDLE DUPLICATE STRINGS โ† THE MORE A SELECTOR IS REPEATED IN A STYLESHEET, THE MORE WORK THE BROWSER HAS TO DO TO RESOLVE ALL STYLES
  7. โ€œWhat Aboutโ€ฆ SoC?โ€ โ† STYLE COMPOSITION IS PERFORMED IN THE

    HTML, BUT NOT USING style OR align ATTRIBUTES โ† PIECES ARE ASSEMBLED FROM A STYLESHEET WRITTEN IN CSS AND HTML BECOMES A CONSUMER OF A CSS โ† EXTREME VISION OF โ€œSEPARATION OF CONCERNSโ€ โ† DEEPLY IMPRACTICAL (DOGMATIC LEVEL)
  8. โ€œWhat Aboutโ€ฆ?โ€ โ† โ€œREDESIGNING/THEMING BECOMES CHALLENGINGโ€ โ† โ€œIT RESULTS IN

    A LOT OF UNUSED CSSโ€ โ† โ€œITโ€™S HARD TO KNOW WHATโ€™S AVAILABLE TO USEโ€ โ† โ€œA WHOLE OTHER LANGUAGE TO LEARN ON TOP OF CSSโ€ โ† โ€ฆ
  9. Tailwind has the potential to alleviate some of the concerns

    outlined for atomic CSS and provides intriguing new benefits.
  10. Conciseness โ† WE'RE ALWAYS TRYING TO WRITE CODE AS EFFICIENTLY

    AS POSSIBLE โ† CSS STILL REQUIRES A SIGNIFICANT AMOUNT OF CODE EVEN WITH PREPROCESSORS โ† ATOMIC CSS ALLOWS DEVELOPERS TO USE SIGNIFICANTLY SMALLER RULES
  11. Conciseness div { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }

    @media (min-width: 1024px) { div { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
  12. Encapsulation โ† STYLING IS CONTAINED IN CLASSES AND ONLY APPLIES

    TO ELEMENTS WITH THOSE CLASSES โ† YOU DON'T HAVE TO WORRY ABOUT ACCIDENTALLY BREAKING SOMETHING WHEN MODIFYING A RULE IN A PARTICULAR CLASS โ† BETTER SPECIFICITY AND CLARITY: EACH ELEMENT CAN HAVE ITS OWN CLASSES FOR ITS PARTICULAR NEEDS
  13. The Good โ† CONCISENESS โ† COMPONENT-BASED FRAMEWORKS โ† ENCAPSULATION /

    CONSISTENCY โ† REDUCES OVERALL CSS โ† COPY-PASTE MARKUP
  14. IT'S 2024 AND I CAN FINALLY TALK ABOUT ATOMIC CSS

    โ† CSS DEFINES THE STYLE AND LAYOUT OF A PAGE = MODIFYING THE APPEARANCE OF A UI ELEMENT REQUIRES CHANGES TO THE CSS โ† THIS DOES NOT ALWAYS MEAN CREATING A NEW RULE
  15. IT'S 2024 AND I CAN FINALLY TALK ABOUT ATOMIC CSS

    โ† THE ONLY DIFFERENCE FROM A MAINTENANCE PERSPECTIVE IS WHERE YOU MAKE THE CHANGE, RATHER THAN WHETHER THE CHANGE IS REQUIRED โ† CHANGES RESTRICTED TO CSS VS RESTRICTED TO HTML โ† WHILE THE CLASS NAMES REMAIN UNCHANGED, YOU STILL NEED TO MODIFY THE CONTENTS OF THE CLASS ATTRIBUTE AS THAT IS YOUR CSS
  16. IT'S 2024 AND I CAN FINALLY TALK ABOUT ATOMIC CSS

    โ† ASSUMPTION: SWITCHING BETWEEN HTML AND CSS ALL THE TIME IS A PAIN โ† IF YOUโ€™RE TWEAKING MARKUP CONSTANTLY WHILE YOUโ€™RE WORKING ON THE APPEARANCE, YOU COULD BE TREATING IT AS AN EXTENSION OF CSS RATHER THAN THE MARKUP FOR THE DOCUMENT STRUCTURE
  17. I often see a popular belief that Tailwind is the

    future of web development, and that it represents the correct way to do things.
  18. Semantic HTML/CSS โ† Tailwind โ† BREAK โ€œCOMPOUNDโ€ SEMANTIC CLASSES INTO

    โ€œATOMICโ€ UTILITY CLASSES WITH SINGLE RESPONSIBILITIES โ† A FAIRLY EASY THING TO DO
  19. Tailwind โ† Semantic HTML/CSS โ† ITโ€™S HARD TO PICTURE A

    TOOL THAT DOES THE REVERSE โ† YOU CANโ€™T REALLY BUILD MORE COMPLEX WITHOUT ASSEMBLING THOSE CLASSES BY HAND โ† YOU COULD ONLY REALISTICALLY CONVERT TAILWIND INTO SOME OTHER UTILITY FRAMEWORK
  20. Utility CSS Lock-in โ† MOVING TO ANY OTHER FRAMEWORK OR

    PREPROCESSOR REQUIRES A CONSIDERABLE AMOUNT OF REFACTORING โ† MOST OF US ARENโ€™T SWAPPING OUT CSS FRAMEWORKS ON PROJECTS ON A REGULAR BASIS โ† BUT ITโ€™S AN IMPORTANT CONSIDERATION FOR APPS THAT YOUโ€™LL BE MAINTAINING IN THE FUTURE
  21. Missing Key CSS Features โ† BACKGROUND GRADIENTS; โ† ANIMATIONS; โ†

    NEW SELECTORS (:is AND :where) โ† NEW FUNCTIONS (min(), max(), AND clamp()) โ† OTHER BLEEDING-EDGE FEATURES
  22. Class Detection โ† TAILWIND DOESN'T PARSE OR EXECUTE ANY OF

    THE CODE โ† IT USES REGULAR EXPRESSIONS TO EXTRACT EVERY STRING THAT COULD POSSIBLY BE A CLASS NAME โ† IT WILL ONLY FIND CLASSES THAT EXIST AS COMPLETE UNBROKEN STRINGS โ† YOU CAN'T CONSTRUCT CLASS NAMES DYNAMICALLY
  23. Hard to Scan <div className="w-16 h-16 rounded text-white bg-black py-1

    px-2 m-1 text-sm md:w-32 md:h-32 md:rounded-md md:text-base lg:w-48 lg:h-48 lg:rounded-lg lg:text-lg"/>
  24. Hard to Scan .class { width: 16px; height: 16px; color:

    white; background-color: black; padding: 0.25rem 0.5rem; margin: 0.25rem; border-radius: 0.25rem; font-size: 0.875rem; line-height: 1.25rem; } @media screen and (min-width: 768px) { .class { width: 32px; height: 32px; border-radius: 0.375rem; font-size: 1rem; line-height: 1.5rem; } } @media screen and (min-width: 1024px) { .class { width: 48px; height: 48px; border-radius: 0.5rem; font-size: 1.125rem; line-height: 1.75rem; } }
  25. Hard to Scan .class { width: 16px; height: 16px; color:

    white; background-color: black; padding: 0.25rem 0.5rem; margin: 0.25rem; border-radius: 0.25rem; font-size: 0.875rem; line-height: 1.25rem; } @media screen and (min-width: 768px) { .class { width: 32px; height: 32px; border-radius: 0.375rem; font-size: 1rem; line-height: 1.5rem; } } @media screen and (min-width: 1024px) { .class { width: 48px; height: 48px; border-radius: 0.5rem; font-size: 1.125rem; line-height: 1.75rem; } }
  26. Hard to Scan โ† IT'S SIMPLER TO SCAN IF YOU

    ONLY NEED TO READ YOUR CODE FROM TOP TO BOTTOM โ† IT'S EASIER TO FIND SPECIFIC PROPERTY-VALUE PAIRS โ† PROPER SYNTAX HIGHLIGHTING SEPARATES PROPERTIES FROM VALUES AND ENHANCES READABILITY
  27. Encouraged Tag Soup โ† THE FOCUS IS ON THE CLASSES,

    NOT THE MARKUP โ† TAILWINDCSS DOESNโ€™T PROMOTE โ€œUGLY HTMLโ€ AT ALL โ† BUT YOU CAN THROW IN AS MANY <div> AS YOU WANT WITHOUT CARING ABOUT MEANING โ† YOU CAN DO IT THE RIGHT WAY, BUT ERGONOMICS LEAN DEEP DOM
  28. #hotTake ๐ŸŒถ The problem lies in thinking in CSS classes

    first, rather than starting with markup and working the CSS from there.
  29. #hotTake ๐ŸŒถ Tools can make it easy to do certain

    things the wrong way. Their approach makes poor semantics the easiest path.
  30. IT'S 2024 AND I CAN FINALLY TALK ABOUT ATOMIC CSS

    #3 DEVELOPER (TOOLS) EXPERIENCE
  31. Hard To Find Components โ† WHEN TROUBLESHOOTING IN A LARGE

    CODE BASE, NARROWING DOWN THE AFFECTED COMPONENT IS IMPORTANT โ† HAVING A CLOSE MAPPING BETWEEN A COMPONENTโ€™S NAME AND ITS CLASS NAMES MAKES IT EASIER TO FIND THE RELEVANT CODE QUICKLY โ† HERE, WE HAVE TWO OPTIONS: KNOWLEDGE OF THE PARTICULAR AREA OF THE APP AND STRINGS
  32. Hard To Find Components โ† NOT EVERYONE ON A TEAM

    MAY BE FAMILIAR WITH EVERY COMPONENT IN THE CODE BASE (ENTERPRISE-SCALE MONOREPOS, NEW HIRES, ETC.) โ† STRINGS MAY GET REUSED ACROSS MULTIPLE COMPONENTS = SEARCHING MAY YIELD MULTIPLE RESULTS โ† ITโ€™S HARD TO DETERMINE WHERE A MODULE STARTS AND ENDS
  33. Hard to Tweak CSS โ† MAKING STYLE TWEAKS THROUGH DEV

    TOOLS IN THE BROWSER IS A COMMON PRACTICE โ† HERE, YOU NEED TO GIVE EACH COMPONENT A NEW CLASS NAME AND USE THAT AS THE SELECTOR โ† โ€ฆOR USE INLINE STYLES, BUT SOMETIMES IT'S HELPFUL TO SEE HOW CHANGES ARE REFLECTED THROUGHOUT THE UI
  34. @for $i from 1 through 10 { .m-#{$i} { margin:

    $i / 4 rem; } } On-Demand Generation: Before
  35. @for $i from 1 through 10 { .m-#{$i} { margin:

    $i / 4 rem; } } .m-1 { margin: 0.25 rem; } .m-2 { margin: 0.5 rem; } / * . . . * / .m-10 { margin: 2.5 rem; } On-Demand Generation: Before
  36. On-Demand Generation: After CSS YOU ARE USING CSS SHIPPED (PROD

    / DEV) CSS GENERATED ON DEMAND SCAN GENERATE SHIP
  37. Beware of @apply NEGATES BENEFITS GAINED OVER TRADITIONAL COMPONENT- DRIVEN

    CSS: โ† THE EXPLICITNESS OF THE CLASS NAME โ† THE TIME SAVED BY NOT HAVING TO WRITE YOUR OWN CSS โ† THE SINGLE SOURCE OF TRUTH
  38. Beware of @apply โ† EXTREMELY DESTRUCTIVE POTENTIAL โ† E.G. USE

    FUNCTIONAL CLASSES AS AN ESCAPE HATCH TO UNDO COMPONENT RULES โ† STRIVE TO BE EXTREMELY STRICT WITH THE APPLICATION OF BOTH SO THAT THEY DO NOT INTERSECT OR SHARE CONCERNS
  39. StyleSheet Composition: Simple import { clsx, type ClassValue } from

    "clsx" import { twMerge } from "tailwind-merge" export function classnames(โ€ฆinputs: ClassValue[]) { return twMerge(clsx(inputs)) }
  40. const Input = ({ className, . . . props })

    = > { return <input className={classnames(' . . . ', className)} { . . . props} />; }; import { clsx, type ClassValue } from "clsx" import { twMerge } from "tailwind-merge" export function classnames(โ€ฆinputs: ClassValue[]) { return twMerge(clsx(inputs)) } StyleSheet Composition: Simple
  41. const buttonVariants = cva( ' . . . ', {

    variants: { intent: { primary: 'bg-green-500 hover:bg-green-600', secondary: 'bg-red-500 hover:bg-red-600', default: 'bg-gray-500 hover:bg-gray-600', }, size: { small: ['text-sm', 'py-1', 'px-2'], medium: ['text-base', 'py-2', 'px-4'], large: ['text-lg', 'py-4', 'px-8'], }, roundness: { square: 'rounded-none', round: 'rounded-md', pill: 'rounded-full', }, }, defaultVariants: { intent: 'default', size: 'medium', roundness: 'round', }, } ); StyleSheet Composition: Complex
  42. intent: { primary: 'bg-green-500 hover:bg-green-600', secondary: 'bg-red-500 hover:bg-red-600', default: 'bg-gray-500

    hover:bg-gray-600', } size: { small: ['text-sm', 'py-1', 'px-2'], medium: ['text-base', 'py-2', 'px-4'], large: ['text-lg', 'py-4', 'px-8'], } roundness: { square: 'rounded-none', round: 'rounded-md', pill: 'rounded-full', } StyleSheet Composition: Complex
  43. StyleSheet Composition: Complex export default function Button({ intent, size, roundness,

    children }) { return ( <button className={buttonVariants({ intent, size, roundness })}>{children}</button> ) }
  44. Accessibility โ† BUILD PROCESS: CATCH ERRORS AS YOU BUILD OUT

    THE APP (AXE-CORE, JSX-A11Y, LIGHTHOUSE AUDITS, ACCESSLINT.JS) โ† CONTINUOUS INTEGRATION: FIND ISSUES IN YOUR PULL REQUESTS (AXE-LINTER OR ACCESSLINT)
  45. #hotTake ๐ŸŒถ It allows us to write our styles in

    a familiar โ€œmonolithicโ€ way, but get Atomic CSS out. โ€” THE SHORTHAND-LONGHAND PROBLEM IN ATOMIC CSS, BY ROBIN WESER
  46. IT'S 2024 AND I CAN FINALLY TALK ABOUT ATOMIC CSS

    #1 ITโ€™S JUST A TOOL BUT THE RIGHT TOOLโ„ข CAN MAKE A HUGE DIFFERENCE
  47. IT'S 2024 AND I CAN FINALLY TALK ABOUT ATOMIC CSS

    #2 THE SOLUTION TO THE PROBLEM ONLY CHANGES THE PROBLEM
  48. IT'S 2024 AND I CAN FINALLY TALK ABOUT ATOMIC CSS

    #2 THE SOLUTION TO THE PROBLEM ONLY CHANGES THE PROBLEM TRADE-OFFS TRADE-OFFS TRADE-OFFS
  49. IT'S 2024 AND I CAN FINALLY TALK ABOUT ATOMIC CSS

    #3 UTILITY CLASSES CAN COEXIST WITH OTHER APPROACHES THE SWEET SPOT FOR YOU MIGHT BE A HYBRID OF DIFFERENT CONVENTIONS
  50. IT'S 2024 AND I CAN FINALLY TALK ABOUT ATOMIC CSS

    #4 TAILWIND SUFFERS FROM THINKING IT APPLIES TO ALL PROJECTS
  51. IT'S 2024 AND I CAN FINALLY TALK ABOUT ATOMIC CSS

    #5 JSX CREATED A SIMILAR FEELING AMONG DEVELOPERS WORLDWIDE โ€ฆIT WAS GOING AGAINST SO MANY BEST PRACTICES THAT IT COULDN'T BE CONSIDERED A GOOD IDEA
  52. If you were to summarize your experience with atomic CSS,

    what would you say? IT'S 2024 AND I CAN FINALLY TALK ABOUT ATOMIC CSS
  53. IT'S 2024 AND I CAN FINALLY TALK ABOUT ATOMIC CSS

    #6 THEREโ€™S AN EMOTIONAL FACET TO DEVELOPERSโ€™ TECH CHOICES THIS SCORCHING PASSION IS USUALLY WHATโ€™S CALLED TEAM IDENTIFICATION IN PSYCHOLOGY
  54. Matheus Albuquerque โ† ๐• ythecombinator โ† ๐Ÿ‘จ๐Ÿ’ป Sr. SWE @

    Medallia โ† โš› PC @ React Su m m it NYC โ† โšก Google Developer Expert
  55. THATโ€™S ALL, FOLKS! THANKS! ๐Ÿ‘‹ QUESTIONS? MATHEUS ALBUQUERQUE โ€ข @ythecombinator

    โ†‘ ALL THE LINKS! IT'S 2024 AND I CAN FINALLY TALK ABOUT ATOMIC CSS