[react-pose]: Passing hostRef to a styled-component
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?
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.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 }) =>
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 .
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.
Could u post a code example of the problematic pattern?
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;
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
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
Take a look at this - https://codesandbox.io/s/4x4kmj3wzx . Cant say why this error happens with v3 though without a sandbox showcasing it
I'm going to take a look at removing innerRef and hostRef now. Bullet to the head
I'm not sure if it's intended but if i just spread the props, it actually works. const BarComponent = (props) => <Bar {..props} />
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
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 Thank you for the example! I just upgraded to the new cra with typescript, got this error, and your example fixed it.