integration.mdx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Integration
  2. ## Module bundler
  3. If you are using a module bundler (for instance, Webpack), you can import it as an ES6 module as shown below
  4. ```js
  5. import { Excalidraw } from "@excalidraw/excalidraw";
  6. ```
  7. :::info
  8. Throughout the documentation we use live, editable Excalidraw examples like the one shown below.
  9. While we aim for the examples to closely reflect what you'd get if you rendered it yourself, we actually initialize it with some props behind the scenes.
  10. For example, we're passing a `theme` prop to it based on the current color theme of the docs you're just reading.
  11. :::
  12. ```jsx live
  13. function App() {
  14. return (
  15. <>
  16. <h1 style={{ textAlign: "center" }}>Excalidraw Example</h1>
  17. <div style={{ height: "500px" }}>
  18. <Excalidraw />
  19. </div>
  20. </>
  21. );
  22. }
  23. ```
  24. ### Rendering Excalidraw only on client
  25. Since _Excalidraw_ doesn't support server side rendering, you should render the component once the host is `mounted`.
  26. The following worfklow shows one way how to render Excalidraw on Next.js. We'll add more detailed and alternative Next.js examples, soon.
  27. ```jsx showLineNumbers
  28. import { useState, useEffect } from "react";
  29. export default function App() {
  30. const [Excalidraw, setExcalidraw] = useState(null);
  31. useEffect(() => {
  32. import("@excalidraw/excalidraw").then((comp) => setExcalidraw(comp.Excalidraw));
  33. }, []);
  34. return <>{Excalidraw && <Excalidraw />}</>;
  35. }
  36. ```
  37. The `types` are available at `@excalidraw/excalidraw/types`, you can view [example for typescript](https://codesandbox.io/s/excalidraw-types-9h2dm)
  38. ## Browser
  39. To use it in a browser directly:
  40. For development use :point_down:
  41. ```js
  42. <script
  43. type="text/javascript"
  44. src="https://unpkg.com/@excalidraw/excalidraw/dist/excalidraw.development.js"
  45. ></script>
  46. ```
  47. For production use :point_down:
  48. ```js
  49. <script
  50. type="text/javascript"
  51. src="https://unpkg.com/@excalidraw/excalidraw/dist/excalidraw.production.min.js"
  52. ></script>
  53. ```
  54. You will need to make sure `react`, `react-dom` is available as shown in the below example. For prod please use the production versions of `react`, `react-dom`.
  55. import Tabs from "@theme/Tabs";
  56. import TabItem from "@theme/TabItem";
  57. <Tabs>
  58. <TabItem value="html" label="html">
  59. ```html
  60. <!DOCTYPE html>
  61. <html>
  62. <head>
  63. <title>Excalidraw in browser</title>
  64. <meta charset="UTF-8" />
  65. <script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
  66. <script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
  67. <script
  68. type="text/javascript"
  69. src="https://unpkg.com/@excalidraw/excalidraw/dist/excalidraw.development.js"
  70. ></script>
  71. </head>
  72. <body>
  73. <div class="container">
  74. <h1>Excalidraw Embed Example</h1>
  75. <div id="app"></div>
  76. </div>
  77. <script type="text/javascript" src="src/index.js"></script>
  78. </body>
  79. </html>
  80. ```
  81. </TabItem>
  82. <TabItem value="js" label="Javascript">
  83. ```js showLineNumbers
  84. const App = () => {
  85. return React.createElement(
  86. React.Fragment,
  87. null,
  88. React.createElement(
  89. "div",
  90. {
  91. style: { height: "500px" },
  92. },
  93. React.createElement(ExcalidrawLib.Excalidraw),
  94. ),
  95. );
  96. };
  97. const excalidrawWrapper = document.getElementById("app");
  98. const root = ReactDOM.createRoot(excalidrawWrapper);
  99. root.render(React.createElement(App));
  100. ```
  101. </TabItem>
  102. </Tabs>