@0xchain/with-login 1.1.0-beta.32 → 1.1.0-beta.33

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
@@ -23,24 +23,12 @@ interface LoginProviderProps {
23
23
  children: React.ReactNode;
24
24
  loginSuccessCallback?: () => Promise<void>;
25
25
  }
26
- /**
27
- * LoginProvider 组件:提供全局登录状态和功能
28
- */
29
26
  export declare function LoginProvider({ children, loginSuccessCallback }: LoginProviderProps): import("react/jsx-runtime").JSX.Element;
30
- /**
31
- * useLogin hook:消费 LoginContext
32
- */
33
27
  export declare function useLogin(): LoginContextValue;
34
28
  interface WithLoginProps extends React.PropsWithChildren, React.HTMLAttributes<HTMLElement> {
35
29
  children?: React.ReactNode;
36
30
  onClick?: () => void;
37
31
  }
38
- /**
39
- * 一个简单封装:
40
- * - children 作为触发区域
41
- * - 使用全局 LoginProvider 的登录功能
42
- * - 如果提供了 onClick,会在登录成功后调用
43
- */
44
32
  export declare function WithLogin({ children, onClick, ...rest }: WithLoginProps): import("react/jsx-runtime").JSX.Element | null;
45
33
  export default WithLogin;
46
34
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAEA,OAAO,KAA8E,MAAM,OAAO,CAAC;AAMnG,eAAO,MAAM,OAAO,eAGnB,CAAC;AAEF,oBAAY,gBAAgB;IAC1B,iBAAiB,oBAAoB;IACrC,aAAa,iBAAiB;CAC/B;AAED,oBAAY,aAAa;IACvB,KAAK,UAAU;IACf,QAAQ,aAAa;CACtB;AAED,MAAM,WAAW,iBAAiB;IAEhC,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,CAAC,IAAI,CAAC,EAAE,aAAa,KAAK,IAAI,CAAC;IAC1C,UAAU,EAAE,MAAM,IAAI,CAAC;IACvB,YAAY,EAAE,CAAC,IAAI,EAAE,OA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;IAChC,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,CAAC,IAAI,CAAC,EAAE,aAAa,KAAK,IAAI,CAAC;IAC1C,UAAU,EAAE,MAAM,IAAI,CAAC;IACvB,YAAY,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;IACtC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC;IACxD,4BAA4B,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,MAAM,IAAI,CAAC;CACpF;AAED,UAAU,kBAAkB;IAC1B,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,oBAAoB,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5C;AAuCD,wBAAgB,aAAa,CAAC,EAAE,QAAQ,EAAE,oBAA8C,EAAE,EAAE,kBAAkB,2CAoG7G;AAED,wBAAgB,QAAQ,IAAI,iBAAiB,CAM5C;AAED,UAAU,cACR,SAAQ,KAAK,CAAC,iBAAiB,EAC7B,KAAK,CAAC,cAAc,CAAC,WAAW,CAAC;IACnC,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC3B,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;CACtB;AAED,wBAAgB,SAAS,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,cAAc,kDAcvE;AAED,eAAe,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -19,6 +19,31 @@ var OpenLoginType = /* @__PURE__ */ ((OpenLoginType2) => {
19
19
  return OpenLoginType2;
20
20
  })(OpenLoginType || {});
21
21
  const LoginContext = createContext(void 0);
22
+ function getStorageTheme(storageKey) {
23
+ const fromCookie = Cookies.get(storageKey);
24
+ if (fromCookie) return fromCookie;
25
+ try {
26
+ return window.localStorage.getItem(storageKey);
27
+ } catch {
28
+ return null;
29
+ }
30
+ }
31
+ function resolveThemeNow(resolvedTheme) {
32
+ if (resolvedTheme === "light" || resolvedTheme === "dark") return resolvedTheme;
33
+ if (typeof document !== "undefined" && document.documentElement.classList.contains("dark")) {
34
+ return "dark";
35
+ }
36
+ const storageKey = `${(process.env.NEXT_PUBLIC_BRAND || "ichatgo").toLowerCase()}-theme`;
37
+ const persisted = getStorageTheme(storageKey);
38
+ if (persisted === "dark" || persisted === "light") return persisted;
39
+ if (persisted === "system") {
40
+ return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
41
+ }
42
+ return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
43
+ }
44
+ function buildAuthUrl(locale, path, theme, redirectUri) {
45
+ return `${process.env.NEXT_PUBLIC_AUTH_URL}/${locale}/${path}?mode=dialog&theme=${theme}&redirect_uri=${encodeURIComponent(redirectUri)}`;
46
+ }
22
47
  function LoginProvider({ children, loginSuccessCallback = () => Promise.resolve() }) {
23
48
  const [isOpen, setIsOpen] = useState(false);
24
49
  const [mounted, setMounted] = useState(false);
@@ -26,27 +51,23 @@ function LoginProvider({ children, loginSuccessCallback = () => Promise.resolve(
26
51
  const callbacksRef = useRef(/* @__PURE__ */ new Set());
27
52
  const locale = useLocale();
28
53
  const { resolvedTheme } = useTheme();
29
- const currentTheme = mounted && resolvedTheme ? resolvedTheme : "light";
54
+ const currentTheme = mounted ? resolveThemeNow(resolvedTheme) : "light";
30
55
  const openLogin = useCallback((type) => {
31
- const redirect_uri = typeof window !== "undefined" ? window.location.href : "";
32
- const loginUrl = `${process.env.NEXT_PUBLIC_AUTH_URL}/${locale}/login?mode=dialog&theme=${currentTheme}&redirect_uri=${redirect_uri}`;
33
- const registerUrl = `${process.env.NEXT_PUBLIC_AUTH_URL}/${locale}/register?mode=dialog&theme=${currentTheme}&redirect_uri=${redirect_uri}`;
56
+ const redirectUri = typeof window !== "undefined" ? window.location.href : "";
57
+ const theme = resolveThemeNow(resolvedTheme);
58
+ const loginUrl = buildAuthUrl(locale, "login", theme, redirectUri);
59
+ const registerUrl = buildAuthUrl(locale, "register", theme, redirectUri);
34
60
  setIsOpen(true);
35
61
  setUrl(type === "register" ? registerUrl : loginUrl);
36
- }, [currentTheme, locale]);
62
+ }, [locale, resolvedTheme]);
37
63
  const closeLogin = useCallback(() => {
38
64
  setIsOpen(false);
65
+ setUrl(null);
39
66
  }, []);
40
67
  const setLoginOpen = useCallback((open) => {
41
68
  setIsOpen(open);
69
+ if (!open) setUrl(null);
42
70
  }, []);
43
- useEffect(() => {
44
- if (currentTheme) {
45
- const redirect_uri = typeof window !== "undefined" ? window.location.href : "";
46
- const loginUrl = `${process.env.NEXT_PUBLIC_AUTH_URL}/${locale}/login?mode=dialog&theme=${currentTheme}&redirect_uri=${redirect_uri}`;
47
- setUrl(loginUrl);
48
- }
49
- }, [currentTheme]);
50
71
  useEffect(() => {
51
72
  setMounted(true);
52
73
  const handleMessage = (event) => {
@@ -54,7 +75,6 @@ function LoginProvider({ children, loginSuccessCallback = () => Promise.resolve(
54
75
  closeLogin();
55
76
  }
56
77
  if (event.data?.type === "loginSuccess") {
57
- console.log("handleMessage", event.data.type);
58
78
  closeLogin();
59
79
  window.location.reload();
60
80
  }
@@ -71,39 +91,30 @@ function LoginProvider({ children, loginSuccessCallback = () => Promise.resolve(
71
91
  };
72
92
  }, []);
73
93
  const Trigger = useCallback(
74
- ({ children: children2, ...rest }) => /* @__PURE__ */ jsx("div", { onClick: () => openLogin(), style: { cursor: "pointer" }, ...rest, children: children2 }),
94
+ ({ children: triggerChildren, ...rest }) => /* @__PURE__ */ jsx("div", { onClick: () => openLogin(), style: { cursor: "pointer" }, ...rest, children: triggerChildren }),
75
95
  [openLogin]
76
96
  );
77
97
  const loginModalElement = React.useMemo(() => {
78
- if (isLogin()) return null;
98
+ if (isLogin() || !isOpen || !url) return null;
79
99
  return /* @__PURE__ */ jsx(
80
100
  "div",
81
101
  {
82
102
  className: twMerge(
83
- " left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 w-full h-full bg-[rgba(0,0,0,0.7)] opacity-0 z-[-1]",
84
- "transition-all duration-500 zoom-in-out fixed block",
85
- isOpen && "z-99999 opacity-100"
103
+ "fixed left-1/2 top-1/2 z-99999 block h-full w-full -translate-x-1/2 -translate-y-1/2",
104
+ "bg-[rgba(0,0,0,0.7)] opacity-100 transition-all duration-500 zoom-in-out"
86
105
  ),
87
- children: url && /* @__PURE__ */ jsx(
106
+ children: /* @__PURE__ */ jsx(
88
107
  "iframe",
89
108
  {
90
- src: url ?? "",
91
- className: twMerge(
92
- "w-full h-full border-none relative z-10 transition-opacity duration-300",
93
- "opacity-100"
94
- ),
109
+ src: url,
110
+ className: "relative z-10 h-full w-full border-none opacity-100 transition-opacity duration-300",
95
111
  title: "Login",
96
112
  sandbox: "allow-scripts allow-forms allow-same-origin"
97
- },
98
- `login-iframe-${currentTheme}`
113
+ }
99
114
  )
100
115
  }
101
116
  );
102
- }, [
103
- currentTheme,
104
- isOpen,
105
- url
106
- ]);
117
+ }, [isOpen, url]);
107
118
  const value = {
108
119
  isOpen,
109
120
  openLogin,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xchain/with-login",
3
- "version": "1.1.0-beta.32",
3
+ "version": "1.1.0-beta.33",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -18,24 +18,24 @@
18
18
  "!**/*.tsbuildinfo"
19
19
  ],
20
20
  "peerDependencies": {
21
- "@0xchain/next-themes": "1.0.0",
22
- "js-cookie": "3.0.5",
23
- "next": "16.1.6",
24
- "next-intl": "4.8.2",
25
- "react": "19.1.1",
26
- "react-dom": "19.1.1",
27
- "tailwind-merge": "3.3.1"
21
+ "@0xchain/next-themes": "catalog:",
22
+ "js-cookie": "catalog:",
23
+ "next": "catalog:",
24
+ "next-intl": "catalog:",
25
+ "react": "catalog:",
26
+ "react-dom": "catalog:",
27
+ "tailwind-merge": "catalog:"
28
28
  },
29
29
  "devDependencies": {
30
- "@0xchain/next-themes": "1.0.0",
31
- "@types/js-cookie": "3.0.6",
32
- "js-cookie": "3.0.5",
33
- "next": "16.1.6",
34
- "next-intl": "4.8.2",
35
- "react": "19.1.1",
36
- "react-dom": "19.1.1",
37
- "rollup-plugin-preserve-use-client": "3.0.1",
38
- "tailwind-merge": "3.3.1"
30
+ "@0xchain/next-themes": "catalog:",
31
+ "@types/js-cookie": "catalog:",
32
+ "js-cookie": "catalog:",
33
+ "next": "catalog:",
34
+ "next-intl": "catalog:",
35
+ "react": "catalog:",
36
+ "react-dom": "catalog:",
37
+ "rollup-plugin-preserve-use-client": "catalog:",
38
+ "tailwind-merge": "catalog:"
39
39
  },
40
40
  "nx": {
41
41
  "tags": [
@@ -45,4 +45,4 @@
45
45
  "publishConfig": {
46
46
  "access": "public"
47
47
  }
48
- }
48
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2024-present 0xchain
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.