Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit 8dad1da

Browse files
authored
improvement(excalidraw_wrapper.tsx): remove 'onResize' callback (#22)
It will be host app's responsiblity to add the listener forr resize Remove npm downloads weekly badge
1 parent 76adb70 commit 8dad1da

6 files changed

Lines changed: 21 additions & 26 deletions

File tree

README.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
### Excalidraw
22

33
![npm](https://img.shields.io/npm/v/excalidraw)
4-
![npm](https://img.shields.io/npm/dw/excalidraw)
54
![npm](https://img.shields.io/npm/dt/excalidraw)
65

76
Excalidraw exported as a component to directly embed in your projects
@@ -34,7 +33,7 @@ FG_Virgil.woff2
3433
### Usage
3534

3635
```javascript
37-
import React, { useState } from "react";
36+
import React, { useEffect, useState } from "react";
3837
import Excalidraw from "excalidraw";
3938
import InitialData from "./initialData";
4039

@@ -46,37 +45,42 @@ export default function App() {
4645
console.log("Elements :", elements, "State : ", state);
4746
};
4847

49-
const onUsernameChange = (username) => {
48+
const onUsernameChange = username => {
5049
console.log("current username", username);
5150
};
5251
const [dimensions, setDimensions] = useState({
5352
width: window.innerWidth,
54-
height: window.innerHeight,
53+
height: window.innerHeight
5554
});
5655

5756
const onResize = () => {
5857
setDimensions({
5958
width: window.innerWidth,
60-
height: window.innerHeight,
59+
height: window.innerHeight
6160
});
6261
};
62+
63+
useEffect(() => {
64+
window.addEventListener("resize", onResize);
65+
66+
return () => window.removeEventListener("resize", onResize);
67+
}, []);
68+
6369
const { width, height } = dimensions;
64-
const options = { zenModeEnabled: true, viewBackgroundColor: "AFEEEE" };
70+
const options = { zenModeEnabled: true, viewBackgroundColor: "#AFEEEE" };
6571
return (
6672
<div className="App">
6773
<Excalidraw
6874
width={width}
6975
height={height}
70-
onResize={onResize}
7176
initialData={InitialData}
7277
onChange={onChange}
73-
options={options}
7478
user={{ name: "Excalidraw User" }}
7579
onUsernameChange={onUsernameChange}
80+
options={options}
7681
/>
7782
</div>
7883
);
79-
}
8084
```
8185
8286
[![Edit excalidraw-embed](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/excalidraw-embed-f60f8?fontsize=14&hidenavigation=1&theme=dark)
@@ -87,7 +91,6 @@ export default function App() {
8791
| ------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
8892
| [width](#width) | Number | window.innerWidth | The width of Excalidraw component |
8993
| [height](#height) | Number | window.innerHeight | The height of Excalidraw component |
90-
| [onResize](#onResize) | Function | | This callback will be called when window resizes |
9194
| [initialData](#initialData) | [ExcalidrawElement[]](https://github.com/excalidraw/excalidraw-embed/blob/58178c388ae577140a1c679b5733f33e3722498a/src/element/types.ts#L44) | [] | The initial data with which app loads. |
9295
| [onChange](#onChange) | Function | | This callback is triggered whenever the component updates due to any change. This callback will receive the excalidraw elements and the current app state. |
9396
| [options](#options) | Object | Each option has a default value. See [options](#options) section for more details | Options to be passed to Excalidraw |
@@ -106,12 +109,6 @@ This props defines the width of the Excalidraw component. Defaults to `window.in
106109
107110
This props defines the height of the Excalidraw component. Defaults to `window.innerHeight` if not passed.
108111
109-
<a name="onResize"><a/>
110-
111-
##### onResize
112-
113-
If this callback is passed, it gets triggered when window resizes. Some calculations which you might want to do here is calculating width and height of Excalidraw and pass it. Incase the updated width and height is not passed, then it will not get updated.
114-
115112
<a name="initialData"><a/>
116113
117114
##### initialData

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "excalidraw",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"main": "dist/excalidraw.min.js",
55
"files": [
66
"dist"

src/components/App.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,6 @@ class App extends React.Component<ExcalidrawProps, AppState> {
448448
}
449449

450450
private onResize = withBatchedUpdates(() => {
451-
const { onResize } = this.props;
452-
if (onResize) {
453-
onResize();
454-
}
455451
globalSceneState
456452
.getElementsIncludingDeleted()
457453
.forEach((element) => invalidateShapeForElement(element));

src/excalidraw_wrapper.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const Excalidraw = (props: ExcalidrawProps) => {
1717
initialData,
1818
user,
1919
onUsernameChange,
20-
onResize,
2120
options,
2221
} = props;
2322
useEffect(() => {
@@ -45,7 +44,6 @@ const Excalidraw = (props: ExcalidrawProps) => {
4544
initialData={initialData}
4645
user={user}
4746
onUsernameChange={onUsernameChange}
48-
onResize={onResize}
4947
options={options}
5048
/>
5149
);

src/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useEffect, useState } from "react";
22
import ReactDOM from "react-dom";
33
import * as Sentry from "@sentry/browser";
44
import * as SentryIntegrations from "@sentry/integrations";
@@ -71,6 +71,12 @@ function ExcalidrawApp() {
7171
});
7272
};
7373

74+
useEffect(() => {
75+
window.addEventListener("resize", onResize);
76+
77+
return () => window.removeEventListener("resize", onResize);
78+
}, []);
79+
7480
const saveDebounced = debounce((elements, state) => {
7581
saveToLocalStorage(elements, state);
7682
}, 300);
@@ -99,7 +105,6 @@ function ExcalidrawApp() {
99105
initialData={initialData}
100106
user={user}
101107
onUsernameChange={onUsernameChange}
102-
onResize={onResize}
103108
/>
104109
</IsMobileProvider>
105110
</TopErrorBoundary>

src/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ export interface ExcalidrawProps {
113113
name?: string | null | undefined;
114114
};
115115
onUsernameChange?: (username: string) => void;
116-
onResize: () => void;
117116
options: {
118117
zenModeEnabled: boolean;
119118
viewBackgroundColor: string;

0 commit comments

Comments
 (0)