Components
This code implements a visually appealing dark mode toggle using React, Framer Motion, and React Icons. It solves the common problem of providing users with a simple and attractive way to switch between light and dark themes. The toggle consists of two buttons, one for 'light' and one for 'dark' mode, each associated with an icon (moon and sun, respectively). The background color of the main container dynamically changes based on the selected mode.
Here's a step-by-step breakdown:
useState hook from React manages the current theme ('light' or 'dark').Example component) changes between white and dark slate. The text color of the toggle buttons also adjusts for contrast.SliderToggle component updates the theme state when clicked.motion.span creates an animated slider that smoothly transitions between the left and right sides of the toggle buttons, providing visual feedback of the selected mode. The animation uses spring physics for a natural feel. The layout prop ensures that the animation respects the element's layout.TOGGLE_CLASSES) provides styling for the buttons and main container. Utility-first CSS classes are used for layout and styling.This component can be easily integrated into any React application to offer a user-friendly dark mode toggle. The use of icons and smooth animation enhances the user experience. For example, this could be integrated into a blog website, a dashboard, or any web application where users want to customize the theme.
Key technologies used include:
Loading preview...
import Example from "@/components/ui/animated-dark-mode-toggle";
export default function DemoOne() {
return <Example />;
}