Skip to content

Commit

Permalink
annotate multiple example
Browse files Browse the repository at this point in the history
  • Loading branch information
Taha-Hassan-Git committed Jan 9, 2024
1 parent 25328f9 commit e952a31
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions apps/examples/src/examples/multiple/MultipleExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { Tldraw } from '@tldraw/tldraw'
import '@tldraw/tldraw/tldraw.css'
import { createContext, useCallback, useContext, useState } from 'react'

// There's a guide at the bottom of this page!

//[1]
const focusedEditorContext = createContext(
{} as {
focusedEditor: string | null
setFocusedEditor: (id: string | null) => void
}
)

// [2]
export default function MultipleExample() {
const [focusedEditor, _setFocusedEditor] = useState<string | null>('A')

Expand Down Expand Up @@ -53,6 +57,7 @@ export default function MultipleExample() {
)
}

// [3]
function EditorA() {
const { focusedEditor, setFocusedEditor } = useContext(focusedEditorContext)
const isFocused = focusedEditor === 'A'
Expand All @@ -74,6 +79,7 @@ function EditorA() {
)
}

// [4]
function EditorB() {
const { focusedEditor, setFocusedEditor } = useContext(focusedEditorContext)
const isFocused = focusedEditor === 'B'
Expand Down Expand Up @@ -116,6 +122,7 @@ function EditorC() {
)
}

// [5]
function ABunchOfText() {
return (
<article style={{ maxWidth: 500 }}>
Expand Down Expand Up @@ -159,3 +166,31 @@ function ABunchOfText() {
</article>
)
}

/*
This example shows how to use multiple editors on the same page. When doing this, you'll
need to make sure that only one editor is focused at a time. We can manage this using
the autofocus prop on the tldraw component, along with React's context and set state
APIs.
[1]
We first create a context that will hold the focused editor id and a setter for that id.
We'll use this to keep track of which editor is focused.
[2]
Wrap the editors in the context provider. This will make the context available to all
of the editors.
[3]
Get the focused editor id and the setter from the context. We'll use these to determine
if the editor should be focused or not. We wrap the Tldraw component in a div and use
the onFocus event to set the focused editor id.
[4]
Same again, but we're using the same persistence key for editors B and C. This means
that they will share a document.
[5]
A long story that doesn't really go anywhere, clearly written by a computer. But it's
a good way to test the scroll behavior of the page.
*/

0 comments on commit e952a31

Please sign in to comment.