@netless/fastboard 0.0.9 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netless/fastboard",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "An open sourced sdk based on white-web-sdk.",
5
5
  "main": "./dist/index.cjs.js",
6
6
  "module": "./dist/index.es.js",
package/src/react.tsx CHANGED
@@ -1,16 +1,42 @@
1
1
  import type { WhiteboardApp } from "./index";
2
2
 
3
- import React, { useEffect, useRef } from "react";
3
+ import React, { forwardRef, useEffect, useRef } from "react";
4
4
  import { useLastValue } from "./internal/hooks";
5
5
 
6
+ // https://itnext.io/reusing-the-ref-from-forwardref-with-react-hooks-4ce9df693dd
7
+ function useCombinedRefs<T>(...refs: React.Ref<T>[]) {
8
+ const targetRef = useRef<T | null>(null);
9
+
10
+ useEffect(() => {
11
+ for (const ref of refs) {
12
+ if (!ref) continue;
13
+
14
+ if (typeof ref === "function") {
15
+ ref(targetRef.current);
16
+ } else {
17
+ (ref as typeof targetRef).current = targetRef.current;
18
+ }
19
+ }
20
+ }, [refs]);
21
+
22
+ return targetRef;
23
+ }
24
+
6
25
  /**
7
26
  * @example
8
27
  * let app = await createWhiteboardApp(config)
9
28
  * <Fastboard app={app} />
10
29
  * await app.dispose()
11
30
  */
12
- export function Fastboard({ app }: { app?: WhiteboardApp | null }) {
13
- const ref = useRef<HTMLDivElement>(null);
31
+ export const Fastboard = forwardRef<
32
+ HTMLDivElement,
33
+ { app?: WhiteboardApp | null } & React.DetailedHTMLProps<
34
+ React.HTMLAttributes<HTMLDivElement>,
35
+ HTMLDivElement
36
+ >
37
+ >(({ app, ...restProps }, outerRef) => {
38
+ const innerRef = useRef<HTMLDivElement>(null);
39
+ const ref = useCombinedRefs(outerRef, innerRef);
14
40
  const previous = useLastValue(app);
15
41
 
16
42
  useEffect(() => {
@@ -20,7 +46,7 @@ export function Fastboard({ app }: { app?: WhiteboardApp | null }) {
20
46
  if (app) {
21
47
  app.bindElement(ref.current);
22
48
  }
23
- }, [app, previous]);
49
+ }, [app, previous, ref]);
24
50
 
25
- return <div className="fastboard" ref={ref} />;
26
- }
51
+ return <div className="fastboard" {...restProps} ref={ref} />;
52
+ });