Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
feat(ui): add animated Lucide navigation icon primitive
thinhlpgb18ef2799efd
diff --git a/web-app/src/components/animated-icon/__tests__/animated-lucide.test.tsx b/web-app/src/components/animated-icon/__tests__/animated-lucide.test.tsxnew file mode 100644index 000000000..31f4dcf43--- /dev/null+++ b/web-app/src/components/animated-icon/__tests__/animated-lucide.test.tsx@@ -0,0 +1,81 @@+import '@testing-library/jest-dom/vitest'+import { createRef, type HTMLAttributes, type ReactNode } from 'react'+import { act, render, screen } from '@testing-library/react'+import { beforeEach, describe, expect, it, vi } from 'vitest'+import type { LucideIcon } from 'lucide-react'++const motionState = vi.hoisted(() => ({+ reduced: false,+ start: vi.fn(),+}))++vi.mock('motion/react', () => ({+ motion: {+ div: (+ props: HTMLAttributes<HTMLDivElement> & {+ animate?: unknown+ variants?: unknown+ children?: ReactNode+ }+ ) => {+ const { animate, variants, children, ...rest } = props+ void animate+ void variants+ return <div {...rest}>{children}</div>+ },+ },+ useAnimation: () => ({ start: motionState.start }),+ useReducedMotion: () => motionState.reduced,+}))++import {+ AnimatedLucideIcon,+ type AnimatedLucideIconHandle,+} from '../animated-lucide'++const MockIcon = (({ size }: { size?: number }) => (+ <svg aria-label="mock icon" height={size} width={size} />+)) as LucideIcon++describe('AnimatedLucideIcon', () => {+ beforeEach(() => {+ motionState.reduced = false+ motionState.start.mockReset()+ })++ it('renders the supplied icon and exposes start/stop controls', () => {+ const ref = createRef<AnimatedLucideIconHandle>()++ render(+ <AnimatedLucideIcon+ ref={ref}+ icon={MockIcon}+ preset="box"+ size={16}+ data-testid="icon-wrapper"+ />+ )++ expect(screen.getByTestId('icon-wrapper')).toContainElement(+ screen.getByLabelText('mock icon')+ )+ expect(screen.getByLabelText('mock icon')).toHaveAttribute('width', '16')++ act(() => ref.current?.startAnimation())+ act(() => ref.current?.stopAnimation())++ expect(motionState.start).toHaveBeenNthCalledWith(1, 'animate')+ expect(motionState.start).toHaveBeenNthCalledWith(2, 'normal')+ })++ it('does not start transforms when reduced motion is enabled', () => {+ motionState.reduced = true+ const ref = createRef<AnimatedLucideIconHandle>()++ render(<AnimatedLucideIcon ref={ref} icon={MockIcon} preset="home" />)++ act(() => ref.current?.startAnimation())++ expect(motionState.start).not.toHaveBeenCalledWith('animate')+ })+})diff --git a/web-app/src/components/animated-icon/animated-lucide.tsx b/web-app/src/components/animated-icon/animated-lucide.tsxnew file mode 100644index 000000000..9183b7d11--- /dev/null+++ b/web-app/src/components/animated-icon/animated-lucide.tsx@@ -0,0 +1,125 @@+'use client'++import type { LucideIcon } from 'lucide-react'+import type { Variants } from 'motion/react'+import { motion, useAnimation, useReducedMotion } from 'motion/react'+import type { HTMLAttributes, MouseEvent } from 'react'+import { forwardRef, useCallback, useImperativeHandle, useRef } from 'react'++import { cn } from '@/lib/utils'++export type AnimatedLucidePreset = 'home' | 'handshake' | 'box' | 'sliders'++export interface AnimatedLucideIconHandle {+ startAnimation: () => void+ stopAnimation: () => void+}++interface AnimatedLucideIconProps extends HTMLAttributes<HTMLDivElement> {+ icon: LucideIcon+ preset: AnimatedLucidePreset+ size?: number+}++const PRESETS: Record<AnimatedLucidePreset, Variants> = {+ home: {+ normal: { rotate: 0, y: 0 },+ animate: {+ rotate: [0, -5, 0],+ y: [0, -1, 0],+ transition: { duration: 0.45, ease: 'easeInOut' },+ },+ },+ handshake: {+ normal: { rotate: 0 },+ animate: {+ rotate: [0, -5, 5, 0],+ transition: { duration: 0.5, ease: 'easeInOut' },+ },+ },+ box: {+ normal: { rotate: 0 },+ animate: {+ rotate: [0, -7, 0],+ transition: { duration: 0.45, ease: 'easeInOut' },+ },+ },+ sliders: {+ normal: { x: 0 },+ animate: {+ x: [0, -1, 1, 0],+ transition: { duration: 0.45, ease: 'easeInOut' },+ },+ },+}++export const AnimatedLucideIcon = forwardRef<+ AnimatedLucideIconHandle,+ AnimatedLucideIconProps+>(function AnimatedLucideIcon(+ {+ icon: Icon,+ preset,+ onMouseEnter,+ onMouseLeave,+ className,+ size = 28,+ ...props+ },+ ref+) {+ const controls = useAnimation()+ const reducedMotion = useReducedMotion()+ const isControlledRef = useRef(false)++ useImperativeHandle(ref, () => {+ isControlledRef.current = true++ return {+ startAnimation: () => {+ if (!reducedMotion) void controls.start('animate')+ },+ stopAnimation: () => {+ void controls.start('normal')+ },+ }+ }, [controls, reducedMotion])++ const handleMouseEnter = useCallback(+ (event: MouseEvent<HTMLDivElement>) => {+ if (isControlledRef.current) {+ onMouseEnter?.(event)+ } else if (!reducedMotion) {+ void controls.start('animate')+ }+ },+ [controls, onMouseEnter, reducedMotion]+ )++ const handleMouseLeave = useCallback(+ (event: MouseEvent<HTMLDivElement>) => {+ if (isControlledRef.current) {+ onMouseLeave?.(event)+ } else {+ void controls.start('normal')+ }+ },+ [controls, onMouseLeave]+ )++ return (+ <div+ {...props}+ className={cn('inline-flex shrink-0', className)}+ onMouseEnter={handleMouseEnter}+ onMouseLeave={handleMouseLeave}+ >+ <motion.div+ animate={controls}+ variants={PRESETS[preset]}+ >+ <Icon aria-hidden="true" size={size} />+ </motion.div>+ </div>+ )+})