Below you can find rules that needs to be applied into every component from Storefront UI to ensure that they are highly customizable, follow common format and coding standards.
Common rules for components
- Each component should be divided into granular parts. Each of the parts should be named the same as component and it's folder:
- SfComponent.html -> markup
- sfComponent.scss -> styles for the component
- SfComponent.ts -> component instance
- SfComponent.spec.ts - unit tests for component
- SfComponent.stories.js - storybook stories for component
- SfComponent.vue -> whole component using above partials. It should look like this:
<script src="./SfComponent.js"></script> <template lang="html" src="./SfComponent.html"></template> <style lang="scss" src="./SfComponent.scss"></style>
- use Sf prefix in component names.
- Try to make components as customizable as possible but without complicating them. Think about the parts that are usually customized and allow simple way to change their look.
- We use atomic design concept for component grouping but without templates and pages.
- Try to make BEM elements matching slot names.
- We use slots and props for content composition. Props should be used to fill default slots content, and slots are meant for markup replacement. In other words every default markup should be repalcable with slots. One slot is usually a single BEM element.
- Make slots optional if it's possible.
- Don't use props for setting colors and other properties that can be set by css (except for background images).
- If you are providing some components by default make sure that they are replacable and still customizable
- Provide a default slot (usually empty) just in case someone wants a fully custom component.
Here you can see example of proper component HTML with all rules applied:
- Use REMs
- Always import a global variables at the top.
- Start with mobile view and write mobile-first CSS. It means that media queries should be only for a desktop view.
- Use BEM methodology as naming convention.
- Don't use scoped styles.
- Make use of global css variables wehenever it's possible.
- Add component-specific SCSS variables on top of the component SCSS file. Try to use global SCSS vars inside them for common properties like primary/secondary colors etc. Naming convention for vars is like in BEM:
$component__block--modifier-property- for example
- Properties that may broke the design in future changes shouldn't be customizable via SCSS properties. A safe set of properties to customize are:
background-size, X and Y justification wuth
- Provide CSS modifiers for most common modifications.
- Don't use any outer positioning for components (like outer margins). The way they're positioned in layout should be determined in outer env.
Here you can find an example of properly styled component with all rules applied:
- The minimum set of tests should contain component:
- External API: props, slots, events
- Internal API: methods
Stories for Storybook
Component stories should contain at least those stories:
- one for all props and CSS modifiers fillable as knobs (as text or select)
- one per every slot
Check our storybook for examples.
Please see this stories for reference.