Building Pixel-Perfect UI Components Using CSS Variables

Rate this content
Bookmark
CSS variables are transforming UI development by enabling dynamic values and mathematical calculations directly within CSS, simplifying the process of making pixel-perfect adjustments. Joy UI, a sister library to Material UI, leverages CSS variables to create adaptable components, enhancing developer experience. Unlike Material UI, where small adjustments can be complex, Joy UI uses CSS variables for easier customization. This approach eliminates the need for JavaScript in styling, making the process more intuitive. The video also highlights how MUI uses CSS variables to improve the developer experience and compares the switch components in Material UI and Joy UI. Developers are encouraged to explore the Joy UI playgrounds and provide feedback.

From Author:

CSS variables have become so sophisticated in recent years that they now enable us to do things that aren't possible with JavaScript. In this lightning talk, I'll explain how MUI's engineers are leveraging the power of CSS variables in our new React component library, Joy UI, to deliver components that automatically adapt their style and structure to the context in which they're rendered.

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

The speaker's most controversial opinion is that they love CSS.

The speaker is excited about CSS variables because they open up many possibilities for UI developers, allowing components to adapt to their surroundings and match the styles and structures of their parent and child components.

MUI leverages CSS variables to deliver a developer experience that sparks joy and delight when customizing React components, enabling components to adapt their styles automatically.

Joy UI is a new sister library to Material UI that implements MUI's own in-house design system along with innovative developer experiences, particularly in how it handles CSS variables.

CSS variables are powerful for design systems because they provide a single source of truth for values like branding colors, making it easier to maintain consistency and future-proof code.

CSS variables improve the developer experience by allowing dynamic values and mathematical calculations within CSS, eliminating the need for JavaScript and simplifying adjustments like pixel-perfect changes.

An example is the switch component in Joy UI. Adjustments to the component, such as changing the width of the track or the size of the thumb, are handled seamlessly thanks to CSS variables.

Developers face issues such as making multiple adjustments when trying to make small changes to components, dealing with the complexity of pixel-perfect adjustments, and performing mathematical calculations for properties like border radii.

Joy UI simplifies adjustments by using CSS variables, which automatically adapt component styles without requiring JavaScript or manual calculations for properties like border radii.

Developers can find CSS variable playgrounds throughout the Joy UI documentation, which allow them to experiment with and customize components easily.

Sam Sycamore
Sam Sycamore
9 min
02 Jun, 2023

Comments

Sign in or register to post your comment.

Video Transcription

1. Introduction to CSS and MUI

Short description:

I love CSS. That's it. That's the take. Specifically excited about CSS variables and their possibilities for UI developers. MUI leverages CSS variables for a delightful developer experience. CSS's role is expanding, opening up new possibilities. MUI's history and understanding of developer needs.

All right, hello everybody. Party people in the house. Wow. That's a lot of faces out there. All right.

I wasn't really sure how to start this talk. I was talking to a friend of mine who said, you should share your spiciest take. So in the next slide, I'm going to share my most controversial opinion in the realm of web dev. This is going to ruffle a lot of feathers in the crowd here today, but I really feel it needs to be said. You ready? Here goes. I love CSS. That's it. That's the take. I told you it's spicy, but I'm not here to make friends. I'm here to speak the truth.

Specifically the thing that I'm really excited about lately is CSS variables and all the possibilities that they open up for us as UI developers. At MUI, we are leveraging the power of CSS variables to deliver a developer experience that sparks joy and delight when customizing React's components. With CSS variables, we can essentially create components that are aware of their surroundings and automatically adapt to match the styles and structures of their parents and children. And best of all, this is all happening in CSS, not JavaScript. Don't send JavaScript to do CSS's job. And the role of CSS is quickly expanding. So for us developers, it opens up so many new possibilities.

But before we explore some of those possibilities, I need to share a little back story here. So MUI as a company began with material UI, which is the React implementation of Google's material design. We do a lot more than just material design these days. But material UI remains one of the most popular open source libraries on GitHub and has a history stretching all the way back to the early days of React itself. Over the course of those nine years or so, we've learned a lot about what developers want and need out of a component library. More than anything else, we've learned a lot about what developers hate. Here's an example of the kind of thing that I see on Twitter from time to time. I get it.

2. CSS Variables and Joy UI

Short description:

It can be frustrating when making small adjustments to a component requires multiple changes. Introducing Joy UI, a sister library to Material UI, with a focus on developer experience. Joy UI leverages CSS variables, allowing for a single source of truth and named variables for consistency and future-proofing. CSS variables have applications beyond branding, enabling dynamic values and mathematical calculations. Joy UI uses CSS variables to create components that adapt to their parents and children. Let's compare Material UI and Joy UI with a switch component.

The king of the haters here, he's got a point. He's not wrong. It can be super frustrating when you're trying to make a teeny tiny, two-pixel adjustment to one little component. And then you realize, oh, suddenly I have to make 10 other adjustments. It's frustrating. I get it. And that's just one of the reasons why we are building Joy UI, which is a brand new sister library to Material UI that implements our own in-house design system along with some bleeding edge innovations in developer experience.

And this is most evident, I think, in the way we've constructed Joy UI to handle CSS variables. So what even is a CSS variable? According to MDN, CSS variables are entities defined by CSS authors that contain specific values to be reused throughout a document. It's easy enough to understand in the context of branding colors, right? You've got a set of colors that are going to be reused throughout a design. And if you need to change one, it's so much simpler if you have one singular source of truth so you're not stuck finding and replacing hundreds of instances of the wrong hex code. Having that one source of truth is so powerful. And one of things that's really cool is you can actually name your variables, right? It helps a lot to maintain consistency across your design system and to help future proof your code, right? So instead of trying to figure out why this piece of text is sized at 1.75 rem, you can see that it has this dynamic value that's set to header to text, which gives you a lot more information about why that style has been applied.

And as you dive deeper down the rabbit hole, what you realize is that CSS variables have some really cool applications with our design systems that extend far beyond mere branding. So here's an example of what's going under the hood of the JoyUI component, the input component, that I shared a few slides back. It's weird, right? It's kind of hard to get used to at first. I mean, what is even going on here? We've got CSS variables that are referencing other variables, and they're doing mathematical calculations based on those variables. And it's all happening in CSS, right? As Josh Como points out, we're just not used to this concept, this mental model of defining CSS properties that have dynamic values. And things get even weirder when you start doing math with your variables and referencing variables inside of variables. Indeed, in the case of JoyUI, we're using CSS variables to create components that can automatically adapt their styles to match those of their parents and children, since they all reference that one singular source of truth for their magic numbers. So OK, that sounds cool, but what difference does it actually make, right? Let's consider a switch component and observe the differences in developer experience between Material UI and JoyUI. So here's some examples of switches.

These are all Material UI switches. Switch, obviously, simple on and off toggle, it's composed of a track and a thumb. So the user clicks the thumb to slide it across the track and make their selection. It's pretty simple on the surface, but it can get quite annoying when you need to make pixel perfect adjustments to the Material UI component. For instance, if we need to change the width of the track here, well, then that throws the thumb completely out of whack because it doesn't know the length of the track. And if I try to change the size of the thumb, well that messes up its path as well. And don't even get me started on border radii. If I need to adjust the rounded corners, it's not as though I can simply pass the same values to both pieces.

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

Design Systems: Walking the Line Between Flexibility and Consistency
React Advanced Conference 2021React Advanced Conference 2021
47 min
Design Systems: Walking the Line Between Flexibility and Consistency
Top Content
The Talk discusses the balance between flexibility and consistency in design systems. It explores the API design of the ActionList component and the customization options it offers. The use of component-based APIs and composability is emphasized for flexibility and customization. The Talk also touches on the ActionMenu component and the concept of building for people. The Q&A session covers topics such as component inclusion in design systems, API complexity, and the decision between creating a custom design system or using a component library.
Towards a Standard Library for JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
Towards a Standard Library for JavaScript Runtimes
Top Content
There is a need for a standard library of APIs for JavaScript runtimes, as there are currently multiple ways to perform fundamental tasks like base64 encoding. JavaScript runtimes have historically lacked a standard library, causing friction and difficulty for developers. The idea of a small core has both benefits and drawbacks, with some runtimes abusing it to limit innovation. There is a misalignment between Node and web browsers in terms of functionality and API standards. The proposal is to involve browser developers in conversations about API standardization and to create a common standard library for JavaScript runtimes.
Build a Design System with React and Tailwind CSS
React Summit 2022React Summit 2022
27 min
Build a Design System with React and Tailwind CSS
Top Content
This Talk discusses design systems and how to build one using React and Tailwind CSS. Tailwind CSS provides utility classes for building complex layouts without writing CSS rules. Custom colors can be added to the Tailwind CSS config file, and font styles and text sizes can be customized. The entire Tailwind CSS configuration can be customized to meet specific requirements. Base styles can be added to the config file itself using a plugin. Reusable components can be created with Tailwind CSS, allowing for easy customization of size and color.
Composition vs Configuration: How to Build Flexible, Resilient and Future-proof Components
React Summit 2022React Summit 2022
17 min
Composition vs Configuration: How to Build Flexible, Resilient and Future-proof Components
Top Content
Today's Talk discusses building flexible, resilient, and future-proof React components using composition and configuration approaches. The composition approach allows for flexibility without excessive conditional logic by using multiple components and passing props. The context API can be used for variant styling, allowing for appropriate styling and class specification. Adding variants and icons is made easy by consuming the variant context. The composition and configuration approaches can be combined for the best of both worlds.
Rethinking CSS - Introducing Stylex
React Finland 2021React Finland 2021
25 min
Rethinking CSS - Introducing Stylex
Top Content
CSS + superpowers - bloat. How Stylex creates a zero-cost abstraction that gives CSS superpowers.
Building Cross-Platform Component Libraries for Web and Native with React
React Advanced Conference 2021React Advanced Conference 2021
21 min
Building Cross-Platform Component Libraries for Web and Native with React
Top Content
This Talk discusses building cross-platform component libraries for React and React Native, based on a successful project with a large government-owned news organization. It covers the requirements for React Native knowledge, building cross-platform components, platform-specific components, styling, and the tools used. The Talk also highlights the challenges of implementing responsive design in React Native.

Workshops on related topic

Build a Data-Rich Beautiful Dashboard With MUI X's Data Grid and Joy UI
React Summit 2023React Summit 2023
137 min
Build a Data-Rich Beautiful Dashboard With MUI X's Data Grid and Joy UI
Top Content
WorkshopFree
Sam Sycamore
Siriwat (Jun) Kunaporn
2 authors
Learn how to put MUI’s complete ecosystem to use to build a beautiful and sophisticated project management dashboard in a fraction of the time that it would take to construct it from scratch. In particular, we’ll see how to integrate the MUI X Data Grid with Joy UI, our newest component library and sibling to the industry-standard Material UI.
Table of contents:- Introducing our project and tools- App setup and package installation- Constructing the dashboard- Prototyping, styling, and themes - Joy UI features- Filtering, sorting, editing - Data Grid features- Conclusion, final thoughts, Q&A
Learn To Use Composables: The Swiss Army Knife Of Vue Developers
Vue.js Live 2024Vue.js Live 2024
163 min
Learn To Use Composables: The Swiss Army Knife Of Vue Developers
Workshop
Juan Andrés Núñez Charro
Juan Andrés Núñez Charro
Composables (composition functions) are stateful/stateless functions that can leverage Vue's reactivity API, decoupling it from components.This shift in perspective opens the possibility for tackling common scenarios in a new and creative way. In this workshop, you will learn how to solve typical problems every Vue developer faces, using composables:
- Data store;- Component cross-communication;- Utility functions (DOM, API, etc);And more.