Skip to content
This repository has been archived by the owner on Apr 14, 2020. It is now read-only.

Simplify API with React Hooks/Optional Implicit Behaviour #90

Open
itsdouges opened this issue Dec 30, 2018 · 4 comments
Open

Simplify API with React Hooks/Optional Implicit Behaviour #90

itsdouges opened this issue Dec 30, 2018 · 4 comments

Comments

@itsdouges
Copy link
Owner

itsdouges commented Dec 30, 2018

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?

import { useBaba, revealMove, circleExpand, wait } from 'yubaba';

const Component = () => {
  // Each animation is now a function, call it with or without args.
  const { ref, style, className } = useBaba('my-baba', [ // lmao this almost sounds like yubaba. i like it.
    revealMove({ duration: 400 }),
    wait(),
    circleExpand({ background: '#ccc' }),
  ], {}); // Third arg has optional props such as "in" prop in current Baba component.

  // alternatively we could have a indefinite args where the last one is dynamically the optional props. would have some perf overhead.
  useBaba('my-baba', revealMove(), wait(), circleExpand(), {});

  return <div ref={ref} style={style} className={className} />;
};

If we compare the above to todays world:

import Baba, { Wait, RevealMove, Expand } from 'yubaba';

<Baba name="my-baba">
  <RevealMove duration={400}>
    <Wait>
      <CircleExpand background="#ccc">
        {({ ref, style, className }) => <div ref={ref} style={style} className={className} />}
      </CircleExpand>
    </Wait>
  </RevealMove>
</Baba>

Much simpler end result with the react elements, however the initial setup seems to be.. a more verbose API.

@itsdouges itsdouges changed the title Migrate to react hooks Investigation migration to react hooks Dec 30, 2018
@itsdouges itsdouges changed the title Investigation migration to react hooks Investigate migration to react hooks Dec 30, 2018
@itsdouges itsdouges changed the title Investigate migration to react hooks Investigate react hooks api Dec 31, 2018
@itsdouges
Copy link
Owner Author

itsdouges commented Feb 19, 2019

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.

@itsdouges
Copy link
Owner Author

Including the baba manager component as well (thoughts: is baba manager considered a parent here of the baba?)

const manager = useBabaManager(/* optional name if multiple child babas */ 'my-baba');
const baba = useBaba('my-baba', revealMove(), wait(), circleExpand());

<div {...manager}>
  <div {...baba} />
</div>

@itsdouges
Copy link
Owner Author

Next thought:

How could we compose animations with hooks?

@itsdouges itsdouges pinned this issue May 15, 2019
@itsdouges itsdouges unpinned this issue Jun 8, 2019
@itsdouges itsdouges changed the title Investigate react hooks api Simplify API with React Hooks/Optional Implicit Behaviour Aug 16, 2019
@itsdouges
Copy link
Owner Author

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>
  );
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant