css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console