2021-11-04 15:48:39 +00:00
< div style = "text-align: center; transform: scale(.5);" >
< img src = "card-repo.png" / >
< / div >
2021-11-02 11:46:25 +00:00
# @tldraw/tldraw
2021-05-09 12:03:39 +00:00
2021-11-16 16:01:29 +00:00
This package contains the [tldraw ](https://tldraw.com ) editor as a React component named `<Tldraw>` . You can use this package to embed the editor in any React application.
2021-07-01 16:46:01 +00:00
2021-11-04 15:48:39 +00:00
💕 Love this library? Consider [becoming a sponsor ](https://github.com/sponsors/steveruizok?frequency=recurring&sponsor=steveruizok ).
2021-07-08 08:39:27 +00:00
2021-11-11 12:25:25 +00:00
🙌 Questions? Join the [Discord channel ](https://discord.gg/SBBEVCA4PG ) or start a [discussion ](https://github.com/tldraw/tldraw/discussions/new ).
2021-11-11 12:23:55 +00:00
2021-11-16 16:01:29 +00:00
🎨 Want to build your own tldraw-ish app instead? Try [@tldraw/core ](https://github.com/tldraw/core ).
2021-11-11 12:23:55 +00:00
2021-11-04 15:48:39 +00:00
## Installation
2021-07-08 08:39:27 +00:00
2021-11-11 09:54:58 +00:00
Use your package manager of choice to install `@tldraw/tldraw` and its peer dependencies.
2021-07-01 16:46:01 +00:00
2021-11-02 11:46:25 +00:00
```bash
yarn add @tldraw/tldraw
2021-11-04 15:48:39 +00:00
# or
npm i @tldraw/tldraw
2021-11-02 11:46:25 +00:00
```
2021-07-01 16:46:01 +00:00
2021-11-02 11:46:25 +00:00
## Usage
2021-08-13 13:31:27 +00:00
2021-11-16 16:01:29 +00:00
Import the `tldraw` React component and use it in your app.
2021-07-01 16:46:01 +00:00
2021-11-02 11:46:25 +00:00
```tsx
2021-11-16 16:01:29 +00:00
import { Tldraw } from '@tldraw/tldraw'
2021-07-01 16:46:01 +00:00
2021-11-02 11:46:25 +00:00
function App() {
2021-11-16 16:01:29 +00:00
return < Tldraw / >
2021-11-02 11:46:25 +00:00
}
```
2021-07-01 16:46:01 +00:00
2021-11-08 14:21:37 +00:00
### Persisting the State
You can use the `id` to persist the state in a user's browser storage.
```tsx
2021-11-16 16:01:29 +00:00
import { Tldraw } from '@tldraw/tldraw'
2021-11-08 14:21:37 +00:00
function App() {
2021-11-16 16:01:29 +00:00
return < Tldraw id = "myState" / >
2021-11-08 14:21:37 +00:00
}
```
### Controlling the Component through Props
2021-11-16 16:01:29 +00:00
You can control the `tldraw` component through its props.
2021-11-04 15:48:39 +00:00
```tsx
2021-11-16 16:01:29 +00:00
import { Tldraw, TDDocument } from '@tldraw/tldraw'
2021-11-04 15:48:39 +00:00
function App() {
2021-11-16 16:01:29 +00:00
const myDocument: TDDocument = {}
2021-11-04 15:48:39 +00:00
2021-11-16 16:01:29 +00:00
return < Tldraw document = {document} / >
2021-11-04 15:48:39 +00:00
}
```
2021-11-16 16:01:29 +00:00
### Controlling the Component through the tldrawApp API
2021-11-08 14:21:37 +00:00
2021-11-16 16:01:29 +00:00
You can also control the `tldraw` component imperatively through the `tldrawApp` API.
2021-11-04 15:48:39 +00:00
```tsx
2021-11-16 16:01:29 +00:00
import { Tldraw, tldrawApp } from '@tldraw/tldraw'
2021-11-04 15:48:39 +00:00
function App() {
2021-11-16 16:01:29 +00:00
const handleMount = React.useCallback((state: tldrawApp) => {
2021-11-08 14:21:37 +00:00
state.selectAll()
2021-11-04 15:48:39 +00:00
}, [])
2021-11-16 16:01:29 +00:00
return < Tldraw onMount = {handleMount} / >
2021-11-04 15:48:39 +00:00
}
```
2021-11-16 16:01:29 +00:00
Internally, the `tldraw` component's user interface uses this API to make changes to the component's state. See the `tldrawApp` section of the [documentation ](guides/documentation ) for more on this API.
2021-11-08 14:21:37 +00:00
### Responding to Changes
2021-11-11 12:11:21 +00:00
You can respond to changes and user actions using the `onChange` callback. For more specific changes, you can also use the `onPatch` , `onCommand` , or `onPersist` callbacks. See the [documentation ](guides/documentation ) for more.
2021-11-08 14:21:37 +00:00
```tsx
2021-11-16 16:01:29 +00:00
import { Tldraw, tldrawApp } from '@tldraw/tldraw'
2021-11-08 14:21:37 +00:00
function App() {
2021-11-16 16:01:29 +00:00
const handleChange = React.useCallback((state: tldrawApp, reason: string) => {
2021-11-11 12:11:21 +00:00
// Do something with the change
}, [])
2021-11-08 14:21:37 +00:00
2021-11-16 16:01:29 +00:00
return < Tldraw onMount = {handleMount} / >
2021-11-08 14:21:37 +00:00
}
```
2021-11-02 11:46:25 +00:00
## Documentation
2021-07-01 16:46:01 +00:00
2021-11-11 12:19:32 +00:00
See the project's [documentation ](/guides/documentation.md ).
2021-11-02 11:46:25 +00:00
2021-11-11 12:11:21 +00:00
## Contribution
2021-11-02 11:46:25 +00:00
2021-11-11 12:11:21 +00:00
See the [contributing guide ](/CONTRIBUTING.md ).
2021-11-04 15:48:39 +00:00
2021-11-11 12:11:21 +00:00
## Development
2021-11-08 14:21:37 +00:00
2021-11-11 12:11:21 +00:00
See the [development guide ](/guides/development.md ).
2021-11-04 15:48:39 +00:00
## Example
2021-11-16 16:01:29 +00:00
See the `example` folder for examples of how to use the `<Tldraw/>` component.
2021-11-04 15:48:39 +00:00
## Community
### Support
Need help? Please [open an issue ](https://github.com/tldraw/tldraw/issues/new ) for support.
### Discussion
2021-11-11 12:25:25 +00:00
Want to connect with other devs? Visit the [Discord channel ](https://discord.gg/SBBEVCA4PG ).
2021-11-04 15:48:39 +00:00
### License
2021-11-11 11:37:57 +00:00
This project is licensed under MIT.
If you're using the library in a commercial product, please consider [becoming a sponsor ](https://github.com/sponsors/steveruizok?frequency=recurring&sponsor=steveruizok ).
2021-11-02 11:46:25 +00:00
2021-11-04 15:48:39 +00:00
## Author
2021-11-02 11:46:25 +00:00
2021-11-04 15:48:39 +00:00
- [@steveruizok ](https://twitter.com/steveruizok )