import { h, render } from 'https://unpkg.com/preact@latest?module';
import {
useReducer,
useMemo,
} from 'https://unpkg.com/preact@latest/hooks/dist/hooks.module.js?module';
function toActionType(jsHook) {
const parts = jsHook.split('-');
let type = parts[1].toLowerCase();
for (let i = 2; i < parts.length; i += 1) {
const part = parts[i];
type += part[0].toUpperCase() + part.substring(1).toLowerCase();
}
return type;
}
function makeHolder(initialPair) {
const holder = {
reducerPair: initialPair,
handleEvent,
};
return holder;
function handleEvent(e) {
const jsHook = findJsHook(e.currentTarget.classList);
if (!jsHook) return;
const reducerPair = holder.reducerPair;
const [_state, dispatch] = reducerPair;
switch (jsHook) {
case 'js-grandparent':
if (event.type === 'click') clickGrandparent(reducerPair, e);
return;
case 'js-parent':
if (event.type === 'click') clickParent(reducerPair, e);
return;
case 'js-clickme':
if (event.type === 'click') clickButton(reducerPair, e);
return;
case 'js-stop-grandparent':
case 'js-stop-parent':
case 'js-stop-click':
if (event.type === 'change')
dispatch({ type: toActionType(jsHook), payload: e.target.checked });
return;
case 'js-clear':
if (event.type === 'click') log(dispatch, '');
return;
}
}
}
function findJsHook(classList) {
for (let i = 0; i < classList.length; i += 1) {
const name = classList.item(i);
if (name.startsWith('js-')) return name;
}
}
function clickGrandparent([{ stopGrandparent }, dispatch], e) {
log(dispatch, 'Handling click via capture on red div');
console.log('Red Div onClick');
if (stopGrandparent) e.stopPropagation();
}
function clickParent([{ stopParent }, dispatch], e) {
log(dispatch, 'Handling click via bubble on blue div');
console.log('Blue div on Click...');
if (stopParent)...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.