-
Notifications
You must be signed in to change notification settings - Fork 13
Simplify API with React Hooks/Optional Implicit Behaviour #90
Comments
We'd also want a better API internally for animations. Today it looks like: class Animation extends React.Component {
beforeAnimate = (data, onFinish, setChildProps) => {
};
animate = (data, onFinish, setChildProps) => {
};
render() {
const { children } = this.props;
return (
<Collector
data={{
action: 'animation',
payload: {
beforeAnimate: this.beforeAnimate,
animate: this.animate,
},
}}
>
{children}
</Collector>
);
}
} If we were to re-imagine using hooks, perhaps it wouldn't even need to be a component or hook - just a function that returns an animation descriptor. Making the entire collector implementation redundant... const animation = () => ({
action: 'animation',
beforeAnimate: (data, onFinish, setChildProps) => {},
animate: (data, onFinish, setChildProps) => {},
}); Simpler yes - but it also puts in the restriction of not allowing any instance state. Maybe that's actually a good thing though. |
Including the baba manager component as well (thoughts: is baba manager considered a parent here of the baba?)
|
Next thought: How could we compose animations with hooks? |
Currently it's render props everywhere. I think the ideal state (considering we still need to use React Context to communicate between some components) is going to be a mix of hooks and components. import { useMotion, useMove, useCircleExpand } from '@element-motion/core';
() => {
// Creates a motion component dynamically.
// Use a component so we can pass around context as needed.
const Motion = useMotion([
// Motion hooks so we can memoize over the react lifecycle
useMove(),
useCircleExpand(),
]);
return (
<Motion>
/* Implicitly pass down className/styles */
<div>hello world</div>
</Motion>
);
return (
<Motion>
/* Explicitly pass down className/styles */
{motion => <div {...motion}>hello world</div>}
</Motion>
);
} |
Should be relatively painless (for consumers) as everything is already a render function. Defining what animations you want to happen could be tricky though. And the collector behaviour we have now probably would need to be re-written. Hmm.
Ignoring implementation details the API could be something like this?
If we compare the above to todays world:
Much simpler end result with the react elements, however the initial setup seems to be.. a more verbose API.
The text was updated successfully, but these errors were encountered: