react-markdown-typewriter 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  This library provides a new component, `MarkdownTypewriter`, that combines the Markdown component of [react-markdown](https://www.npmjs.com/package/react-markdown) with the animation of typewriter. The animation was created entirely with [motion](https://www.npmjs.com/package/motion).
4
4
 
5
+ Live demo: <https://codesandbox.io/p/sandbox/react-markdown-typewriter-rgjf6t>
6
+
5
7
  ## Why?
6
8
 
7
9
  This library was born during the development of my game engine [pixi-vn](https://www.npmjs.com/package/@drincs/pixi-vn). I needed a component that would display the current dialogue of a character with the "Typewriter" effect and I also wanted to give the developer the possibility to use Markdown to add style to the text.
@@ -23,7 +25,7 @@ The component accepts all the props of the `react-markdown` component and adds s
23
25
  This is a very simple example of how to use the component:
24
26
 
25
27
  ```tsx
26
- import MarkdownTypewriter from "../components/Typewriter";
28
+ import { MarkdownTypewriter } from "react-markdown-typewriter";
27
29
 
28
30
  export default function NarrationScreen() {
29
31
  return (
@@ -40,25 +42,34 @@ This is a more complex example:
40
42
  import { useRef } from "react";
41
43
  import rehypeRaw from "rehype-raw";
42
44
  import remarkGfm from "remark-gfm";
43
- import MarkdownTypewriter from "../components/Typewriter";
45
+ import { MarkdownTypewriter } from "react-markdown-typewriter";
44
46
 
45
47
  export default function NarrationScreen() {
46
48
  const paragraphRef = useRef<HTMLDivElement>(null);
49
+ const scrollToEnd = useCallback((ref: { current: HTMLSpanElement | null }) => {
50
+ if (paragraphRef.current && ref.current) {
51
+ let scrollTop = ref.current.offsetTop - paragraphRef.current.clientHeight / 2;
52
+ paragraphRef.current.scrollTo({
53
+ top: scrollTop,
54
+ behavior: "auto",
55
+ });
56
+ }
57
+ }, []);
47
58
  return (
48
59
  <div ref={paragraphRef}>
49
60
  <MarkdownTypewriter
50
61
  remarkPlugins={[remarkGfm]}
51
62
  rehypePlugins={[rehypeRaw]}
52
63
  delay={20}
53
- scrollRef={paragraphRef}
54
64
  motionProps={{
55
65
  onAnimationComplete: () => {
56
66
  console.log("Typewriter finished");
57
67
  },
58
- letterVariants: {
68
+ characterVariants: {
59
69
  hidden: { opacity: 0 },
60
70
  visible: { opacity: 1, transition: { opacity: { duration: 0 } } },
61
71
  },
72
+ onCharacterAnimationComplete: scrollToEnd,
62
73
  }}
63
74
  >
64
75
  Hello World
@@ -75,7 +86,7 @@ export default function NarrationScreen() {
75
86
  In addition to the `react-markdown` component props, the component accepts the following props:
76
87
 
77
88
  * `delay`: The delay in milliseconds between the appearance of one letter and the next. Default: `10`. (Optional)
78
- * `scrollRef`: The reference to the element that will be scrolled when the text exceeds the height of the container. (Optional)
79
89
  * `motionProps` (Optional):
80
90
  * The props to pass to the [motion span](https://motion.dev/docs/react-motion-component).
81
- * `letterVariants`: The motion variants for each individual letter. Default: `{ hidden: { opacity: 0 }, visible: { opacity: 1, transition: { opacity: { duration: 0 } } } }` (Optional).
91
+ * `characterVariants`: The motion variants for each individual letter. Default: `{ hidden: { opacity: 0 }, visible: { opacity: 1, transition: { opacity: { duration: 0 } } } }` (Optional).
92
+ * `onCharacterAnimationComplete`: A callback that is called when the animation of a letter is complete. The callback is called with the reference to the letter. (Optional)