popmotion icon indicating copy to clipboard operation
popmotion copied to clipboard

[react-pose]: Passing hostRef to a styled-component

Open daydream05 opened this issue 8 years ago • 12 comments

Is there a clean way to pass host-ref to a styled component instead of wrapping it to another component?

For example, this code causes error since it needs posed to pass hostRef to the element being animated:

const Bar = styled.div`
  width: 25px;
  height: 3px;
  background-color: white;
  margin: 4px 0;
  transition: 0.4s;
`

const Bar1 = posed(Bar)(poseAnimation)

So i end up rewriting this to this:

const Bar = styled.div`
  width: 25px;
  height: 3px;
  background-color: white;
  margin: 4px 0;
  transition: 0.4s;
`

const BarComponent = ({ hostRef }) => <Bar ref={hostRef} />
const Bar1 = posed(BarComponent)(poseAnimation)

Is there a way for me to just pass the hostRef directly to a styled component without having to create a BarComponent?

daydream05 avatar Oct 29 '18 19:10 daydream05

I usually wrap the posed component in styled(posed.div())

I’ll look into this to see if it can be easier in the forwardRef world. On Mon, 29 Oct 2018 at 19:53, Vince P. [email protected] wrote:

Is there a clean way to pass host-ref to a styled component instead of wrapping it to another component?

For example, this code causes error since it needs posed to pass hostRef to the element being animated:

const Bar = styled.divwidth: 25px; height: 3px; background-color: white; margin: 4px 0; transition: 0.4s;

const Bar1 = posed(Bar)(poseAnimation)

So i end up rewriting this to this:

const Bar = styled.divwidth: 25px; height: 3px; background-color: white; margin: 4px 0; transition: 0.4s;

const BarComponent = ({ hostRef }) => const Bar1 = posed(BarComponent)(poseAnimation)

Is there a way for me to just pass the hostRef directly to a styled component without having to create a BarComponent?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Popmotion/popmotion/issues/595, or mute the thread https://github.com/notifications/unsubscribe-auth/AHfLKl-O1BfZWZseegeGoDa2_9uvkCVyks5up1zHgaJpZM4YAEcP .

mattgperry avatar Oct 29 '18 20:10 mattgperry

Thanks! I just realized i've used that method before and I think i found a use case where it wouldn't work. Let's say if I need the same Bar component be used multiple times but with different animations.

daydream05 avatar Oct 29 '18 20:10 daydream05

Could u post a code example of the problematic pattern?

Andarist avatar Oct 29 '18 20:10 Andarist

When directly passing a styled component into the posed hoc, it triggers the missing hostRef error.

import React from "react";
import styled from "styled-components";
import posed from "react-pose";

const transition = {
  transition: {
    duration: 2000
  }
};

const BurgerContainer = styled.div`
  display: inline-block;
  cursor: pointer;
  position: fixed;
  left: 20px;
  top: 20px;
  z-index: 9999;
  background: black;
  padding: 10px;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
`;

*** Here's the pattern ***

const Bar = styled.div`
  width: 25px;
  height: 3px;
  background-color: white;
  margin: 4px 0;
  transform-origin: top left;
`;

const Bar1 = posed(Bar)({
  closed: {
    rotate: 0,
    x: "0",
    y: "0"
  },
  open: {
    rotate: "45deg",
    x: "4px",
    y: "-1px"
  },
  ...transition
});

const Bar2 = posed(Bar)({
  closed: {
    opacity: 1
  },
  open: {
    tranform: "none",
    opacity: 0
  },
  ...transition
});

const Bar3 = posed(Bar)({
  closed: {
    rotate: 0,
    x: "0",
    y: "0"
  },
  open: {
    rotate: "-45deg",
    x: "2px",
    y: "2px"
  },
  ...transition
});

*** end pattern ***

const BurgerIcon = ({ isOpen, ...props }) => {
  return (
    <BurgerContainer {...props}>
      <Bar1 key="b1" pose={isOpen ? "open" : "closed"} />
      <Bar2 key="b2" pose={isOpen ? "open" : "closed"} />
      <Bar3 key="b3" pose={isOpen ? "open" : "closed"} />
    </BurgerContainer>
  );
};

export default BurgerIcon;

daydream05 avatar Oct 29 '18 20:10 daydream05

Which version of styled-components do u use right now? Could u prepare a quick codesandbox.io showcasing the problem? If you are using v4 there is a pending PR for the issue https://github.com/Popmotion/popmotion/pull/580

Andarist avatar Oct 29 '18 21:10 Andarist

Here it is. I actually use v4 https://codesandbox.io/s/j7mnzjwn5

But I have a gatsby project that uses v3 and this error happens even with the const BarComponent = ({ hostRef }) => <Bar ref={hostRef} /> workaround

daydream05 avatar Oct 29 '18 22:10 daydream05

Take a look at this - https://codesandbox.io/s/4x4kmj3wzx . Cant say why this error happens with v3 though without a sandbox showcasing it

Andarist avatar Oct 29 '18 22:10 Andarist

I'm going to take a look at removing innerRef and hostRef now. Bullet to the head

mattgperry avatar Nov 01 '18 11:11 mattgperry

I'm not sure if it's intended but if i just spread the props, it actually works. const BarComponent = (props) => <Bar {..props} />

daydream05 avatar Nov 01 '18 20:11 daydream05

How does it work in the latest pose version ? I keep getting this error:

HEY, LISTEN! No valid DOM ref found. If you're converting an existing component via posed(Component), you must ensure you're passing the ref to the host DOM node via the React.forwardRef function.

https://codesandbox.io/s/k5n561vp3r

melMass avatar Jan 09 '19 16:01 melMass

@melMass Here you go with fixed example - https://codesandbox.io/s/jjjrmq36nw . Note that Bar doesnt have to be wrapped with React.forwardRef because it's already used by styled components under the hood (so Bar1 and Bar2 are created as posed(Bar)({})). Just for demonstration purposes I've created Bar3 using React.forwardRef.

btw @InventingWithMonster - this issue can be closed

Andarist avatar Jan 09 '19 17:01 Andarist

@Andarist Thank you for the example! I just upgraded to the new cra with typescript, got this error, and your example fixed it.

jacobwicks avatar Feb 27 '19 21:02 jacobwicks