forked from Renovamen/playground-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor dock, improve dock hover effect
- Loading branch information
Showing
8 changed files
with
264 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from "react"; | ||
import { useMotionValue } from "framer-motion"; | ||
import apps from "../../configs/apps"; | ||
import DockItem from "./DockItem"; | ||
|
||
export default function Dock({ open, showApps, toggleLaunchpad, hidde }) { | ||
const openApp = (id) => { | ||
if (id === "launchpad") toggleLaunchpad(); | ||
else { | ||
toggleLaunchpad(false); | ||
open(id); | ||
} | ||
}; | ||
|
||
const mouseX = useMotionValue(null); | ||
|
||
return ( | ||
<div | ||
className="dock w-full fixed bottom-0" | ||
style={{ | ||
zIndex: hidde ? 0 : 99999 | ||
}} | ||
onMouseMove={(e) => mouseX.set(e.nativeEvent.x)} | ||
onMouseLeave={() => mouseX.set(null)} | ||
> | ||
<ul className="mx-auto w-max px-2 space-x-2 flex flex-row justify-center justify-between bg-white bg-opacity-20 blur rounded-t-lg shadow-2xl"> | ||
{apps.map((app) => ( | ||
<DockItem | ||
key={`dock-${app.id}`} | ||
id={app.id} | ||
title={app.title} | ||
img={app.img} | ||
mouseX={mouseX} | ||
desktop={app.desktop} | ||
openApp={openApp} | ||
isOpen={app.desktop && showApps[app.id]} | ||
link={app.link} | ||
/> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { useRef } from "react"; | ||
import useRaf from "@rooks/use-raf"; | ||
import { motion, useMotionValue, useSpring, useTransform } from "framer-motion"; | ||
|
||
// Hover effect is adopted from https://github.com/PuruVJ/macos-web/blob/main/src/components/dock/DockItem.tsx | ||
|
||
const baseWidth = 50; | ||
const distanceLimit = baseWidth * 6; | ||
const distanceInput = [ | ||
-distanceLimit, | ||
-distanceLimit / 1.25, | ||
-distanceLimit / 2, | ||
0, | ||
distanceLimit / 2, | ||
distanceLimit / 1.25, | ||
distanceLimit | ||
]; | ||
const widthOutput = [ | ||
baseWidth, | ||
baseWidth * 1.1, | ||
baseWidth * 1.5, | ||
baseWidth * 2, | ||
baseWidth * 1.5, | ||
baseWidth * 1.1, | ||
baseWidth | ||
]; | ||
const beyondTheDistanceLimit = distanceLimit + 1; | ||
|
||
const useDockHoverAnimation = (mouseX, ref) => { | ||
const distance = useMotionValue(beyondTheDistanceLimit); | ||
const widthPX = useSpring( | ||
useTransform(distance, distanceInput, widthOutput), | ||
{ | ||
stiffness: 1300, | ||
damping: 82 | ||
} | ||
); | ||
|
||
const width = useTransform(widthPX, (width) => `${width / 16}rem`); | ||
|
||
useRaf(() => { | ||
const el = ref.current; | ||
const mouseXVal = mouseX.get(); | ||
if (el && mouseXVal !== null) { | ||
const rect = el.getBoundingClientRect(); | ||
const imgCenterX = rect.left + rect.width / 2; | ||
// difference between the x coordinate value of the mouse pointer | ||
// and the img center x coordinate value | ||
const distanceDelta = mouseXVal - imgCenterX; | ||
distance.set(distanceDelta); | ||
return; | ||
} | ||
|
||
distance.set(beyondTheDistanceLimit); | ||
}, true); | ||
|
||
console.log(widthPX); | ||
return { width, widthPX }; | ||
}; | ||
|
||
export default function DockItem({ | ||
id, | ||
title, | ||
img, | ||
mouseX, | ||
desktop, | ||
openApp, | ||
isOpen, | ||
link | ||
}) { | ||
const imgRef = useRef(); | ||
const { width } = useDockHoverAnimation(mouseX, imgRef); | ||
|
||
return ( | ||
<li | ||
id={`dock-${id}`} | ||
onClick={desktop || id === "launchpad" ? () => openApp(id) : () => {}} | ||
className="flex flex-col items-center justify-end mb-1 transition duration-150 ease-in origin-bottom" | ||
> | ||
<p className="tooltip text-black text-sm absolute -top-full px-3 py-1 bg-gray-300 bg-opacity-80 blur-sm rounded-md"> | ||
{title} | ||
</p> | ||
{link ? ( | ||
<a href={link} target="_blank" rel="noreferrer"> | ||
<motion.img | ||
ref={imgRef} | ||
src={img} | ||
alt={title} | ||
title={title} | ||
draggable={false} | ||
style={{ width, willChange: "width" }} | ||
/> | ||
</a> | ||
) : ( | ||
<motion.img | ||
ref={imgRef} | ||
src={img} | ||
alt={title} | ||
title={title} | ||
draggable={false} | ||
style={{ width, willChange: "width" }} | ||
/> | ||
)} | ||
<div | ||
className={`h-1 w-1 m-0 rounded-full bg-gray-800 ${ | ||
isOpen ? "" : "invisible" | ||
}`} | ||
/> | ||
</li> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,17 @@ import Notepad from "../components/apps/Notepad"; | |
import VSCode from "../components/apps/VSCode"; | ||
|
||
const apps = [ | ||
{ | ||
id: "launchpad", | ||
title: "Launchpad", | ||
desktop: false, | ||
img: "icons/launchpad.png" | ||
}, | ||
{ | ||
id: "notepad", | ||
title: "Notepad", | ||
desktop: true, | ||
show: true, | ||
show: false, | ||
img: "icons/text.png", | ||
content: <Notepad /> | ||
}, | ||
|
@@ -44,6 +50,20 @@ const apps = [ | |
show: false, | ||
img: "icons/terminal.png", | ||
content: <Terminal /> | ||
}, | ||
{ | ||
id: "email", | ||
title: "Email", | ||
desktop: false, | ||
img: "icons/mail.png", | ||
link: "mailto:[email protected]" | ||
}, | ||
{ | ||
id: "github", | ||
title: "Github", | ||
desktop: false, | ||
img: "icons/github.png", | ||
link: "https://github.com/Renovamen" | ||
} | ||
]; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.