flimflamjs / ff-toggle-box

0 stars 1 forks source link

change API to just have open$ stream #1

Open jayrbolton opened 7 years ago

jayrbolton commented 7 years ago

My suggestion is to eliminate the thisID and the parentState properties in the parameter to toggleBox and just pass in a open$ stream, which makes the box open up when the stream contains a truthy value:

  const open$ = flyd.stream(false)
  toggleBox({ 
      open$
    , top: h('div', 'Click to toggle')
    , bottom: h('div', 'This section is hidden by default')
    })

  open$(true)
  open$(false)
  open$(true)
jayrbolton commented 7 years ago

Or at least replace the parentState property with just the toggleBoxIDs$ stream:

  toggleBox({ 
      toggleBoxIDs$
    , top: h('div', 'Click to toggle')
    , bottom: h('div', 'This section is hidden by default')
    })

Why pass in the entire parent state? How does the programmer recognize what stream the toggle box is actually using inside the parent's state?

jayrbolton commented 7 years ago

also, line 9 - 11, instead of:

  state.parentState.toggleBoxIDs$(
    R.assoc(thisID, IDs[thisID] ? false : true, IDs)
)

you can do:

state.parentState.toggleBoxIDs$(R.assoc(thisID, !IDs[thisID], IDs))

! will toggle a boolean

jayrbolton commented 7 years ago

example how to manage things from the parent state with just open$ streams

function init() {
  const boxes = {
     openBox1$: flyd.stream(true)
  ,  openBox2$: flyd.stream(false)
  }
  return {boxes}
}

function view(state) {
  return h('div', [
    toggleBox({open$: state.boxes.openBox1$, ...})
  , toggleBox({open$: state.boxes.openBox2$, ...})
  ])
}