next-language-selector 0.2.3 → 0.2.5
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/README.md +5 -0
- package/dist/index.cjs +1 -120
- package/dist/index.mjs +1 -92
- package/package.json +8 -2
package/README.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# next-language-selector
|
|
2
2
|
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
3
8
|
A lightweight, configurable language selector for Next.js (App Router & Pages Router).
|
|
4
9
|
Efficiently manages the `NEXT_LOCALE` cookie and works seamlessly with `next-intl` or any other i18n solution.
|
|
5
10
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,120 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
LanguageSelector: () => LanguageSelector2,
|
|
24
|
-
setLocaleCookie: () => setLocaleCookie
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(index_exports);
|
|
27
|
-
|
|
28
|
-
// src/selector.tsx
|
|
29
|
-
var import_react = require("react");
|
|
30
|
-
|
|
31
|
-
// src/utils.ts
|
|
32
|
-
var setLocaleCookie = (locale, cookieName = "NEXT_LOCALE", autoReload = true) => {
|
|
33
|
-
if (typeof document === "undefined") return;
|
|
34
|
-
document.cookie = `${cookieName}=${locale}; max-age=31536000; path=/`;
|
|
35
|
-
if (autoReload) {
|
|
36
|
-
window.location.reload();
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
// src/selector.tsx
|
|
41
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
42
|
-
function LanguageSelector(props) {
|
|
43
|
-
const {
|
|
44
|
-
locales,
|
|
45
|
-
defaultLocale,
|
|
46
|
-
cookieName = "NEXT_LOCALE",
|
|
47
|
-
isDropdown = false,
|
|
48
|
-
autoReload = true,
|
|
49
|
-
renderCustom,
|
|
50
|
-
className,
|
|
51
|
-
activeColor = "red"
|
|
52
|
-
} = props;
|
|
53
|
-
const [current, setCurrent] = (0, import_react.useState)(defaultLocale);
|
|
54
|
-
(0, import_react.useEffect)(() => {
|
|
55
|
-
const saved = document.cookie.split("; ").find((row) => row.startsWith(`${cookieName}=`))?.split("=")[1];
|
|
56
|
-
if (saved) setCurrent(saved);
|
|
57
|
-
}, [cookieName]);
|
|
58
|
-
const handleSelect = (code) => {
|
|
59
|
-
setCurrent(code);
|
|
60
|
-
setLocaleCookie(code, cookieName, autoReload);
|
|
61
|
-
};
|
|
62
|
-
if (renderCustom) {
|
|
63
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: renderCustom({
|
|
64
|
-
locales,
|
|
65
|
-
currentLocale: current,
|
|
66
|
-
onChange: handleSelect
|
|
67
|
-
}) });
|
|
68
|
-
}
|
|
69
|
-
if (!isDropdown) {
|
|
70
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
71
|
-
"div",
|
|
72
|
-
{
|
|
73
|
-
style: { display: "flex", gap: "8px", alignItems: "center" },
|
|
74
|
-
className,
|
|
75
|
-
children: locales.map((l) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
76
|
-
"button",
|
|
77
|
-
{
|
|
78
|
-
onClick: () => handleSelect(l.code),
|
|
79
|
-
style: {
|
|
80
|
-
cursor: "pointer",
|
|
81
|
-
background: "none",
|
|
82
|
-
border: "none",
|
|
83
|
-
borderBottom: current === l.code ? `1px solid ${activeColor}` : "1px solid transparent",
|
|
84
|
-
color: current === l.code ? "inherit" : "gray",
|
|
85
|
-
fontSize: "12px",
|
|
86
|
-
padding: "2px 4px"
|
|
87
|
-
},
|
|
88
|
-
children: [
|
|
89
|
-
l.flag,
|
|
90
|
-
" ",
|
|
91
|
-
l.name
|
|
92
|
-
]
|
|
93
|
-
},
|
|
94
|
-
l.code
|
|
95
|
-
))
|
|
96
|
-
}
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
100
|
-
"select",
|
|
101
|
-
{
|
|
102
|
-
value: current,
|
|
103
|
-
onChange: (e) => handleSelect(e.target.value),
|
|
104
|
-
className,
|
|
105
|
-
children: locales.map((l) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("option", { value: l.code, children: [
|
|
106
|
-
l.flag,
|
|
107
|
-
" ",
|
|
108
|
-
l.name
|
|
109
|
-
] }, l.code))
|
|
110
|
-
}
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// src/index.ts
|
|
115
|
-
var LanguageSelector2 = LanguageSelector;
|
|
116
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
117
|
-
0 && (module.exports = {
|
|
118
|
-
LanguageSelector,
|
|
119
|
-
setLocaleCookie
|
|
120
|
-
});
|
|
1
|
+
"use strict";var u=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var v=(t,o)=>{for(var a in o)u(t,a,{get:o[a],enumerable:!0})},E=(t,o,a,c)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of k(o))!y.call(t,n)&&n!==a&&u(t,n,{get:()=>o[n],enumerable:!(c=x(o,n))||c.enumerable});return t};var b=t=>E(u({},"__esModule",{value:!0}),t);var N={};v(N,{LanguageSelector:()=>h,setLocaleCookie:()=>i});module.exports=b(N);var p=require("react");var i=(t,o="NEXT_LOCALE",a=!0)=>{typeof document>"u"||(document.cookie=`${o}=${t}; max-age=31536000; path=/`,a&&window.location.reload())};var r=require("react/jsx-runtime");function f(t){let{locales:o,defaultLocale:a,cookieName:c="NEXT_LOCALE",isDropdown:n=!1,autoReload:L=!0,renderCustom:g,className:m,activeColor:C="red"}=t,[s,d]=(0,p.useState)(a);(0,p.useEffect)(()=>{let e=document.cookie.split("; ").find(S=>S.startsWith(`${c}=`))?.split("=")[1];e&&d(e)},[c]);let l=e=>{d(e),i(e,c,L)};return g?(0,r.jsx)(r.Fragment,{children:g({locales:o,currentLocale:s,onChange:l})}):n?(0,r.jsx)("select",{value:s,onChange:e=>l(e.target.value),className:m,children:o.map(e=>(0,r.jsxs)("option",{value:e.code,children:[e.flag," ",e.name]},e.code))}):(0,r.jsx)("div",{style:{display:"flex",gap:"8px",alignItems:"center"},className:m,children:o.map(e=>(0,r.jsxs)("button",{onClick:()=>l(e.code),style:{cursor:"pointer",background:"none",border:"none",borderBottom:s===e.code?`1px solid ${C}`:"1px solid transparent",color:s===e.code?"inherit":"gray",fontSize:"12px",padding:"2px 4px"},children:[e.flag," ",e.name]},e.code))})}var h=f;0&&(module.exports={LanguageSelector,setLocaleCookie});
|
package/dist/index.mjs
CHANGED
|
@@ -1,92 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { useEffect, useState } from "react";
|
|
3
|
-
|
|
4
|
-
// src/utils.ts
|
|
5
|
-
var setLocaleCookie = (locale, cookieName = "NEXT_LOCALE", autoReload = true) => {
|
|
6
|
-
if (typeof document === "undefined") return;
|
|
7
|
-
document.cookie = `${cookieName}=${locale}; max-age=31536000; path=/`;
|
|
8
|
-
if (autoReload) {
|
|
9
|
-
window.location.reload();
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
// src/selector.tsx
|
|
14
|
-
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
15
|
-
function LanguageSelector(props) {
|
|
16
|
-
const {
|
|
17
|
-
locales,
|
|
18
|
-
defaultLocale,
|
|
19
|
-
cookieName = "NEXT_LOCALE",
|
|
20
|
-
isDropdown = false,
|
|
21
|
-
autoReload = true,
|
|
22
|
-
renderCustom,
|
|
23
|
-
className,
|
|
24
|
-
activeColor = "red"
|
|
25
|
-
} = props;
|
|
26
|
-
const [current, setCurrent] = useState(defaultLocale);
|
|
27
|
-
useEffect(() => {
|
|
28
|
-
const saved = document.cookie.split("; ").find((row) => row.startsWith(`${cookieName}=`))?.split("=")[1];
|
|
29
|
-
if (saved) setCurrent(saved);
|
|
30
|
-
}, [cookieName]);
|
|
31
|
-
const handleSelect = (code) => {
|
|
32
|
-
setCurrent(code);
|
|
33
|
-
setLocaleCookie(code, cookieName, autoReload);
|
|
34
|
-
};
|
|
35
|
-
if (renderCustom) {
|
|
36
|
-
return /* @__PURE__ */ jsx(Fragment, { children: renderCustom({
|
|
37
|
-
locales,
|
|
38
|
-
currentLocale: current,
|
|
39
|
-
onChange: handleSelect
|
|
40
|
-
}) });
|
|
41
|
-
}
|
|
42
|
-
if (!isDropdown) {
|
|
43
|
-
return /* @__PURE__ */ jsx(
|
|
44
|
-
"div",
|
|
45
|
-
{
|
|
46
|
-
style: { display: "flex", gap: "8px", alignItems: "center" },
|
|
47
|
-
className,
|
|
48
|
-
children: locales.map((l) => /* @__PURE__ */ jsxs(
|
|
49
|
-
"button",
|
|
50
|
-
{
|
|
51
|
-
onClick: () => handleSelect(l.code),
|
|
52
|
-
style: {
|
|
53
|
-
cursor: "pointer",
|
|
54
|
-
background: "none",
|
|
55
|
-
border: "none",
|
|
56
|
-
borderBottom: current === l.code ? `1px solid ${activeColor}` : "1px solid transparent",
|
|
57
|
-
color: current === l.code ? "inherit" : "gray",
|
|
58
|
-
fontSize: "12px",
|
|
59
|
-
padding: "2px 4px"
|
|
60
|
-
},
|
|
61
|
-
children: [
|
|
62
|
-
l.flag,
|
|
63
|
-
" ",
|
|
64
|
-
l.name
|
|
65
|
-
]
|
|
66
|
-
},
|
|
67
|
-
l.code
|
|
68
|
-
))
|
|
69
|
-
}
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
return /* @__PURE__ */ jsx(
|
|
73
|
-
"select",
|
|
74
|
-
{
|
|
75
|
-
value: current,
|
|
76
|
-
onChange: (e) => handleSelect(e.target.value),
|
|
77
|
-
className,
|
|
78
|
-
children: locales.map((l) => /* @__PURE__ */ jsxs("option", { value: l.code, children: [
|
|
79
|
-
l.flag,
|
|
80
|
-
" ",
|
|
81
|
-
l.name
|
|
82
|
-
] }, l.code))
|
|
83
|
-
}
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// src/index.ts
|
|
88
|
-
var LanguageSelector2 = LanguageSelector;
|
|
89
|
-
export {
|
|
90
|
-
LanguageSelector2 as LanguageSelector,
|
|
91
|
-
setLocaleCookie
|
|
92
|
-
};
|
|
1
|
+
import{useEffect as S,useState as x}from"react";var s=(r,o="NEXT_LOCALE",a=!0)=>{typeof document>"u"||(document.cookie=`${o}=${r}; max-age=31536000; path=/`,a&&window.location.reload())};import{Fragment as k,jsx as i,jsxs as g}from"react/jsx-runtime";function m(r){let{locales:o,defaultLocale:a,cookieName:n="NEXT_LOCALE",isDropdown:d=!1,autoReload:f=!0,renderCustom:p,className:l,activeColor:L="red"}=r,[t,u]=x(a);S(()=>{let e=document.cookie.split("; ").find(C=>C.startsWith(`${n}=`))?.split("=")[1];e&&u(e)},[n]);let c=e=>{u(e),s(e,n,f)};return p?i(k,{children:p({locales:o,currentLocale:t,onChange:c})}):d?i("select",{value:t,onChange:e=>c(e.target.value),className:l,children:o.map(e=>g("option",{value:e.code,children:[e.flag," ",e.name]},e.code))}):i("div",{style:{display:"flex",gap:"8px",alignItems:"center"},className:l,children:o.map(e=>g("button",{onClick:()=>c(e.code),style:{cursor:"pointer",background:"none",border:"none",borderBottom:t===e.code?`1px solid ${L}`:"1px solid transparent",color:t===e.code?"inherit":"gray",fontSize:"12px",padding:"2px 4px"},children:[e.flag," ",e.name]},e.code))})}var X=m;export{X as LanguageSelector,s as setLocaleCookie};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-language-selector",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "Configurable language selector for Next.js",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -10,11 +10,17 @@
|
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"import": "./dist/index.mjs",
|
|
12
12
|
"require": "./dist/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
"./selector": {
|
|
15
|
+
"types": "./dist/selector.d.ts",
|
|
16
|
+
"import": "./dist/selector.mjs",
|
|
17
|
+
"require": "./dist/selector.cjs"
|
|
13
18
|
}
|
|
14
19
|
},
|
|
15
20
|
"scripts": {
|
|
16
21
|
"build": "tsup",
|
|
17
|
-
"dev": "tsup --watch"
|
|
22
|
+
"dev": "tsup --watch",
|
|
23
|
+
"lint": "tsc --noEmit"
|
|
18
24
|
},
|
|
19
25
|
"files": [
|
|
20
26
|
"dist"
|