Created
July 10, 2024 12:55
-
-
Save spraddles/a29d6d8f82399fd99850e1bab096d990 to your computer and use it in GitHub Desktop.
Excalidraw on Nuxt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// plugins/reactExacalidraw.js | |
import { createElement } from 'react' | |
import { createRoot } from 'react-dom/client' | |
import { Excalidraw } from '@excalidraw/excalidraw' | |
export default defineNuxtPlugin((nuxtApp) => { | |
nuxtApp.provide('excalidraw', { createElement, createRoot, Excalidraw }) | |
}) | |
// component.vue or page.vue | |
<template> | |
<ClientOnly> | |
<div ref="reactRef"></div> | |
</ClientOnly> | |
</template> | |
<script setup> | |
const reactRef = ref(null) | |
const mountExcalidraw = () => { | |
const { $excalidraw } = useNuxtApp() | |
if ($excalidraw) { | |
const { createElement, createRoot, Excalidraw } = $excalidraw | |
requestAnimationFrame(() => { | |
const el = reactRef.value | |
const root = createRoot(el) | |
root.render(createElement(Excalidraw, { | |
// example callback | |
onChange: (elements, appState, files) => onChangeHandler(elements, appState, files) | |
}, null)) | |
}) | |
} | |
} | |
const onChangeHandler = (elements, appState, files) => { | |
console.log('appState: ', appState) | |
} | |
onMounted(async () => { | |
mountExcalidraw() | |
}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment