From 44105854731ee475f71a864ec6253d249c0ddb74 Mon Sep 17 00:00:00 2001 From: Adnan Shabbir Husain Date: Thu, 23 Nov 2023 21:24:15 +0530 Subject: [PATCH] fix: multiple issues with the follower --- src/component/follower.tsx | 4 +- src/component/follower_div.tsx | 70 ++++++++++++++++++++------------- src/component/follower_init.tsx | 44 +++++++++++++++------ src/store/index.ts | 70 +++++++++++++++++++-------------- tsconfig.json | 2 +- 5 files changed, 117 insertions(+), 73 deletions(-) diff --git a/src/component/follower.tsx b/src/component/follower.tsx index a8a893d..976018c 100644 --- a/src/component/follower.tsx +++ b/src/component/follower.tsx @@ -1,7 +1,5 @@ -import useMouseStore from '../store/index.js'; import { FollowerInitialiserComponent } from './follower_init.js'; export function Follower() { - const { options } = useMouseStore((state) => ({ options: state.curSettings })); - return ; + return ; } diff --git a/src/component/follower_div.tsx b/src/component/follower_div.tsx index a050fe7..a8e85ba 100644 --- a/src/component/follower_div.tsx +++ b/src/component/follower_div.tsx @@ -1,14 +1,15 @@ import { MousePosition, MouseSettings } from '../types/index.js'; -import { motion } from 'framer-motion'; +import { AnimatePresence, motion } from 'framer-motion'; export function FollowerDiv({ pos, options }: { pos: MousePosition; options: MouseSettings }) { const calculatePosition = (): MousePosition => { - if (options.customLocation != undefined) { + if (options.customLocation != null) { return { x: options.customLocation.x, y: options.customLocation.y }; - } else if (options.customPosition != undefined) { + } else if (options.customPosition != null) { const rect = options.customPosition.current.getBoundingClientRect(); - const x = rect.left + rect.width / 2 - options.radius; - const y = rect.top + rect.height / 2 - options.radius; + const radius = options.radius ? options.radius : 12 / 2; + const x = rect.left + rect.width / 2 - radius; + const y = rect.top + rect.height / 2 - radius; return { x, y }; } else { return { x: pos.x, y: pos.y }; @@ -20,22 +21,20 @@ export function FollowerDiv({ pos, options }: { pos: MousePosition; options: Mou x: pos.x, y: pos.y, scale: 0, + backgroundColor: options.backgroundColor || 'black', + zIndex: options.zIndex || -5, + mixBlendMode: options.mixBlendMode || 'initial', }} animate={{ x: calculatePosition().x, y: calculatePosition().y, - scale: options.scale || 1, + scale: options.scale != null ? options.scale : 1, rotate: options.rotate || 0, - }} - exit={{ - x: pos.x, - y: pos.y, - scale: 0, - }} - style={{ backgroundColor: options.backgroundColor || 'black', - mixBlendMode: options.mixBlendMode || 'initial', zIndex: options.zIndex || -5, + mixBlendMode: options.mixBlendMode || 'initial', + }} + style={{ position: 'fixed', inset: 0, pointerEvents: 'none', @@ -72,21 +71,36 @@ export function FollowerDiv({ pos, options }: { pos: MousePosition; options: Mou }} > {options.text && !options.backgroundElement ? ( -

- {options.text} -

+ + + {options.text} + + ) : null} - {options.backgroundElement ? options.backgroundElement : null} + + {options.backgroundElement ? ( + + {options.backgroundElement} + + ) : null} + diff --git a/src/component/follower_init.tsx b/src/component/follower_init.tsx index aefb208..6ac9269 100644 --- a/src/component/follower_init.tsx +++ b/src/component/follower_init.tsx @@ -1,14 +1,16 @@ import { useEffect, useState } from 'react'; -import { AnimatePresence } from 'framer-motion'; -import type { MousePosition, MouseSettings } from '../types/index.js'; +import { MouseSettings, type MousePosition } from '../types/index.js'; import { FollowerDiv } from './follower_div.js'; +import useMouseStore from '../store/index.js'; +import { AnimatePresence } from 'framer-motion'; + +const defaultRadius = 12 / 2; -export function FollowerInitialiserComponent({ options }: { options: MouseSettings }) { +export function FollowerInitialiserComponent() { const [isHovering, setIsHovering] = useState(false); - const [pos, setPos] = useState({ - x: 0, - y: 0, - }); + + const options = useMouseStore((store) => store.curSettings); + // console.log(options); useEffect(() => { const handleMouseLeave = () => { @@ -29,12 +31,30 @@ export function FollowerInitialiserComponent({ options }: { options: MouseSettin }; }, []); + return ( + + ); +} + +function ManagePosition({ options }: { options: MouseSettings }) { + const [pos, setPos] = useState({ + x: 0, + y: 0, + }); + useEffect(() => { const mouseMove = (event: any) => { - setPos({ - x: event.clientX - options.radius, - y: event.clientY - options.radius, - }); + if (options.radius != null) { + setPos({ + x: event.clientX - options.radius, + y: event.clientY - options.radius, + }); + } else { + setPos({ + x: event.clientX - defaultRadius, + y: event.clientY - defaultRadius, + }); + } }; window.addEventListener('mousemove', mouseMove); return () => { @@ -44,7 +64,7 @@ export function FollowerInitialiserComponent({ options }: { options: MouseSettin return ( - {isHovering && options.visible !== false ? : null} + {options.visible !== false ? : null} ); } diff --git a/src/store/index.ts b/src/store/index.ts index 2e2f82e..bdf64b8 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,42 +1,54 @@ -import { create } from 'zustand'; +// import { create } from 'zustand'; +import { create, StateCreator, StoreApi, SetState, GetState } from 'zustand'; import { MouseSettings } from '../types/index.js'; -const defaultSettings: MouseSettings = { - radius: 12 / 2, -}; - interface useMouseStoreInterface { curSettings: MouseSettings; - layers: [MouseSettings]; + layers: MouseSettings[]; pushLayer: (newLayer: MouseSettings) => void; popLayer: () => void; clearLayers: () => void; } -const useMouseStore = create((set) => ({ - curSettings: defaultSettings, - layers: [defaultSettings], +const log = + (config: StateCreator) => + (set: SetState, get: GetState, api: StoreApi) => + config( + (args) => { + console.log(' applying', args); + set(args); + console.log(' new state', get()); + }, + get, + api, + ); + +const useMouseStore = create( + log((set) => ({ + curSettings: {}, + layers: [], - pushLayer: (newLayer: MouseSettings) => - set((state) => { - const newCur = { ...state.curSettings, ...newLayer }; - state.layers.push(newCur); - return { layers: state.layers, curSettings: newCur }; - }), - popLayer: () => - set((state) => { - if (state.layers.length > 1) { - state.layers.pop(); - return { layers: state.layers, curSettings: state.layers.at(state.layers.length - 1) }; - } else { - return { layers: [defaultSettings], curSettings: defaultSettings }; - } - }), - clearLayers: () => - set((state) => { - return { layers: [defaultSettings], curSettings: defaultSettings }; - }), -})); + pushLayer: (newLayer: MouseSettings) => + set((state) => { + const newCur = { ...state.curSettings, ...newLayer }; + state.layers.push(newCur); + return { layers: state.layers, curSettings: newCur }; + }), + popLayer: () => + set((state) => { + if (state.layers.length > 1) { + state.layers.pop(); + return { layers: state.layers, curSettings: state.layers.at(state.layers.length - 1) }; + } else { + return { layers: [], curSettings: {} }; + } + }), + clearLayers: () => + set((state) => { + return { layers: [], curSettings: {} }; + }), + })), +); export default useMouseStore; diff --git a/tsconfig.json b/tsconfig.json index b6ad655..e247d53 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ "target": "es6", "useDefineForClassFields": true, "lib": ["es5", "es2015", "es2016", "dom", "esnext", "es2020"], - "module": "ESNext", + "module": "NodeNext", "moduleResolution": "nodenext", "noImplicitAny": true, "skipLibCheck": true,