From 53569de2d7f1e3ca62b8bf0e8487a248c404e938 Mon Sep 17 00:00:00 2001 From: Alexander Liu Date: Sun, 22 Oct 2023 02:24:29 -0700 Subject: [PATCH] refactor: centralize sticker positioning logic to base sticker component --- .../Sticker/BaseSticker.module.scss | 15 +++++- .../src/components/Sticker/BaseSticker.tsx | 54 +++++++++++++------ .../Stickers/HeartSticker/HeartSticker.tsx | 32 +++++++---- .../components/ApplyButton/ApplyButton.tsx | 24 +-------- 4 files changed, 75 insertions(+), 50 deletions(-) diff --git a/apps/site/src/components/Sticker/BaseSticker.module.scss b/apps/site/src/components/Sticker/BaseSticker.module.scss index 775c4965..de1004ba 100644 --- a/apps/site/src/components/Sticker/BaseSticker.module.scss +++ b/apps/site/src/components/Sticker/BaseSticker.module.scss @@ -1,3 +1,16 @@ -.stickerContainer { +.stickerPositionContainer { + position: relative; + display: flex; +} + +.stickerParent { + position: "relative"; + width: 0; + height: 0; +} + +.sticker { cursor: grab; + position: absolute; + z-index: 100; } diff --git a/apps/site/src/components/Sticker/BaseSticker.tsx b/apps/site/src/components/Sticker/BaseSticker.tsx index f6ad05eb..ff91dfa0 100644 --- a/apps/site/src/components/Sticker/BaseSticker.tsx +++ b/apps/site/src/components/Sticker/BaseSticker.tsx @@ -1,10 +1,13 @@ "use client"; +import { MutableRefObject, useRef } from "react"; import { motion } from "framer-motion"; + import styles from "./BaseSticker.module.scss"; -import { MutableRefObject, useRef } from "react"; interface StickerProps { + children?: React.ReactNode; + position?: "top-left" | "top-right" | "bottom-left" | "bottom-right"; imageSrc: string; alt: string; height?: number; @@ -14,10 +17,11 @@ interface StickerProps { // dragConstraints prop can be an object containing coordinates, a Falsy boolean, or a parent ref (https://www.framer.com/motion/gestures/#:~:text=%23-,dragConstraints%3A,-false%20%7C%20Partial%3CBoundingBox2D) animate?: object | undefined; transition?: object | undefined; - style?: object | undefined; } -export default function Sticker({ +const BaseSticker: React.FC = ({ + children, + position = "bottom-right", imageSrc, alt, height = 100, @@ -26,8 +30,7 @@ export default function Sticker({ dragConstraints = false, animate = {}, transition = {}, - style = {}, -}: StickerProps) { +}) => { // prevent next from throwing error involving DOM API const pageRef = typeof document !== "undefined" @@ -60,6 +63,7 @@ export default function Sticker({ }px rgba(0, 0, 0, 0.2))`, }, drag: true, + initial: { x: -width / 2, y: -height / 2 }, dragMomentum: false, dragConstraints: dragConstraints ? dragConstraints : pageRef, dragElastic: 0.2, @@ -68,15 +72,33 @@ export default function Sticker({ : {}; return ( - +
+ {children} +
+ +
+
); -} +}; + +export default BaseSticker; diff --git a/apps/site/src/components/Sticker/Stickers/HeartSticker/HeartSticker.tsx b/apps/site/src/components/Sticker/Stickers/HeartSticker/HeartSticker.tsx index 9eb7063e..2a544d68 100644 --- a/apps/site/src/components/Sticker/Stickers/HeartSticker/HeartSticker.tsx +++ b/apps/site/src/components/Sticker/Stickers/HeartSticker/HeartSticker.tsx @@ -1,16 +1,26 @@ import HeartEmoji from "@/assets/images/heart_emoji.png"; import BaseSticker from "../../BaseSticker"; import { fastShake } from "@/components/animation"; +import React from "react"; -export default function HeartSticker(style: any) { - return ( - - ); +interface HeartStickerProps { + children: React.ReactNode; + position?: "top-left" | "top-right" | "bottom-left" | "bottom-right"; } + +const HeartSticker = React.forwardRef< + React.ElementRef, + HeartStickerProps +>(({ ...props }, ref) => ( + +)); +HeartSticker.displayName = "HeartSticker"; + +export default HeartSticker; diff --git a/apps/site/src/views/Home/components/ApplyButton/ApplyButton.tsx b/apps/site/src/views/Home/components/ApplyButton/ApplyButton.tsx index db84ef96..6858fc13 100644 --- a/apps/site/src/views/Home/components/ApplyButton/ApplyButton.tsx +++ b/apps/site/src/views/Home/components/ApplyButton/ApplyButton.tsx @@ -5,12 +5,7 @@ import styles from "./ApplyButton.module.scss"; export default function ApplyButton() { return ( -
+ -
- -
-
+ ); }