@ichaingo/with-login 1.4.29 → 1.4.31

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,7 +1,32 @@
1
+ import { default as React } from 'react';
2
+ export declare const isLogin: () => boolean;
3
+ export interface UseLoginResult {
4
+ isOpen: boolean;
5
+ openLogin: () => void;
6
+ closeLogin: () => void;
7
+ setLoginOpen: (open: boolean) => void;
8
+ loginUrl: string;
9
+ currentTheme: string;
10
+ iframeLoaded: boolean;
11
+ handleIframeLoad: () => void;
12
+ mounted: boolean;
13
+ Trigger: React.FC<React.HTMLAttributes<HTMLDivElement>>;
14
+ LoginModal: React.FC;
15
+ }
16
+ /**
17
+ * 登录逻辑 + UI 全都在这个 hook 里面
18
+ * - 返回 Trigger:包裹触发登录的元素
19
+ * - 返回 LoginModal:实际渲染登录弹窗(使用 createPortal)
20
+ */
21
+ export declare function useLogin(): UseLoginResult;
1
22
  interface WithLoginProps extends React.PropsWithChildren, React.HTMLAttributes<any> {
2
- children: React.ReactNode;
3
- onClick: () => void;
23
+ children?: React.ReactNode;
4
24
  }
5
- export default function WithLogin({ children, onClick, ...rest }: WithLoginProps): import("react/jsx-runtime").JSX.Element;
6
- export {};
25
+ /**
26
+ * 一个简单封装:
27
+ * - children 作为触发区域
28
+ * - 内部自动渲染 LoginModal
29
+ */
30
+ export declare function WithLogin({ children, ...rest }: WithLoginProps): import("react/jsx-runtime").JSX.Element;
31
+ export default WithLogin;
7
32
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAOA,UAAU,cAAe,SAAQ,KAAK,CAAC,iBAAiB,EAAE,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC;IACjF,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAED,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,cAAc,2CAmF/E"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAEA,OAAO,KAA2C,MAAM,OAAO,CAAC;AAOhE,eAAO,MAAM,OAAO,eAEnB,CAAC;AAEF,MAAM,WAAW,cAAc;IAE7B,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,MAAM,IAAI,CAAC;IACtB,UAAU,EAAE,MAAM,IAAI,CAAC;IACvB,YAAY,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;IACtC,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,OAAO,CAAC;IACtB,gBAAgB,EAAE,MAAM,IAAI,CAAC;IAC7B,OAAO,EAAE,OAAO,CAAC;IAEjB,OAAO,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC;IACxD,UAAU,EAAE,KAAK,CAAC,EAAE,CAAC;CACtB;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,IAAI,cAAc,CAiHzC;AAED,UAAU,cACR,SAAQ,KAAK,CAAC,iBAAiB,EAC7B,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC;IAC3B,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CAC5B;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,EAAE,QAAQ,EAAE,GAAG,IAAI,EAAE,EAAE,cAAc,2CAS9D;AAED,eAAe,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -1,76 +1,170 @@
1
1
  'use client';
2
- import { jsxs as m, Fragment as x, jsx as o } from "react/jsx-runtime";
3
- import { useState as i, useEffect as f } from "react";
4
- import { createPortal as y } from "react-dom";
5
- import { useLocale as v } from "next-intl";
6
- import { twMerge as t } from "tailwind-merge";
7
- import { useTheme as N } from "@ichaingo/next-themes";
8
- function _({ children: d, onClick: T, ...u }) {
9
- const [e, a] = i(!1), p = v(), { resolvedTheme: n } = N(), [r, g] = i(!1), [l, s] = i(!1);
10
- f(() => {
11
- g(!0);
2
+ import { jsx as d, jsxs as x, Fragment as j } from "react/jsx-runtime";
3
+ import { useState as h, useEffect as w, useCallback as f } from "react";
4
+ import { createPortal as k } from "react-dom";
5
+ import { useLocale as R } from "next-intl";
6
+ import { twMerge as L } from "tailwind-merge";
7
+ import { useTheme as B } from "@ichaingo/next-themes";
8
+ /*! js-cookie v3.0.5 | MIT */
9
+ function g(e) {
10
+ for (var r = 1; r < arguments.length; r++) {
11
+ var t = arguments[r];
12
+ for (var a in t)
13
+ e[a] = t[a];
14
+ }
15
+ return e;
16
+ }
17
+ var O = {
18
+ read: function(e) {
19
+ return e[0] === '"' && (e = e.slice(1, -1)), e.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent);
20
+ },
21
+ write: function(e) {
22
+ return encodeURIComponent(e).replace(
23
+ /%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,
24
+ decodeURIComponent
25
+ );
26
+ }
27
+ };
28
+ function C(e, r) {
29
+ function t(o, c, n) {
30
+ if (!(typeof document > "u")) {
31
+ n = g({}, r, n), typeof n.expires == "number" && (n.expires = new Date(Date.now() + n.expires * 864e5)), n.expires && (n.expires = n.expires.toUTCString()), o = encodeURIComponent(o).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent).replace(/[()]/g, escape);
32
+ var s = "";
33
+ for (var i in n)
34
+ n[i] && (s += "; " + i, n[i] !== !0 && (s += "=" + n[i].split(";")[0]));
35
+ return document.cookie = o + "=" + e.write(c, o) + s;
36
+ }
37
+ }
38
+ function a(o) {
39
+ if (!(typeof document > "u" || arguments.length && !o)) {
40
+ for (var c = document.cookie ? document.cookie.split("; ") : [], n = {}, s = 0; s < c.length; s++) {
41
+ var i = c[s].split("="), u = i.slice(1).join("=");
42
+ try {
43
+ var l = decodeURIComponent(i[0]);
44
+ if (n[l] = e.read(u, l), o === l)
45
+ break;
46
+ } catch {
47
+ }
48
+ }
49
+ return o ? n[o] : n;
50
+ }
51
+ }
52
+ return Object.create(
53
+ {
54
+ set: t,
55
+ get: a,
56
+ remove: function(o, c) {
57
+ t(
58
+ o,
59
+ "",
60
+ g({}, c, {
61
+ expires: -1
62
+ })
63
+ );
64
+ },
65
+ withAttributes: function(o) {
66
+ return C(this.converter, g({}, this.attributes, o));
67
+ },
68
+ withConverter: function(o) {
69
+ return C(g({}, this.converter, o), this.attributes);
70
+ }
71
+ },
72
+ {
73
+ attributes: { value: Object.freeze(r) },
74
+ converter: { value: Object.freeze(e) }
75
+ }
76
+ );
77
+ }
78
+ var z = C(O, { path: "/" });
79
+ const b = () => !!z.get("id_token");
80
+ function A() {
81
+ const [e, r] = h(!1), [t, a] = h(!1), [o, c] = h(!1), n = R(), { resolvedTheme: s } = B();
82
+ w(() => {
83
+ a(!0);
12
84
  }, []);
13
- const c = r && n ? n : "light", h = `${process.env.NEXT_PUBLIC_AUTH_URL}/${p}/login?mode=dialog&theme=${c}`, L = () => {
14
- a(!0), s(!1);
15
- }, b = () => a(!1);
16
- f(() => {
17
- e && r && s(!1);
18
- }, [n, e, r]);
19
- const w = () => {
20
- s(!0);
21
- };
22
- return /* @__PURE__ */ m(x, { children: [
23
- /* @__PURE__ */ o("div", { onClick: L, style: { cursor: "pointer" }, ...u, children: d }),
24
- e && y(
25
- /* @__PURE__ */ m(
26
- "div",
27
- {
28
- className: t(
29
- "fixed w-0 h-9 bg-[rgba(0,0,0,0.7)] z-1000",
30
- e && "top-0 left-0 w-full h-full"
85
+ const i = t && s ? s : "light", u = `${process.env.NEXT_PUBLIC_AUTH_URL}/${n}/login?mode=dialog&theme=${i}`, l = f(() => {
86
+ r(!0), c(!1);
87
+ }, []), m = f(() => {
88
+ r(!1);
89
+ }, []), y = f((p) => {
90
+ r(p), p && c(!1);
91
+ }, []);
92
+ w(() => {
93
+ e && t && c(!1);
94
+ }, [s, e, t]);
95
+ const v = f(() => {
96
+ c(!0);
97
+ }, []), U = f(
98
+ ({ children: p, ...T }) => /* @__PURE__ */ d("div", { onClick: l, style: { cursor: "pointer" }, ...T, children: p }),
99
+ [l]
100
+ ), I = f(() => e ? k(
101
+ /* @__PURE__ */ x(
102
+ "div",
103
+ {
104
+ className: L(
105
+ "fixed w-0 h-9 bg-[rgba(0,0,0,0.7)] z-1000",
106
+ e && "top-0 left-0 w-full h-full"
107
+ ),
108
+ children: [
109
+ /* @__PURE__ */ d(
110
+ "div",
111
+ {
112
+ className: L(
113
+ "absolute inset-0 transition-opacity duration-300",
114
+ o ? "opacity-0 pointer-events-none" : "opacity-100 z-30"
115
+ )
116
+ }
31
117
  ),
32
- children: [
33
- /* @__PURE__ */ o(
34
- "div",
35
- {
36
- className: t(
37
- "absolute inset-0 transition-opacity duration-300",
38
- l ? "opacity-0 pointer-events-none" : "opacity-100 z-30"
39
- )
40
- }
41
- ),
42
- /* @__PURE__ */ o(
43
- "iframe",
44
- {
45
- src: h,
46
- className: t(
47
- "w-full h-full border-none relative z-10 transition-opacity duration-300",
48
- l ? "opacity-100" : "opacity-0"
49
- ),
50
- title: "Login",
51
- onLoad: w,
52
- sandbox: "allow-scripts allow-forms allow-same-origin"
53
- },
54
- `login-iframe-${c}`
55
- ),
56
- /* @__PURE__ */ o(
57
- "button",
58
- {
59
- onClick: b,
60
- className: t(
61
- "absolute top-2 right-2 bg-transparent border-none text-2xl cursor-pointer z-20",
62
- "text-white"
63
- ),
64
- children: "×"
65
- }
66
- )
67
- ]
68
- }
69
- ),
70
- document.body
71
- )
118
+ /* @__PURE__ */ d(
119
+ "iframe",
120
+ {
121
+ src: u,
122
+ className: L(
123
+ "w-full h-full border-none relative z-10 transition-opacity duration-300",
124
+ o ? "opacity-100" : "opacity-0"
125
+ ),
126
+ title: "Login",
127
+ onLoad: v,
128
+ sandbox: "allow-scripts allow-forms allow-same-origin"
129
+ },
130
+ `login-iframe-${i}`
131
+ )
132
+ ]
133
+ }
134
+ ),
135
+ document.body
136
+ ) : null, [
137
+ m,
138
+ i,
139
+ v,
140
+ o,
141
+ e,
142
+ u
143
+ ]);
144
+ return {
145
+ isOpen: e,
146
+ openLogin: l,
147
+ closeLogin: m,
148
+ setLoginOpen: y,
149
+ loginUrl: u,
150
+ currentTheme: i,
151
+ iframeLoaded: o,
152
+ handleIframeLoad: v,
153
+ mounted: t,
154
+ Trigger: U,
155
+ LoginModal: I
156
+ };
157
+ }
158
+ function P({ children: e, ...r }) {
159
+ const { Trigger: t, LoginModal: a } = A();
160
+ return /* @__PURE__ */ x(j, { children: [
161
+ e && /* @__PURE__ */ d(t, { ...r, children: e }),
162
+ /* @__PURE__ */ d(a, {})
72
163
  ] });
73
164
  }
74
165
  export {
75
- _ as default
166
+ P as WithLogin,
167
+ P as default,
168
+ b as isLogin,
169
+ A as useLogin
76
170
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ichaingo/with-login",
3
- "version": "1.4.29",
3
+ "version": "1.4.31",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",