@sigmacomputing/react-embed-sdk 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ import * as react from 'react';
1
2
  import { WorkbookLoadedEvent, WorkbookErrorEvent } from '@sigmacomputing/embed-sdk';
2
3
  export * from '@sigmacomputing/embed-sdk';
3
4
 
@@ -48,5 +49,31 @@ declare function useWorkbookLoaded(iframeRef: React.RefObject<HTMLIFrameElement>
48
49
  *
49
50
  */
50
51
  declare function useWorkbookError(iframeRef: React.RefObject<HTMLIFrameElement>, onError: OnError): void;
52
+ /**
53
+ * A hook that returns a ref to be used with an iframe element, and the loading and error state of the embed.
54
+ *
55
+ * @example
56
+ * ```
57
+ * function MyEmbed() {
58
+ * const { iframeRef, loading, error } = useSigmaIframe();
59
+ * return (
60
+ * <>
61
+ * {loading && <p>Loading...</p>}
62
+ * {error && <p>Error: {error.message}</p>}
63
+ * <iframe
64
+ * className={loading || error ? "hidden" : "show"}
65
+ * ref={iframeRef}
66
+ * src="https://sigmacomputing.app/embed"
67
+ * />
68
+ * </>
69
+ * );
70
+ * }
71
+ * ```
72
+ */
73
+ declare function useSigmaIframe(): {
74
+ iframeRef: react.RefObject<HTMLIFrameElement>;
75
+ loading: boolean;
76
+ error: WorkbookErrorEvent | null;
77
+ };
51
78
 
52
- export { type OnError, type OnLoaded, useWorkbookError, useWorkbookLoaded };
79
+ export { type OnError, type OnLoaded, useSigmaIframe, useWorkbookError, useWorkbookLoaded };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import * as react from 'react';
1
2
  import { WorkbookLoadedEvent, WorkbookErrorEvent } from '@sigmacomputing/embed-sdk';
2
3
  export * from '@sigmacomputing/embed-sdk';
3
4
 
@@ -48,5 +49,31 @@ declare function useWorkbookLoaded(iframeRef: React.RefObject<HTMLIFrameElement>
48
49
  *
49
50
  */
50
51
  declare function useWorkbookError(iframeRef: React.RefObject<HTMLIFrameElement>, onError: OnError): void;
52
+ /**
53
+ * A hook that returns a ref to be used with an iframe element, and the loading and error state of the embed.
54
+ *
55
+ * @example
56
+ * ```
57
+ * function MyEmbed() {
58
+ * const { iframeRef, loading, error } = useSigmaIframe();
59
+ * return (
60
+ * <>
61
+ * {loading && <p>Loading...</p>}
62
+ * {error && <p>Error: {error.message}</p>}
63
+ * <iframe
64
+ * className={loading || error ? "hidden" : "show"}
65
+ * ref={iframeRef}
66
+ * src="https://sigmacomputing.app/embed"
67
+ * />
68
+ * </>
69
+ * );
70
+ * }
71
+ * ```
72
+ */
73
+ declare function useSigmaIframe(): {
74
+ iframeRef: react.RefObject<HTMLIFrameElement>;
75
+ loading: boolean;
76
+ error: WorkbookErrorEvent | null;
77
+ };
51
78
 
52
- export { type OnError, type OnLoaded, useWorkbookError, useWorkbookLoaded };
79
+ export { type OnError, type OnLoaded, useSigmaIframe, useWorkbookError, useWorkbookLoaded };
package/dist/index.js CHANGED
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
+ useSigmaIframe: () => useSigmaIframe,
23
24
  useWorkbookError: () => useWorkbookError,
24
25
  useWorkbookLoaded: () => useWorkbookLoaded
25
26
  });
@@ -50,8 +51,22 @@ function useWorkbookError(iframeRef, onError) {
50
51
  return () => window.removeEventListener("message", listener);
51
52
  }, [iframeRef, onError]);
52
53
  }
54
+ function useSigmaIframe() {
55
+ const iframeRef = (0, import_react.useRef)(null);
56
+ const [loading, setLoading] = (0, import_react.useState)(true);
57
+ const [error, setError] = (0, import_react.useState)(null);
58
+ const loadingCallback = (0, import_react.useCallback)(() => setLoading(false), []);
59
+ const errorCallback = (0, import_react.useCallback)((event) => {
60
+ setError(event);
61
+ setLoading(false);
62
+ }, []);
63
+ useWorkbookLoaded(iframeRef, loadingCallback);
64
+ useWorkbookError(iframeRef, errorCallback);
65
+ return { iframeRef, loading, error };
66
+ }
53
67
  // Annotate the CommonJS export names for ESM import in node:
54
68
  0 && (module.exports = {
69
+ useSigmaIframe,
55
70
  useWorkbookError,
56
71
  useWorkbookLoaded
57
72
  });
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/index.ts
2
- import { useEffect } from "react";
2
+ import { useCallback, useEffect, useRef, useState } from "react";
3
3
  import {
4
4
  workbookErrorListener,
5
5
  workbookLoadedListener
@@ -28,7 +28,21 @@ function useWorkbookError(iframeRef, onError) {
28
28
  return () => window.removeEventListener("message", listener);
29
29
  }, [iframeRef, onError]);
30
30
  }
31
+ function useSigmaIframe() {
32
+ const iframeRef = useRef(null);
33
+ const [loading, setLoading] = useState(true);
34
+ const [error, setError] = useState(null);
35
+ const loadingCallback = useCallback(() => setLoading(false), []);
36
+ const errorCallback = useCallback((event) => {
37
+ setError(event);
38
+ setLoading(false);
39
+ }, []);
40
+ useWorkbookLoaded(iframeRef, loadingCallback);
41
+ useWorkbookError(iframeRef, errorCallback);
42
+ return { iframeRef, loading, error };
43
+ }
31
44
  export {
45
+ useSigmaIframe,
32
46
  useWorkbookError,
33
47
  useWorkbookLoaded
34
48
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sigmacomputing/react-embed-sdk",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "React JavaScript SDK to interact with Sigma Computing's Embed API",
5
5
  "scripts": {
6
6
  "prepublish": "npm run build",