Git Product home page Git Product logo

bem's Introduction

jQuery.BEM

jQuery.BEM is small jQuery plugin for comfortable working with HTML made by BEM method.

Selecting elements

HTML:

<div class="b-product">
	<div class="b-product__name">Coffe</div>
	<div class="b-product__price">$2</div>
</div>
<div class="b-product">
	<div class="b-product__name">Tea</div>
	<div class="b-product__price">$1</div>
</div>

JS:

$('.b-product:first').elem('name');              // $('.b-product:first > .b-product__name')
$('.b-product:first').elem('name').up();         // $('.b-product:first > .b-product__name').closest('.b-product')
$('.b-product:first').elem('name').up('price');  // $('.b-product:first > .b-product__name').siblings('.b-product__price')

Working with modifiers

Setting modifier

HTML:

<div class="b-product">...</div>

JS:

$('.b-product').setMod('theme', 'premium');  // will get .b-product.b-product_theme_premium
$('.b-product').setMod('premium');           // will get .b-product.b-product_premium_yes

Remove modifier

HTML:

<div class="b-product b-product_theme_premium">...</div>

JS:

$('.b-product').delMod('theme', 'premium');  // $('.b-product').removeClass('.b-product_theme_premium');
$('.b-product').delMod('theme');             // remove the modifier "theme" of any value (.b-product_theme_*)

Getting modifier

HTML:

<div class="b-product b-product_theme_premium">...</div>

JS:

$('.b-product').getMod('theme')      // will return "premium"
$('.b-product').getMod('discount');  // will rerurn null (non-existent modifier)

Checking modifier

HTML:

<div class="b-product b-product_theme_premium">...</div>

JS:

$('.b-product').hasMod('theme');             // true
$('.b-product').hasMod('theme', 'premium');  // true
$('.b-product').hasMod('theme', 'discount')  // false

Filtering by modifier

HTML:

<div class="b-product b-product_theme_premium">...</div>
<div class="b-product">...</div>

JS:

$('.b-product').byMod('theme', 'premium');     // instead of $('.b-product.b-product_theme_premium')
$('.b-product').byNotMod('theme', 'premium');  // instead of $('.b-product').not('.b-product_theme_premium')
$('.b-product').byMod('theme');                // filtering by modifier "theme" of any value (.b-product_theme_*)

Declarations

bem.decl('b-product', {
		onInit: function($this) {
				// Add to element "price" modifier "size" with a value of "large" when DOM ready.
				$this.elem('price').setMod('size', 'large');
		},
		title: {
				onMouseover: function($this) {
						// Add to root block "b-product" modifier "state" with a value "hover"
						$this.up().setMod('state', 'hover');

						// Call the helper function.
						this._customFunction();
				},
				_customFunction: function() {
						console.log('I am helper function');
				}
		},
		price: {
				onSetmod: function($this, e, modKey, modVal) {
						// Print to console information about the installed modifier of element "price".
						console.log('Modifier set:', modKey, modVal);
				},
				onDelmod($this, e, modKey, modVal) {
						//
				}
		}
});

Override syntax

If you are not satisfied with the standard BEM syntax (for example prefixes), use

bem.setConfig({
	namePrefix: '',     // Names prefix (RegExp)
	elemPrefix: '-',    // Element prefix
	modPrefix: '_',     // Modifier prefix
	modDlmtr: '-'       // Delimiter for the modifier key and value
});

Now the classes must be named on another:

.block
.block-element
.block-element_modifier-value

bem's People

Contributors

maxpoletaev avatar

Watchers

 avatar  avatar

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.