Skip to content

Commit

Permalink
feat(shape): allow replace option
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmeinke committed Jul 20, 2017
1 parent 58bff6c commit 9b4415c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 33 deletions.
14 changes: 9 additions & 5 deletions examples/dom-node/src.js
Expand Up @@ -2,11 +2,15 @@ import { shape, render, timeline, play } from '../../src'

const el = document.querySelector('rect')

const square = shape({ el }, {
el,
fill: 'yellow',
transforms: [[ 'offset', 80, 80 ]]
})
const square = shape(
{ el },
{
el,
fill: 'yellow',
transforms: [[ 'offset', 70, 70 ]]
},
{ replace: el }
)

const animation = timeline(square, {
alternate: true,
Expand Down
58 changes: 31 additions & 27 deletions src/render.js
Expand Up @@ -4,28 +4,17 @@ import { frame } from 'wilderness-core'
import { node } from 'wilderness-dom-node'
import { tick } from './timeline'

const createNode = (shape, frameShape) => {
shape.node = node(frameShape)
const appendNode = (container, shape) => {
if (shape.replace) {
shape.replace.parentNode.replaceChild(shape.node, shape.replace)
delete shape.replace
} else {
container.appendChild(shape.node)
}
}

const split = shapesAndTimelines => {
const shapes = []
const timelines = []

shapesAndTimelines.map(x => {
if (x.keyframes) {
if (__DEV__ && x.timeline) {
throw new Error(`You cannot render a shape that has been placed on a timeline, instead render the timeline`)
}

shapes.push(x)
} else {
// @todo validate timeline
timelines.push(x)
}
})

return { shapes, timelines }
const createNode = (shape, frameShape) => {
shape.node = node(frameShape)
}

const render = (container, ...shapesAndTimelines) => {
Expand All @@ -49,19 +38,34 @@ const render = (container, ...shapesAndTimelines) => {
})
})

shapes.map(shape => {
container.appendChild(shape.node)
})
shapes.map(shape => appendNode(container, shape))

timelines.map(timeline => {
timeline.timelineShapes.map(({ shape }) => {
container.appendChild(shape.node)
})

timeline.timelineShapes.map(({ shape }) => appendNode(container, shape))
timeline.state.rendered = true
})

tick()
}

const split = shapesAndTimelines => {
const shapes = []
const timelines = []

shapesAndTimelines.map(x => {
if (x.keyframes) {
if (__DEV__ && x.timeline) {
throw new Error(`You cannot render a shape that has been placed on a timeline, instead render the timeline`)
}

shapes.push(x)
} else {
// @todo validate timeline
timelines.push(x)
}
})

return { shapes, timelines }
}

export default render
12 changes: 11 additions & 1 deletion src/shape.js
Expand Up @@ -2,7 +2,7 @@ import { plainShapeObject } from 'wilderness-dom-node'
import { shape as coreShape } from 'wilderness-core'

const shape = (...props) => {
return coreShape(...props.map(prop => {
const s = coreShape(...props.map(prop => {
if (prop.el) {
const p = {
...plainShapeObject(prop.el),
Expand All @@ -16,6 +16,16 @@ const shape = (...props) => {

return prop
}))

const { replace } = props.length > 1 && typeof props[ props.length - 1 ].type === 'undefined'
? props[ props.length - 1 ]
: {}

if (replace) {
s.replace = replace
}

return s
}

export default shape

0 comments on commit 9b4415c

Please sign in to comment.