@teambit/react.ui.loader-fallback 0.0.75 → 0.0.79

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/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { LoaderFallback } from './loader-fallback';
2
- export type { LoaderProps } from './loader-fallback';
1
+ export { LoaderFallback, useFallback } from './loader-fallback';
2
+ export type { LoaderProps, useFallbackOptions } from './loader-fallback';
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LoaderFallback = void 0;
3
+ exports.useFallback = exports.LoaderFallback = void 0;
4
4
  var loader_fallback_1 = require("./loader-fallback");
5
5
  Object.defineProperty(exports, "LoaderFallback", { enumerable: true, get: function () { return loader_fallback_1.LoaderFallback; } });
6
+ Object.defineProperty(exports, "useFallback", { enumerable: true, get: function () { return loader_fallback_1.useFallback; } });
6
7
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,qDAAmD;AAA1C,iHAAA,cAAc,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,qDAAgE;AAAvD,iHAAA,cAAc,OAAA;AAAE,8GAAA,WAAW,OAAA"}
@@ -1,12 +1,17 @@
1
- import { ComponentType } from 'react';
1
+ import React, { ComponentType, ReactElement } from 'react';
2
2
  export declare type LoaderProps = {
3
3
  /** component to render */
4
4
  Target: ComponentType | undefined;
5
+ /** component to render when Target is undefined */
6
+ DefaultComponent: ComponentType;
5
7
  /** component to render when target is missing, for a grace period, until rendering the default */
6
8
  Loader?: ComponentType;
7
9
  /** cool-down period (in ms) to show Loader, before showing the default */
8
10
  timeout?: number;
9
- /** component to render when Target is undefined */
10
- DefaultComponent: ComponentType;
11
11
  };
12
- export declare function LoaderFallback({ Target, Loader, DefaultComponent, timeout }: LoaderProps): JSX.Element;
12
+ export declare function LoaderFallback({ Target, Loader, DefaultComponent, timeout }: LoaderProps): React.ReactElement<any, string | React.JSXElementConstructor<any>>;
13
+ export declare type useFallbackOptions = {
14
+ timeout?: number;
15
+ loader?: ReactElement;
16
+ };
17
+ export declare function useFallback(target: ReactElement | null | undefined, fallback: ReactElement, { timeout, loader }?: useFallbackOptions): ReactElement | null;
@@ -1,13 +1,25 @@
1
1
  import { LoaderFallback } from './loader-fallback';
2
2
 
3
- Renders a component, and show a placeholder when it is undefined.
3
+ Safely render a component, with a default fallback when it is `undefined`.
4
+ The `LoaderFallback` component also provides a grace period, where it shows a loader before switching to the default.
5
+ Set `timeout = 0` to skip the grace period.
4
6
 
5
- Logic is as follows:
7
+ #### Example usage:
6
8
 
7
- 1. when component is defined -> render it
8
- 1. when the initial value of the component is undefined -> show the default immediately
9
- 1. when the component had just changed to be undefined -> show Loader for _x_ seconds
10
- 1. then, after _x_ seconds - show NotFound
9
+ ```tsx
10
+ // as a hook:
11
+ const safeTarget = useFallback(Target && <Target />, <DefaultComponent />, { timeout: 10000, loader: <Loader /> });
12
+
13
+ // as a component:
14
+ <LoaderFallback Target={Target} DefaultComponent={DefaultComponent} timeout={10000} loader={Loader}>
15
+ ```
16
+
17
+ ### Logic is as follows:
18
+
19
+ 1. when component is `defined` -> render it
20
+ 1. when the initial value of the component is `undefined` -> show the default immediately
21
+ 1. when the component changes to be `undefined` -> show Loader for _x_ seconds
22
+ 1. then, after _x_ seconds - show the default.
11
23
 
12
24
  <!-- live playground doesn't keep state when editing :( -->
13
25
  <!-- Try it out:
@@ -19,51 +19,33 @@ var __importStar = (this && this.__importStar) || function (mod) {
19
19
  return result;
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.LoaderFallback = void 0;
22
+ exports.useFallback = exports.LoaderFallback = void 0;
23
23
  const react_1 = __importStar(require("react"));
24
24
  const base_ui_loaders_loader_ribbon_1 = require("@teambit/base-ui.loaders.loader-ribbon");
25
25
  function LoaderFallback({ Target, Loader = LoaderComponent, DefaultComponent, timeout = 15000 }) {
26
- const [ToRender, setRenderTarget] = (0, react_1.useState)(Target ? react_1.default.createElement(Target, null) : react_1.default.createElement(DefaultComponent, null));
27
- const prevValue = (0, react_1.useRef)(undefined);
28
- // could re-implement with a state machine or a reducer
26
+ return useFallback(Target && react_1.default.createElement(Target, null), react_1.default.createElement(DefaultComponent, null), { timeout, loader: react_1.default.createElement(Loader, null) });
27
+ }
28
+ exports.LoaderFallback = LoaderFallback;
29
+ function useFallback(target, fallback, { timeout = 15000, loader = react_1.default.createElement(LoaderComponent, null) } = {}) {
30
+ const [working, setWorking] = (0, react_1.useState)(!!target);
31
+ const hasTarget = !!target;
29
32
  (0, react_1.useEffect)(() => {
30
- const prev = prevValue.current;
31
- prevValue.current = Target;
32
- if (Target) {
33
- setRenderTarget(react_1.default.createElement(Target, null));
33
+ if (timeout <= 0)
34
+ return () => { };
35
+ if (hasTarget) {
36
+ setWorking(true);
34
37
  return () => { };
35
38
  }
36
- // else -> Target === undefined
37
- // Target has changed from a value to undefined.
38
- // show loader and hope webpack will supply a component
39
- if (prev) {
40
- setRenderTarget(react_1.default.createElement(Loader, null));
41
- const tmId = setTimeout(() => setRenderTarget(react_1.default.createElement(DefaultComponent, null)), timeout);
42
- return () => clearTimeout(tmId);
43
- }
44
- // else -> Target === undefined, prev === undefined
45
- // comp changed from undefined to undefined
46
- // could happen on first render, or when one of the other values change.
47
- return () => { }; // nothing to do here
48
- }, [Target, DefaultComponent, Loader, timeout]);
49
- return ToRender;
39
+ const tmId = setTimeout(() => setWorking(false), timeout);
40
+ return () => clearTimeout(tmId);
41
+ }, [hasTarget, timeout]);
42
+ if (target)
43
+ return target;
44
+ if (working && timeout > 0)
45
+ return loader;
46
+ return fallback;
50
47
  }
51
- exports.LoaderFallback = LoaderFallback;
52
- /*
53
- * TIP:
54
- * useState() and setState() can receive a function as value.
55
- * So, be careful when using a react Function Component, like:
56
- * ```
57
- * useState(ButtonComponent) // will try to run ButtonComponent as a function
58
- * setState(ButtonComponent)
59
- *
60
- * // instead, do:
61
- * useState(() => ButtonComponent)
62
- * setState(() => ButtonComponent)
63
- * ```
64
- *
65
- * or don't set components as state to begin with.
66
- */
48
+ exports.useFallback = useFallback;
67
49
  function LoaderComponent() {
68
50
  return react_1.default.createElement(base_ui_loaders_loader_ribbon_1.LoaderRibbon, { active: true, style: { position: 'fixed', top: 0, left: 0, right: 0 } });
69
51
  }
@@ -1 +1 @@
1
- {"version":3,"file":"loader-fallback.js","sourceRoot":"","sources":["../loader-fallback.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,+CAA0E;AAC1E,0FAAsE;AAatE,SAAgB,cAAc,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,eAAe,EAAE,gBAAgB,EAAE,OAAO,GAAG,KAAK,EAAe;IACjH,MAAM,CAAC,QAAQ,EAAE,eAAe,CAAC,GAAG,IAAA,gBAAQ,EAAqB,MAAM,CAAC,CAAC,CAAC,8BAAC,MAAM,OAAG,CAAC,CAAC,CAAC,8BAAC,gBAAgB,OAAG,CAAC,CAAC;IAC7G,MAAM,SAAS,GAAG,IAAA,cAAM,EAA4B,SAAS,CAAC,CAAC;IAE/D,uDAAuD;IACvD,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,IAAI,GAAG,SAAS,CAAC,OAAO,CAAC;QAC/B,SAAS,CAAC,OAAO,GAAG,MAAM,CAAC;QAE3B,IAAI,MAAM,EAAE;YACV,eAAe,CAAC,8BAAC,MAAM,OAAG,CAAC,CAAC;YAC5B,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC;SACjB;QACD,+BAA+B;QAE/B,gDAAgD;QAChD,uDAAuD;QACvD,IAAI,IAAI,EAAE;YACR,eAAe,CAAC,8BAAC,MAAM,OAAG,CAAC,CAAC;YAE5B,MAAM,IAAI,GAAG,UAAU,CAAC,GAA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C,CAAC,MAAM,CAAC,CAAC;IACjD,MAAM,SAAS,GAAG,CAAC,CAAC,MAAM,CAAC;IAE3B,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,OAAO,IAAI,CAAC;YAAE,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC;QAClC,IAAI,SAAS,EAAE;YACb,UAAU,CAAC,IAAI,CAAC,CAAC;YACjB,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC;SACjB;QAED,MAAM,IAAI,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC;QAC1D,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IAClC,CAAC,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC;IAEzB,IAAI,MAAM;QAAE,OAAO,MAAM,CAAC;IAC1B,IAAI,OAAO,IAAI,OAAO,GAAG,CAAC;QAAE,OAAO,MAAM,CAAC;IAC1C,OAAO,QAAQ,CAAC;AAClB,CAAC;AAtBD,kCAsBC;AAED,SAAS,eAAe;IACtB,OAAO,8BAAC,4CAAY,IAAC,MAAM,QAAC,KAAK,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAI,CAAC;AAC1F,CAAC"}
package/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { LoaderFallback } from './loader-fallback';
2
- export type { LoaderProps } from './loader-fallback';
1
+ export { LoaderFallback, useFallback } from './loader-fallback';
2
+ export type { LoaderProps, useFallbackOptions } from './loader-fallback';
@@ -1,13 +1,25 @@
1
1
  import { LoaderFallback } from './loader-fallback';
2
2
 
3
- Renders a component, and show a placeholder when it is undefined.
3
+ Safely render a component, with a default fallback when it is `undefined`.
4
+ The `LoaderFallback` component also provides a grace period, where it shows a loader before switching to the default.
5
+ Set `timeout = 0` to skip the grace period.
4
6
 
5
- Logic is as follows:
7
+ #### Example usage:
6
8
 
7
- 1. when component is defined -> render it
8
- 1. when the initial value of the component is undefined -> show the default immediately
9
- 1. when the component had just changed to be undefined -> show Loader for _x_ seconds
10
- 1. then, after _x_ seconds - show NotFound
9
+ ```tsx
10
+ // as a hook:
11
+ const safeTarget = useFallback(Target && <Target />, <DefaultComponent />, { timeout: 10000, loader: <Loader /> });
12
+
13
+ // as a component:
14
+ <LoaderFallback Target={Target} DefaultComponent={DefaultComponent} timeout={10000} loader={Loader}>
15
+ ```
16
+
17
+ ### Logic is as follows:
18
+
19
+ 1. when component is `defined` -> render it
20
+ 1. when the initial value of the component is `undefined` -> show the default immediately
21
+ 1. when the component changes to be `undefined` -> show Loader for _x_ seconds
22
+ 1. then, after _x_ seconds - show the default.
11
23
 
12
24
  <!-- live playground doesn't keep state when editing :( -->
13
25
  <!-- Try it out:
@@ -1,67 +1,47 @@
1
- import React, { ComponentType, useRef, useEffect, useState } from 'react';
1
+ import React, { ComponentType, useEffect, useState, ReactElement } from 'react';
2
2
  import { LoaderRibbon } from '@teambit/base-ui.loaders.loader-ribbon';
3
3
 
4
4
  export type LoaderProps = {
5
5
  /** component to render */
6
6
  Target: ComponentType | undefined;
7
+ /** component to render when Target is undefined */
8
+ DefaultComponent: ComponentType;
7
9
  /** component to render when target is missing, for a grace period, until rendering the default */
8
10
  Loader?: ComponentType;
9
11
  /** cool-down period (in ms) to show Loader, before showing the default */
10
12
  timeout?: number;
11
- /** component to render when Target is undefined */
12
- DefaultComponent: ComponentType;
13
13
  };
14
14
 
15
15
  export function LoaderFallback({ Target, Loader = LoaderComponent, DefaultComponent, timeout = 15000 }: LoaderProps) {
16
- const [ToRender, setRenderTarget] = useState<JSX.Element | null>(Target ? <Target /> : <DefaultComponent />);
17
- const prevValue = useRef<ComponentType | undefined>(undefined);
16
+ return useFallback(Target && <Target />, <DefaultComponent />, { timeout, loader: <Loader /> });
17
+ }
18
18
 
19
- // could re-implement with a state machine or a reducer
20
- useEffect(() => {
21
- const prev = prevValue.current;
22
- prevValue.current = Target;
19
+ export type useFallbackOptions = { timeout?: number; loader?: ReactElement };
23
20
 
24
- if (Target) {
25
- setRenderTarget(<Target />);
26
- return () => {};
27
- }
28
- // else -> Target === undefined
29
-
30
- // Target has changed from a value to undefined.
31
- // show loader and hope webpack will supply a component
32
- if (prev) {
33
- setRenderTarget(<Loader />);
21
+ export function useFallback(
22
+ target: ReactElement | null | undefined,
23
+ fallback: ReactElement,
24
+ { timeout = 15000, loader = <LoaderComponent /> }: useFallbackOptions = {}
25
+ ): ReactElement | null {
26
+ const [working, setWorking] = useState(!!target);
27
+ const hasTarget = !!target;
34
28
 
35
- const tmId = setTimeout(() => setRenderTarget(<DefaultComponent />), timeout);
36
- return () => clearTimeout(tmId);
29
+ useEffect(() => {
30
+ if (timeout <= 0) return () => {};
31
+ if (hasTarget) {
32
+ setWorking(true);
33
+ return () => {};
37
34
  }
38
- // else -> Target === undefined, prev === undefined
39
-
40
- // comp changed from undefined to undefined
41
- // could happen on first render, or when one of the other values change.
42
35
 
43
- return () => {}; // nothing to do here
44
- }, [Target, DefaultComponent, Loader, timeout]);
36
+ const tmId = setTimeout(() => setWorking(false), timeout);
37
+ return () => clearTimeout(tmId);
38
+ }, [hasTarget, timeout]);
45
39
 
46
- return ToRender;
40
+ if (target) return target;
41
+ if (working && timeout > 0) return loader;
42
+ return fallback;
47
43
  }
48
44
 
49
- /*
50
- * TIP:
51
- * useState() and setState() can receive a function as value.
52
- * So, be careful when using a react Function Component, like:
53
- * ```
54
- * useState(ButtonComponent) // will try to run ButtonComponent as a function
55
- * setState(ButtonComponent)
56
- *
57
- * // instead, do:
58
- * useState(() => ButtonComponent)
59
- * setState(() => ButtonComponent)
60
- * ```
61
- *
62
- * or don't set components as state to begin with.
63
- */
64
-
65
45
  function LoaderComponent() {
66
46
  return <LoaderRibbon active style={{ position: 'fixed', top: 0, left: 0, right: 0 }} />;
67
47
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@teambit/react.ui.loader-fallback",
3
- "version": "0.0.75",
3
+ "version": "0.0.79",
4
4
  "homepage": "https://bit.dev/teambit/react/ui/loader-fallback",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.react",
8
8
  "name": "ui/loader-fallback",
9
- "version": "0.0.75"
9
+ "version": "0.0.79"
10
10
  },
11
11
  "dependencies": {
12
12
  "core-js": "^3.0.0",
@@ -23,7 +23,7 @@
23
23
  "@types/node": "12.20.4"
24
24
  },
25
25
  "peerDependencies": {
26
- "@teambit/legacy": "1.0.182",
26
+ "@teambit/legacy": "1.0.185",
27
27
  "react-dom": "^16.8.0 || ^17.0.0",
28
28
  "react": "^16.8.0 || ^17.0.0"
29
29
  },
@@ -51,7 +51,7 @@
51
51
  "react": "-"
52
52
  },
53
53
  "peerDependencies": {
54
- "@teambit/legacy": "1.0.182",
54
+ "@teambit/legacy": "1.0.185",
55
55
  "react-dom": "^16.8.0 || ^17.0.0",
56
56
  "react": "^16.8.0 || ^17.0.0"
57
57
  }