fixed color change effect

This commit is contained in:
2023-10-09 10:11:31 -05:00
parent cb4aae9ebf
commit f517189633
3 changed files with 15 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
'use client' 'use client'
import { FC } from "react"; import type { FC } from "react";
import useColorShift, { UseColorShiftReturnType, type ColorListType } from "../../hooks/useColorShift"; import useColorShift, { UseColorShiftReturnType, type ColorListType } from "../../hooks/useColorShift";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
export { default as useColorShift } from "../../hooks/useColorShift"; export { default as useColorShift } from "../../hooks/useColorShift";
@@ -50,15 +50,15 @@ export const InlineLogo: FC<LogoProps> = ({ shiftInterval, customColorList, cust
if (customHookInstance) return ( if (customHookInstance) return (
<button onClick={() => router.push('/')} id="inline-logo-container" className="flex w-auto h-auto justify-center"> <button onClick={() => router.push('/')} id="inline-logo-container" className="flex w-auto h-auto justify-center">
<div className={`flex flex-col items-center justify-center h-16 w-16 bg-opacity-75 ${customHookInstance.firstColor} transition-colors duration-[5000ms] rounded-full`}> <div className={`flex flex-col items-center justify-center h-16 w-16 bg-opacity-75 ${customHookInstance.firstColor} transition-colors duration-1000 rounded-full`}>
<p className="text-black dark:text-white text-2xl font-bold opacity-100">M</p> <p className="text-black dark:text-white text-2xl font-bold opacity-100">M</p>
</div> </div>
<div className={`flex flex-col -ml-3 items-center justify-center h-16 w-16 bg-opacity-75 ${customHookInstance.secondColor} transition-colors duration-[5000ms] rounded-full`}> <div className={`flex flex-col -ml-3 items-center justify-center h-16 w-16 bg-opacity-75 ${customHookInstance.secondColor} transition-colors duration-1000 rounded-full`}>
<p className="text-black dark:text-white text-2xl font-bold opacity-100">C</p> <p className="text-black dark:text-white text-2xl font-bold opacity-100">C</p>
</div> </div>
<div className={`flex flex-col -ml-3 items-center justify-center h-16 w-16 bg-opacity-75 ${customHookInstance.thirdColor} transition-colors duration-[5000ms] rounded-full`}> <div className={`flex flex-col -ml-3 items-center justify-center h-16 w-16 bg-opacity-75 ${customHookInstance.thirdColor} transition-colors duration-1000 rounded-full`}>
<p className="text-black dark:text-white text-2xl font-bold opacity-100">D</p> <p className="text-black dark:text-white text-2xl font-bold opacity-100">D</p>
</div> </div>
</button> </button>

View File

@@ -1,4 +1,5 @@
import { useEffect, useState } from "react"; 'use client';
import { useCallback, useEffect, useState } from "react";
import { DefaultColors } from "tailwindcss/types/generated/colors"; import { DefaultColors } from "tailwindcss/types/generated/colors";
export type ColorListType = (`bg-${keyof DefaultColors}` | `bg-${keyof DefaultColors}-${string}` | ""); export type ColorListType = (`bg-${keyof DefaultColors}` | `bg-${keyof DefaultColors}-${string}` | "");
@@ -24,13 +25,13 @@ export interface UseColorShiftReturnType {
const useColorShift = (shiftInterval?: number, disableShift = false, customColorList?: ColorListType[]): UseColorShiftReturnType => { const useColorShift = (shiftInterval?: number, disableShift = false, customColorList?: ColorListType[]): UseColorShiftReturnType => {
if (shiftInterval && shiftInterval <= 0) throw new Error("shiftInterval must be greater than 0") if (shiftInterval && shiftInterval <= 0) throw new Error("shiftInterval must be greater than 0")
const randomColor = () => { const randomColor = useCallback(() => {
const list = customColorList ?? colorList; const list = customColorList ?? colorList;
const color = list[Math.floor(Math.random() * list.length || 0)]; const color = list[Math.floor(Math.random() * list.length || 0)];
if (!color) throw new Error("color is undefined"); if (!color) throw new Error("color is undefined");
return color; return color;
} }, [customColorList]);
const [circleColors, setCircleColors] = useState<{ const [circleColors, setCircleColors] = useState<{
firstColor: ColorListType | "", firstColor: ColorListType | "",
@@ -42,20 +43,20 @@ const useColorShift = (shiftInterval?: number, disableShift = false, customColor
thirdColor: colorList[2] ?? randomColor(), thirdColor: colorList[2] ?? randomColor(),
}) })
function shift() { const shift = useCallback(() => {
const firstColor = randomColor(); const firstColor = randomColor();
const secondColor = randomColor(); const secondColor = randomColor();
const thirdColor = randomColor(); const thirdColor = randomColor();
setCircleColors({ firstColor, secondColor, thirdColor }); setCircleColors({ firstColor, secondColor, thirdColor });
return { firstColor, secondColor, thirdColor }; return { firstColor, secondColor, thirdColor };
} }, [randomColor]);
// perform initial mount of color changing pattern // perform initial mount of color changing pattern
useEffect(() => { useEffect(() => {
if (!disableShift) return; if (!disableShift) return;
shift(); shift();
}, []); }, [disableShift, shift]);
// set this function to repeat // set this function to repeat
useEffect(() => { useEffect(() => {
@@ -64,7 +65,7 @@ const useColorShift = (shiftInterval?: number, disableShift = false, customColor
}, shiftInterval ?? 3000); }, shiftInterval ?? 3000);
return () => clearInterval(interval); return () => clearInterval(interval);
}, []) }, [shift, shiftInterval])
return { ...circleColors, shift, shiftInterval }; return { ...circleColors, shift, shiftInterval };
} }

View File

@@ -11,6 +11,9 @@ module.exports = {
}, },
{ {
pattern: /to-.*/, pattern: /to-.*/,
},
{
pattern: /bg-.*/,
} }
], ],
theme: { theme: {