Skip to content

feat: add custom cursor component #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "@/once-ui/tokens/index.scss";

import classNames from "classnames";

import { Footer, Header, RouteGuard } from "@/components";
import { Footer, Header, RouteGuard, CustomCursor } from "@/components";
import { baseURL, effects, style, font, home } from "@/app/resources";

import { Background, Column, Flex, ThemeProvider, ToastProvider } from "@/once-ui/components";
Expand Down Expand Up @@ -71,6 +71,7 @@ export default async function RootLayout({ children }: RootLayoutProps) {
<ThemeProvider>
<ToastProvider>
<Column style={{ minHeight: "100vh" }} as="body" fillWidth margin="0" padding="0">
<CustomCursor />
<Background
position="fixed"
mask={{
Expand Down
24 changes: 24 additions & 0 deletions src/components/CustomCursor.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.cursor {
position: fixed;
top: 0;
left: 0;
width: 24px;
height: 24px;
border-radius: 50%;
background: rgb(172, 172, 172); /* iPadOS-like semi-transparent */
border: 2px solid rgba(255,255,255,0.5);
box-shadow: 0 2px 8px 2px rgba(0,0,0,0.10);
pointer-events: none;
z-index: 2147483647;
mix-blend-mode: normal;
transition: transform 0.12s cubic-bezier(0.22, 1, 0.36, 1), background 0.2s, box-shadow 0.2s;
will-change: transform, background;
display: block;
transform: translate3d(-50%, -50%, 0); /* Center the cursor on pointer */
}

@media (max-width: 768px) {
.cursor {
display: none;
}
}
28 changes: 28 additions & 0 deletions src/components/CustomCursor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use client";

import React, { useEffect, useRef } from "react";
import styles from "./CustomCursor.module.scss";

const CustomCursor: React.FC = () => {
const cursorRef = useRef<HTMLDivElement>(null);

useEffect(() => {
// Hide the default cursor
document.body.style.cursor = "none";
const moveCursor = (e: MouseEvent) => {
if (cursorRef.current) {
// Center the custom cursor on the pointer
cursorRef.current.style.transform = `translate3d(${e.clientX - 10}px, ${e.clientY - 10}px, 0)`;
}
};
document.addEventListener("mousemove", moveCursor);
return () => {
document.body.style.cursor = "";
document.removeEventListener("mousemove", moveCursor);
};
}, []);

return <div ref={cursorRef} className={styles.cursor} />;
};

export default CustomCursor;
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { Mailchimp } from "@/components/Mailchimp";
export { ProjectCard } from "@/components/ProjectCard";
export { HeadingLink } from "@/components/HeadingLink";
export { RouteGuard } from "@/components/RouteGuard";
export { default as CustomCursor } from "@/components/CustomCursor";