@zoreal/oauth2-react 0.2.21 → 0.2.23

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.cjs CHANGED
@@ -42,7 +42,7 @@ var import_react2 = require("react");
42
42
 
43
43
  // src/wire.ts
44
44
  var WIRE_VERSION = 1;
45
- var SDK_VERSION = "0.2.21";
45
+ var SDK_VERSION = "0.2.23";
46
46
  var DEFAULT_ISSUER = "https://id.zoreal.com";
47
47
  var POLL_INTERVAL_MS = 2e3;
48
48
  var POLL_INTERVAL_ENROLLING_MS = 5e3;
@@ -1642,7 +1642,7 @@ function useZorealOAuth() {
1642
1642
  }
1643
1643
 
1644
1644
  // src/ZorealLogin.tsx
1645
- var import_react5 = require("react");
1645
+ var import_react4 = require("react");
1646
1646
 
1647
1647
  // src/useZorealLogin.ts
1648
1648
  var import_react3 = require("react");
@@ -1786,6 +1786,30 @@ function markReturnDone(requestId) {
1786
1786
  function isReturnDone(requestId) {
1787
1787
  return storage()?.getItem(DONE + requestId) !== null && storage()?.getItem(DONE + requestId) !== void 0;
1788
1788
  }
1789
+ function visibleOrDone(requestId, signal) {
1790
+ if (typeof document === "undefined" || document.visibilityState !== "hidden") {
1791
+ return Promise.resolve(!isReturnDone(requestId));
1792
+ }
1793
+ return new Promise((resolve) => {
1794
+ const finish = (value) => {
1795
+ document.removeEventListener("visibilitychange", onVisible);
1796
+ window.removeEventListener("storage", onStorage);
1797
+ signal?.removeEventListener("abort", onAbort);
1798
+ resolve(value);
1799
+ };
1800
+ const onVisible = () => {
1801
+ if (document.visibilityState !== "hidden") finish(!isReturnDone(requestId));
1802
+ };
1803
+ const onStorage = () => {
1804
+ if (isReturnDone(requestId)) finish(false);
1805
+ };
1806
+ const onAbort = () => finish(false);
1807
+ document.addEventListener("visibilitychange", onVisible);
1808
+ window.addEventListener("storage", onStorage);
1809
+ signal?.addEventListener("abort", onAbort, { once: true });
1810
+ if (isReturnDone(requestId)) finish(false);
1811
+ });
1812
+ }
1789
1813
  function returnToUrl() {
1790
1814
  if (typeof window === "undefined") return void 0;
1791
1815
  const { href } = window.location;
@@ -2348,7 +2372,7 @@ function useZorealFlow(options) {
2348
2372
  controller.signal,
2349
2373
  { tolerateUnknownUntil: Date.now() + 15e3 }
2350
2374
  );
2351
- if (isReturnDone(requestId)) {
2375
+ if (!await visibleOrDone(requestId, controller.signal)) {
2352
2376
  throw new DOMException("aborted", "AbortError");
2353
2377
  }
2354
2378
  } else {
@@ -2494,79 +2518,8 @@ function useZorealLogin(options) {
2494
2518
  }).login;
2495
2519
  }
2496
2520
 
2497
- // src/ring.tsx
2498
- var import_react4 = require("react");
2499
- var import_jsx_runtime5 = require("react/jsx-runtime");
2500
- var TAIL2 = 0.3;
2501
- var STACK2 = Array.from({ length: 12 }, (_, i) => 12 - i);
2502
- var HALO2 = [3, 2, 1];
2503
- function ZorealBusyRing({
2504
- busy,
2505
- radius = 8,
2506
- theme = "auto",
2507
- block = false,
2508
- className,
2509
- style,
2510
- children
2511
- }) {
2512
- (0, import_react4.useEffect)(() => {
2513
- ensureStyles();
2514
- }, []);
2515
- const wrapRef = (0, import_react4.useRef)(null);
2516
- const measureRef = (0, import_react4.useRef)(null);
2517
- (0, import_react4.useLayoutEffect)(() => {
2518
- const wrap = wrapRef.current;
2519
- const rect = measureRef.current;
2520
- if (!wrap || !rect) return;
2521
- const measure = () => {
2522
- const length = rect.getTotalLength();
2523
- if (length > 0) wrap.style.setProperty("--zrl-ring-len", `${length}px`);
2524
- };
2525
- measure();
2526
- if (typeof ResizeObserver === "undefined") return;
2527
- const observer = new ResizeObserver(measure);
2528
- observer.observe(wrap);
2529
- return () => observer.disconnect();
2530
- }, [radius]);
2531
- const rx = radius + 2;
2532
- const layer = (key, name, len, alpha, ref) => /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
2533
- "rect",
2534
- {
2535
- ref,
2536
- className: cx(name),
2537
- rx,
2538
- ry: rx,
2539
- style: {
2540
- strokeDasharray: `calc(var(--zrl-l) * ${len}) calc(var(--zrl-l) * ${1 - len})`,
2541
- "--zrl-s": `calc(var(--zrl-l) * ${-(TAIL2 - len)})`,
2542
- opacity: alpha
2543
- }
2544
- },
2545
- key
2546
- );
2547
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
2548
- "span",
2549
- {
2550
- ref: wrapRef,
2551
- className: className ? `${cx("root")} ${cx("ring")} ${className}` : `${cx("root")} ${cx("ring")}`,
2552
- "data-theme": theme,
2553
- "data-busy": busy,
2554
- style: block ? { display: "flex", ...style } : style,
2555
- children: [
2556
- children,
2557
- /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("svg", { className: cx("ring-svg"), "aria-hidden": "true", children: [
2558
- HALO2.map(
2559
- (n, i) => layer(`h${n}`, "ring-halo", TAIL2 * n / HALO2.length, `calc(var(--zrl-glow-opacity) * 0.6 / ${n})`, i === 0 ? measureRef : void 0)
2560
- ),
2561
- STACK2.map((n) => layer(`t${n}`, n <= 2 ? "ring-head" : "ring-tail", TAIL2 * n / STACK2.length, String(1 / n)))
2562
- ] })
2563
- ]
2564
- }
2565
- );
2566
- }
2567
-
2568
2521
  // src/ZorealLogin.tsx
2569
- var import_jsx_runtime6 = require("react/jsx-runtime");
2522
+ var import_jsx_runtime5 = require("react/jsx-runtime");
2570
2523
  var TEXTS = {
2571
2524
  continue_with: null,
2572
2525
  signin_with: "Sign in with ZOREAL",
@@ -2597,31 +2550,18 @@ function ZorealLogin(props) {
2597
2550
  } = props;
2598
2551
  const { locale } = useZorealOAuth();
2599
2552
  const label = TEXTS[text] ?? strings(locale).buttonContinue;
2600
- const [busy, setBusy] = (0, import_react5.useState)(false);
2601
2553
  const { login } = useZorealFlow({
2602
2554
  ...request,
2603
2555
  flow,
2604
- onCredential: flow === "browser-direct" ? (r) => {
2605
- setBusy(false);
2606
- onSuccess(r);
2607
- } : void 0,
2608
- onCode: flow === "auth-code" ? (r) => {
2609
- setBusy(false);
2610
- onSuccess(r);
2611
- } : void 0,
2612
- onError: (e) => {
2613
- setBusy(false);
2614
- onError?.({ type: "unknown", description: e.description ?? e.error });
2615
- },
2616
- onNonOAuthError: (e) => {
2617
- setBusy(false);
2618
- onError?.(e);
2619
- }
2556
+ onCredential: flow === "browser-direct" ? onSuccess : void 0,
2557
+ onCode: flow === "auth-code" ? onSuccess : void 0,
2558
+ onError: (e) => onError?.({ type: "unknown", description: e.description ?? e.error }),
2559
+ onNonOAuthError: (e) => onError?.(e)
2620
2560
  });
2621
2561
  const s = SIZES[size];
2622
2562
  const radius = shape === "pill" ? s.height / 2 : shape === "square" ? 4 : s.radius;
2623
2563
  const brandMark = theme !== "filled";
2624
- const style = (0, import_react5.useMemo)(
2564
+ const style = (0, import_react4.useMemo)(
2625
2565
  () => ({
2626
2566
  display: "inline-flex",
2627
2567
  alignItems: "center",
@@ -2639,25 +2579,92 @@ function ZorealLogin(props) {
2639
2579
  }),
2640
2580
  [logo_alignment, s, radius, theme, width]
2641
2581
  );
2642
- return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { ...containerProps, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ZorealBusyRing, { busy, radius, theme: theme === "outline" ? "auto" : "light", children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
2582
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { ...containerProps, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
2643
2583
  "button",
2644
2584
  {
2645
2585
  type: "button",
2646
- style: busy ? { ...style, cursor: "progress" } : style,
2647
- disabled: busy,
2648
- "aria-busy": busy,
2649
- onClick: () => {
2650
- if (busy) return;
2586
+ style,
2587
+ onClick: (event) => {
2651
2588
  click_listener?.();
2652
- setBusy(true);
2653
- login();
2589
+ login(event);
2654
2590
  },
2655
2591
  children: [
2656
- /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ZorealMark, { size: s.mark, brand: brandMark }),
2592
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ZorealMark, { size: s.mark, brand: brandMark }),
2657
2593
  type === "standard" && label
2658
2594
  ]
2659
2595
  }
2660
- ) }) });
2596
+ ) });
2597
+ }
2598
+
2599
+ // src/ring.tsx
2600
+ var import_react5 = require("react");
2601
+ var import_jsx_runtime6 = require("react/jsx-runtime");
2602
+ var TAIL2 = 0.3;
2603
+ var STACK2 = Array.from({ length: 12 }, (_, i) => 12 - i);
2604
+ var HALO2 = [3, 2, 1];
2605
+ function ZorealBusyRing({
2606
+ busy,
2607
+ radius = 8,
2608
+ theme = "auto",
2609
+ block = false,
2610
+ className,
2611
+ style,
2612
+ children
2613
+ }) {
2614
+ (0, import_react5.useEffect)(() => {
2615
+ ensureStyles();
2616
+ }, []);
2617
+ const wrapRef = (0, import_react5.useRef)(null);
2618
+ const measureRef = (0, import_react5.useRef)(null);
2619
+ (0, import_react5.useLayoutEffect)(() => {
2620
+ const wrap = wrapRef.current;
2621
+ const rect = measureRef.current;
2622
+ if (!wrap || !rect) return;
2623
+ const measure = () => {
2624
+ const length = rect.getTotalLength();
2625
+ if (length > 0) wrap.style.setProperty("--zrl-ring-len", `${length}px`);
2626
+ };
2627
+ measure();
2628
+ if (typeof ResizeObserver === "undefined") return;
2629
+ const observer = new ResizeObserver(measure);
2630
+ observer.observe(wrap);
2631
+ return () => observer.disconnect();
2632
+ }, [radius]);
2633
+ const rx = radius + 2;
2634
+ const layer = (key, name, len, alpha, ref) => /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
2635
+ "rect",
2636
+ {
2637
+ ref,
2638
+ className: cx(name),
2639
+ rx,
2640
+ ry: rx,
2641
+ style: {
2642
+ strokeDasharray: `calc(var(--zrl-l) * ${len}) calc(var(--zrl-l) * ${1 - len})`,
2643
+ "--zrl-s": `calc(var(--zrl-l) * ${-(TAIL2 - len)})`,
2644
+ opacity: alpha
2645
+ }
2646
+ },
2647
+ key
2648
+ );
2649
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
2650
+ "span",
2651
+ {
2652
+ ref: wrapRef,
2653
+ className: className ? `${cx("root")} ${cx("ring")} ${className}` : `${cx("root")} ${cx("ring")}`,
2654
+ "data-theme": theme,
2655
+ "data-busy": busy,
2656
+ style: block ? { display: "flex", ...style } : style,
2657
+ children: [
2658
+ children,
2659
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("svg", { className: cx("ring-svg"), "aria-hidden": "true", children: [
2660
+ HALO2.map(
2661
+ (n, i) => layer(`h${n}`, "ring-halo", TAIL2 * n / HALO2.length, `calc(var(--zrl-glow-opacity) * 0.6 / ${n})`, i === 0 ? measureRef : void 0)
2662
+ ),
2663
+ STACK2.map((n) => layer(`t${n}`, n <= 2 ? "ring-head" : "ring-tail", TAIL2 * n / STACK2.length, String(1 / n)))
2664
+ ] })
2665
+ ]
2666
+ }
2667
+ );
2661
2668
  }
2662
2669
 
2663
2670
  // src/useZorealAutoLogin.ts