Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React example not working #32

Open
muratcankaracabey opened this issue Nov 17, 2024 · 1 comment
Open

React example not working #32

muratcankaracabey opened this issue Nov 17, 2024 · 1 comment
Labels
help wanted Extra attention is needed react

Comments

@muratcankaracabey
Copy link

I could not make the react animation work

import React, { useState, useEffect } from "react";
import { createHighlighter, type HighlighterCore } from "shiki";
import { ShikiMagicMove } from "shiki-magic-move/react";

const CodePage: React.FC = () => {
  const [code, setCode] = useState(
    `function greet() {
    console.log('Hello, World!');
  }
  greet();`
  );
  const [highlighter, setHighlighter] = useState<HighlighterCore>();

  useEffect(() => {
    async function initializeHighlighter() {
      const highlighter = await createHighlighter({
        themes: ["nord"],
        langs: ["javascript", "typescript"],
      });
      setHighlighter(highlighter);
    }
    initializeHighlighter();
  }, []);

  function animate() {
    setCode(`const name = 'Alice';

    function greet() {
      console.log('Hello, ' + name + '!');
    }
  
    greet();`);
  }

  return (
    <div>
      {highlighter && (
        <>
          <ShikiMagicMove
            lang="ts"
            theme="nord"
            highlighter={highlighter}
            code={code}
            options={{ duration: 750, stagger: 7, lineNumbers: true }}
          />
          <button onClick={animate}>Animate</button>
        </>
      )}
    </div>
  );
};

export default CodePage;

Probably I am missing something because I just copied the example from readme.
It is not as smooth as it is seen at https://shiki-magic-move.netlify.app/

For quickly trying you can use this codesandbox: https://codesandbox.io/p/sandbox/yk9dxt?file=%2Fsrc%2FApp.tsx%3A29%2C22

@antfu antfu added help wanted Extra attention is needed react labels Nov 18, 2024
@wood3n
Copy link

wood3n commented Nov 26, 2024

@muratcankaracabey you should import css: import "shiki-magic-move/dist/style.css";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed react
Projects
None yet
Development

No branches or pull requests

3 participants