Skip to content

Commit

Permalink
feat: refactor dock, improve dock hover effect
Browse files Browse the repository at this point in the history
  • Loading branch information
Renovamen committed May 1, 2021
1 parent ead3b26 commit e13e4ea
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 90 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@
},
"dependencies": {
"@craco/craco": "^6.1.2",
"@rooks/use-raf": "^4.11.2",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"date-fns": "2.16.1",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"framer-motion": "^4.1.11",
"nightwind": "^1.1.11",
"prettier": "^2.2.1",
"react": "^17.0.2",
Expand Down
73 changes: 0 additions & 73 deletions src/components/Dock.js

This file was deleted.

43 changes: 43 additions & 0 deletions src/components/dock/Dock.js
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>
);
}
111 changes: 111 additions & 0 deletions src/components/dock/DockItem.js
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>
);
}
22 changes: 21 additions & 1 deletion src/configs/apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 />
},
Expand Down Expand Up @@ -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"
}
];

Expand Down
16 changes: 10 additions & 6 deletions src/pages/Desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { Component } from "react";
import nightwind from "nightwind/helper";

import MenuBar from "../components/MenuBar";
import Dock from "../components/Dock";
import Dock from "../components/dock/Dock";
import Launchpad from "../components/Launchpad";
import ControlCenterMenu from "../components/ControlCenterMenu";
import Window from "../components/Window";
Expand Down Expand Up @@ -83,9 +83,11 @@ export default class Desktop extends Component {
};

openApp = (id) => {
// add it to the shown app list
let showApps = this.state.showApps;
showApps[id] = true;

// move to the top (use a maximum z-index)
let appsZ = this.state.appsZ;
let maxZ = this.state.maxZ + 1;
appsZ[id] = maxZ;
Expand All @@ -100,14 +102,15 @@ export default class Desktop extends Component {
});

let minApps = this.state.minApps;
// if the app has already been shown but minimized
if (minApps[id]) {
// set window"s last position
// move to window's last position
var r = document.querySelector(`#window-${id}`);
r.style.transform = `translate(${r.style.getPropertyValue(
"--window-transform-x"
)}, ${r.style.getPropertyValue("--window-transform-y")}) scale(1)`;
r.style.transition = "ease-in 0.3s";

// remove it from the minimized app list
minApps[id] = false;
this.setState({ minApps });
}
Expand Down Expand Up @@ -137,23 +140,24 @@ export default class Desktop extends Component {

this.setWinowsPosition(id);

// get corrosponding dock icon"s position
// get the corrosponding dock icon's position
var r = document.querySelector(`#dock-${id}`);
const dockApp = r.getBoundingClientRect();

r = document.querySelector(`#window-${id}`);
// translate window to that position
// translate the window to that position
r.style.transform = `translate(${
dockApp.x.toFixed(1) - 290
}px, ${posy}px) scale(0.2)`;
r.style.transition = "ease-out 0.3s";

// add it to the minimized app list
this.setAppMin(id, true);
};

renderAppWindows = () => {
return apps.map((app) => {
if (this.state.showApps[app.id]) {
if (app.desktop && this.state.showApps[app.id]) {
const props = {
title: app.title,
id: app.id,
Expand Down
18 changes: 9 additions & 9 deletions src/styles/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ html, body {

/* --------- dock --------- */

.dock li {
transition: all 0.3s;
-webkit-transition: all 0.3s;
transform-origin: 40% 100%;
-webkit-transform-origin: 40% 100%;
.dock ul {
height: 65px;
}

.dock li:hover {
transform: scale(1.6);
-webkit-transform: scale(1.6);
margin: 0 12px;
.dock li .tooltip {
display: none;
}

.dock li:hover .tooltip {
display: block;
box-shadow: hsla(0deg, 0%, 0%, 20%) 0px 1px 5px 2px, 0 0 0 0 white;
}

/* --------- slider --------- */
Expand Down
Loading

0 comments on commit e13e4ea

Please sign in to comment.