1.1.0 • Published 1 year ago

react-styled-guide v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

React Styled Guide

GitHub license npm version install size npm bundle size npm downloads PRs Welcome Known Vulnerabilities

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.

Requirements

  • react: >=18.2
  • styled-components: >=5.3

Installation

Using npm:

$ npm install react-styled-guide

Using yarn:

$ yarn add react-styled-guide

Usage

Configure provider

Once 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 StyledGuideProps schema/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>
);

Examples

Style

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'
`;

Theme

Set theme to 'dark':

// my-component.jsx
const MyComponent = () => {
  const { setDarkTheme } = useTheme();

  return <Button onClick={setDarkTheme}>SET DARK THEME</Button>;
};

Application

You can also check an entire application in example/ folder:

$ cd example
$ npm i # or yarn to install dependencies
$ npm start # or yarn start

API

Schemes/Types

StyledGuideProps

keytyperequireddefault
themeThemeTypefalse-
styleGuideStyleGuidetrue-

ThemeType

keyvalue
dark'DARK'
light'LIGHT'

StyleGuide

keytyperequireddefault
colorsColorstrue-
fontSizesFontSizestrue-
fontWeightsFontWeightstrue-
sizesSizestrue-

Colors

keytyperequireddefault
primaryColorTokenstrue-
secondaryColorTokenstrue-
neutralColorTokenstrue-
successColorTokenstrue-
warningColorTokenstrue-
errorColorTokenstrue-

ColorTokens

keytyperequireddefault
darkeststringtrue-
darkerstringtrue-
darkstringtrue-
tinyDarkstringtrue-
mediumstringtrue-
tinyLightstringtrue-
lightstringtrue-
lighterstringtrue-
lighteststringtrue-

FontSizes

keytyperequireddefault
xxxlstringtrue-
xxlstringtrue-
xlstringtrue-
lstringtrue-
mstringtrue-
sstringtrue-
xsstringtrue-
xxsstringtrue-
xxxsstringtrue-

FontWeights

keytyperequireddefault
boldstringtrue-
mediumstringtrue-
regularstringtrue-
lightstringtrue-

Sizes

keytyperequireddefault
giantstringtrue-
hugestringtrue-
xxxlstringtrue-
xxlstringtrue-
xlstringtrue-
lstringtrue-
mstringtrue-
sstringtrue-
xsstringtrue-
xxsstringtrue-
xxxsstringtrue-
nanostringtrue-
quarkstringtrue-

UseThemeType

keytypedescription
themeThemeType-
setDarkThemefunctiondispatch action to change theme state to dark.
setLightThemefunctiondispatch action to change theme state to light.

Style helpers

getStyleGuide()

  • 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'
  )};
`;

getColor()

  • 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'
`;

getColorByTheme()

  • Description: Used to access Colors object to get one of its value according to Theme state.
    • The first callback function argument will be called if theme is DARK.
    • The second callback function argument will be called if theme is LIGHT.
  • Arguments:
    • function(Colors) => string
    • function(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'
  )};
`;

getFontSize()

  • 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'
`;

getFontWeight()

  • 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
`;

getSize()

  • 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'
`;

Theme hook

useTheme()

  • 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>
  );
};

Contributing

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

Cloning repository

$ git clone https://github.com/luizclr/react-styled-guide.git
$ cd react-styled-guide

Installing dependencies

$ yarn install

Developing

Start development server on watch mode:

$ yarn start

Testing

Run test suite:

$ yarn test

Building

Run build script:

$ yarn build

Check dist/ folder to see parsing resulte.

Linting

Run lint script:

$ yarn lint

Example Application

Enter in example/ foder and start development server on watch mode:

$ cd example
$ yarn start

Open a browser and access localhost:1234.

LICENSE

MIT

1.1.0

1 year ago

1.0.0

1 year ago

0.2.0

1 year ago

0.1.0

1 year ago