All Projects → rikschennink → Conditioner

rikschennink / Conditioner

Licence: mit
💆🏻 Frizz free, context-aware, JavaScript modules

Programming Languages

javascript
184084 projects - #8 most used programming language
es6
455 projects

Projects that are alternatives of or similar to Conditioner

Blockly Samples
Plugins, codelabs, and examples related to the Blockly library.
Stars: ✭ 222 (-78.92%)
Mutual labels:  webpack, requirejs
TypeScript-AMD-Boilerplate
A TypeScript AMD Grunt Boilerplate with RequireJS
Stars: ✭ 46 (-95.63%)
Mutual labels:  amd, requirejs
Modular Css
A streamlined reinterpretation of CSS Modules via CLI, API, Browserify, Rollup, Webpack, or PostCSS
Stars: ✭ 234 (-77.78%)
Mutual labels:  webpack, browserify
Webpack Require From
Webpack plugin that allows to configure path or URL for fetching dynamic imports
Stars: ✭ 142 (-86.51%)
Mutual labels:  webpack, import
Hugo Theme Hello Friend
Pretty basic theme for Hugo that covers all of the essentials. All you have to do is start typing!
Stars: ✭ 586 (-44.35%)
Mutual labels:  webpack, responsive
React Echarts Modules
这个例子给你提供在react中使用echarts的最优方案
Stars: ✭ 185 (-82.43%)
Mutual labels:  webpack, import
Webpack
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
Stars: ✭ 60,034 (+5601.23%)
Mutual labels:  webpack, amd
Requirejs Demo
《RequreJS学习笔记》
Stars: ✭ 164 (-84.43%)
Mutual labels:  requirejs, amd
Ngx Admin
Customizable admin dashboard template based on Angular 10+
Stars: ✭ 23,286 (+2111.4%)
Mutual labels:  webpack, responsive
vscode-requirejs
Provides goto definition functionality for require js modules.
Stars: ✭ 20 (-98.1%)
Mutual labels:  amd, requirejs
Babel Plugin Prismjs
A babel plugin to use PrismJS with standard bundlers.
Stars: ✭ 114 (-89.17%)
Mutual labels:  webpack, browserify
Xhr
A small xhr wrapper
Stars: ✭ 801 (-23.93%)
Mutual labels:  webpack, browserify
Npm Pipeline Rails
Use npm as part of your Rails asset pipeline
Stars: ✭ 93 (-91.17%)
Mutual labels:  webpack, browserify
Minipack
📦 A simplified example of a modern module bundler written in JavaScript
Stars: ✭ 2,625 (+149.29%)
Mutual labels:  webpack, browserify
Template.js
A javascript template engine, simple, easy & extras, support webpack, rollup, parcel, browserify, fis and gulp
Stars: ✭ 1,201 (+14.06%)
Mutual labels:  webpack, browserify
Routr
A component that provides router related functionalities for both client and server.
Stars: ✭ 241 (-77.11%)
Mutual labels:  webpack, browserify
Angular Async Loader
Load modules and components asynchronously for angular 1.x application.
Stars: ✭ 137 (-86.99%)
Mutual labels:  requirejs, amd
Require Vuejs
RequireJS plugin to async and dynamic load and parse .vue components
Stars: ✭ 143 (-86.42%)
Mutual labels:  requirejs, amd
array-sort-by
Powerful mechanism to sort arrays or array of objects by one or more properties. You can also specify a custom comparer function.
Stars: ✭ 37 (-96.49%)
Mutual labels:  amd, requirejs
Public
Repository for wallaby.js questions and issues
Stars: ✭ 662 (-37.13%)
Mutual labels:  webpack, browserify

Conditioner

Conditioner provides a straight forward Progressive Enhancement based solution for linking JavaScript modules to DOM elements. Modules can be linked based on contextual parameters like viewport size and element visibilty making Conditioner your perfect Responsive Design companion.

License: MIT npm version

Example

Mount a component (like a Date Picker, Section Toggler or Carrousel), but only do it on wide viewports and when the user has seen it.

<h2 data-module="/ui/component.js"
    data-context="@media (min-width:30em) and was @visible"> ... </h2>

If the viewport is resized or rotated and suddenly it's smaller than 30em Conditioner will automatically unmount the component.

Demo

Features

  • Progressive Enhancement as a starting point 💆🏻
  • Perfect for a Responsive Design strategy
  • Declarative way to bind logic to elements, why this is good
  • No dependencies and small footprint (~1KB gzipped)
  • Compatible with ES import(), AMD require() and webpack
  • Can easily be extended with plugins

Resources

Installation

Install with npm:

npm install conditioner-core --save

The package includes both development and product versions. Use conditioner-core.min.js for production. The ES Module version of Conditioner (conditioner-core.esm.js) is not compressed.

Using a CDN:

<script src="https://unpkg.com/conditioner-core/conditioner-core.js"></script>

Setup

Using Conditioner on the global scope:

<script src="https://unpkg.com/conditioner-core/conditioner-core.js"></script>
<script>

// mount modules!
conditioner.hydrate( document.documentElement );

</script>

Using Conditioner async with ES modules:

import('conditioner-core/conditioner-core.esm.js').then(conditioner => {
  conditioner.addPlugin({
    // converts module aliases to paths
    moduleSetName: name => `/ui/${name}.js`,

    // get the module constructor
    moduleGetConstructor: module => module.default,

    // fetch module with dynamic import
    moduleImport: name => import(name)
  });

  // mount modules!
  conditioner.hydrate(document.documentElement);
});

Using Conditioner with webpack:

import * as conditioner from 'conditioner-core/conditioner-core.esm';

conditioner.addPlugin({
  // converts module aliases to paths
  moduleSetName: name => `./ui/${name}.js`,

  // get the module constructor
  moduleGetConstructor: module => module.default,

  // override the import
  moduleImport: name => import(`${name}`)
});

// lets go!
conditioner.hydrate(document.documentElement);

Using Conditioner in AMD modules:

require(['conditioner-core.js'], function(conditioner) {
  // setup AMD require
  conditioner.addPlugin({
    // converts module aliases to paths
    moduleSetName: function(name) {
      return '/ui/' + name + '.js';
    },

    // setup AMD require
    moduleImport: function(name) {
      return new Promise(function(resolve) {
        require([name], function(module) {
          resolve(module);
        });
      });
    }
  });

  // mount modules!
  conditioner.hydrate(document.documentElement);
});

A collection of boilerplates to get you started with various project setups:

API

Public Methods

Inspired by React and Babel, Conditioner has a tiny but extensible API.

Method Description
hydrate(element) Mount modules found in the subtree of the passed element, returns an array of bound module objects.
monitor(context[, element]) Manually monitor a context. Returns a context monitor object.
addPlugin(plugin) Add a plugin to Conditioner to extend its core functionality.

Bound Module

Bound modules are returned by the hydrate method. Each bound module object wraps a module. It exposes a set of properties, methods and callbacks to interact with the module and its element.

Property / Method Description
alias Name found in dataset.module.
name Module path after name has been passed through moduleSetName.
element The element the module is bound to.
mounted Boolean indicating wether the module is currently mounted.
mount() Manually mount the module.
unmount() Manually unmount the module.
destroy() Unmounts the module and then removes any monitors.
onmount(boundModule) Callback that runs when the module has been mounted. Scoped to element.
onmounterror(error, boundModule) Callback that runs when an error occurs during the mount process. Scoped to element.
onunmount(boundModule) Callback that runs when the module has been unmounted. Scoped to element.
ondestroy(boundModule) Callback that runs when the module has been destroyed. Scoped to element.

Context Monitor

Context Monitors are returned by the monitor method.

Property / Method Description
matches Boolean indicating wether the context is currently matches
active Boolean indicating wether it's actively monitoring the context
start() Start monitoring the context
stop() Stop monitoring the context
destroy() Stops monitoring the context and cleans up the monitor array
onchange(matches) Callback that runs when one of the monitors in the context query reported a change

An example setup is shown below.

const viewportMonitor = conditioner.monitor('@media (min-width:30em) and was @visible');

viewportMonitor.onchange = matches => {
  console.log('context is matched', matches);
};

viewportMonitor.start();

Plugins

Adding a plugin can be done with the addPlugin method, the method expects a plugin definition object.

Plugins can be used to override internal methods or add custom monitors to Conditioner.

We can link our plugins to the following hooks:

Hook Description
moduleSelector(context) Selects all elements with modules within the given context and returns a NodeList.
moduleGetContext(element) Returns context requirements for the module. By default returns the element.dataset.context attribute.
moduleImport(name) Imports the module with the given name, should return a Promise. By default searches global scope for module name.
moduleGetConstructor(module) Gets the constructor method from the module object, by default expects module parameter itself to be a factory function.
moduleGetDestructor(moduleExports) Gets the destructor method from the module constructor return value, by default expects a single function.
moduleSetConstructorArguments(name, element) Use to alter the arguments supplied to the module constructor, expects an array as return value.
moduleGetName(element) Called to get the name of the module, by default retrieves the element.dataset.module value.
moduleSetName(name) Called when the module name has been retrieved just before setting it.
moduleWillMount(boundModule) Called before the module is mounted.
moduleDidMount(boundModule) Called after the module is mounted.
moduleWillUnmount(boundModule) Called before the module is unmounted.
moduleDidUnmount(boundModule) Called after the module is unmounted.
moduleWillDestroy(boundModule) Called before the module is destroyed.
moduleDidDestroy(boundModule) Called after the module is destroyed.
moduleDidCatch(error, boundModule) Called when module import throws an error.
monitor A collection of registered monitors. See monitor setup instructions below.

File Extension Plugin Example

Instead of referencing each module with file extension (datepicker.js) we want to leave out the extension and add it automatically.

We'll use the moduleSetName hook to achieve this:

conditioner.addPlugin({
  moduleSetName: name => `${name}.js`
});

Element Visibility Monitor

Let's add a visible monitor using the IntersectionObserver API. All our custom monitors can be used in context queries by prefixing the name with an @.

Monitor plugins should mimic the MediaQueryList API. Each monitor should at least expose a matches property and an addListener method. To allow for the monitor to be unloaded we should also add the removeListener method, but it's optional.

conditioner.addPlugin({
  // the plugin "monitor" hook
  monitor: {
    // the name of our monitor, not prefixed with "@"
    name: 'visible',

    // the monitor factory method, this will create our monitor
    create: (context, element) => ({
      // current match state
      matches: false,

      // called by conditioner to start listening for changes
      addListener(change) {
        new IntersectionObserver(entries => {
          // update the matches state
          this.matches = entries.pop().isIntersecting == context;

          // inform conditioner of the new state
          change();
        }).observe(element);
      }
    })
  }
});

With our visible monitor registered, we are ready to use it in a context query.

<div data-module="/ui/component.js" data-context="@visible true"></div>

To make context queries easier to read Conditioner will automatically set the context value to true if it's omitted. So the following context query is exactle the same as @visible true.

<div data-module="/ui/component.js" data-context="@visible"></div>

To invert the monitor state we can use the not operator. Instead of writing @visible false we can now write not @visible, which again makes queries easier to read.

The @visible state context monitor will unload modules when they are no longer visible. This might be exactly what we want, for instance when animating element in and out of view. It's however more likely we want the modules to stick around after they've been loaded for the first time.

You can achieve this by adding the was statement.

<div data-module="/ui/component.js" data-context="was @visible"></div>

Now the module will stay mounted after its context has been matched for the first time.

Let's string multiple monitors together with the and operator so we can do more precise context queries.

<div data-module="/ui/component.js" data-context="@media (min-width:30em) and was @visible"></div>

Last but not least we can use the or to make exceptions. On small viewports we only mount the module when the element was visible, on big viewports we always mount the module.

<div data-module="/ui/component.js" data-context="@media (max-width:30em) and was @visible or @media(min-width:30em)"></div>

Polyfilling

To use Conditioner on older browsers you'll have to polyfill some modern JavaScript features. You could also opt to use a Mustard Cut and prevent your JavaScript from running on older browsers by wrapping the hydrate method in a feature detection statement.

// this will only run on IE10 and up
// https://caniuse.com/#feat=pagevisibility
if ('visibilityState' in document) {
  conditioner.hydrate();
}

Polyfills required for Internet Explorer 11

Internet Explorer 10

You can either polyfill dataset or add the following plugin to override the moduleGetName and moduleGetContext hooks (which use dataset).

conditioner.addPlugin({
  moduleGetName: function(element) {
    return element.getAttribute('data-module');
  },
  moduleGetContext: function(element) {
    return element.getAttribute('data-context');
  }
});

The above plugin will also be required when you need to mount modules on SVG elements. Browser support for use of dataset on SVG elements is a lot worse than HTML elements.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].