This repository was archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
This repository was archived by the owner on Mar 12, 2020. It is now read-only.
Improve CodePen examples #14
Copy link
Copy link
Open
Labels
Description
I found the examples a bit tricky to read, I suggest improving them as follows:
- Follow Hyperapp's style: 2 space indentation, bracket spacing, double quotes, avoid
_
for paramless functions - Avoid cryptic/shortened variable names
- Use named params for state, actions & view for readability
- Shorten shuffle algo with destructuring assignment
- For
add
, always place the new message at the beginning of the array so it appears on top - it's confusing having it appear randomly in the order (to me) - Styling could also be enhanced to make it prettier 😜
Here is Toasts:
const { h, app } = hyperapp
const { Move, Enter, Exit } = transitions
/** @jsx h */
const Toast = ({ message, remove }, children) => (
<Move easing="ease-in-out">
<Exit
easing="ease-in-out"
css={{ transform: "scale(2.0, 2.0)", opacity: "0" }}
>
<Enter
easing="ease-in-out"
css={{ transform: "translateX(100%)", opacity: "0" }}
>
<div class="message" key={message} onclick={() => remove(message)}>
{message}
</div>
</Enter>
</Exit>
</Move>
)
const state = {
messages: []
}
const actions = {
add: () => state => {
const newMessage = btoa("" + Math.random()).slice(3, 11)
return { messages: [newMessage, ...state.messages] }
},
remove: message => state => ({
messages: state.messages.filter(m => m !== message)
}),
reset: () => ({ messages: [] }),
shuffle: () => state => {
const messages = [...state.messages]
let m = messages.length
while (m) {
const i = Math.floor(Math.random() * m--)
;[messages[m], messages[i]] = [messages[i], messages[m]]
}
return { messages }
}
}
const view = (state, actions) => (
<main class="app">
<button onclick={actions.add}>Add</button>
<button onclick={actions.reset}>Reset</button>
<button onclick={actions.shuffle}>Shuffle</button>
Click toasts to remove them.
<div class="messages">
{state.messages.map(message => (
<Toast message={message} remove={actions.remove} />
))}
</div>
</main>
)
app(state, actions, view, document.body)