React Styled Guide is a package that offers you an easy way to configure your own style guide and handle theme state in a React application build with Styled-Components.
react: >=18.2styled-components: >=5.3
Using npm:
$ npm install react-styled-guideUsing yarn:
$ yarn add react-styled-guideOnce the package is installed, you can import the library using import or require approach:
- Step 1: Import package provider (
StyledGuideProvider) according to your JavaScript environment;
CommonJS
// index.jsx
const { StyledGuideProvider } = require('react-styled-guide');ECMAScript
// index.jsx
import { StyledGuideProvider } from 'react-styled-guide';TypeScript
// index.tsx
import { StyledGuideProvider, StyledGuideProps } from 'react-styled-guide';- Step 2: Create your own StyleGuide using
StyledGuidePropsschema/type;
CommonJS/ECMAScript:
// index.jsx
const initialStyleGuide = {
...
};TypeScript:
// index.tsx
const initialStyleGuide: StyledGuideProps = {
...
};- Step 3: wrap your components with
<StyledGuideProvider />passing the initial state you have just created in previous step;
// index.tsx
...
const container = document.getElementById('root');
const root = createRoot(container);
root.render(
<StyledGuideProvider value={initialStyleGuide}>
{/* add your components here */}
</StyledGuideProvider>
);Set a color of a text:
// text.styles.jsx
import { styled } from 'styled-components';
import { getColor } from 'react-styled-guide';
export const Text = styled.p`
color: ${getColor(colors => colors.primary.medium)}; // '#01c4e7'
`;Set theme to 'dark':
// my-component.jsx
const MyComponent = () => {
const { setDarkTheme } = useTheme();
return <Button onClick={setDarkTheme}>SET DARK THEME</Button>;
};You can also check an entire application in example/ folder:
$ cd example
$ npm i # or yarn to install dependencies
$ npm start # or yarn start| key | type | required | default |
|---|---|---|---|
| theme | ThemeType | false | - |
| styleGuide | StyleGuide | true | - |
| key | value |
|---|---|
| dark | 'DARK' |
| light | 'LIGHT' |
| key | type | required | default |
|---|---|---|---|
| colors | Colors | true | - |
| fontSizes | FontSizes | true | - |
| fontWeights | FontWeights | true | - |
| sizes | Sizes | true | - |
| key | type | required | default |
|---|---|---|---|
| primary | ColorTokens | true | - |
| secondary | ColorTokens | true | - |
| neutral | ColorTokens | true | - |
| success | ColorTokens | true | - |
| warning | ColorTokens | true | - |
| error | ColorTokens | true | - |
| key | type | required | default |
|---|---|---|---|
| darkest | string |
true | - |
| darker | string |
true | - |
| dark | string |
true | - |
| tinyDark | string |
true | - |
| medium | string |
true | - |
| tinyLight | string |
true | - |
| light | string |
true | - |
| lighter | string |
true | - |
| lightest | string |
true | - |
| key | type | required | default |
|---|---|---|---|
| xxxl | string |
true | - |
| xxl | string |
true | - |
| xl | string |
true | - |
| l | string |
true | - |
| m | string |
true | - |
| s | string |
true | - |
| xs | string |
true | - |
| xxs | string |
true | - |
| xxxs | string |
true | - |
| key | type | required | default |
|---|---|---|---|
| bold | string |
true | - |
| medium | string |
true | - |
| regular | string |
true | - |
| light | string |
true | - |
| key | type | required | default |
|---|---|---|---|
| giant | string |
true | - |
| huge | string |
true | - |
| xxxl | string |
true | - |
| xxl | string |
true | - |
| xl | string |
true | - |
| l | string |
true | - |
| m | string |
true | - |
| s | string |
true | - |
| xs | string |
true | - |
| xxs | string |
true | - |
| xxxs | string |
true | - |
| nano | string |
true | - |
| quark | string |
true | - |
| key | type | description |
|---|---|---|
| theme | ThemeType | - |
| setDarkTheme | function |
dispatch action to change theme state to dark. |
| setLightTheme | function |
dispatch action to change theme state to light. |
- Description: Used to access StyleGuide object to get one of its children's value.
- Arguments:
function(StyleGuide) => string | number
- Returns:
string|number - Example:
import { styled } from 'styled-components';
import { getStyleGuide } from 'react-styled-guide';
export const Text = styled.p`
color: ${getStyleGuide(
styleGuide => styleGuide.colors.primary.medium // e.g.: '#01c4e7'
)};
`;- Description: Used to access Colors object to get one of its value.
- Arguments:
function(Colors) => string
- Returns:
string - Example:
import { styled } from 'styled-components';
import { getColor } from 'react-styled-guide';
export const Text = styled.p`
color: ${getColor(colors => colors.primary.medium)}; // e.g.: '#01c4e7'
`;- Description: Used to access Colors object to get one of its value according to
Themestate.- The first callback function argument will be called if
themeisDARK. - The second callback function argument will be called if
themeisLIGHT.
- The first callback function argument will be called if
- Arguments:
function(Colors) => stringfunction(Colors) => string
- Returns:
string - Example:
import { styled } from 'styled-components';
import { getColorByTheme } from 'react-styled-guide';
export const Text = styled.p`
color: ${getColorByTheme(
colors => colors.primary.darker, // e.g.: '#3F3F3F'
colors => colors.primary.lighter // e.g.: '#DADADA'
)};
`;- Description: Used to access FontSizes object to get one of its value.
- Arguments:
function(FontSizes) => string
- Returns:
string - Example:
import { styled } from 'styled-components';
import { getFontSize } from 'react-styled-guide';
export const Text = styled.p`
front-size: ${getFontSize(fontSizes => fontSizes.xs)}; // e.g.: '14px'
`;- Description: Used to access FontWeights object to get one of its value.
- Arguments:
function(FontWeights) => number
- Returns:
number - Example:
import { styled } from 'styled-components';
import { getFontWeight } from 'react-styled-guide';
export const Text = styled.p`
front-weight: ${getFontWeight(fontWeights => fontWeights.bold)}; // e.g.: 600
`;- Description: Used to access Sizes object to get one of its value.
- Arguments:
function(Sizes) => number
- Returns:
number - Example:
import { styled } from 'styled-components';
import { getSize } from 'react-styled-guide';
export const Card = styled.div`
height: ${getSize(sizes => sizes.giant)}; // e.g.: '200px'
padding: ${getSize(sizes => sizes.xxxs)}; // e.g.: '16px'
`;- Description: Used to get UseThemeType object to access and manipulate theme state.
- Arguments: -
- Returns: UseThemeType
- Example:
import { ThemeTypes, useTheme } from 'react-styled-guide';
export const App = () => {
const { theme, setDarkTheme, setLightTheme } = useTheme();
const changeTheme = () => {
if (theme === ThemeTypes.light) setDarkTheme();
else setLightTheme();
};
return (
<div>
<p>
Current Theme: <span>{theme}</span>
</p>
<button onClick={changeTheme}>CHANGE THEME</button>
</div>
);
};This package welcomes all constructive contributions. Feel free to add contributions from the following items (but not only) :
- bug fixes and enhancements
- new features
- additions and fixes to documentation
- additional tests
- triaging incoming pull requests and issues
$ git clone https://github.com/luizclr/react-styled-guide.git
$ cd react-styled-guide$ yarn installStart development server on watch mode:
$ yarn startRun test suite:
$ yarn testRun build script:
$ yarn buildCheck
dist/folder to see parsing resulte.
Run lint script:
$ yarn lintEnter in example/ foder and start development server on watch mode:
$ cd example
$ yarn startOpen a browser and access localhost:1234.