next-language-selector 0.1.6 → 0.1.7

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
@@ -20,17 +20,95 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ LanguageSelector: () => LanguageSelector,
23
24
  setLocaleCookie: () => setLocaleCookie
24
25
  });
25
26
  module.exports = __toCommonJS(index_exports);
26
27
 
28
+ // src/selector.tsx
29
+ var import_react = require("react");
30
+
27
31
  // src/utils.ts
28
32
  var setLocaleCookie = (locale, cookieName = "NEXT_LOCALE") => {
29
33
  if (typeof document === "undefined") return;
30
34
  document.cookie = `${cookieName}=${locale}; max-age=31536000; path=/`;
31
35
  window.location.reload();
32
36
  };
37
+
38
+ // src/selector.tsx
39
+ var import_jsx_runtime = require("react/jsx-runtime");
40
+ function LanguageSelector(props) {
41
+ const {
42
+ locales,
43
+ defaultLocale,
44
+ cookieName = "NEXT_LOCALE",
45
+ isDropdown = true,
46
+ renderCustom,
47
+ className,
48
+ activeColor = "red"
49
+ } = props;
50
+ const [current, setCurrent] = (0, import_react.useState)(defaultLocale);
51
+ (0, import_react.useEffect)(() => {
52
+ const saved = document.cookie.split("; ").find((row) => row.startsWith(`${cookieName}=`))?.split("=")[1];
53
+ if (saved) setCurrent(saved);
54
+ }, [cookieName]);
55
+ const handleSelect = (code) => {
56
+ setCurrent(code);
57
+ setLocaleCookie(code, cookieName);
58
+ };
59
+ if (renderCustom) {
60
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: renderCustom({
61
+ locales,
62
+ currentLocale: current,
63
+ onChange: handleSelect
64
+ }) });
65
+ }
66
+ if (!isDropdown) {
67
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
68
+ "div",
69
+ {
70
+ style: { display: "flex", gap: "8px", alignItems: "center" },
71
+ className,
72
+ children: locales.map((l) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
73
+ "button",
74
+ {
75
+ onClick: () => handleSelect(l.code),
76
+ style: {
77
+ cursor: "pointer",
78
+ background: "none",
79
+ border: "none",
80
+ borderBottom: current === l.code ? `1px solid ${activeColor}` : "1px solid transparent",
81
+ color: current === l.code ? "inherit" : "gray",
82
+ fontSize: "12px",
83
+ padding: "2px 4px"
84
+ },
85
+ children: [
86
+ l.flag,
87
+ " ",
88
+ l.name
89
+ ]
90
+ },
91
+ l.code
92
+ ))
93
+ }
94
+ );
95
+ }
96
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
97
+ "select",
98
+ {
99
+ value: current,
100
+ onChange: (e) => handleSelect(e.target.value),
101
+ className,
102
+ children: locales.map((l) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("option", { value: l.code, children: [
103
+ l.flag,
104
+ " ",
105
+ l.name
106
+ ] }, l.code))
107
+ }
108
+ );
109
+ }
33
110
  // Annotate the CommonJS export names for ESM import in node:
34
111
  0 && (module.exports = {
112
+ LanguageSelector,
35
113
  setLocaleCookie
36
114
  });
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import React$1, { ReactNode, ComponentProps } from 'react';
1
+ import React, { ReactNode } from 'react';
2
2
 
3
3
  interface LocaleConfig {
4
4
  name: string;
@@ -19,10 +19,8 @@ interface LanguageSelectorProps {
19
19
  }) => ReactNode;
20
20
  }
21
21
 
22
- declare function LanguageSelector$1(props: LanguageSelectorProps): React$1.JSX.Element;
22
+ declare function LanguageSelector(props: LanguageSelectorProps): React.JSX.Element;
23
23
 
24
24
  declare const setLocaleCookie: (locale: string, cookieName?: string) => void;
25
25
 
26
- declare const LanguageSelector: (props: ComponentProps<typeof LanguageSelector$1>) => React.JSX.Element;
27
-
28
26
  export { LanguageSelector, type LanguageSelectorProps, type LocaleConfig, setLocaleCookie };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import React$1, { ReactNode, ComponentProps } from 'react';
1
+ import React, { ReactNode } from 'react';
2
2
 
3
3
  interface LocaleConfig {
4
4
  name: string;
@@ -19,10 +19,8 @@ interface LanguageSelectorProps {
19
19
  }) => ReactNode;
20
20
  }
21
21
 
22
- declare function LanguageSelector$1(props: LanguageSelectorProps): React$1.JSX.Element;
22
+ declare function LanguageSelector(props: LanguageSelectorProps): React.JSX.Element;
23
23
 
24
24
  declare const setLocaleCookie: (locale: string, cookieName?: string) => void;
25
25
 
26
- declare const LanguageSelector: (props: ComponentProps<typeof LanguageSelector$1>) => React.JSX.Element;
27
-
28
26
  export { LanguageSelector, type LanguageSelectorProps, type LocaleConfig, setLocaleCookie };
package/dist/index.mjs CHANGED
@@ -1,9 +1,86 @@
1
+ // src/selector.tsx
2
+ import { useEffect, useState } from "react";
3
+
1
4
  // src/utils.ts
2
5
  var setLocaleCookie = (locale, cookieName = "NEXT_LOCALE") => {
3
6
  if (typeof document === "undefined") return;
4
7
  document.cookie = `${cookieName}=${locale}; max-age=31536000; path=/`;
5
8
  window.location.reload();
6
9
  };
10
+
11
+ // src/selector.tsx
12
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
13
+ function LanguageSelector(props) {
14
+ const {
15
+ locales,
16
+ defaultLocale,
17
+ cookieName = "NEXT_LOCALE",
18
+ isDropdown = true,
19
+ renderCustom,
20
+ className,
21
+ activeColor = "red"
22
+ } = props;
23
+ const [current, setCurrent] = useState(defaultLocale);
24
+ useEffect(() => {
25
+ const saved = document.cookie.split("; ").find((row) => row.startsWith(`${cookieName}=`))?.split("=")[1];
26
+ if (saved) setCurrent(saved);
27
+ }, [cookieName]);
28
+ const handleSelect = (code) => {
29
+ setCurrent(code);
30
+ setLocaleCookie(code, cookieName);
31
+ };
32
+ if (renderCustom) {
33
+ return /* @__PURE__ */ jsx(Fragment, { children: renderCustom({
34
+ locales,
35
+ currentLocale: current,
36
+ onChange: handleSelect
37
+ }) });
38
+ }
39
+ if (!isDropdown) {
40
+ return /* @__PURE__ */ jsx(
41
+ "div",
42
+ {
43
+ style: { display: "flex", gap: "8px", alignItems: "center" },
44
+ className,
45
+ children: locales.map((l) => /* @__PURE__ */ jsxs(
46
+ "button",
47
+ {
48
+ onClick: () => handleSelect(l.code),
49
+ style: {
50
+ cursor: "pointer",
51
+ background: "none",
52
+ border: "none",
53
+ borderBottom: current === l.code ? `1px solid ${activeColor}` : "1px solid transparent",
54
+ color: current === l.code ? "inherit" : "gray",
55
+ fontSize: "12px",
56
+ padding: "2px 4px"
57
+ },
58
+ children: [
59
+ l.flag,
60
+ " ",
61
+ l.name
62
+ ]
63
+ },
64
+ l.code
65
+ ))
66
+ }
67
+ );
68
+ }
69
+ return /* @__PURE__ */ jsx(
70
+ "select",
71
+ {
72
+ value: current,
73
+ onChange: (e) => handleSelect(e.target.value),
74
+ className,
75
+ children: locales.map((l) => /* @__PURE__ */ jsxs("option", { value: l.code, children: [
76
+ l.flag,
77
+ " ",
78
+ l.name
79
+ ] }, l.code))
80
+ }
81
+ );
82
+ }
7
83
  export {
84
+ LanguageSelector,
8
85
  setLocaleCookie
9
86
  };
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "next-language-selector",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Configurable language selector for Next.js",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
7
7
  "types": "./dist/index.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
- "types": "./dist/index.d.ts",
11
10
  "import": "./dist/index.mjs",
12
11
  "require": "./dist/index.cjs"
13
12
  }