From 0d511d32de4756f661e2dcdd04cf0ffee116faff Mon Sep 17 00:00:00 2001 From: Viren070 Date: Mon, 28 Apr 2025 22:27:10 +0100 Subject: [PATCH] fix(frontend): fix switch animation for formatter preview --- .../components/FormatterPreview.module.css | 8 +++- .../src/components/FormatterPreview.tsx | 37 ++++++++++++++++--- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/packages/frontend/src/components/FormatterPreview.module.css b/packages/frontend/src/components/FormatterPreview.module.css index 8954023a..f7ada576 100644 --- a/packages/frontend/src/components/FormatterPreview.module.css +++ b/packages/frontend/src/components/FormatterPreview.module.css @@ -225,6 +225,12 @@ input.toggleInput { border-radius: 50%; background: #aaa; transition: 0.3s all ease; + transform: translateX(0); +} + +/* Force animation during state changes */ +.toggleSwitch.animating::after { + transition: 0.3s transform ease; } .toggleInput:checked + .toggleSwitch { @@ -233,7 +239,7 @@ input.toggleInput { } .toggleInput:checked + .toggleSwitch::after { - left: calc(100% - 15px); + transform: translateX(18px); background-color: #ffffff; box-shadow: 0 0 12px rgba(255, 255, 255, 0.7); } diff --git a/packages/frontend/src/components/FormatterPreview.tsx b/packages/frontend/src/components/FormatterPreview.tsx index a9e2fb12..bdc991dd 100644 --- a/packages/frontend/src/components/FormatterPreview.tsx +++ b/packages/frontend/src/components/FormatterPreview.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState, useEffect } from 'react'; import styles from './FormatterPreview.module.css'; import { ParsedStream } from '@aiostreams/types'; import { @@ -81,7 +81,7 @@ const FormatterPreview: React.FC = ({ formatter }) => { setFilename(DEFAULT_FILENAME); }; - // Toggle switch component + // Toggle switch component with animation fix const ToggleSwitch = ({ label, isChecked, @@ -91,6 +91,31 @@ const FormatterPreview: React.FC = ({ formatter }) => { isChecked: boolean; setChecked: (checked: boolean) => void; }) => { + const [visualState, setVisualState] = useState(isChecked); + const [isAnimating, setIsAnimating] = useState(false); + + // Sync visual state with actual state + useEffect(() => { + if (!isAnimating) { + setVisualState(isChecked); + } + }, [isChecked, isAnimating]); + + const handleChange = (e: React.ChangeEvent) => { + const newState = e.target.checked; + setIsAnimating(true); + setVisualState(newState); + + // Allow animation to play before updating the actual state + setTimeout(() => { + setChecked(newState); + // Reset animating state after animation completes + setTimeout(() => { + setIsAnimating(false); + }, 50); + }, 250); // Slightly shorter than the CSS transition duration + }; + return (