Git Product home page Git Product logo

component-library-starter's Introduction

Component Library Starter

a small starter for building a component library with styled-components πŸ’…

Overview

This component lib is set up to use styled-components, polished, and styled-components-modifiers πŸ’…. This starter is designed to help you create your own components and easily publish them to npm.

Up & Running

To install dependencies with Yarn, run:

$ yarn

or to install with npm, run:

$ npm install

File Structure

This component library borrows its structure from BEM and is set up to use Blocks, Elements, and Modifiers. Below is a description of each.

Blocks

Blocks are the highest level of abstraction in the Blocks, Elements, Modifiers concept. They are responsible for providing the context for Elements, handling UI logic, and rendering the Elements within the Block. They are not connected to application state, nor do they handle any business logic.

Elements

Elements are the smallest, indivisible parts of UI. They are responsible for actually rendering the UI. They do not handle application logic or UI logic, but they do handle their own modifiers which modify the element’s style. Elements generally exist within the context of a Block (as their own file in the Block’s directory) allowing the reuse of that set of Elements, but they are not exclusively bound to blocks. An example of a stand-alone Element would be an A, Link, H3, or P. These common elements live in lib/elements.

Modifiers

This library utilizes styled-components-modifiers to build modifiers. Modifiers are small functions that allow us to alter the properties of an Element. They primarily live in the Element's file and are solely responsible for modifying styles. Some modifiers are common to multiple Elements. An example would be fontWeights. These common modifiers live in lib/modifiers

An Example Structure

β”œ lib/
β”œβ”€β”€ blocks/
|   β”œβ”€β”€ Card
|   |   β”œβ”€β”€ Body.js     // <- Element
|   |   β”œβ”€β”€ Footer.js   // <- Element
|   |   β”œβ”€β”€ Header.js   // <- Element
|   |   └── index.js    // <- Block
|   └── index.js        // <- export for all Blocks
β”œβ”€β”€ elements/
|   β”œβ”€β”€ A
|   |   β”œβ”€β”€ __tests__
|   |   |   β”œβ”€β”€ __snapshots__
|   |   |   |   └── index.js.snap   // <- Snapshot Test
|   |   |   └── index.js            // <- Test
|   |   └── index.js                // <- Element
|   β”œβ”€β”€ Link
|   |   └── index.js                // <- Element
|   β”œβ”€β”€ H3
|   |   └── index.js                // <- Element
|   β”œβ”€β”€ P
|   |   └── index.js                // <- Element
|   └── etc.
|   └── index.js                    // <- export for all Blocks
β”œβ”€β”€ modifiers/
|   β”œβ”€β”€ fontWeights
|   └── etc.
└── index.js                        // <- main export for the library

Local Development

Module Development Workflow

Helpful information on development workflow in this library lives here.

Linting

NOTE: The linter will run against everything in the lib directory. I've added an initial .eslintrc file for some basic configuration. Feel free to edit or replace it as needed. The intent is to help give you a guide for syntax as you build your application. However, if the linters are too distracting and / or confusing, feel free to ignore them.

JavaScript Linting

This assumes you have eslint and eslint-watch installed. If you don't, run the following:

$ npm i -g eslint eslint-watch

or if you need permissions:

$ sudo npm i -g eslint eslint-watch

To run the linter once:

$ yarn lint:js

To run the watch task:

$ yarn lint:js:watch

Style Linting

I've also added a style linter for Sass / SCSS.

To run the style linter:

$ yarn lint:style

Linting JavaScript & Styles

To run both linters:

$ yarn lint

Testing

An initial test suite has been setup with two tests (one passing and one intentionally failing). We're using Jest Snapshots for our initial test setup, though Enzym and Expect are also available. The basic test setup lives in ./__tests__. The main configuration for Jest lives at the bottom of package.json. I've also added a few handy scripts, which I've listed below. Jest also gives us a test coverage tool for free, so I've added that too. The setup is at the bottom of package.json. Everything is set to 90% coverage, but your welcome to update that to whatever you'd like.

To run the tests once:

$ npm test

To run the watch script (for only relevant test files)

$ npm run test:watch

To run the watch script (for all test files)

$ npm run test:watchAll

To view the coverage report:

$ npm run test:coverage:report

Review

If you'd like to run the linters and tests at once (this is a nice check before pushing to Github or deploys), you can run:

$ npm run review

Build

NOTE: When you run build, Babel will create a build directory. This is what your users will interact with when they use your library. Nothing in lib gets shipped with your published module.

Run once:

$ npm run build

Run the watch script:

$ npm run build:watch

NOTE: the build script runs in the prepublish script just before you publish to npm.

Publishing

If you already have an account with npm, you can simply run:

$ npm login
$ npm publish

If you don't have an account with npm:

NOTE: Your email address is public

$ npm set init.author.name "Your Name"
$ npm set init.author.email "[email protected]"
$ npm set init.author.url "http://yourblog.com"
$ npm adduser
$ npm publish

Contributing

I am thankful for any contributions made by the community. By contributing you agree to abide by the Code of Conduct in the Contributing Guidelines.

License

MIT

component-library-starter's People

Contributors

alanbsmith avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

component-library-starter's Issues

Can't run styleguide

EXPECTED BEHAVIOR

Building the library works fine, but trying to run the styleguide fails

ACTUAL BEHAVIOR

Module parse failed: Unexpected token (13:2)
You may need an appropriate loader to handle this file type.
|
| const modifierConfig = {
|   ...fontColors,
|   ...fontSizes,
|   ...fontStyles,

STEPS TO REPRODUCE

Clone repo
Run yarn or npm install
Run yarn styleguide

SUGGESTED SOLUTION

It seems to be a problem with the spread operator being used here, digging around it may be a problem with react-styleguidist and how they are using webpack/babel, but I'm not certain.

Structure question about BEM

We used BEM a lot and thinking about setting up a component library for react which follows BEM guidelines. No I am confused about your structure. You separate blocks, elements and modifiers in different folders. When the library grows this will become a mess or am I wrong? I thought that each component holds it's own elements and modifiers, because an element and modifiers, in BEM, only belong to one block. And when you do this you can just reuse the one component of that library.

Maybe I am wrong :) Please give me hint

---- after looking into the project
I see in the code that the Button for example uses global modifiers and then you can implement a custom. That's fine, I got it. So global modifiers and custom for a block. But why storing elements in a separate folder ? An Element only belongs to one block, so it should be moved there. If you want to have a global "Icon", p or Text -> than this is actually a block.

Validating modifiers in a parent component throws warnings for customModifiers defined in child components

EXPECTED BEHAVIOR

When defining a custom modifier in a child component, I would not expect to see proptype warnings from it's parent component saying those modifiers are invalid.

ACTUAL BEHAVIOR

These warnings come from parent components that use styleModifierPropTypes, that don't seem to allow for the modifiers of the children.

STEPS TO REPRODUCE

  1. Clone in a fresh component-library-starter project.
  2. Inside elements/Icon/index.js add modifier validation by importing styleModifierPropTypes and adding:
Icon.propTypes = {
  modifiers: styleModifierPropTypes(modifierConfig),
}
  1. Observe in console the following:

Warning: Failed prop type: Invalid modifier left used in prop modifiers[0] and supplied to Styled(UnstyledIcon). Validation failed.
in Styled(UnstyledIcon) (created by PreviewComponent)
in PreviewComponent
in ThemeProvider (created by ThemeWrapper)

This is because Button.Icon defines a customModifier 'left'.

Is there a recommended way to validate modifiers for components that are built off of other components?

Thanks!

configureFonts incorrectly written?

Hello.

I was using this as a part of a project I'm working on, and specifically looking around configureFonts.js
It doesn't seem like you have this file set up correctly. FontFaceObserver is made to take a string for the family, not an array of strings. This as far as I can tell causes all loads to fail when written in this manner.

Shouldn't it be an array of FontFaceObservers that get passed to a mapping function? One observer per font.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.