cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <button class="incButton">Increment</button>
<button class="decButton">Decrement</button>
<button class="decaIncButton">Increment by 10 in 3 seconds</button>

<my-counter></my-counter>
            
          
!
            
              class ZeroFux {
  constructor(element) {
    if (element) {
      this.dispatcher = element;
    } else {
      this.dispatcher = document.querySelector('body');
    }
  }

  // The dipatch method takes an action argument
  // of the previously defined action type.
  dispatch(action) {
	  this.dispatcher.dispatchEvent(
		  new CustomEvent(action.type, {
		    detail: action,
		    // In case you set a custom dispatcher element
		    // and want them to bubble.
		    bubbles: true,
		    // In case your custom dispatcher is in the
		    // Shadow DOM and you want them to bubble between
		    // the borders or Shadow DOM and regular DOM.
		    compose: true,
		  })
		)
  }

  // This method takes an array of action types
  // that can influence a component's state,
  // an object with reducers with the same names
	// as the action types and a reference
	// to the component on which we want to set
	// the state propery.
  setReducers(actionTypes, reducers, component) {
		actionTypes.forEach(actionType => {
			if (reducers[actionType]) {
				this.dispatcher.addEventListener(actionType, e => {
					const action = e.detail;
					component.state = reducers[actionType](component.state, action);
				});
			} else {
				throw new Error(
					`Please add a reducer for the "${actionType}" action.`
				);
			}
		});
	}
}

const zeroFux = new ZeroFux();

const actionTypes = ['INCREMENT', 'DECREMENT', 'INCREMENT_X'];

const reducers = {
  'INCREMENT': (state, action) => parseInt(state) + 1,
  'DECREMENT': (state, action) => parseInt(state) - 1,
  'INCREMENT_X': (state, action) => parseInt(state) + action.payload,
}

// Update the counter with ZeroFux:
class Counter extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.state = 0;
  }

  get state() {
    return this._state;
  }

  set state(s) {
		this._state = s;
    this.render();
  }

  connectedCallback() {
    this.render();
    zeroFux.setReducers(actionTypes, reducers, this);
  }

  render() {
    const counterText = document.createTextNode(this.state);
    this.shadowRoot.innerHTML = '';
    this.shadowRoot.appendChild(counterText);
  }
}

window.customElements.define('my-counter', Counter);

class SideEffects {
  run() {
    zeroFux.dispatcher.addEventListener('ADD_TEN_IN_THREE', () => {
      window.setTimeout(() => zeroFux.dispatch({
        type: 'INCREMENT_X',
        payload: 10
      }), 3000);
    });
  }
}

const sideEffects = new SideEffects();
sideEffects.run();

const incButton = document.querySelector('.incButton');
const decButton = document.querySelector('.decButton');
const decaIncButton = document.querySelector('.decaIncButton');

incButton.addEventListener('click', e => zeroFux.dispatch({ type: 'INCREMENT' }));
decButton.addEventListener('click', e => zeroFux.dispatch({ type: 'DECREMENT' }));
decaIncButton.addEventListener('click', e => zeroFux.dispatch({ type: 'ADD_TEN_IN_THREE' }));

            
          
!
999px
Loading ..................

Console