Skip to content

Instantly share code, notes, and snippets.

@Swiip
Last active June 19, 2018 07:30
Show Gist options
  • Save Swiip/7d1f40dc97b766c11c2a0a3be02ec365 to your computer and use it in GitHub Desktop.
Save Swiip/7d1f40dc97b766c11c2a0a3be02ec365 to your computer and use it in GitHub Desktop.
Compo counter example
import {
html,
css,
createStore,
component,
withProp,
withStore,
withStyle,
withMarkup,
} from 'compo-lib';
createStore((state, action) => {
switch (action.type) {
case 'ADD': return state + 1;
case 'SUB': return state - 1;
default: return state;
}
}, 0);
component(
'my-counter-label',
withProp('value'),
withStyle(({ value }) => css`
:host {
color: ${value < 1 ? 'red' : 'black'}
}
`),
);
component(
'my-counter',
withStore(({ getState, dispatch }) => ({
counter: getState(),
add: () => dispatch({ type: 'ADD' }),
sub: () => dispatch({ type: 'SUB' }),
})),
withMarkup(({ counter, add, sub }) => html`
<div>
<my-counter-label value=${counter}>${counter}</my-counter-label>
<button onclick=${add}>+</button>
<button onclick=${sub}>-</button>
</div>
`),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment