Styling
The whole essence of building frontend UIs with React is how flexible it is. React offers various approaches and methods for build UIs with a great look and feel for the best user experience. It is important to know that whatever styling strategy is used... it is still CSS — we are writing CSS like we’ve always done. The difference is that the strategies available, allow us to make the styles more flexible or reactive. In other words we are able to write less and do more by combining our styles with the power of JS.
Before diving into our chosen approach it is important to cover some basic things regarding CSS. Knowing the advantage and pitfalls of writing plain CSS will help better understand why there are so many different methods, cover some of their advantages and clarify the choices we made.

Writing plain CSS
CSS has been around since the early days of web development. Throughout the years it has become much better, most of the cross browser issues have been resolved and new features that solve modern day requirements have been added.
Without diving into CSS too deeply the main things to know about are Specificity and the Cascade.
Specificity - Read more
Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied. Specificity is based on the matching rules which are composed of different sorts of CSS selectors
In traditional CSS styles that are applied by classes, specificity can create more problems that it solves. Chaining up multiple classes to an element in order to override other styles can cause issues when trying to change an elements look & feel via JS down the line.
The cascade - Read more
Stylesheets cascade — at a very simple level, this means that the order of CSS rules matters; when two rules apply that have equal specificity, the one that comes last in the CSS is the one that will be used.
This can create unexpected results when multiple large css files a complied into one stylesheet in the browser. Various styles can have clashing selectors and the styles get overridden unintentionally.
Styling react components
Understanding the above two concepts is crucial as it explains the issues that we aim to solve by using the different styling methods in react. What we aim to do can be split into two points
- Avoid issues with specificity and the cascade by scoping (encapsulating) our styles/rules
- Add JS magic to our styles so that we can dynamically enhance the look & feel or behavior of our elements/components
There are many ways to address these issues. Using css pre/post processors, using css modules or JSS but we don't have to dive into these.
Styling priority order
Before reaching for custom styling, check these in order:
- Reusable components from the
@bhag-business-platform/luna-componentslibrary. - MUI 5 out of the box — the default Luna theme is applied automatically.
- Custom styling — only when the above can't be used or produce the desired result. For custom styling, prefer MUI's
sxprop and built-in component style props (e.g.<Box display="flex">) for simple cases, and reserve a co-located.styles.jsxfile (the Emotion approach below) for larger or reused styles.
When a co-located styles file is the right choice, the method we use is styled-components.
Styled components - Emotion
styled-components is a library for React and React Native that allows you to use component-level styles in your application that are written with a mixture of JavaScript and CSS. What we use is a library heavily influenced by styled-components but made more framework agnostic and developer friendly. More information and guides available at the Emotion docs.
It was created with the same method of operation of CSS Modules, a way to write CSS that’s scoped to a single component, and not accessible to any other element in the page or even component.
styled-components allows developers to write plain CSS in React components without having to worry about clashing of class names.
For example, if we need to implement styling in our Button.js file using styled components from Emotion, we would first need to carry out the following steps:
- Firstly make sure the library is installed (this is already done in Luna).
- We then need to set up our styled component or in this case a styled button (we chose to do this in a separate jsx file. This keeps the component code nice and tidy and the styles separate).
- Now we can create a variable by selecting a particular HTML element where we store our style keys (we import the Button component from material ui to leverage all of it's functionality).
- Then we import our new styled Button in the component to be used.
The code below is an implementation of all the steps we mentioned above.
// Component.styles.jsx
import styled from '@emotion/styled';
import { Button } from '@mui/material';
export const StyledButton = styled(Button)`
color: turquoise;
` // Component.jsx
import { StyledButton } from './Component.styles'
const Component = () => {
return (
<StyledButton>Super awesome button</StyledButton>
);
};
export default Component;Dark theme
Luna has a dark theme that can be toggled on and off. All MUI components have been styled to work with the dark theme. When creating new components, make sure to test them in both light and dark mode to ensure they are styled correctly. If your component has custom styles, you may need to add a theme prop to your styled component to ensure it works with the dark theme.
Your current theme will follow your devices theme settings, but you can also toggle it manually in the settings menu. 
If you need to access the theme in your component, you can use the useTheme hook from MUI. For example to get the primary text color:
import { useTheme } from '@mui/material/styles';
const theme = useTheme();
...
color: theme.palette.text.primaryThings to avoid
- Adding inline styles to components. (this makes it extremely difficult to manipulate component styling with JS)
- Defining styled-component variables withing the component jsx. (We want to keep styles separate in a .styles.jsx file of the same name or a shared one if the style can be reused)
- Adding classNames to elements with styles attached (we have mixed approaches and we are trying to bring consistency and convert these into styled components where possible)
