ISSUE-017 - Figured out how to allow for Transition and Dragging on the same element.

The secret? Don't! Use another throw-away div and put it around the draggable div.
Also adopted animated.div into the Styled-Component. Really cool how it just works.
This commit is contained in:
Kyle Geib committed 2022-01-12 14:52:38 -05:00
1 parent 9541306894
commit a6a5eb7751
2 files changed
+28 -8

No files matched your search

@@ -5,7 +5,7 @@ import { useDrag } from "@use-gesture/react";
import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector"; import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector";
import { setPosition } from "../../../features/overlayTool/overlayToolSlice"; import { setPosition } from "../../../features/overlayTool/overlayToolSlice";
import { OverlayItemContainer } from "./styles"; import { AnimatedOverlayItemContainer } from "./styles";
export function OverlayDisplay() { export function OverlayDisplay() {
const { currentOverlayItem, visible } = useSelector(overlayToolSelector); const { currentOverlayItem, visible } = useSelector(overlayToolSelector);
@@ -37,21 +37,20 @@ export function OverlayDisplay() {
return ( return (
transition((style, visible) => transition((style, visible) =>
visible ? ( visible ? (
<animated.div <animated.div style={{ ...style, position: "absolute" }}>
<AnimatedOverlayItemContainer
{...bindOverlayItem({ xPos, yPos })} {...bindOverlayItem({ xPos, yPos })}
style={{ style={{
...style,
left: xPos, left: xPos,
// top: style.top.get() + yPos, // This doesn't work.
top: yPos, top: yPos,
position: "absolute", position: "absolute",
}} }}
visible={visible}
> >
<OverlayItemContainer position={position} visible={visible}>
{id} {id}
<br /> <br />
(x:{position.x}px, y:{position.y}px) (x:{position.x}px, y:{position.y}px)
</OverlayItemContainer> </AnimatedOverlayItemContainer>
</animated.div> </animated.div>
) : null, ) : null,
) )
@@ -1,3 +1,4 @@
import { animated } from "@react-spring/web";
import styled from "styled-components"; import styled from "styled-components";
import { OverlayPosition } from "../../../types/state"; import { OverlayPosition } from "../../../types/state";
@@ -11,8 +12,8 @@ export const OverlayItemContainer = styled.div.attrs<OverlayItemContainerProps>(
visible, visible,
}) => ({ }) => ({
style: { style: {
// top: y, top: y,
// left: x, left: x,
display: visible ? undefined : "none", display: visible ? undefined : "none",
}, },
}))<OverlayItemContainerProps>` }))<OverlayItemContainerProps>`
@@ -24,3 +25,23 @@ export const OverlayItemContainer = styled.div.attrs<OverlayItemContainerProps>(
`; `;
OverlayItemContainer.displayName = "OverlayItemContainer"; OverlayItemContainer.displayName = "OverlayItemContainer";
type AnimatedOverlayItemContainerProps = {
visible: boolean,
}
export const AnimatedOverlayItemContainer = styled(animated.div).attrs<AnimatedOverlayItemContainerProps>(({
visible,
}) => ({
style: {
display: visible ? undefined : "none",
},
}))<AnimatedOverlayItemContainerProps>`
position: absolute;
z-index: 1;
/* Temporary. Overlay will be transparent and based on an image. */
width: 150px;
height: 150px;
background-color: white;
box-shadow: 0px 7px 11px 1px black;
`;