telpick 1.0.8 → 1.0.9

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.

Potentially problematic release.


This version of telpick might be problematic. Click here for more details.

@@ -553,24 +553,19 @@ const A = [
553
553
  }
554
554
  ];
555
555
  class E {
556
- constructor({ code: t = null, onChange: d = () => {
557
- }, styleOverrides: c = {} } = {}) {
558
- this.code = t, this.onChange = d, this.styleOverrides = c, this.codes = [], this.selectedCode = t, this.isDropdownOpen = !1, this.searchQuery = "", this.container = null, this.dropdown = null, this._outsideHandler = null;
556
+ constructor({ code: c = null, onChange: s = () => {
557
+ }, styleOverrides: t = {} } = {}) {
558
+ this.code = c, this.onChange = s, this.styleOverrides = t, this.codes = [], this.selectedCode = c, this.isDropdownOpen = !1, this.searchQuery = "", this.container = null, this.dropdown = null, this._outsideHandler = null;
559
559
  }
560
- _getAssetsBase() {
561
- if (typeof import.meta < "u" && import.meta.url)
562
- try {
563
- return new URL("./assets", self.location).href + "/";
564
- } catch {
565
- }
566
- return "";
560
+ _getFlagUrl(c) {
561
+ return c ? `https://flagcdn.com/w40/${String(c).toLowerCase()}.png` : "";
567
562
  }
568
- async init(t) {
569
- if (this.container = t, this.codes = [...A].sort((c, a) => c.country.localeCompare(a.country, "es")), this._assetsBase = this._getAssetsBase(), this.code) {
570
- const c = this.codes.find((a) => a.country_code === this.code);
571
- c && (this.selectedCode = c.country_code);
563
+ async init(c) {
564
+ if (this.container = c, this.codes = [...A].sort((t, a) => t.country.localeCompare(a.country, "es")), this.code) {
565
+ const t = this.codes.find((a) => a.country_code === this.code);
566
+ t && (this.selectedCode = t.country_code);
572
567
  } else {
573
- const c = [
568
+ const t = [
574
569
  async () => {
575
570
  try {
576
571
  const e = await (await fetch("https://ip-api.com/json/?fields=countryCode")).json();
@@ -600,7 +595,7 @@ class E {
600
595
  }
601
596
  ];
602
597
  let a = null;
603
- for (const n of c)
598
+ for (const n of t)
604
599
  try {
605
600
  if (a = await Promise.race([
606
601
  n(),
@@ -685,68 +680,68 @@ class E {
685
680
  }
686
681
  }
687
682
  this.render();
688
- const d = this.codes.find((c) => c.country_code === this.selectedCode);
689
- d && this.onChange(d), this._setupOutsideClick();
683
+ const s = this.codes.find((t) => t.country_code === this.selectedCode);
684
+ s && this.onChange(s), this._setupOutsideClick();
690
685
  }
691
686
  render() {
692
687
  this.container.innerHTML = "";
693
- const t = document.createElement("button");
694
- t.className = "telpick-btn", Object.assign(t.style, this.styleOverrides), t.onclick = () => {
688
+ const c = document.createElement("button");
689
+ c.className = "telpick-btn", Object.assign(c.style, this.styleOverrides), c.onclick = () => {
695
690
  this.isDropdownOpen = !this.isDropdownOpen, this.render();
696
691
  };
697
- const d = document.createElement("div");
698
- d.className = "telpick-flag";
699
- const c = this.codes.find((e) => e.country_code === this.selectedCode) || { flag: "", code: "", country: "" };
700
- if (c.flag) {
692
+ const s = document.createElement("div");
693
+ s.className = "telpick-flag";
694
+ const t = this.codes.find((e) => e.country_code === this.selectedCode) || { flag: "", code: "", country: "" };
695
+ if (t.flag) {
701
696
  const e = document.createElement("img");
702
- e.src = (this._assetsBase || "src/assets") + (c.flag.startsWith("/") ? c.flag : "/" + c.flag), e.className = "w-full h-full object-cover", e.alt = "flag", d.appendChild(e);
697
+ e.src = this._getFlagUrl(t.country_code), e.className = "w-full h-full object-cover", e.alt = "flag", s.appendChild(e);
703
698
  }
704
- t.appendChild(d);
699
+ c.appendChild(s);
705
700
  const a = document.createElement("span");
706
- a.textContent = c.code, t.appendChild(a);
701
+ a.textContent = t.code, c.appendChild(a);
707
702
  const n = document.createElement("span");
708
- if (n.className = "ml-auto", n.textContent = "▼", t.appendChild(n), this.container.appendChild(t), this.isDropdownOpen) {
703
+ if (n.className = "ml-auto", n.textContent = "▼", c.appendChild(n), this.container.appendChild(c), this.isDropdownOpen) {
709
704
  this.dropdown = document.createElement("div"), this.dropdown.className = "telpick-dropdown", this.dropdown.onclick = (o) => o.stopPropagation(), this.dropdown.onmousedown = (o) => o.stopPropagation();
710
705
  const e = document.createElement("input");
711
706
  e.className = "telpick-search", e.type = "text", e.placeholder = "Buscar país...", e.value = this.searchQuery, e.oninput = (o) => {
712
- var g;
707
+ var y;
713
708
  o.stopPropagation();
714
- const l = o.target, y = l.selectionStart || 0, u = l.value;
709
+ const l = o.target, f = l.selectionStart || 0, u = l.value;
715
710
  this.searchQuery = u;
716
- const i = (g = this.dropdown) == null ? void 0 : g.querySelector("ul");
717
- i ? (i.innerHTML = "", (this.searchQuery ? this.codes.filter((s) => s.country.toLowerCase().includes(this.searchQuery.toLowerCase())) : this.codes).forEach((s) => {
718
- const f = document.createElement("li"), C = s.country_code === this.selectedCode && this.selectedCode !== null && this.selectedCode !== void 0;
719
- f.className = `telpick-item ${C ? "telpick-item-selected" : ""}`, f.onclick = () => {
720
- this.selectedCode = s.country_code, this.onChange(s), this.isDropdownOpen = !1, this.searchQuery = "", this.render();
711
+ const i = (y = this.dropdown) == null ? void 0 : y.querySelector("ul");
712
+ i ? (i.innerHTML = "", (this.searchQuery ? this.codes.filter((d) => d.country.toLowerCase().includes(this.searchQuery.toLowerCase())) : this.codes).forEach((d) => {
713
+ const g = document.createElement("li"), C = d.country_code === this.selectedCode && this.selectedCode !== null && this.selectedCode !== void 0;
714
+ g.className = `telpick-item ${C ? "telpick-item-selected" : ""}`, g.onclick = () => {
715
+ this.selectedCode = d.country_code, this.onChange(d), this.isDropdownOpen = !1, this.searchQuery = "", this.render();
721
716
  };
722
717
  const w = document.createElement("div");
723
718
  w.className = "telpick-flag";
724
719
  const h = document.createElement("img");
725
- h.src = (this._assetsBase || "src/assets") + (s.flag.startsWith("/") ? s.flag : "/" + s.flag), h.className = "w-full h-full object-cover", h.alt = "flag", w.appendChild(h), f.appendChild(w);
720
+ h.src = this._getFlagUrl(d.country_code), h.className = "w-full h-full object-cover", h.alt = "flag", w.appendChild(h), g.appendChild(w);
726
721
  const b = document.createElement("span");
727
- b.textContent = s.country, f.appendChild(b);
722
+ b.textContent = d.country, g.appendChild(b);
728
723
  const _ = document.createElement("span");
729
- _.className = "ml-auto", _.textContent = s.code, f.appendChild(_), i.appendChild(f);
724
+ _.className = "ml-auto", _.textContent = d.code, g.appendChild(_), i.appendChild(g);
730
725
  }), requestAnimationFrame(() => {
731
726
  if (e) {
732
727
  e.focus();
733
- const s = Math.min(y + 1, u.length);
734
- e.setSelectionRange(s, s);
728
+ const d = Math.min(f + 1, u.length);
729
+ e.setSelectionRange(d, d);
735
730
  }
736
731
  })) : this.render();
737
732
  }, e.onclick = (o) => o.stopPropagation(), e.onmousedown = (o) => o.stopPropagation(), this.dropdown.appendChild(e);
738
733
  const r = document.createElement("ul");
739
734
  r.style.maxHeight = "130px", r.style.overflowY = "auto", (this.searchQuery ? this.codes.filter((o) => o.country.toLowerCase().includes(this.searchQuery.toLowerCase())) : this.codes).forEach((o) => {
740
- const l = document.createElement("li"), y = o.country_code === this.selectedCode && this.selectedCode !== null && this.selectedCode !== void 0;
741
- l.className = `telpick-item ${y ? "telpick-item-selected" : ""}`, l.setAttribute("aria-selected", y), l.onclick = () => {
735
+ const l = document.createElement("li"), f = o.country_code === this.selectedCode && this.selectedCode !== null && this.selectedCode !== void 0;
736
+ l.className = `telpick-item ${f ? "telpick-item-selected" : ""}`, l.setAttribute("aria-selected", f), l.onclick = () => {
742
737
  this.selectedCode = o.country_code, this.onChange(o), this.isDropdownOpen = !1, this.searchQuery = "", this.render();
743
738
  };
744
739
  const u = document.createElement("div");
745
740
  u.className = "telpick-flag";
746
741
  const i = document.createElement("img");
747
- i.src = (this._assetsBase || "src/assets") + (o.flag.startsWith("/") ? o.flag : "/" + o.flag), i.className = "w-full h-full object-cover", i.alt = "flag", u.appendChild(i), l.appendChild(u);
748
- const g = document.createElement("span");
749
- g.textContent = o.country, l.appendChild(g);
742
+ i.src = this._getFlagUrl(o.country_code), i.className = "w-full h-full object-cover", i.alt = "flag", u.appendChild(i), l.appendChild(u);
743
+ const y = document.createElement("span");
744
+ y.textContent = o.country, l.appendChild(y);
750
745
  const p = document.createElement("span");
751
746
  p.className = "ml-auto", p.textContent = o.code, l.appendChild(p), r.appendChild(l);
752
747
  }), this.dropdown.appendChild(r), this.container.appendChild(this.dropdown), requestAnimationFrame(() => {
@@ -755,11 +750,11 @@ class E {
755
750
  }
756
751
  }
757
752
  _setupOutsideClick() {
758
- this._outsideHandler && document.removeEventListener("click", this._outsideHandler), this._outsideHandler = (t) => {
759
- const d = t.target;
760
- if (this.isDropdownOpen && this.container && d) {
761
- const c = this.container.querySelector(".telpick-dropdown");
762
- !this.container.contains(d) && (!c || !c.contains(d)) && (this.isDropdownOpen = !1, this.render());
753
+ this._outsideHandler && document.removeEventListener("click", this._outsideHandler), this._outsideHandler = (c) => {
754
+ const s = c.target;
755
+ if (this.isDropdownOpen && this.container && s) {
756
+ const t = this.container.querySelector(".telpick-dropdown");
757
+ !this.container.contains(s) && (!t || !t.contains(s)) && (this.isDropdownOpen = !1, this.render());
763
758
  }
764
759
  }, document.addEventListener("click", this._outsideHandler, !0);
765
760
  }
@@ -1 +1 @@
1
- (function(f,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(f=typeof globalThis<"u"?globalThis:f||self,l(f.Telpick={}))})(this,function(f){"use strict";var l=typeof document<"u"?document.currentScript:null;const N="",S=[{country:"Afganistán",code:"+93",flag:"/flags/af.png",country_code:"AF"},{country:"Albania",code:"+355",flag:"/flags/al.webp",country_code:"AL"},{country:"Alemania",code:"+49",flag:"/flags/de.webp",country_code:"DE"},{country:"Andorra",code:"+376",flag:"/flags/ad.webp",country_code:"AD"},{country:"Antigua y Barbuda",code:"+1",flag:"/flags/ag.webp",country_code:"AG"},{country:"Arabia Saudita",code:"+966",flag:"/flags/sa.webp",country_code:"SA"},{country:"Argentina",code:"+54",flag:"/flags/ar.webp",country_code:"AR"},{country:"Australia",code:"+61",flag:"/flags/au.webp",country_code:"AU"},{country:"Austria",code:"+43",flag:"/flags/at.webp",country_code:"AT"},{country:"Bahamas",code:"+1",flag:"/flags/bs.webp",country_code:"BS"},{country:"Bangladés",code:"+880",flag:"/flags/bd.webp",country_code:"BD"},{country:"Barbados",code:"+1",flag:"/flags/bb.webp",country_code:"BB"},{country:"Bélgica",code:"+32",flag:"/flags/be.webp",country_code:"BE"},{country:"Belice",code:"+501",flag:"/flags/bz.webp",country_code:"BZ"},{country:"Bielorrusia",code:"+375",flag:"/flags/by.webp",country_code:"BY"},{country:"Bolivia",code:"+591",flag:"/flags/bo.webp",country_code:"BO"},{country:"Bosnia y Herzegovina",code:"+387",flag:"/flags/ba.webp",country_code:"BA"},{country:"Brasil",code:"+55",flag:"/flags/br.webp",country_code:"BR"},{country:"Bulgaria",code:"+359",flag:"/flags/bg.webp",country_code:"BG"},{country:"Canadá",code:"+1",flag:"/flags/ca.webp",country_code:"CA"},{country:"Chile",code:"+56",flag:"/flags/cl.webp",country_code:"CL"},{country:"China",code:"+86",flag:"/flags/cn.webp",country_code:"CN"},{country:"Colombia",code:"+57",flag:"/flags/co.webp",country_code:"CO"},{country:"Corea del Sur",code:"+82",flag:"/flags/kr.webp",country_code:"KR"},{country:"Costa Rica",code:"+506",flag:"/flags/cr.webp",country_code:"CR"},{country:"Croacia",code:"+385",flag:"/flags/hr.webp",country_code:"HR"},{country:"Cuba",code:"+53",flag:"/flags/cu.webp",country_code:"CU"},{country:"Dinamarca",code:"+45",flag:"/flags/dk.webp",country_code:"DK"},{country:"Dominica",code:"+1",flag:"/flags/dm.webp",country_code:"DM"},{country:"Ecuador",code:"+593",flag:"/flags/ec.webp",country_code:"EC"},{country:"Egipto",code:"+20",flag:"/flags/eg.webp",country_code:"EG"},{country:"El Salvador",code:"+503",flag:"/flags/sv.webp",country_code:"SV"},{country:"España",code:"+34",flag:"/flags/es.webp",country_code:"ES"},{country:"Estados Unidos",code:"+1",flag:"/flags/us.webp",country_code:"US"},{country:"Estonia",code:"+372",flag:"/flags/ee.webp",country_code:"EE"},{country:"Filipinas",code:"+63",flag:"/flags/ph.webp",country_code:"PH"},{country:"Finlandia",code:"+358",flag:"/flags/fi.webp",country_code:"FI"},{country:"Francia",code:"+33",flag:"/flags/fr.webp",country_code:"FR"},{country:"Granada",code:"+1",flag:"/flags/gd.webp",country_code:"GD"},{country:"Grecia",code:"+30",flag:"/flags/gr.webp",country_code:"GR"},{country:"Guatemala",code:"+502",flag:"/flags/gt.webp",country_code:"GT"},{country:"Guyana",code:"+592",flag:"/flags/gy.webp",country_code:"GY"},{country:"Haití",code:"+509",flag:"/flags/ht.webp",country_code:"HT"},{country:"Honduras",code:"+504",flag:"/flags/hn.webp",country_code:"HN"},{country:"Hungría",code:"+36",flag:"/flags/hu.webp",country_code:"HU"},{country:"India",code:"+91",flag:"/flags/in.webp",country_code:"IN"},{country:"Indonesia",code:"+62",flag:"/flags/id.webp",country_code:"ID"},{country:"Irak",code:"+964",flag:"/flags/iq.webp",country_code:"IQ"},{country:"Irlanda",code:"+353",flag:"/flags/ie.webp",country_code:"IE"},{country:"Islandia",code:"+354",flag:"/flags/is.webp",country_code:"IS"},{country:"Israel",code:"+972",flag:"/flags/il.webp",country_code:"IL"},{country:"Italia",code:"+39",flag:"/flags/it.webp",country_code:"IT"},{country:"Jamaica",code:"+1",flag:"/flags/jm.webp",country_code:"JM"},{country:"Japón",code:"+81",flag:"/flags/jp.webp",country_code:"JP"},{country:"Kenia",code:"+254",flag:"/flags/ke.webp",country_code:"KE"},{country:"Letonia",code:"+371",flag:"/flags/lv.webp",country_code:"LV"},{country:"Lituania",code:"+370",flag:"/flags/lt.webp",country_code:"LT"},{country:"Luxemburgo",code:"+352",flag:"/flags/lu.webp",country_code:"LU"},{country:"Malasia",code:"+60",flag:"/flags/my.webp",country_code:"MY"},{country:"Malta",code:"+356",flag:"/flags/mt.webp",country_code:"MT"},{country:"México",code:"+52",flag:"/flags/mx.webp",country_code:"MX"},{country:"Mónaco",code:"+377",flag:"/flags/mc.webp",country_code:"MC"},{country:"Nicaragua",code:"+505",flag:"/flags/ni.webp",country_code:"NI"},{country:"Nigeria",code:"+234",flag:"/flags/ng.webp",country_code:"NG"},{country:"Noruega",code:"+47",flag:"/flags/no.webp",country_code:"NO"},{country:"Nueva Zelanda",code:"+64",flag:"/flags/nz.webp",country_code:"NZ"},{country:"Países Bajos",code:"+31",flag:"/flags/nl.webp",country_code:"NL"},{country:"Pakistán",code:"+92",flag:"/flags/pk.webp",country_code:"PK"},{country:"Panamá",code:"+507",flag:"/flags/pa.webp",country_code:"PA"},{country:"Paraguay",code:"+595",flag:"/flags/py.webp",country_code:"PY"},{country:"Perú",code:"+51",flag:"/flags/pe.webp",country_code:"PE"},{country:"Polonia",code:"+48",flag:"/flags/pl.webp",country_code:"PL"},{country:"Portugal",code:"+351",flag:"/flags/pt.webp",country_code:"PT"},{country:"Reino Unido",code:"+44",flag:"/flags/gb.webp",country_code:"GB"},{country:"República Checa",code:"+420",flag:"/flags/cz.webp",country_code:"CZ"},{country:"República Dominicana",code:"+1",flag:"/flags/do.webp",country_code:"DO"},{country:"Rumanía",code:"+40",flag:"/flags/ro.webp",country_code:"RO"},{country:"Rusia",code:"+7",flag:"/flags/ru.webp",country_code:"RU"},{country:"San Cristóbal y Nieves",code:"+1",flag:"/flags/kn.webp",country_code:"KN"},{country:"San Vicente y las Granadinas",code:"+1",flag:"/flags/vc.webp",country_code:"VC"},{country:"Santa Lucía",code:"+1",flag:"/flags/lc.webp",country_code:"LC"},{country:"Singapur",code:"+65",flag:"/flags/sg.webp",country_code:"SG"},{country:"Sudáfrica",code:"+27",flag:"/flags/za.webp",country_code:"ZA"},{country:"Suecia",code:"+46",flag:"/flags/se.webp",country_code:"SE"},{country:"Suiza",code:"+41",flag:"/flags/ch.webp",country_code:"CH"},{country:"Surinam",code:"+597",flag:"/flags/sr.webp",country_code:"SR"},{country:"Tailandia",code:"+66",flag:"/flags/th.webp",country_code:"TH"},{country:"Trinidad y Tobago",code:"+1",flag:"/flags/tt.webp",country_code:"TT"},{country:"Ucrania",code:"+380",flag:"/flags/ua.webp",country_code:"UA"},{country:"Uruguay",code:"+598",flag:"/flags/uy.webp",country_code:"UY"},{country:"Venezuela",code:"+58",flag:"/flags/ve.webp",country_code:"VE"},{country:"Vietnam",code:"+84",flag:"/flags/vn.webp",country_code:"VN"}];class C{constructor({code:t=null,onChange:d=()=>{},styleOverrides:c={}}={}){this.code=t,this.onChange=d,this.styleOverrides=c,this.codes=[],this.selectedCode=t,this.isDropdownOpen=!1,this.searchQuery="",this.container=null,this.dropdown=null,this._outsideHandler=null}_getAssetsBase(){if(typeof{url:typeof document>"u"&&typeof location>"u"?require("url").pathToFileURL(__filename).href:typeof document>"u"?location.href:l&&l.tagName.toUpperCase()==="SCRIPT"&&l.src||new URL("telpick.umd.js",document.baseURI).href}<"u"&&(typeof document>"u"&&typeof location>"u"?require("url").pathToFileURL(__filename).href:typeof document>"u"?location.href:l&&l.tagName.toUpperCase()==="SCRIPT"&&l.src||new URL("telpick.umd.js",document.baseURI).href))try{return new URL("./assets",self.location).href+"/"}catch{}return""}async init(t){if(this.container=t,this.codes=[...S].sort((c,a)=>c.country.localeCompare(a.country,"es")),this._assetsBase=this._getAssetsBase(),this.code){const c=this.codes.find(a=>a.country_code===this.code);c&&(this.selectedCode=c.country_code)}else{const c=[async()=>{try{const e=await(await fetch("https://ip-api.com/json/?fields=countryCode")).json();if(e.countryCode)return this.codes.find(r=>r.country_code===e.countryCode)}catch{}return null},async()=>{try{const e=await(await fetch("https://get.geojs.io/v1/ip/country.json")).json();if(e.country)return this.codes.find(r=>r.country_code===e.country)}catch{}return null},async()=>{try{const e=await(await fetch("https://ipapi.co/json/")).json();if(e.country_code)return this.codes.find(r=>r.country_code===e.country_code)}catch{}return null}];let a=null;for(const n of c)try{if(a=await Promise.race([n(),new Promise(e=>setTimeout(()=>e(null),3e3))]),a)break}catch{}if(!a)try{const n=Intl.DateTimeFormat().resolvedOptions().timeZone,r={"America/Lima":"PE","America/Bogota":"CO","America/Mexico_City":"MX","America/Argentina/Buenos_Aires":"AR","America/Santiago":"CL","America/Caracas":"VE","America/Montevideo":"UY","America/Asuncion":"PY","America/La_Paz":"BO","America/Guayaquil":"EC","America/Panama":"PA","America/Costa_Rica":"CR","America/Managua":"NI","America/Tegucigalpa":"HN","America/Guatemala":"GT","America/El_Salvador":"SV","America/Havana":"CU","America/Santo_Domingo":"DO","America/Jamaica":"JM","America/Port-au-Prince":"HT","Europe/Madrid":"ES","Europe/London":"GB","Europe/Paris":"FR","Europe/Berlin":"DE","Europe/Rome":"IT","Europe/Amsterdam":"NL","Europe/Brussels":"BE","Europe/Vienna":"AT","Europe/Zurich":"CH","Europe/Stockholm":"SE","Europe/Oslo":"NO","Europe/Copenhagen":"DK","Europe/Helsinki":"FI","Europe/Warsaw":"PL","Europe/Prague":"CZ","Europe/Bucharest":"RO","Europe/Moscow":"RU","America/New_York":"US","America/Chicago":"US","America/Denver":"US","America/Los_Angeles":"US","America/Toronto":"CA","America/Vancouver":"CA","Asia/Tokyo":"JP","Asia/Shanghai":"CN","Asia/Hong_Kong":"CN","Asia/Seoul":"KR","Asia/Singapore":"SG","Asia/Bangkok":"TH","Asia/Jakarta":"ID","Asia/Manila":"PH","Asia/Kolkata":"IN","Asia/Dubai":"AE","Asia/Riyadh":"SA","Australia/Sydney":"AU","Australia/Melbourne":"AU","Pacific/Auckland":"NZ","Africa/Cairo":"EG","Africa/Johannesburg":"ZA","Africa/Nairobi":"KE","Africa/Lagos":"NG"}[n];r&&(a=this.codes.find(A=>A.country_code===r))}catch{}if(a)this.selectedCode=a.country_code;else{const n=this.codes.find(e=>e.country_code==="CO");n&&(this.selectedCode=n.country_code)}}this.render();const d=this.codes.find(c=>c.country_code===this.selectedCode);d&&this.onChange(d),this._setupOutsideClick()}render(){this.container.innerHTML="";const t=document.createElement("button");t.className="telpick-btn",Object.assign(t.style,this.styleOverrides),t.onclick=()=>{this.isDropdownOpen=!this.isDropdownOpen,this.render()};const d=document.createElement("div");d.className="telpick-flag";const c=this.codes.find(e=>e.country_code===this.selectedCode)||{flag:"",code:"",country:""};if(c.flag){const e=document.createElement("img");e.src=(this._assetsBase||"src/assets")+(c.flag.startsWith("/")?c.flag:"/"+c.flag),e.className="w-full h-full object-cover",e.alt="flag",d.appendChild(e)}t.appendChild(d);const a=document.createElement("span");a.textContent=c.code,t.appendChild(a);const n=document.createElement("span");if(n.className="ml-auto",n.textContent="▼",t.appendChild(n),this.container.appendChild(t),this.isDropdownOpen){this.dropdown=document.createElement("div"),this.dropdown.className="telpick-dropdown",this.dropdown.onclick=o=>o.stopPropagation(),this.dropdown.onmousedown=o=>o.stopPropagation();const e=document.createElement("input");e.className="telpick-search",e.type="text",e.placeholder="Buscar país...",e.value=this.searchQuery,e.oninput=o=>{var p;o.stopPropagation();const i=o.target,h=i.selectionStart||0,g=i.value;this.searchQuery=g;const u=(p=this.dropdown)==null?void 0:p.querySelector("ul");u?(u.innerHTML="",(this.searchQuery?this.codes.filter(s=>s.country.toLowerCase().includes(this.searchQuery.toLowerCase())):this.codes).forEach(s=>{const y=document.createElement("li"),k=s.country_code===this.selectedCode&&this.selectedCode!==null&&this.selectedCode!==void 0;y.className=`telpick-item ${k?"telpick-item-selected":""}`,y.onclick=()=>{this.selectedCode=s.country_code,this.onChange(s),this.isDropdownOpen=!1,this.searchQuery="",this.render()};const _=document.createElement("div");_.className="telpick-flag";const w=document.createElement("img");w.src=(this._assetsBase||"src/assets")+(s.flag.startsWith("/")?s.flag:"/"+s.flag),w.className="w-full h-full object-cover",w.alt="flag",_.appendChild(w),y.appendChild(_);const E=document.createElement("span");E.textContent=s.country,y.appendChild(E);const b=document.createElement("span");b.className="ml-auto",b.textContent=s.code,y.appendChild(b),u.appendChild(y)}),requestAnimationFrame(()=>{if(e){e.focus();const s=Math.min(h+1,g.length);e.setSelectionRange(s,s)}})):this.render()},e.onclick=o=>o.stopPropagation(),e.onmousedown=o=>o.stopPropagation(),this.dropdown.appendChild(e);const r=document.createElement("ul");r.style.maxHeight="130px",r.style.overflowY="auto",(this.searchQuery?this.codes.filter(o=>o.country.toLowerCase().includes(this.searchQuery.toLowerCase())):this.codes).forEach(o=>{const i=document.createElement("li"),h=o.country_code===this.selectedCode&&this.selectedCode!==null&&this.selectedCode!==void 0;i.className=`telpick-item ${h?"telpick-item-selected":""}`,i.setAttribute("aria-selected",h),i.onclick=()=>{this.selectedCode=o.country_code,this.onChange(o),this.isDropdownOpen=!1,this.searchQuery="",this.render()};const g=document.createElement("div");g.className="telpick-flag";const u=document.createElement("img");u.src=(this._assetsBase||"src/assets")+(o.flag.startsWith("/")?o.flag:"/"+o.flag),u.className="w-full h-full object-cover",u.alt="flag",g.appendChild(u),i.appendChild(g);const p=document.createElement("span");p.textContent=o.country,i.appendChild(p);const m=document.createElement("span");m.className="ml-auto",m.textContent=o.code,i.appendChild(m),r.appendChild(i)}),this.dropdown.appendChild(r),this.container.appendChild(this.dropdown),requestAnimationFrame(()=>{e&&e.focus()})}}_setupOutsideClick(){this._outsideHandler&&document.removeEventListener("click",this._outsideHandler),this._outsideHandler=t=>{const d=t.target;if(this.isDropdownOpen&&this.container&&d){const c=this.container.querySelector(".telpick-dropdown");!this.container.contains(d)&&(!c||!c.contains(d))&&(this.isDropdownOpen=!1,this.render())}},document.addEventListener("click",this._outsideHandler,!0)}destroy(){this._outsideHandler&&document.removeEventListener("mousedown",this._outsideHandler),this.container.innerHTML=""}}window.Telpick=C,f.Telpick=C,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
1
+ (function(u,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(u=typeof globalThis<"u"?globalThis:u||self,p(u.Telpick={}))})(this,function(u){"use strict";const p="",S=[{country:"Afganistán",code:"+93",flag:"/flags/af.png",country_code:"AF"},{country:"Albania",code:"+355",flag:"/flags/al.webp",country_code:"AL"},{country:"Alemania",code:"+49",flag:"/flags/de.webp",country_code:"DE"},{country:"Andorra",code:"+376",flag:"/flags/ad.webp",country_code:"AD"},{country:"Antigua y Barbuda",code:"+1",flag:"/flags/ag.webp",country_code:"AG"},{country:"Arabia Saudita",code:"+966",flag:"/flags/sa.webp",country_code:"SA"},{country:"Argentina",code:"+54",flag:"/flags/ar.webp",country_code:"AR"},{country:"Australia",code:"+61",flag:"/flags/au.webp",country_code:"AU"},{country:"Austria",code:"+43",flag:"/flags/at.webp",country_code:"AT"},{country:"Bahamas",code:"+1",flag:"/flags/bs.webp",country_code:"BS"},{country:"Bangladés",code:"+880",flag:"/flags/bd.webp",country_code:"BD"},{country:"Barbados",code:"+1",flag:"/flags/bb.webp",country_code:"BB"},{country:"Bélgica",code:"+32",flag:"/flags/be.webp",country_code:"BE"},{country:"Belice",code:"+501",flag:"/flags/bz.webp",country_code:"BZ"},{country:"Bielorrusia",code:"+375",flag:"/flags/by.webp",country_code:"BY"},{country:"Bolivia",code:"+591",flag:"/flags/bo.webp",country_code:"BO"},{country:"Bosnia y Herzegovina",code:"+387",flag:"/flags/ba.webp",country_code:"BA"},{country:"Brasil",code:"+55",flag:"/flags/br.webp",country_code:"BR"},{country:"Bulgaria",code:"+359",flag:"/flags/bg.webp",country_code:"BG"},{country:"Canadá",code:"+1",flag:"/flags/ca.webp",country_code:"CA"},{country:"Chile",code:"+56",flag:"/flags/cl.webp",country_code:"CL"},{country:"China",code:"+86",flag:"/flags/cn.webp",country_code:"CN"},{country:"Colombia",code:"+57",flag:"/flags/co.webp",country_code:"CO"},{country:"Corea del Sur",code:"+82",flag:"/flags/kr.webp",country_code:"KR"},{country:"Costa Rica",code:"+506",flag:"/flags/cr.webp",country_code:"CR"},{country:"Croacia",code:"+385",flag:"/flags/hr.webp",country_code:"HR"},{country:"Cuba",code:"+53",flag:"/flags/cu.webp",country_code:"CU"},{country:"Dinamarca",code:"+45",flag:"/flags/dk.webp",country_code:"DK"},{country:"Dominica",code:"+1",flag:"/flags/dm.webp",country_code:"DM"},{country:"Ecuador",code:"+593",flag:"/flags/ec.webp",country_code:"EC"},{country:"Egipto",code:"+20",flag:"/flags/eg.webp",country_code:"EG"},{country:"El Salvador",code:"+503",flag:"/flags/sv.webp",country_code:"SV"},{country:"España",code:"+34",flag:"/flags/es.webp",country_code:"ES"},{country:"Estados Unidos",code:"+1",flag:"/flags/us.webp",country_code:"US"},{country:"Estonia",code:"+372",flag:"/flags/ee.webp",country_code:"EE"},{country:"Filipinas",code:"+63",flag:"/flags/ph.webp",country_code:"PH"},{country:"Finlandia",code:"+358",flag:"/flags/fi.webp",country_code:"FI"},{country:"Francia",code:"+33",flag:"/flags/fr.webp",country_code:"FR"},{country:"Granada",code:"+1",flag:"/flags/gd.webp",country_code:"GD"},{country:"Grecia",code:"+30",flag:"/flags/gr.webp",country_code:"GR"},{country:"Guatemala",code:"+502",flag:"/flags/gt.webp",country_code:"GT"},{country:"Guyana",code:"+592",flag:"/flags/gy.webp",country_code:"GY"},{country:"Haití",code:"+509",flag:"/flags/ht.webp",country_code:"HT"},{country:"Honduras",code:"+504",flag:"/flags/hn.webp",country_code:"HN"},{country:"Hungría",code:"+36",flag:"/flags/hu.webp",country_code:"HU"},{country:"India",code:"+91",flag:"/flags/in.webp",country_code:"IN"},{country:"Indonesia",code:"+62",flag:"/flags/id.webp",country_code:"ID"},{country:"Irak",code:"+964",flag:"/flags/iq.webp",country_code:"IQ"},{country:"Irlanda",code:"+353",flag:"/flags/ie.webp",country_code:"IE"},{country:"Islandia",code:"+354",flag:"/flags/is.webp",country_code:"IS"},{country:"Israel",code:"+972",flag:"/flags/il.webp",country_code:"IL"},{country:"Italia",code:"+39",flag:"/flags/it.webp",country_code:"IT"},{country:"Jamaica",code:"+1",flag:"/flags/jm.webp",country_code:"JM"},{country:"Japón",code:"+81",flag:"/flags/jp.webp",country_code:"JP"},{country:"Kenia",code:"+254",flag:"/flags/ke.webp",country_code:"KE"},{country:"Letonia",code:"+371",flag:"/flags/lv.webp",country_code:"LV"},{country:"Lituania",code:"+370",flag:"/flags/lt.webp",country_code:"LT"},{country:"Luxemburgo",code:"+352",flag:"/flags/lu.webp",country_code:"LU"},{country:"Malasia",code:"+60",flag:"/flags/my.webp",country_code:"MY"},{country:"Malta",code:"+356",flag:"/flags/mt.webp",country_code:"MT"},{country:"México",code:"+52",flag:"/flags/mx.webp",country_code:"MX"},{country:"Mónaco",code:"+377",flag:"/flags/mc.webp",country_code:"MC"},{country:"Nicaragua",code:"+505",flag:"/flags/ni.webp",country_code:"NI"},{country:"Nigeria",code:"+234",flag:"/flags/ng.webp",country_code:"NG"},{country:"Noruega",code:"+47",flag:"/flags/no.webp",country_code:"NO"},{country:"Nueva Zelanda",code:"+64",flag:"/flags/nz.webp",country_code:"NZ"},{country:"Países Bajos",code:"+31",flag:"/flags/nl.webp",country_code:"NL"},{country:"Pakistán",code:"+92",flag:"/flags/pk.webp",country_code:"PK"},{country:"Panamá",code:"+507",flag:"/flags/pa.webp",country_code:"PA"},{country:"Paraguay",code:"+595",flag:"/flags/py.webp",country_code:"PY"},{country:"Perú",code:"+51",flag:"/flags/pe.webp",country_code:"PE"},{country:"Polonia",code:"+48",flag:"/flags/pl.webp",country_code:"PL"},{country:"Portugal",code:"+351",flag:"/flags/pt.webp",country_code:"PT"},{country:"Reino Unido",code:"+44",flag:"/flags/gb.webp",country_code:"GB"},{country:"República Checa",code:"+420",flag:"/flags/cz.webp",country_code:"CZ"},{country:"República Dominicana",code:"+1",flag:"/flags/do.webp",country_code:"DO"},{country:"Rumanía",code:"+40",flag:"/flags/ro.webp",country_code:"RO"},{country:"Rusia",code:"+7",flag:"/flags/ru.webp",country_code:"RU"},{country:"San Cristóbal y Nieves",code:"+1",flag:"/flags/kn.webp",country_code:"KN"},{country:"San Vicente y las Granadinas",code:"+1",flag:"/flags/vc.webp",country_code:"VC"},{country:"Santa Lucía",code:"+1",flag:"/flags/lc.webp",country_code:"LC"},{country:"Singapur",code:"+65",flag:"/flags/sg.webp",country_code:"SG"},{country:"Sudáfrica",code:"+27",flag:"/flags/za.webp",country_code:"ZA"},{country:"Suecia",code:"+46",flag:"/flags/se.webp",country_code:"SE"},{country:"Suiza",code:"+41",flag:"/flags/ch.webp",country_code:"CH"},{country:"Surinam",code:"+597",flag:"/flags/sr.webp",country_code:"SR"},{country:"Tailandia",code:"+66",flag:"/flags/th.webp",country_code:"TH"},{country:"Trinidad y Tobago",code:"+1",flag:"/flags/tt.webp",country_code:"TT"},{country:"Ucrania",code:"+380",flag:"/flags/ua.webp",country_code:"UA"},{country:"Uruguay",code:"+598",flag:"/flags/uy.webp",country_code:"UY"},{country:"Venezuela",code:"+58",flag:"/flags/ve.webp",country_code:"VE"},{country:"Vietnam",code:"+84",flag:"/flags/vn.webp",country_code:"VN"}];class C{constructor({code:c=null,onChange:s=()=>{},styleOverrides:t={}}={}){this.code=c,this.onChange=s,this.styleOverrides=t,this.codes=[],this.selectedCode=c,this.isDropdownOpen=!1,this.searchQuery="",this.container=null,this.dropdown=null,this._outsideHandler=null}_getFlagUrl(c){return c?`https://flagcdn.com/w40/${String(c).toLowerCase()}.png`:""}async init(c){if(this.container=c,this.codes=[...S].sort((t,a)=>t.country.localeCompare(a.country,"es")),this.code){const t=this.codes.find(a=>a.country_code===this.code);t&&(this.selectedCode=t.country_code)}else{const t=[async()=>{try{const e=await(await fetch("https://ip-api.com/json/?fields=countryCode")).json();if(e.countryCode)return this.codes.find(r=>r.country_code===e.countryCode)}catch{}return null},async()=>{try{const e=await(await fetch("https://get.geojs.io/v1/ip/country.json")).json();if(e.country)return this.codes.find(r=>r.country_code===e.country)}catch{}return null},async()=>{try{const e=await(await fetch("https://ipapi.co/json/")).json();if(e.country_code)return this.codes.find(r=>r.country_code===e.country_code)}catch{}return null}];let a=null;for(const n of t)try{if(a=await Promise.race([n(),new Promise(e=>setTimeout(()=>e(null),3e3))]),a)break}catch{}if(!a)try{const n=Intl.DateTimeFormat().resolvedOptions().timeZone,r={"America/Lima":"PE","America/Bogota":"CO","America/Mexico_City":"MX","America/Argentina/Buenos_Aires":"AR","America/Santiago":"CL","America/Caracas":"VE","America/Montevideo":"UY","America/Asuncion":"PY","America/La_Paz":"BO","America/Guayaquil":"EC","America/Panama":"PA","America/Costa_Rica":"CR","America/Managua":"NI","America/Tegucigalpa":"HN","America/Guatemala":"GT","America/El_Salvador":"SV","America/Havana":"CU","America/Santo_Domingo":"DO","America/Jamaica":"JM","America/Port-au-Prince":"HT","Europe/Madrid":"ES","Europe/London":"GB","Europe/Paris":"FR","Europe/Berlin":"DE","Europe/Rome":"IT","Europe/Amsterdam":"NL","Europe/Brussels":"BE","Europe/Vienna":"AT","Europe/Zurich":"CH","Europe/Stockholm":"SE","Europe/Oslo":"NO","Europe/Copenhagen":"DK","Europe/Helsinki":"FI","Europe/Warsaw":"PL","Europe/Prague":"CZ","Europe/Bucharest":"RO","Europe/Moscow":"RU","America/New_York":"US","America/Chicago":"US","America/Denver":"US","America/Los_Angeles":"US","America/Toronto":"CA","America/Vancouver":"CA","Asia/Tokyo":"JP","Asia/Shanghai":"CN","Asia/Hong_Kong":"CN","Asia/Seoul":"KR","Asia/Singapore":"SG","Asia/Bangkok":"TH","Asia/Jakarta":"ID","Asia/Manila":"PH","Asia/Kolkata":"IN","Asia/Dubai":"AE","Asia/Riyadh":"SA","Australia/Sydney":"AU","Australia/Melbourne":"AU","Pacific/Auckland":"NZ","Africa/Cairo":"EG","Africa/Johannesburg":"ZA","Africa/Nairobi":"KE","Africa/Lagos":"NG"}[n];r&&(a=this.codes.find(A=>A.country_code===r))}catch{}if(a)this.selectedCode=a.country_code;else{const n=this.codes.find(e=>e.country_code==="CO");n&&(this.selectedCode=n.country_code)}}this.render();const s=this.codes.find(t=>t.country_code===this.selectedCode);s&&this.onChange(s),this._setupOutsideClick()}render(){this.container.innerHTML="";const c=document.createElement("button");c.className="telpick-btn",Object.assign(c.style,this.styleOverrides),c.onclick=()=>{this.isDropdownOpen=!this.isDropdownOpen,this.render()};const s=document.createElement("div");s.className="telpick-flag";const t=this.codes.find(e=>e.country_code===this.selectedCode)||{flag:"",code:"",country:""};if(t.flag){const e=document.createElement("img");e.src=this._getFlagUrl(t.country_code),e.className="w-full h-full object-cover",e.alt="flag",s.appendChild(e)}c.appendChild(s);const a=document.createElement("span");a.textContent=t.code,c.appendChild(a);const n=document.createElement("span");if(n.className="ml-auto",n.textContent="▼",c.appendChild(n),this.container.appendChild(c),this.isDropdownOpen){this.dropdown=document.createElement("div"),this.dropdown.className="telpick-dropdown",this.dropdown.onclick=o=>o.stopPropagation(),this.dropdown.onmousedown=o=>o.stopPropagation();const e=document.createElement("input");e.className="telpick-search",e.type="text",e.placeholder="Buscar país...",e.value=this.searchQuery,e.oninput=o=>{var y;o.stopPropagation();const l=o.target,h=l.selectionStart||0,f=l.value;this.searchQuery=f;const i=(y=this.dropdown)==null?void 0:y.querySelector("ul");i?(i.innerHTML="",(this.searchQuery?this.codes.filter(d=>d.country.toLowerCase().includes(this.searchQuery.toLowerCase())):this.codes).forEach(d=>{const g=document.createElement("li"),k=d.country_code===this.selectedCode&&this.selectedCode!==null&&this.selectedCode!==void 0;g.className=`telpick-item ${k?"telpick-item-selected":""}`,g.onclick=()=>{this.selectedCode=d.country_code,this.onChange(d),this.isDropdownOpen=!1,this.searchQuery="",this.render()};const _=document.createElement("div");_.className="telpick-flag";const m=document.createElement("img");m.src=this._getFlagUrl(d.country_code),m.className="w-full h-full object-cover",m.alt="flag",_.appendChild(m),g.appendChild(_);const E=document.createElement("span");E.textContent=d.country,g.appendChild(E);const b=document.createElement("span");b.className="ml-auto",b.textContent=d.code,g.appendChild(b),i.appendChild(g)}),requestAnimationFrame(()=>{if(e){e.focus();const d=Math.min(h+1,f.length);e.setSelectionRange(d,d)}})):this.render()},e.onclick=o=>o.stopPropagation(),e.onmousedown=o=>o.stopPropagation(),this.dropdown.appendChild(e);const r=document.createElement("ul");r.style.maxHeight="130px",r.style.overflowY="auto",(this.searchQuery?this.codes.filter(o=>o.country.toLowerCase().includes(this.searchQuery.toLowerCase())):this.codes).forEach(o=>{const l=document.createElement("li"),h=o.country_code===this.selectedCode&&this.selectedCode!==null&&this.selectedCode!==void 0;l.className=`telpick-item ${h?"telpick-item-selected":""}`,l.setAttribute("aria-selected",h),l.onclick=()=>{this.selectedCode=o.country_code,this.onChange(o),this.isDropdownOpen=!1,this.searchQuery="",this.render()};const f=document.createElement("div");f.className="telpick-flag";const i=document.createElement("img");i.src=this._getFlagUrl(o.country_code),i.className="w-full h-full object-cover",i.alt="flag",f.appendChild(i),l.appendChild(f);const y=document.createElement("span");y.textContent=o.country,l.appendChild(y);const w=document.createElement("span");w.className="ml-auto",w.textContent=o.code,l.appendChild(w),r.appendChild(l)}),this.dropdown.appendChild(r),this.container.appendChild(this.dropdown),requestAnimationFrame(()=>{e&&e.focus()})}}_setupOutsideClick(){this._outsideHandler&&document.removeEventListener("click",this._outsideHandler),this._outsideHandler=c=>{const s=c.target;if(this.isDropdownOpen&&this.container&&s){const t=this.container.querySelector(".telpick-dropdown");!this.container.contains(s)&&(!t||!t.contains(s))&&(this.isDropdownOpen=!1,this.render())}},document.addEventListener("click",this._outsideHandler,!0)}destroy(){this._outsideHandler&&document.removeEventListener("mousedown",this._outsideHandler),this.container.innerHTML=""}}window.Telpick=C,u.Telpick=C,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "telpick",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Multi-platform country and phone code selector with a modern design, smooth animations, and automatic IP detection.",
5
5
  "main": "dist/telpick.es.js",
6
6
  "module": "dist/telpick.es.js",
package/src/telpick.js CHANGED
@@ -14,19 +14,14 @@ class Telpick {
14
14
  this._outsideHandler = null
15
15
  }
16
16
 
17
- _getAssetsBase() {
18
- if (typeof import.meta !== 'undefined' && import.meta.url) {
19
- try {
20
- return new URL('./assets', import.meta.url).href + '/'
21
- } catch {}
22
- }
23
- return ''
17
+ _getFlagUrl(countryCode) {
18
+ if (!countryCode) return ''
19
+ return `https://flagcdn.com/w40/${String(countryCode).toLowerCase()}.png`
24
20
  }
25
21
 
26
22
  async init(container) {
27
23
  this.container = container
28
24
  this.codes = [...countryCodesData].sort((a, b) => a.country.localeCompare(b.country, 'es'))
29
- this._assetsBase = this._getAssetsBase()
30
25
  if (!this.code) {
31
26
  const services = [
32
27
  async () => {
@@ -136,7 +131,7 @@ class Telpick {
136
131
  const selectedCountry = this.codes.find(c => c.country_code === this.selectedCode) || { flag: '', code: '', country: '' }
137
132
  if (selectedCountry.flag) {
138
133
  const img = document.createElement('img')
139
- img.src = (this._assetsBase || 'src/assets') + (selectedCountry.flag.startsWith('/') ? selectedCountry.flag : '/' + selectedCountry.flag)
134
+ img.src = this._getFlagUrl(selectedCountry.country_code)
140
135
  img.className = 'w-full h-full object-cover'
141
136
  img.alt = 'flag'
142
137
  flagDiv.appendChild(img)
@@ -186,7 +181,7 @@ class Telpick {
186
181
  const flag = document.createElement('div')
187
182
  flag.className = 'telpick-flag'
188
183
  const img = document.createElement('img')
189
- img.src = (this._assetsBase || 'src/assets') + (item.flag.startsWith('/') ? item.flag : '/' + item.flag)
184
+ img.src = this._getFlagUrl(item.country_code)
190
185
  img.className = 'w-full h-full object-cover'
191
186
  img.alt = 'flag'
192
187
  flag.appendChild(img)
@@ -233,7 +228,7 @@ class Telpick {
233
228
  const flag = document.createElement('div')
234
229
  flag.className = 'telpick-flag'
235
230
  const img = document.createElement('img')
236
- img.src = (this._assetsBase || 'src/assets') + (item.flag.startsWith('/') ? item.flag : '/' + item.flag)
231
+ img.src = this._getFlagUrl(item.country_code)
237
232
  img.className = 'w-full h-full object-cover'
238
233
  img.alt = 'flag'
239
234
  flag.appendChild(img)