Typing animation with markdown
Hi! I am trying to test my Nuxt skills in more complex scenario. I want to create typing animation - it sounds simple, but I want to support markdown and I want to do that without sending no markdown related libraries to the client! I managed to implement component like this:
And In parent i am trying to pass rendered markdown and refresh it:
Problem is that I am using server components and I have to wait until it finishes rendering, sending to client and then run animation. But I have no idea how to do that,
nextTick
won't work...
I really don't want to use markdown-it
, because I managed to use mdc (from nuxt content) everywhere, so I don't want to add it only for this simple thing4 Replies
Hi @Norbiros. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
I understand you're trying to create a typing animation that supports markdown content rendered on the server side. This is an interesting challenge that involves coordinating server-rendered content with client-side animations.
The Challenge
Your approach using a server component (
MarkdownContent
) inside your AnimatedText
component creates a timing issue. The server component renders on the server, but your animation needs to run on the client after the content is fully available.
Solution Options
1. Use onMounted with nextTick
When using server components, you need to ensure your client-side code runs after the component is mounted and the DOM is updated. For .client
components, you typically need to add await nextTick()
in the onMounted()
hook:
However, as you mentioned, this might not be sufficient when working with server components.
2. Use a Watcher for Content Changes
You could watch for changes in the rendered content:
3. Use Nuxt Content's Binding Capabilities
Since you're already using Nuxt Content, you could leverage its data binding capabilities to know when content is ready:
The ContentRenderer
component from Nuxt Content can help you render markdown and know when it's done as shown in the documentation.
4. Server Component with Client Component Pairing
Nuxt supports pairing server and client components. You could create:
The server component would render the markdown, and the client component would handle the animation as described in the documentation.
Implementation Example
Here's a potential approach using Nuxt Content's ContentRenderer
:
If you want to avoid sending markdown libraries to the client, the server component approach is ideal. You can enable this with:
Then create a server component for markdown rendering as shown in this Stack Overflow example.
Remember that server components must have a single root element and props are passed via URL parameters, so be careful with large content as noted in the documentation.
Would you like me to elaborate on any specific part of this solution?
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@770620808644919307> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
I don't use @nuxt/content, only mdc. No options for waiting to server compoennt to refresh work
@kapa.ai
Let me check the knowledge base for you again. One moment! :books:
Sorry @Norbiros, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?