-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
Website: Implement glowing cursor effect #1009
Merged
Merged
Changes from 35 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
48f2298
Add GlowHoverContainer component
IPogorelova 9acb966
Add usage example to the main page (TODO: remove the example before m…
IPogorelova c2773a9
Remove unnecessary comments
IPogorelova d618b55
Prettified Code!
IPogorelova 4aa2dcf
Merge branch 'main' into implement-glowing-cursor-effect
ssandino 1dd4189
Add missing wrapperClassName prop to BaseContainerProps
IPogorelova b3b8e29
Merge remote-tracking branch 'origin/implement-glowing-cursor-effect'…
IPogorelova 8e07d04
wip fix
ssandino 90e5f3a
Prettified Code!
ssandino 81691b7
wip fix 2
ssandino 9388ae6
Merge remote-tracking branch 'origin/sandino/implement-glowing-cursor…
ssandino 49de84b
Prettified Code!
ssandino ef2fe22
wip fix 3
ssandino 4304be9
Merge remote-tracking branch 'origin/sandino/implement-glowing-cursor…
ssandino 5dd5de4
wip fix 4
ssandino 0484469
Prettified Code!
ssandino 1756d12
Remove temporary GlowHoverContainer example from the main page
IPogorelova 52b2a98
Refactor newsletter popup form, extract it into a separate component,…
IPogorelova b04805a
Add translations
IPogorelova d81b462
Separate GlowHoverContainer from BaseContainer
IPogorelova 1aa8515
Merge branch 'main' into implement-glowing-cursor-effect
IPogorelova f38a457
Refactor BaseContainer after resolving conflicts with the main branch
IPogorelova 30208ea
Clean up the mess caused by experiments
IPogorelova 1a1ad0b
Prettified Code!
IPogorelova 140b6ba
Apply CodeRabbitAI refactoring suggestions
IPogorelova 3b81b8d
Merge remote-tracking branch 'origin/implement-glowing-cursor-effect'…
IPogorelova 35f3fa1
Clean mess caused by experiments
IPogorelova 23af142
Prettified Code!
IPogorelova ff3cb17
Remove unused prop
IPogorelova 53813b8
Add types for NewsletterForm component
IPogorelova 45958cb
Prettified Code!
IPogorelova 0184c9e
Add types for NewsletterForm component
IPogorelova 6664ce6
Merge remote-tracking branch 'origin/implement-glowing-cursor-effect'…
IPogorelova d79d76c
Prettified Code!
IPogorelova c115353
Tune types for NewsletterForm component
IPogorelova 6b777e7
Tune types for NewsletterForm component
IPogorelova a36828c
Tune types for NewsletterGlowContainer component
IPogorelova 723b102
Prettified Code!
IPogorelova 55ce638
Tune types for NewsletterGlowContainer component
IPogorelova 62501a4
Merge remote-tracking branch 'origin/implement-glowing-cursor-effect'…
IPogorelova 765506b
Rename NewsletterForm props to avoid collisions
IPogorelova 1d68453
Rollback unnecessary change
IPogorelova 292e08c
Rollback unnecessary change
IPogorelova e103dae
Merge branch 'main' into implement-glowing-cursor-effect
mkue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,8 @@ | |
"toast-failure": "Oops! Etwas ist schief gelaufen. Bitte versuche es nochmals, oder kontaktiere uns auf [email protected]", | ||
"email-placeholder": "Deine E-Mail Adresse", | ||
"button-subscribe": "Newsletter abonnieren" | ||
}, | ||
"campaign": { | ||
"information-label": "Erhalte Updates von diesem Fundraiser und von Social Income." | ||
} | ||
} |
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 |
---|---|---|
|
@@ -25,5 +25,8 @@ | |
"toast-failure": "Something went wrong. Try again or contact us at [email protected]", | ||
"email-placeholder": "Your email", | ||
"button-subscribe": "Subscribe now" | ||
}, | ||
"campaign": { | ||
"information-label": "Get updates from the fundraiser and Social Income." | ||
} | ||
} |
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 |
---|---|---|
|
@@ -25,5 +25,8 @@ | |
"toast-failure": "Oups! Ça n’a pas marché. Essaie encore une fois ou prend contact avec nous ([email protected])", | ||
"email-placeholder": "Ton adresse E-Mail", | ||
"button-subscribe": "S'abonner" | ||
}, | ||
"campaign": { | ||
"information-label": "Reçois des mises à jour du collecteur de fonds et de Social Income." | ||
} | ||
} |
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 |
---|---|---|
|
@@ -25,5 +25,8 @@ | |
"toast-failure": "Qualcosa è andato storto. Prova di nuovo o contattaci a [email protected]", | ||
"email-placeholder": "La tua email", | ||
"button-subscribe": "Iscriviti ora" | ||
}, | ||
"campaign": { | ||
"information-label": "Ricevi aggiornamenti dal fundraiser e da Social Income." | ||
} | ||
} |
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
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,23 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
import { twMerge } from 'tailwind-merge'; | ||
import { useGlowHover } from '../use-glow-hover'; | ||
import { BaseContainer } from './base-container'; | ||
|
||
import { BaseContainerProps } from './base-container'; | ||
|
||
export const GlowHoverContainer = React.forwardRef<HTMLDivElement, Omit<BaseContainerProps, 'wrapperRef'>>( | ||
({ className, ...props }, ref) => { | ||
const refCard = useGlowHover({ lightColor: '#CEFF00' }); | ||
|
||
return ( | ||
<BaseContainer | ||
wrapperClassName={twMerge('theme-blue', className)} | ||
ref={ref} | ||
{...props} | ||
wrapperRef={refCard as React.Ref<HTMLDivElement>} | ||
/> | ||
); | ||
}, | ||
); |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './base-container'; | ||
export * from './glow-hover-container'; |
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,266 @@ | ||
'use client'; | ||
|
||
import { linearAnimation } from './linear-animation'; | ||
import { presets } from './presets'; | ||
|
||
export type GlowHoverOptions = { | ||
hoverBg?: string; | ||
lightSize?: number; | ||
lightSizeEnterAnimationTime?: number; | ||
lightSizeLeaveAnimationTime?: number; | ||
isElementMovable?: boolean; | ||
customStaticBg?: string; | ||
enableBurst?: boolean; | ||
} & ( | ||
| { | ||
preset: keyof typeof presets; | ||
lightColor?: string; | ||
} | ||
| { | ||
preset?: undefined; | ||
lightColor: string; | ||
} | ||
); | ||
|
||
type Coords = { | ||
x: number; | ||
y: number; | ||
}; | ||
|
||
const BURST_TIME = 300; | ||
|
||
export function parseColor(colorToParse: string) { | ||
const div = document.createElement('div'); | ||
div.style.color = colorToParse; | ||
div.style.position = 'absolute'; | ||
div.style.display = 'none'; | ||
document.body.appendChild(div); | ||
const colorFromEl = getComputedStyle(div).color; | ||
document.body.removeChild(div); | ||
const parsedColor = colorFromEl.match(/^rgba?\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+)\s*)?\)$/i); | ||
if (parsedColor) { | ||
const alpha = typeof parsedColor[4] === 'undefined' ? 1 : parsedColor[4]; | ||
return [parsedColor[1], parsedColor[2], parsedColor[3], alpha]; | ||
} else { | ||
console.error(`Color ${colorToParse} could not be parsed.`); | ||
return [0, 0, 0, 0]; | ||
} | ||
} | ||
|
||
export const glowHoverEffect = (el: HTMLElement, { preset, ...options }: GlowHoverOptions) => { | ||
if (!el) { | ||
return () => {}; | ||
} | ||
|
||
const lightColor = options.lightColor ?? '#CEFF00'; | ||
const lightSize = options.lightSize ?? 130; | ||
const lightSizeEnterAnimationTime = options.lightSizeEnterAnimationTime ?? 100; | ||
const lightSizeLeaveAnimationTime = options.lightSizeLeaveAnimationTime ?? 50; | ||
const isElementMovable = options.isElementMovable ?? false; | ||
const customStaticBg = options.customStaticBg ?? null; | ||
|
||
const enableBurst = options.enableBurst ?? false; | ||
|
||
const getResolvedHoverBg = () => getComputedStyle(el).backgroundColor; | ||
|
||
let resolvedHoverBg = getResolvedHoverBg(); | ||
|
||
// default bg (if not defined) is rgba(0, 0, 0, 0) which is bugged in gradients in Safari | ||
// so we use transparent lightColor instead | ||
const parsedLightColor = parseColor(lightColor); | ||
const parsedLightColorRGBString = parsedLightColor.slice(0, 3).join(','); | ||
const resolvedGradientBg = `rgba(${parsedLightColorRGBString},0)`; | ||
|
||
let isMouseInside = false; | ||
let currentLightSize = 0; | ||
let blownSize = 0; | ||
let lightSizeEnterAnimationId: number | undefined = undefined; | ||
let lightSizeLeaveAnimationId: number | undefined = undefined; | ||
let blownSizeIncreaseAnimationId: number | undefined = undefined; | ||
let blownSizeDecreaseAnimationId: number | undefined = undefined; | ||
let lastMousePos: Coords; | ||
const defaultBox = el.getBoundingClientRect(); | ||
let lastElPos: Coords = { x: defaultBox.left, y: defaultBox.top }; | ||
|
||
const updateGlowEffect = () => { | ||
if (!lastMousePos) { | ||
return; | ||
} | ||
const gradientXPos = lastMousePos.x - lastElPos.x; | ||
const gradientYPos = lastMousePos.y - lastElPos.y; | ||
// we do not use transparent color here because of dirty gradient in Safari (more info: https://stackoverflow.com/questions/38391457/linear-gradient-to-transparent-bug-in-latest-safari) | ||
const gradient = `radial-gradient(circle at ${gradientXPos}px ${gradientYPos}px, ${lightColor} 0%, ${resolvedGradientBg} calc(${ | ||
blownSize * 2.5 | ||
}% + ${currentLightSize}px)) no-repeat`; | ||
|
||
// we duplicate resolvedHoverBg layer here because of transition "blinking" without it | ||
el.style.background = `${gradient} border-box border-box ${resolvedHoverBg}`; | ||
}; | ||
|
||
const updateEffectWithPosition = () => { | ||
if (isMouseInside) { | ||
const curBox = el.getBoundingClientRect(); | ||
lastElPos = { x: curBox.left, y: curBox.top }; | ||
updateGlowEffect(); | ||
} | ||
}; | ||
|
||
const onMouseEnter = (e: MouseEvent) => { | ||
resolvedHoverBg = getResolvedHoverBg(); | ||
lastMousePos = { x: e.clientX, y: e.clientY }; | ||
const curBox = el.getBoundingClientRect(); | ||
lastElPos = { x: curBox.left, y: curBox.top }; | ||
isMouseInside = true; | ||
if (lightSizeEnterAnimationId !== undefined) { | ||
window.cancelAnimationFrame(lightSizeEnterAnimationId); | ||
} | ||
if (lightSizeLeaveAnimationId !== undefined) { | ||
window.cancelAnimationFrame(lightSizeLeaveAnimationId); | ||
} | ||
|
||
// animate currentLightSize from 0 to lightSize | ||
linearAnimation({ | ||
onProgress: (progress) => { | ||
currentLightSize = lightSize * progress; | ||
updateGlowEffect(); | ||
}, | ||
time: lightSizeEnterAnimationTime, | ||
initialProgress: currentLightSize / lightSize, | ||
onIdUpdate: (newId) => (lightSizeEnterAnimationId = newId), | ||
}); | ||
}; | ||
|
||
const onMouseMove = (e: MouseEvent) => { | ||
lastMousePos = { x: e.clientX, y: e.clientY }; | ||
if (isElementMovable) { | ||
updateEffectWithPosition(); | ||
} else { | ||
updateGlowEffect(); | ||
} | ||
}; | ||
|
||
const onMouseLeave = () => { | ||
isMouseInside = false; | ||
if (lightSizeEnterAnimationId !== undefined) { | ||
window.cancelAnimationFrame(lightSizeEnterAnimationId); | ||
} | ||
if (lightSizeLeaveAnimationId !== undefined) { | ||
window.cancelAnimationFrame(lightSizeLeaveAnimationId); | ||
} | ||
if (blownSizeIncreaseAnimationId !== undefined) { | ||
window.cancelAnimationFrame(blownSizeIncreaseAnimationId); | ||
} | ||
if (blownSizeDecreaseAnimationId !== undefined) { | ||
window.cancelAnimationFrame(blownSizeDecreaseAnimationId); | ||
} | ||
|
||
// animate currentLightSize from lightSize to 0 | ||
linearAnimation({ | ||
onProgress: (progress) => { | ||
currentLightSize = lightSize * (1 - progress); | ||
blownSize = Math.min(blownSize, (1 - progress) * 100); | ||
|
||
if (progress < 1) { | ||
updateGlowEffect(); | ||
} else { | ||
el.style.background = customStaticBg ? customStaticBg : ''; | ||
} | ||
}, | ||
time: lightSizeLeaveAnimationTime, | ||
initialProgress: 1 - currentLightSize / lightSize, | ||
onIdUpdate: (newId) => (lightSizeLeaveAnimationId = newId), | ||
}); | ||
}; | ||
|
||
const onMouseDown = (e: MouseEvent) => { | ||
lastMousePos = { x: e.clientX, y: e.clientY }; | ||
const curBox = el.getBoundingClientRect(); | ||
lastElPos = { x: curBox.left, y: curBox.top }; | ||
if (blownSizeIncreaseAnimationId !== undefined) { | ||
window.cancelAnimationFrame(blownSizeIncreaseAnimationId); | ||
} | ||
if (blownSizeDecreaseAnimationId !== undefined) { | ||
window.cancelAnimationFrame(blownSizeDecreaseAnimationId); | ||
} | ||
|
||
// animate blownSize from 0 to 100 | ||
linearAnimation({ | ||
onProgress: (progress) => { | ||
blownSize = 100 * progress; | ||
updateGlowEffect(); | ||
}, | ||
time: BURST_TIME, | ||
initialProgress: blownSize / 100, | ||
onIdUpdate: (newId) => (blownSizeIncreaseAnimationId = newId), | ||
}); | ||
}; | ||
|
||
const onMouseUp = (e: MouseEvent) => { | ||
lastMousePos = { x: e.clientX, y: e.clientY }; | ||
const curBox = el.getBoundingClientRect(); | ||
lastElPos = { x: curBox.left, y: curBox.top }; | ||
if (blownSizeIncreaseAnimationId !== undefined) { | ||
window.cancelAnimationFrame(blownSizeIncreaseAnimationId); | ||
} | ||
if (blownSizeDecreaseAnimationId !== undefined) { | ||
window.cancelAnimationFrame(blownSizeDecreaseAnimationId); | ||
} | ||
|
||
// animate blownSize from 100 to 0 | ||
linearAnimation({ | ||
onProgress: (progress) => { | ||
blownSize = (1 - progress) * 100; | ||
updateGlowEffect(); | ||
}, | ||
time: BURST_TIME, | ||
initialProgress: 1 - blownSize / 100, | ||
onIdUpdate: (newId) => (blownSizeDecreaseAnimationId = newId), | ||
}); | ||
}; | ||
|
||
document.addEventListener('scroll', updateEffectWithPosition); | ||
window.addEventListener('resize', updateEffectWithPosition); | ||
el.addEventListener('mouseenter', onMouseEnter); | ||
el.addEventListener('mousemove', onMouseMove); | ||
el.addEventListener('mouseleave', onMouseLeave); | ||
if (enableBurst) { | ||
el.addEventListener('mousedown', onMouseDown); | ||
el.addEventListener('mouseup', onMouseUp); | ||
} | ||
|
||
let resizeObserver: ResizeObserver; | ||
if (window.ResizeObserver) { | ||
resizeObserver = new ResizeObserver(updateEffectWithPosition); | ||
resizeObserver.observe(el); | ||
} | ||
|
||
return () => { | ||
if (lightSizeEnterAnimationId !== undefined) { | ||
window.cancelAnimationFrame(lightSizeEnterAnimationId); | ||
} | ||
if (lightSizeLeaveAnimationId !== undefined) { | ||
window.cancelAnimationFrame(lightSizeLeaveAnimationId); | ||
} | ||
if (blownSizeIncreaseAnimationId !== undefined) { | ||
window.cancelAnimationFrame(blownSizeIncreaseAnimationId); | ||
} | ||
if (blownSizeDecreaseAnimationId !== undefined) { | ||
window.cancelAnimationFrame(blownSizeDecreaseAnimationId); | ||
} | ||
|
||
document.removeEventListener('scroll', updateEffectWithPosition); | ||
window.removeEventListener('resize', updateEffectWithPosition); | ||
el.removeEventListener('mouseenter', onMouseEnter); | ||
el.removeEventListener('mousemove', onMouseMove); | ||
el.removeEventListener('mouseleave', onMouseLeave); | ||
if (enableBurst) { | ||
el.removeEventListener('mousedown', onMouseDown); | ||
el.removeEventListener('mouseup', onMouseUp); | ||
} | ||
|
||
if (resizeObserver) { | ||
resizeObserver.unobserve(el); | ||
resizeObserver.disconnect(); | ||
} | ||
}; | ||
}; |
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,4 @@ | ||
export { glowHoverEffect } from './glow-hover-effect'; | ||
export type { GlowHoverOptions } from './glow-hover-effect'; | ||
export { useGlowHover } from './use-glow-hover'; | ||
export type { GlowHoverHookOptions } from './use-glow-hover'; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Refactor animation ID management.