Moving on From Runtime Css-In-Js at Scale

Rate this content
Bookmark

In this talk, Siddharth shares the challenges his team faced with optimising runtime css in js (styled-components) for performance. At GitHub, there are about 4000 React components that contain styles, Siddharth dives into the reasons for rethinking styling architecture, the challenges faced and lessons learned by migrating a big application.

This talk has been presented at React Summit 2023, check out the latest edition of this React Conference.

Watch video on a separate page

FAQ

Primer React is the design system that powers GitHub, providing the styling architecture for various React components used by the platform.

Changing the styling architecture for Primer React involves significant challenges due to the cascading effects any modifications can have across all of GitHub, which makes it a critical and complex task.

With the introduction of React and later CSS modules, it became possible to create a more organized dependency tree between styles and components, allowing for smarter bundles, dead code elimination, and a one-to-one match between component JavaScript and CSS.

Styled components allow for runtime theming, where themes can dynamically change and propagate across the application. This provides a more flexible theming approach compared to pre-compiled themes in traditional CSS setups.

TypeScript enhances styling with JavaScript by providing type safety, which helps in detecting and correcting errors related to theme properties directly in the development environment, thus improving code reliability and maintainability.

Using more JavaScript in styling can introduce features like dynamic theming and better developer tools, but it can also lead to performance issues, especially in server-side rendering contexts where style computations can significantly affect render times.

Reducing JavaScript usage in styling is considered due to improvements in CSS capabilities such as CSS variables and native support for features that previously required JavaScript, leading to more efficient and simpler implementations.

Automation can help in migrating styling systems by transforming existing code to new formats or standards, reducing manual effort, and ensuring consistency across large codebases, though it often requires human oversight for optimal results.

Siddharth Kshetrapal
Siddharth Kshetrapal
29 min
02 Jun, 2023

Comments

Sign in or register to post your comment.
Video Summary and Transcription
This Talk explores the evolution of styling architecture, dynamic theming with style components, and optimizing style updates. It discusses the challenges of CSS migration and the choice between JavaScript and CSS native tooling. The Talk also touches on CSS tools and libraries, including Tailwind CSS and CSS in JS major libraries like MUI. The importance of picking a stack based on team members' strengths and the use of namespacing CSS for conflict-free dependency trees are highlighted.

1. Introduction to Styling Architecture

Short description:

Earlier this year, I found myself thinking about components, styles, and the amount of JavaScript needed for rendering. Assigned the task of figuring out the styling architecture for Primer React, which powers GitHub. The challenge was that any change would have cascading effects on all of GitHub. Let's explore how we got here and the state of the styling infrastructure.

I have a lot of slides, so I'm going to be very quick about this. Earlier this year, I found myself thinking about components, thinking about styles, but also thinking a lot about the amount of JavaScript we needed to render our styles. I was going on this spectrum of JavaScript on, we should have very little JavaScript to do our styles, do we actually need a lot more JavaScript to do it? I just spent a lot of time just tilting in this one line.

How I got there was, I was basically assigned this task. Which is, figure out the styling architecture for Primer React. What is Primer React? Primer React is the design system that powers GitHub. I'm Siddharth. I work on the design engineering team at GitHub. This was one big, it's funny that we call them epic, because it was kind of an epic task. And before GitHub, I used to work at all of these companies and for the last six years I've been basically working on design systems and component libraries. Fun fact, just like React, I also started my career exactly 10 years ago today, so thank you for that. I appreciate the balloon. Thank you.

All right, so, back to the moving slide. And why this was such a confusing task? Because when I was trying to figure out the styling architecture for Primer, it ended up being the styling architecture for a lot of React at GitHub. Any tiny change that I make would have cascading effects, get it? Cascading? Effects on all of GitHub. And that's a scary thing to do. So let me first talk about how did we land up in the space that we are.

This is not just GitHub. This is mostly the entire styling infrastructure. Think about it. It's 2015, and this meme just came out. This is that old. The best movie of all time, Mad Max, was also released in 2015, and Drake gave us this extremely cool song. Internet Explorer 11 was still around. Edge wouldn't be released until later that year. Which also means things like CSS variables that we take for granted now weren't actually supported in much except maybe like Chrome or new Firefox. I think Firefox is the first that implemented it all the way in 2014. Different times. You require very little JavaScript to get your styles done.

Read also

2. Styling Architecture Evolution

Short description:

You write your style like this. This is a button component. You put your colors, put your padding. With a little amount of JavaScript, you could nest your styles and compile them out to CSS. React introduced the concept of defining components in different files and creating a dependency tree between them, allowing for smarter bundles and elimination of dead code. CSS modules further enhanced this by creating a one-to-one relationship between component JavaScript and CSS, enabling efficient bundling and dead code elimination.

You write your style like this. This is a button component. You put your colors, put your padding. You see a lot of hex codes, pixel values hard coded over here. This isn't as nice as you'd like it to be, but there was some tooling that existed.

I put it on like slightly more JavaScript because you have to use Node.js and the compiler for JavaScript is written in JavaScript. With a little amount of JavaScript these are the kinds of things you could do. You could nest your styles and have slightly better DX and then you could compile it out back to CSS.

The other thing that you got was variables. You could define these variables in a single place, which is huge for our design systems. And then in our components you could just use those variables. So when React came around in 2013, and then when we started using React, you would use it something like this. You have a button component and it uses the class name. And then you could use this button component in another type of page.

The nice thing that we just did, by defining a component in a different file, and then using it in a new file, is that we've created this relation. We've created a dependency tree between components. And that helps us create smarter bundles, eliminate dead code. You get a lot of tooling from that. But the same kind of tooling doesn't really exist for... Or didn't really exist for CSS.

This class that is coming from button, there wasn't really a good way of knowing which file does it come from. You would just have to guess and bundle a lot more CSS. That of course changed very quickly with just a little more JavaScript with CSS modules. So you could configure CSS modules, you could basically fake an import, is what I'd call it. Because there is no actual CSS import that's happening this way. But we are creating a dependency tree. So now that your CSS dependency tree matches your JavaScript dependency tree, you get this really nice one-to-one between component JavaScript and CSS. And then you can bundle them together. You can tree shake them, you can eliminate dead code very quickly.

The next interesting thing that gets to is even though you have variables in Sass, wow, I see your eyes, I'm sorry about that.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Guide to React Rendering Behavior
React Advanced 2022React Advanced 2022
25 min
A Guide to React Rendering Behavior
Top Content
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize React rendering.
Scaling Up with Remix and Micro Frontends
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Scaling Up with Remix and Micro Frontends
Top Content
This talk discusses the usage of Microfrontends in Remix and introduces the Tiny Frontend library. Kazoo, a used car buying platform, follows a domain-driven design approach and encountered issues with granular slicing. Tiny Frontend aims to solve the slicing problem and promotes type safety and compatibility of shared dependencies. The speaker demonstrates how Tiny Frontend works with server-side rendering and how Remix can consume and update components without redeploying the app. The talk also explores the usage of micro frontends and the future support for Webpack Module Federation in Remix.
Speeding Up Your React App With Less JavaScript
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Top Content
Watch video: Speeding Up Your React App With Less JavaScript
Mishko, the creator of Angular and AngularJS, discusses the challenges of website performance and JavaScript hydration. He explains the differences between client-side and server-side rendering and introduces Quik as a solution for efficient component hydration. Mishko demonstrates examples of state management and intercommunication using Quik. He highlights the performance benefits of using Quik with React and emphasizes the importance of reducing JavaScript size for better performance. Finally, he mentions the use of QUIC in both MPA and SPA applications for improved startup performance.
React Concurrency, Explained
React Summit 2023React Summit 2023
23 min
React Concurrency, Explained
Top Content
Watch video: React Concurrency, Explained
React 18's concurrent rendering, specifically the useTransition hook, optimizes app performance by allowing non-urgent updates to be processed without freezing the UI. However, there are drawbacks such as longer processing time for non-urgent updates and increased CPU usage. The useTransition hook works similarly to throttling or bouncing, making it useful for addressing performance issues caused by multiple small components. Libraries like React Query may require the use of alternative APIs to handle urgent and non-urgent updates effectively.
Understanding React’s Fiber Architecture
React Advanced 2022React Advanced 2022
29 min
Understanding React’s Fiber Architecture
Top Content
This Talk explores React's internal jargon, specifically fiber, which is an internal unit of work for rendering and committing. Fibers facilitate efficient updates to elements and play a crucial role in the reconciliation process. The work loop, complete work, and commit phase are essential steps in the rendering process. Understanding React's internals can help with optimizing code and pull request reviews. React 18 introduces the work loop sync and async functions for concurrent features and prioritization. Fiber brings benefits like async rendering and the ability to discard work-in-progress trees, improving user experience.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.

Workshops on related topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
AI on Demand: Serverless AI
DevOps.js Conf 2024DevOps.js Conf 2024
163 min
AI on Demand: Serverless AI
Top Content
Featured WorkshopFree
Nathan Disidore
Nathan Disidore
In this workshop, we discuss the merits of serverless architecture and how it can be applied to the AI space. We'll explore options around building serverless RAG applications for a more lambda-esque approach to AI. Next, we'll get hands on and build a sample CRUD app that allows you to store information and query it using an LLM with Workers AI, Vectorize, D1, and Cloudflare Workers.
Building WebApps That Light Up the Internet with QwikCity
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
Featured WorkshopFree
Miško Hevery
Miško Hevery
Building instant-on web applications at scale have been elusive. Real-world sites need tracking, analytics, and complex user interfaces and interactions. We always start with the best intentions but end up with a less-than-ideal site.
QwikCity is a new meta-framework that allows you to build large-scale applications with constant startup-up performance. We will look at how to build a QwikCity application and what makes it unique. The workshop will show you how to set up a QwikCitp project. How routing works with layout. The demo application will fetch data and present it to the user in an editable form. And finally, how one can use authentication. All of the basic parts for any large-scale applications.
Along the way, we will also look at what makes Qwik unique, and how resumability enables constant startup performance no matter the application complexity.
Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
Next.js 13: Data Fetching Strategies
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Top Content
WorkshopFree
Alice De Mauro
Alice De Mauro
- Introduction- Prerequisites for the workshop- Fetching strategies: fundamentals- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)- Test your build and serve it on Vercel- Future: Server components VS Client components- Workshop easter egg (unrelated to the topic, calling out accessibility)- Wrapping up
Building a Shopify App with React & Node
React Summit Remote Edition 2021React Summit Remote Edition 2021
87 min
Building a Shopify App with React & Node
Top Content
WorkshopFree
Jennifer Gray
Hanna Chen
2 authors
Shopify merchants have a diverse set of needs, and developers have a unique opportunity to meet those needs building apps. Building an app can be tough work but Shopify has created a set of tools and resources to help you build out a seamless app experience as quickly as possible. Get hands on experience building an embedded Shopify app using the Shopify App CLI, Polaris and Shopify App Bridge.We’ll show you how to create an app that accesses information from a development store and can run in your local environment.