MagicMotion is a powerful and flexible code animation library that allows developers to easily create dynamic code animations. Whether you're building educational tools, showcasing code snippets, or creating interactive tutorials, MagicMotion makes it easy to animate code transitions with smooth and customizable effects.
Example on Stackblitz: https://stackblitz.com/edit/vitejs-vite-pgb6rr?file=src%2FApp.tsx
You can install MagicMotion via npm or yarn:
npm install magic-motion
Or with yarn:
yarn add magic-motion
To use MagicMotion, simply import the library and pass the initial content to the MagicMotion
component.
On button click lets assign handler which will update the value of content which we want animate to. Component
animates whenever the value of animateTo
is changing.
Note: To prevent undesired behaviour, update the value of
animateTo
only when animation is finished. You could useonAnimationFinished
callback to verify when animation is finished.
import { MagicMotion } from 'magic-motion';
const App = () => {
const [animateTo, setAnimateTo] = useState();
const buttonHandler = () => {
setAnimateTo('Hello my friends');
};
return (
<>
<MagicMotion
initialContent="Hello World!!!"
animateTo={animateTo}
/>
<button onClick={buttonHandler}>Animate</button>
</>
);
};
export default App;
MagicMotion
useses prismjs underhood for code highlighting, so you could use any theme provided by prismjs
. Here is example list of themes which could be used.
So lets install these themes.
npm install prism-themes
Or with yarn:
yarn add prism-themes
Import styles.
import { MagicMotion } from 'magic-motion';
import 'prism-themes/themes/prism-one-dark.css';
const App = () => {
const [animateTo, setAnimateTo] = useState();
const buttonHandler = () => {
setAnimateTo('const sum = (a, b) => a + b;');
};
return (
<>
<MagicMotion
initialContent="const value = 5;"
animateTo={animateTo}
codeHighlight={{
languageName: 'javascript',
}}
/>
<button onClick={buttonHandler}>Animate</button>
</>
);
};
export default App;
Don't forget to define the languageName
(e.g. javascript, css, html).
The MagicMotionConfig
interface provides a range of options to customize your code animations. Here's a breakdown of each property:
Property | Type | Description |
---|---|---|
initialContent |
string , required |
The initial code content to be displayed. |
animateTo |
string , optional |
The target code content to animate to. If not provided, only the initial content will be displayed. |
duration |
very slow , slow , normal , fast , very fast , number , optional |
The duration of the animation. Specify the duration in seconds or milliseconds. |
variant |
move later , move instantly , optional |
The animation variant or style. Customize the transition effect between the initial and target content. |
styles |
any , optional |
Custom CSS styles to apply to the animated code content. |
fontSize |
number , optional |
Set the font size of the code content. |
codeHighlight |
CodeHighlight , optional |
Define syntax highlighting options for the code content. |
children |
JSX.Element , optional |
Pass additional JSX elements to be rendered alongside the animated content. |
onAnimationFinished |
function , optional |
A callback function triggered when the animation finishes. |
onAnimationStart |
function , optional |
A callback function triggered when the animation starts. |
Contributions are welcome! Please feel free to submit a Pull Request or open an issue if you have any ideas, improvements, or bugs to report.
MagicMotion is licensed under the MIT License. See the LICENSE file for more information.
This README provides a comprehensive overview of your library, including installation, usage, configuration options, and examples. You can further enhance it by adding more detailed documentation, links to live demos, or badges for things like build status or npm version.