Theming
This step consists in introducing onekijs-ui
which is a library that provides many components. Some of them are very complex like a data table with infinite scrolling.
In order to have a consistent look and feel (consistent color, padding, marging, ...), the style of these components is centralized in a customizable theme.
In this step we present how the theme works and how we can use it to update the look and feel of our website.
In the next step, we will introduce some of the components provided by onekijs-ui
To style the components, onekijs-ui
uses the styled components library. It's strongly recommended to learn this library !
Final result
The result of this step is as follows:
The background color of the <Navbar />
is from the theme
The font family is also from the theme
- Vite App
- Nextjs App
Adding onekijs-ui to the project
onekijs-ui
is a standalone library. So we have to add it to the project
yarn add onekijs-ui
Once the library is added, the entrypoint (index.tsx
) must be updated to wrap all components in a theme
We use <ClarityTheme />
which is a theme provided by onekijs-ui
. This theme implements the Clarity Design System from VMware
You can have a look at all the parameters defining the theme here
onekijs-ui
allows you to build your own theme from scratch or extend an existing theme to adapt it.
- Vite App
- Nextjs App
loading...
loading...
Theme colors
We want to update the navbar so the background color is coming from the theme and not from style.css
Concerning the colors, Onekijs-ui is inspired by [Bootstrap and a theme defines several ones:
name | Description |
---|---|
primary | The main color. Generally used for important element like a submit button |
secondary | The second main color. Generally used for less important element |
success | A color to represent a success |
info | A color to represnet some information |
warning | A color to indicate a warning |
danger | A color to indicate a danger / error |
white | white color |
black | black color |
light | a light color |
lightest | a very light color |
dark | a dark color |
darkest | a very dark color |
This means that if a component defines its background color as secondary
, the color will be the one identified by the key secondary
in the theme.
Actually, more colors are defined by the theme, the list is available here
The theme defines not only the colors but also the spacing, border radius, padding, ...
Upgrading the Navbar component
As we want to use a color coming from the theme, we can't use anymore style.css
to define the background color of the <Navbar/>
Therefore, the first thing to do is to update <Navbar>
to use a classname generated by styled-components instead of the classname defined in style.css
import { FCC, ... } from 'onekijs';
import React from 'react';
import styled from 'styled-components';
const Navbar: FCC = ({ className }) => { // FCC is like React.FC but add children and className to the props
...
return (
<div className={className}>
...
</div>
);
};
The prop className
is injected by styled-components like this
//instead of exporting the Navbar directly, we export the styled version
export default styled(Navbar)`
// CSS HERE
`;
To define the style of the <Navbar/>
, we could use standard CSS like this:
import styled from 'styled-components';
export default styled(Navbar)`
width: 100%;
height: 68px;
background-color: #1976d2;
padding: 16px;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
h1 {
color: white;
}
`;
But we want to use the color from the theme and not an hardcoded one.
To achieve that, for each CSS property, Onekijs-ui provides a method that has access to the theme and therefore can reference a property in the theme
import {
alignItems,
backgroundColor,
color,
display,
flexDirection,
height,
justifyContent,
padding,
width,
} from 'onekijs-ui';
import styled from 'styled-components';
export default styled(Navbar)`
${backgroundColor('secondary')} // use the key 'secondary' from the theme to define the color
${width('100%')}
${height('68px')}
${padding('lg')} // use the key 'lg' from the theme to define the size
${display('flex')}
${flexDirection('row')}
${justifyContent('space-between')}
${alignItems('center')}
h1 {
${color('white')}
}
`;
the CSS methods provided by oneki (backgroundColor, width, ...) can do more that just retrieving a color from the theme
Please read this documentation to learn more
The final <Navbar />
now looks like this
- Vite App
- Nextjs App
loading...
loading...
Next step
The getting started tutorial is over. You should now have a good understanding of what onekijs
can do to help you build enterprise applications !