• Stars
    star
    332
  • Rank 126,957 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 12 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Set of components for sites development

BEM Components Library

bem-components is an open-source library that provides a set of ready-made visual components (blocks) for creating web interfaces.

The library provides design themes. This version introduces the islands theme, which implements the new Yandex design. Features include supporting multiple themes at once, and creating new themes.

GitHub Release Build Status Coverage Status devDependency Status

Contents

Additional information

Levels

  • common.blocks β€” support of any devices and browsers.
  • desktop.blocks β€” support of desktop browsers.
  • touch.blocks β€” implementation of specific features for touch platforms.
  • touch-phone.blocks β€” implementation of specific features for smartphones.
  • touch-pad.blocks β€” implementation of specific features for tablets.
  • design/<common|desktop|touch|touch-phone|touch-pad>.blocks β€” implementation of various designs (themes).

Blocks

Supported browsers

  • Desktop

    • Firefox (the last two stable versions)
    • Chrome (the last two stable versions)
    • Safari (the last two stable versions)
    • Yandex (the last two stable versions)
    • Opera 12.6+
    • Internet Explorer 9+
    • Partial support for Internet Explorer 8
  • Touch

    • Android 4+
    • iOS 5+
    • Internet Explorer 10+

Support for Internet Explorer 8

To support Internet Explorer 8, you must add:

  • es5-shim;
  • style files with the *.ie.styl extension to the page. To do this, specify them in the build config (see this example) and add comments in the page section (see this example). You can also enable styles for Internet Explorer 8 at the template level.

Technologies

Tools

Assemblers

Optimizers

Code analysis

Other

  • Autoprefixer β€” used during assembly to generate vendor prefixes for supported browsers based on the configuration.

Usage

There are several ways to start using bem-components. The best approach depends on your project’s requirements and its compatibility with the technologies and tools of the library, as well as on your experience with BEM projects.

Choose the most suitable method for your project:

Dist Source Compiled
Pre-assembled CSS and JavaScript code and library templates. Integrated by linking to the page. Doesn't require assembly or compatibility with your project. The full source code of the library. Requires assembly. Your project must be fully compatible with the library’s technologies and tools. The full source code of the library. Requires assembly. Differs from the Source method in that PostCSS is compiled into CSS. Suitable for projects where PostCSS is not used.

The way you are using the library determines how to integrate it into your project:

Integrating the library source code (Source and Compiled)

We recommend using ENB or bem-tools to integrate the library.

As an example, you can use project-stub, where the library is enabled by default. You can also create a project and use the Yo generator to connect the library (this allows you to create the necessary project configuration).

Integrating the pre-assembled library files (Dist)

The easiest way to connect the library to the project is to download the pre-assembled library files and use <link> and <script> elements to add them to HTML pages. There are several ways to do this:

For information about how to use the connected library files, see Working with the library.

Structure of the pre-assembled library

There are separate sets of files available for three platforms:

  • desktop
  • touch-pad
  • touch-phone

Each set includes:

  • bem-components.css β€” Styles;
  • bem-components.ie.css β€” Styles for IE8 (more information);
  • bem-components.js β€” JavaScript;
  • bem-components.bemhtml.js β€” BEMHTML templates;
  • bem-components.bh.js β€” BH templates;
  • bem-components.js+bemhtml.js β€” Combines JavaScript code and BEMHTML templates for using templates in the browser;
  • bem-components.js+bh.js β€” Combines JavaScript code and BH templates for using templates in the browser;
  • bem-components.no-autoinit.js - JavaScript without automatic initialization. Use it if you need to write your own code in i-bem.js;
  • bem-components.no-autoinit.js+bemhtml.js;
  • bem-components.no-autoinit.js+bh.js.

Each set also includes the development versions of the same files (preserving the original formatting and comments).

Connecting files from a CDN

The fastest and easiest way to connect the library to the project is to add the <link> and <script> elements to HTML pages:

<link rel="stylesheet" href="https://yastatic.net/bem-components/latest/desktop/bem-components.css">
<script src="https://yastatic.net/bem-components/latest/desktop/bem-components.js+bemhtml.js"></script>

Pattern for connecting a file from the CDN: //yastatic.net/library-name/version/platform/file-name.

Example: //yastatic.net/bem-components/latest/desktop/bem-components.dev.js+bemhtml.js.

Loading gzipped

Select the appropriate library version and download the archive. Unzip it. Add the files to the page using <link> and <script> elements:

<link rel="stylesheet" href="desktop/bem-components.css">
<script src="desktop/bem-components.js+bemhtml.js"></script>

Assembling files from the source code

The library code is on Github: https://github.com/bem/bem-components.

To run the build, use these commands:

# Clone the library source code
git clone https://github.com/bem/bem-components.git
# Switch to the library folder
cd bem-components
# Install the required dependencies
npm install
# Build Dist
npm run dist

As a result of the build, the files will be available in the bem-components-dist folder. Connect the files in the page's HTML:

<link rel="stylesheet" href="bower_components/bem-components-dist/desktop/bem-components.css">
<script src="bower_components/bem-components-dist/desktop/bem-components.js+bemhtml.js"></script>

Installation using Bower

Provided Bower is already in your project, run the following command:

bower i bem/bem-components-dist

As a result of the build, the files will be available in the bem-components-dist folder. Connect the files the same way as for the previous method:

<link rel="stylesheet" href="bower_components/bem-components-dist/desktop/bem-components.css">
<script src="bower_components/bem-components-dist/desktop/bem-components.js+bemhtml.js"></script>

Working with the library as Dist

There are two steps to working with the library:

  1. Find the right block on the library website: bem.info. For example, you can use select.
  2. Get the necessary HTML.

The first step is always the same. The second step can be performed in three different ways, which you can combine as you wish:

Method 1. Copy the HTML from the example. To do this, go to the HTML tab in the header of the example.

This is the easiest approach, but if templates are updated in future versions of the library, you will have to make the changes manually in each updated block.

Method 2. Use templating in the browser. Assembling the Dist library includes pre-assembled BEMHTML and BH templates to choose from.

To do this, copy the sample BEMJSON code from the documentation and paste it into the page's HTML code. Use the BEMJSON tab in the header of the example.

The page's HTML code will look like this:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>bem-components as a library</title>
    <link rel="stylesheet" href="https://yastatic.net/bem-components/latest/desktop/bem-components.css">
</head>
<body class="page page_theme_islands">
    <form class="form" action="/"></form>
<script src="https://yastatic.net/bem-components/latest/desktop/bem-components.no-autoinit.js+bemhtml.js"></script>
<script>
modules.require(['i-bem__dom', 'BEMHTML', 'jquery', 'i-bem__dom_init'], function(BEMDOM, BEMHTML, $, init) {
    var html = BEMHTML.apply({
        block : 'select',
        mods : { mode : 'check', theme : 'islands', size : 'm' },
        name : 'select1',
        val : [2, 3],
        text : 'Conference programme',
        options : [
            { val : 1, text : 'Report' },
            { val : 2, text : 'Master class' },
            { val : 3, text : 'Round Table' }
        ]
    });

    BEMDOM.append($('.form'), html);

    // NOTE: explicitly call `init();` if you need to initialize blocks from HTML markup
});
</script>
</body>
</html>

In contrast to the first method, this code won't require any changes to the markup when the library is updated to a new version. However, client-generated markup might not be indexed as well by search engines.

Method 3. Execute {BEMHTML,BH}.apply() in Node.js and give the browser the prepared HTML:

var BEMHTML = require('./dist/desktop/bem-components.bemhtml.js').BEMHTML;

BEMHTML.apply({
    block : 'select',
    mods : { mode : 'check', theme : 'islands', size : 'm' },
    name : 'select1',
    val : [2, 3],
    text : 'Conference programme',
    options : [
        { val : 1, text : 'Report' },
        { val : 2, text : 'Master class' },
        { val : 3, text : 'Round Table' }
    ]
}); // returns HTML line

Library concepts

The block and its states

The library consists of blocks, which you can see represented visually in the bem-components showcase. Blocks have states that determine the behavioral model. A block’s state is expressed through modifiers and specialized fields. Changing a modifier creates an event that can be used for working with the block.

There is no need to create a special BEM event if you can work with the modifier change event. Depending on what is used for changing a component’s state (a modifier or a field), different events are used:

  • To respond to changing the "state" of value fields, the change event is used.
  • To respond to setting or removing a modifier, various modifier change events are used.

Controls in bem-components

The controls in bem-components can be used as the basis for creating other library components. Such controls don't have models as in HTML, and they can be used for tasks that don't require the semantics of a specific HTML model.

As an example, think of the "behavioral model" for the HTMLInputElement, which is a higher-level interface designed specifically for editing data. What is different about bem-components blocks is that they can be used as the basis of a block in another library that will solve the same problem. But along with that, they can serve other purposes in the interface that don't require HTML input semantics.

Implementation details

focused modifier

Controls in bem-components have two types of focus, which are set using the modifiers focused and focused-hard. The type of focus determines the appearance of the control.

The modifier is chosen automatically based on how the focus is set:

  • focused β€” Set for a mouse click on the control.
  • focused-hard β€” Set when the control is selected using the keyboard or through JavaScript. It highlights the component more obviously when it gets the focus. For example, in the Islands theme, most of the controls get an extra border when focused-hard is set.

Development

Working copy

Get sources:

$ git clone git://github.com/bem/bem-components.git
$ cd bem-components

Install dependencies (of tools):

$ npm install

To then run locally installed tools, use export PATH=./node_modules/.bin:$PATH or any alternative method.

Install dependent libraries:

$ bower install

Build examples and tests:

$ npm run build-all

Start the development server:

$ npm start
$ open http://localhost:8080/

Note: For information about assembling individual blocks, see the section Tests for templates.

Code analysis:

$ npm run lint

Testing

JavaScript unit tests

The npm run test-specs command launches unit tests on JS.

To launch point-based assembly, use the command enb make specs desktop.specs/<block-name> (for example, enb make specs desktop.specs/input).

Tests will be run automatically on Travis for each pull request.

Regression tests for layout

Hermione is used for layout testing.

Tests for each block are stored in a separate block-name.hermione.js file in the hermione/ directory. Locally, test are executed manually. On Travis, tests are executed automatically. For Selenium Grid, the SauceLabs service is used.

For executing tests locally, you need to:

  1. Create an OpenSauce account in SauceLabs.
  2. Install the Sauce Connect utility.
  3. Set up environment variables (SAUCE_USERNAME and SAUCE_ACCESS_KEY).
  4. Launch the sc utility (SauceConnect) and wait for the tunnel to be opened.
  5. Run the tests using hermione.
  6. If you need to make new versions of screenshots, use the command hermione gui.

When developing new tests to speed up local execution:

  1. Install and run Selenium Server or PhantomJS.
  2. In the .hermione.conf.js file, change the gridUrl option to http://localhost:4444/.
  3. Run the tests.

For more information about using Hermione with various backends, read the article Hermione quick start.

Note: You need to save screenshots from SauceLabs in the repository. This helps to avoid discrepancies when rendering fonts.

Before commiting new or modified reference images, you must:

  1. Make sure the images are correct. To find differences between the old and new versions, use the Araxis Merge utility or something similar.
  2. Use ImageOptim to compress the images (this is the most effective tool for compressing images as of May, 2014).

Tests for templates

Build tools for the library allow you to build and run tests on BEMHTML and BH block templates.

Add a test for a block

  • Create a directory named [block name].tmpl-specs in the block's directory at the necessary level. Test files will be stored in this directory.
  • Create a pair of BEMJSON and HTML files for each test. The BEMJSON file contains an example for the block, and the HTML file contains the sample HTML code that should result after applying the block template to the BEMJSON example. File names must match (other than the extensions) for the same test. For example, 10-default.bemjson.js and 10-default.html.

Multiple tests can be written for a block and, accordingly, each test consists of two files (BEMJSON and HTML) with the same name.

desktop.blocks
    └── myblocks
        β”œβ”€β”€ myblock.bemhtml.js
        β”œβ”€β”€ myblock.bh.js
        β”œβ”€β”€ ...
        └── myblock.tmpl-specs
            β”œβ”€β”€ 10-default.bemjson.js
            β”œβ”€β”€ 10-default.html
            β”œβ”€β”€ 20-advanced.bemjson.js
            └── 20-advanced.html

To build and run tests, use:

magic run tmpl-specs

To build tests on the required definition level, use:

magic make desktop.tmpl-specs

To build tests only for a specific block, use:

magic make desktop.tmpl-specs/button

If the build procedure is successful, tests are run automatically, and you will see all test results. If the result of applying the template does not match with the block sample in HTML, you will see an error in the log indicating how it differs from the block sample.

All tests are run automatically using Travis on each pull request.

Maintainers

Workflow

Current tasks are listed on a special Agile Board.

Task statuses:

  • backlog β€” Unsorted tasks that need to be discussed by the team to evaluate them and decide how to implement them. This status also applies to tasks that need additional information.
  • ready β€” Tasks that have been investigated and a decision has been made on how to implement them.
  • in progress β€” Tasks that have a specific assignee and are currently in progress.
  • done β€” Tasks that have been closed over the last seven days (this is a temporary technical limitation of our Agile Board).

License

Code and documentation Β© 2012 YANDEX LLC. Code released under the Mozilla Public License 2.0.

More Repositories

1

bem-react

A set of tools for developing user interfaces using the BEM methodology in React
TypeScript
440
star
2

yandex-ui

Yandex UI Kit build on React and bem-react
TypeScript
352
star
3

project-stub

deps
JavaScript
313
star
4

bem-core

BEM Core Library
JavaScript
275
star
5

html-differ

Π‘ompares two HTML
JavaScript
211
star
6

bem-bl

Base BEM library
JavaScript
197
star
7

bem-xjst

bem-xjst (eXtensible JavaScript Templates): declarative template engine for the browser and server
JavaScript
115
star
8

bem-sdk

BEM SDK packages
JavaScript
88
star
9

next-global-css

A preset for nextjs allowing using 3d party libraries with global css
JavaScript
86
star
10

themekit

Build system of design-tokens for any platforms
TypeScript
73
star
11

bh

BH template engine
JavaScript
68
star
12

bem-express

BEM project-stub with BEMTREE and express
JavaScript
43
star
13

bh-php

PHP port of https://github.com/bem/bh. It's cool thing but better use this:
PHP
33
star
14

bem-react-boilerplate

DEPRECATED! A bare minimum frontend boilerplate based on create-react-app and bem-react-core.
TypeScript
31
star
15

sssr

Master class for BEMup
JavaScript
30
star
16

bem-react-components

Components library for develop with React and BEM methodology
JavaScript
29
star
17

bem-mvc

Yet another MVC for i-bem
JavaScript
29
star
18

web-platform

React SDK for building modern, accessible and cross-platforms interfaces
TypeScript
28
star
19

gulp-bem

Usefull gulp modules and plugins to work with BEM methodology
JavaScript
25
star
20

webpack-bem-loader

Webpack BEM loader
JavaScript
24
star
21

bem-history

BEM wrap for History API
JavaScript
22
star
22

bem-identity

BEM brand identity
20
star
23

bem-calendar

calendar based on bem-components
JavaScript
16
star
24

storybook-to-figma

The tool for importing Storybook components to Figma
TypeScript
15
star
25

yandex-ui-icons

TypeScript
13
star
26

yandex-ui-themer

A simple way to create your theme for @yandex/ui with Themekit
TypeScript
12
star
27

babel-plugin-bem-import

BEM import resolver
JavaScript
9
star
28

eslint-plugin-bem-xjst

ESLint environments for bem-xjst
JavaScript
9
star
29

bem-components-php

Set of bh.php templates for bem-components library
PHP
9
star
30

figma-extractor

TypeScript
8
star
31

bem-components-dist

Prebuilt version of bem-components library
JavaScript
8
star
32

jscs-bem

jscs bem preset and some tasty rules
JavaScript
6
star
33

bem-forum

BEM forum on top of github issues
JavaScript
6
star
34

bem-core-dist

Prebuilt version of bem-core library
JavaScript
6
star
35

bem-core-php

bh.php templates for bem-core
PHP
5
star
36

babel-plugin-bem

Babel plugin BEM
JavaScript
5
star
37

ts-docgen-next

Webpack loader for generating documentation from typescript files.
TypeScript
4
star
38

jsd

JSDoc parser
JavaScript
4
star
39

cra-template-yandex-ui

TypeScript
2
star
40

bem-jsdoc3-plugin

Plugin for http://usejsdoc.org/ for documenting bem enteties
JavaScript
2
star
41

es5-shims

es5 shims moved from bem-core
JavaScript
2
star
42

webpack-bem-plugin

JavaScript
1
star
43

design-system

TypeScript
1
star
44

webpack-bem-i18n-loader

JavaScript
1
star
45

bem-jsd

Wrapper for use JSD with BEM plugins
JavaScript
1
star