integration.mdx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. ```jsx showLineNumbers
  27. import { useState, useEffect } from "react";
  28. export default function App() {
  29. const [Comp, setComp] = useState(null);
  30. useEffect(() => {
  31. import("@excalidraw/excalidraw").then((comp) => setComp(comp.default));
  32. }, []);
  33. return <>{Comp && <Comp />}</>;
  34. }
  35. ```
  36. The `types` are available at `@excalidraw/excalidraw/types`, you can view [example for typescript](https://codesandbox.io/s/excalidraw-types-9h2dm)
  37. ## Browser
  38. To use it in a browser directly:
  39. For development use :point_down:
  40. ```js
  41. <script
  42. type="text/javascript"
  43. src="https://unpkg.com/@excalidraw/excalidraw/dist/excalidraw.development.js"
  44. ></script>
  45. ```
  46. For production use :point_down:
  47. ```js
  48. <script
  49. type="text/javascript"
  50. src="https://unpkg.com/@excalidraw/excalidraw/dist/excalidraw.production.min.js"
  51. ></script>
  52. ```
  53. 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`.
  54. import Tabs from "@theme/Tabs";
  55. import TabItem from "@theme/TabItem";
  56. <Tabs>
  57. <TabItem value="html" label="html">
  58. ```html
  59. <!DOCTYPE html>
  60. <html>
  61. <head>
  62. <title>Excalidraw in browser</title>
  63. <meta charset="UTF-8" />
  64. <script src="https://unpkg.com/react@18.2.0/umd/react.development.js"></script>
  65. <script src="https://unpkg.com/react-dom@18.2.0/umd/react-dom.development.js"></script>
  66. <script
  67. type="text/javascript"
  68. src="https://unpkg.com/@excalidraw/excalidraw/dist/excalidraw.development.js"
  69. ></script>
  70. </head>
  71. <body>
  72. <div class="container">
  73. <h1>Excalidraw Embed Example</h1>
  74. <div id="app"></div>
  75. </div>
  76. <script type="text/javascript" src="src/index.js"></script>
  77. </body>
  78. </html>
  79. ```
  80. </TabItem>
  81. <TabItem value="js" label="Javascript">
  82. ```js showLineNumbers
  83. const App = () => {
  84. return React.createElement(
  85. React.Fragment,
  86. null,
  87. React.createElement(
  88. "div",
  89. {
  90. style: { height: "500px" },
  91. },
  92. React.createElement(ExcalidrawLib.Excalidraw),
  93. ),
  94. );
  95. };
  96. const excalidrawWrapper = document.getElementById("app");
  97. const root = ReactDOM.createRoot(excalidrawWrapper);
  98. root.render(React.createElement(App));
  99. ```
  100. </TabItem>
  101. </Tabs>