npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zero-bem

v0.2.5

Published

Minimal ES6 bem library implementation

Downloads

18

Readme

npm version

zero-bem

Minimal ES6 bem-stack implementation library.

Based, inspired and partly inherits the code from famous client-side framework i-bem but it can work without y-modules, borschik, enb and other endemic Yandex' tools.

Installation

npm i -S zero-bem

Using

Using in runtime

Basic usage case (rehydratiing dom, adding new nodes & entities):


  // const $ = require('jquery');

  // document.body.innerHTML = '<div class="Wrapper"><div class="App bem-js" data-bem=\'{"Test":{"test":true}}\'>App content</div></div>';

  const { BEMDOM, BEMHTML } = require('zero-bem');

  // Init frozen DOM (came from app template)
  BEMDOM.hydrate();

  // Find target block
  // const appBlock = BEMDOM.findEntities({ domElem: $('body'), block: 'App' });
  const appBlock = $('.App').bem('App');

  // Find parent container
  const wrapperBlock = appBlock.findParentBlock('Wrapper');

  // Try to dynamically create blocks from template
  const template = {
    block: 'Demo',
    mix: {
      block: 'Mixed',
      mods: { test: 'val' },
      js: { param: 1 },
    },
    mods: { test: true },
    content: [
      { elem: 'Button', modName: 'id', modVal: 'action' },
    ],
  };

  // Create html from template
  const html = BEMHTML.apply(template);

  // Add dom node
  const dom = BEMDOM.prepend(wrapperBlock, html);

  // Get bem entity
  const demoBlock = dom.bem('Demo');

  // const mixedBlock = wrapperBlock.findChildBlock('Mixed');
  const mixedBlock = demoBlock.findMixedBlock({ block: 'Mixed', modName: 'test', modVal: 'val' });
  console.log('Mixed block params:', mixedBlock.params);

  // Find child
  const buttonElem = demoBlock.findChildElem('Button');
  console.log('Button element id:', buttonElem.getMod('id'));

  mixedBlock.setMod('alreadyFound');

  // Remove created node & all linked & nested bem entities (Demo itself, all mixes, siblings and children)
  BEMDOM.remove(demoBlock);

Using as modules

Suppose you have component Demo. Then you can create some 'technologies' modules for it in (for example) folder src/components/Demo/:

JS runtime code (main module, which includes any other components' technology modules, as shown below) -- Demo.js:

const { BEMDOM } = require('zero-bem');

require('./Demo.bemhtml'); // Include bemhtml templates
require('./Demo.pcss'); // Include styles

const Demo_proto = /** @lends Demo.prototype */ {

  /** Initialize the component event handler
   */
  onInit: function() {

    this.__base();

    console.log('Demo:onInit: params', this.params); // eslint-disable-line no-console

    // Emit demo event
    setTimeout(() => {
      this.emit('demoEvent', { ok: true });
    }, 1000);

  },

};

export default BEMDOM.declBlock('Demo', Demo_proto);

BEMHTML templates -- Demo.bemhtml:

block('Demo')(

  // Adding modifiers
  addMods()(function(){
    return this.extend({
      // Test passing modifiers
      bemhtml: 'ok',
    }, applyNext());
  }),

  // Live entity
  js()(true)

);

Some styles if you need/want -- Demo.pcss:

.Demo {
  padding: $(itemPadding)px;
  @mixin debug gray, 10%;
}

Hereafter you can use your component in other module.

You must use import component to let the webpack to know that it must to include it in the application build.

For example application main component App.js uses our Demo:

import('components/Demo');

const App_proto = /** @lends App.prototype */ {

  /** Initialize the app event handler
   */
  onInit: function() {

    // Find demo block
    const demoBlock = this.findChildBlock('Demo');

    // Do something with Demo: subscribe to events, change state, get properties, call it methods...

  },

};

export default BEMDOM.declBlock('App', App_proto);

NOTE: Other documentation is in progress...

Webpack configuration

You need to add webpack-zero-bemhtml-loader as .bemhtml files loader.

(ZEROBEM_PATH hereafter means the location of the zero-bem library, for example ./node_modules/zero-bem/ or <rootDir>/node_modules/zero-bem/ etc...)

webpack.config.js sample fragment:

const nanoBemHtmlLoaderPath = path.join(ZEROBEM_PATH, 'bemhtml-loader/webpack-zero-bemhtml-loader'); // Or use `require.resolve`
// ...
module.exports = (env, argv) => {
  return {
    // ...
    module: { rules: [
      // ...
      { // bemhtml begin
        test: /\.(bemhtml)?$/,
        exclude: /node_modules/,
        use: [
          // ...
          {
            // Specify loader path
            loader: path.resolve(nanoBemHtmlLoaderPath),
            // Loader options
            options: {
              // Allow sourcemaps (not supported now)
              sourceMap: sourceMaps,
              // Add required modules to resulted code
              requires: {
                // For example< we can use common configuration
                config: 'src/config/config',
                // ...
              },
            },
          },
          // ...
        ],
      }, // bemhtmlk end
      // ...
    ]}, // module.rules end
    // ...
  }; // return end
}; // module.exports end

Jest configuration

You need to add jest-transform-zero-bemhtml as jest transform rule.

jest.config.js sample fragment:

module.exports = {
  // ...
  transform: {
    // ...
    '.+\\.(bemhtml)$': path.join(ZEROBEM_PATH, 'bemhtml-loader/jest-transform-zero-bemhtml.js'),
    // ...
  },
  // ...
};

Webpack example

Webpack minimal working example configuration included in packag in folder webpack-example.

You can run npm scripts webpack-example-server or webpack-example-build (or webpack-example-build-prod if you wish to play around production build) for testing/experiments.

Original Yandex bem libs:

See also: