import { forwardRef, useEffect, useImperativeHandle, useState } from "react"
import { motion, useReducedMotion } from "framer-motion"
export interface SearchIconHandle {
play: () => void
stop: () => void
reset: () => void
}
export interface SearchIconProps {
size?: number
color?: string
strokeWidth?: number
playOnHover?: boolean
playOnMount?: boolean
className?: string
}
export const SearchIcon = forwardRef<SearchIconHandle, SearchIconProps>(
function SearchIcon(
{
size = 24,
color = "currentColor",
strokeWidth = 2,
playOnHover = true,
playOnMount = false,
className,
},
ref,
) {
const reduced = useReducedMotion()
const [runId, setRunId] = useState(playOnMount ? 1 : 0)
const active = runId > 0
useEffect(() => {
if (playOnMount && !reduced) setRunId((n) => n + 1)
}, [playOnMount, reduced])
useImperativeHandle(ref, () => ({
play: () => setRunId((n) => n + 1),
stop: () => setRunId(0),
reset: () => setRunId(0),
}))
return (
<svg
key={runId}
xmlns="http://www.w3.org/2000/svg"
width={size}
height={size}
viewBox="0 0 24 24"
overflow="visible"
fill="none"
stroke={color}
strokeWidth={strokeWidth}
strokeLinecap="round"
strokeLinejoin="round"
className={className}
style={{ overflow: "visible" }}
onPointerEnter={playOnHover ? () => setRunId((n) => n + 1) : undefined}
>
<motion.g
style={{ transformOrigin: "43.75% 43.75%", transformBox: "view-box" }}
animate={active && !reduced ? {"scale":[1,0.96,1.04,1]} : undefined}
transition={{"duration":0.34650000000000003,"ease":[0.34,1.45,0.64,1]}}
>
<circle cx={10.5} cy={10.5} r={6.5} />
</motion.g>
<motion.g
style={{ transformOrigin: "43.75% 43.75%", transformBox: "view-box" }}
animate={active && !reduced ? {"rotate":[0,-22,28,-6,0]} : undefined}
transition={{"duration":0.396,"ease":[0.34,1.45,0.64,1]}}
>
<line x1={15.2} y1={15.2} x2={20.5} y2={20.5} />
</motion.g>
<motion.g
style={{ transformOrigin: "43.75% 43.75%", transformBox: "view-box" }}
animate={active && !reduced ? {"x":[0,0.4,-0.9,0.15,0],"y":[0,0.4,-0.9,0.15,0],"scale":[1,0.94,1.22,0.99,1]} : undefined}
transition={{"duration":0.34650000000000003,"ease":[0.34,1.45,0.64,1]}}
>
<path d="M8 8.5a3 3 0 0 1 4.2-.4" />
</motion.g>
</svg>
)
},
)