Skip to content

Commit

Permalink
refactor: centralize sticker positioning logic to base sticker component
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderl19 committed Oct 29, 2023
1 parent 8266595 commit ae8d3eb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 36 deletions.
15 changes: 14 additions & 1 deletion apps/site/src/components/Sticker/BaseSticker.module.scss
Original file line number Diff line number Diff line change
@@ -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;
}
53 changes: 38 additions & 15 deletions apps/site/src/components/Sticker/BaseSticker.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<StickerProps> = ({
children,
position = "bottom-right",
imageSrc,
alt,
height = 100,
Expand All @@ -26,8 +30,7 @@ export default function Sticker({
dragConstraints = false,
animate = {},
transition = {},
style = {},
}: StickerProps) {
}) => {
// prevent next from throwing error involving DOM API
const pageRef = useRef(
typeof document !== "undefined" ? document.documentElement : undefined,
Expand Down Expand Up @@ -55,6 +58,7 @@ export default function Sticker({
filter: `drop-shadow(10px 14px 10px rgba(0, 0, 0, 0.2))`,
},
drag: true,
initial: { x: -width / 2, y: -height / 2 },
dragMomentum: false,
dragConstraints: dragConstraints ? dragConstraints : pageRef,
dragElastic: 0.2,
Expand All @@ -63,14 +67,33 @@ export default function Sticker({
: {};

return (
<motion.img
className={styles.stickerContainer}
animate={animateProps}
src={imageSrc}
alt={alt}
height={height}
width={width}
{...drag}
/>
<div
className={styles.stickerPositionContainer}
style={{
flexDirection:
position === "top-left" || position === "bottom-left"
? "row-reverse"
: "row",
alignItems:
position === "bottom-left" || position === "bottom-right"
? "flex-end"
: "flex-start",
}}
>
{children}
<div className={styles.stickerParent}>
<motion.img
src={imageSrc}
alt={alt}
height={height}
width={width}
className={styles.sticker}
animate={animateProps}
{...drag}
/>
</div>
</div>
);
}
};

export default BaseSticker;
Original file line number Diff line number Diff line change
@@ -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 (
<BaseSticker
style={{ ...style }}
imageSrc={HeartEmoji.src}
alt="heart emoji sticker"
height={150}
width={150}
{...fastShake}
/>
);
interface HeartStickerProps {
children: React.ReactNode;
position?: "top-left" | "top-right" | "bottom-left" | "bottom-right";
}

const HeartSticker = React.forwardRef<
React.ElementRef<typeof BaseSticker>,
HeartStickerProps
>(({ ...props }, ref) => (
<BaseSticker
imageSrc={HeartEmoji.src}
alt="heart emoji sticker"
height={150}
width={150}
{...fastShake}
{...props}
/>
));
HeartSticker.displayName = "HeartSticker";

export default HeartSticker;
19 changes: 10 additions & 9 deletions apps/site/src/views/Home/components/ApplyButton/ApplyButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import styles from "./ApplyButton.module.scss";

export default function ApplyButton() {
return (
<Button
className={styles.applyButton}
href="/apply"
variant=""
target="_blank"
disabled
>
<div>Applications have closed!</div>
</Button>
<HeartSticker>
<Button
className={styles.applyButton}
href="/apply"
variant=""
target="_blank"
>
<div>Apply</div>
</Button>
</HeartSticker>
);
}

0 comments on commit ae8d3eb

Please sign in to comment.