country-select-pro 6.1.3 โ 6.2.0
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 +162 -103
- package/dist/country-select.js +858 -399
- package/dist/country-select.min.js +2 -0
- package/package.json +13 -7
- package/dist/flags/1x1/null +0 -1
- package/dist/flags/4x3/null +0 -1
package/README.md
CHANGED
|
@@ -1,184 +1,243 @@
|
|
|
1
1
|
# CountrySelect Pro ๐
|
|
2
2
|
|
|
3
3
|
[](LICENSE)
|
|
4
|
-
[](https://www.npmjs.com/package/country-select-pro)
|
|
5
|
+
[](https://www.jsdelivr.com/package/npm/country-select-pro)
|
|
5
6
|

|
|
6
|
-

|
|
7
8
|
|
|
8
|
-
A **lightweight, zero-dependency, vanilla JavaScript country selector** with
|
|
9
|
-
|
|
10
|
-
CountrySelect Pro provides a clean and fast country dropdown with **search, phone codes, accessibility support and smart browser validation**, without requiring jQuery or any external framework.
|
|
9
|
+
A **lightweight, zero-dependency, vanilla JavaScript country selector** with flags, phone codes, search, keyboard and screen-reader support. Works with **Bootstrap 5** (including dark mode) or **without any framework**.
|
|
11
10
|
|
|
12
11
|
---
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
## โจ Features
|
|
15
14
|
|
|
16
|
-
* ๐ **Zero
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* ๐ **
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
15
|
+
* ๐ **Zero dependencies** โ no jQuery, no framework.
|
|
16
|
+
* ๐จ **Bootstrap 5 or plain CSS** โ themable with `--cs-*` CSS variables, follows Bootstrap's `--bs-*` variables (and `data-bs-theme="dark"`) automatically.
|
|
17
|
+
* ๐ **Any list** โ built-in list of 250 countries, or your own: array, inline JSON, URL (ajax), or the `<option>`s of a `<select>`.
|
|
18
|
+
* ๐ **Smart search** โ accent-insensitive (`cote` โ Cรดteโฆ, `ฮตฮปฮปฮฑฮดฮฑ` โ ฮฮปฮปฮฌฮดฮฑ), by name, ISO code or phone code, best matches first.
|
|
19
|
+
* ๐ **Localized names** โ country names in any language via `Intl.DisplayNames` (`data-lang="el"`).
|
|
20
|
+
* โฟ **Accessible** โ ARIA combobox / listbox, full keyboard support, live result count, forced-colors and reduced-motion support.
|
|
21
|
+
* โก **SEO & performance friendly** โ progressive enhancement with `<select>`, one shared data request per page, lazy flags with fixed size (no layout shift), self-hosting option.
|
|
22
|
+
* ๐ **htmx ready** โ auto-init on swapped / morphed / cloned content.
|
|
24
23
|
|
|
25
24
|
---
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
Try the interactive demo:
|
|
26
|
+
## ๐ฎ Live Demo
|
|
30
27
|
|
|
31
28
|
https://nikmauro.github.io/country-select-pro/docs/
|
|
32
29
|
|
|
33
30
|
---
|
|
34
31
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
## CDN (Recommended)
|
|
32
|
+
## ๐ฆ Installation
|
|
38
33
|
|
|
39
|
-
|
|
34
|
+
### CDN
|
|
40
35
|
|
|
41
36
|
```html
|
|
42
|
-
<script src="https://cdn.jsdelivr.net/
|
|
37
|
+
<script src="https://cdn.jsdelivr.net/npm/country-select-pro@6.2.0/dist/country-select.min.js" defer></script>
|
|
43
38
|
```
|
|
44
39
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
## Local Installation
|
|
40
|
+
### npm
|
|
48
41
|
|
|
49
|
-
|
|
42
|
+
```bash
|
|
43
|
+
npm install country-select-pro
|
|
44
|
+
```
|
|
50
45
|
|
|
51
|
-
```
|
|
52
|
-
|
|
46
|
+
```javascript
|
|
47
|
+
import 'country-select-pro'; // auto-init + window.CountrySelect
|
|
53
48
|
```
|
|
54
49
|
|
|
55
50
|
---
|
|
56
51
|
|
|
57
|
-
|
|
52
|
+
## ๐ Usage
|
|
53
|
+
|
|
54
|
+
Add the class `country-select` โ that's it. The value of the input is the ISO code (or phone code / name, see `data-value-type`).
|
|
58
55
|
|
|
59
56
|
```html
|
|
60
|
-
<input
|
|
57
|
+
<input type="text" class="country-select" name="country" value="GR">
|
|
61
58
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
});
|
|
66
|
-
</script>
|
|
59
|
+
<!-- phone code -->
|
|
60
|
+
<input type="text" class="country-select" name="phone_code" value="+30"
|
|
61
|
+
data-value-type="phone" data-schema-return="{img} {phone}">
|
|
67
62
|
```
|
|
68
63
|
|
|
69
|
-
|
|
64
|
+
### Progressive enhancement with `<select>` (recommended for SEO / no-JS)
|
|
70
65
|
|
|
71
|
-
|
|
66
|
+
The list stays in your HTML: it works without JavaScript and the options are your own list.
|
|
72
67
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
68
|
+
```html
|
|
69
|
+
<select class="country-select" name="shipping_country" required>
|
|
70
|
+
<option value="">Choose a country</option>
|
|
71
|
+
<option value="GR">Greece</option>
|
|
72
|
+
<option value="CY" selected>Cyprus</option>
|
|
73
|
+
<option value="DE">Germany</option>
|
|
74
|
+
</select>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Optional per option: `data-code`, `data-phone`, `data-flag` (image URL). An empty option text takes the name from the built-in list.
|
|
80
78
|
|
|
81
|
-
|
|
79
|
+
### JavaScript
|
|
82
80
|
|
|
83
81
|
```javascript
|
|
84
|
-
new CountrySelect(
|
|
85
|
-
defaultCountry:
|
|
86
|
-
preferredCountries: [
|
|
87
|
-
|
|
82
|
+
const cs = new CountrySelect('#country', {
|
|
83
|
+
defaultCountry: 'GR',
|
|
84
|
+
preferredCountries: ['GR', 'CY'],
|
|
85
|
+
lang: 'el'
|
|
88
86
|
});
|
|
87
|
+
|
|
88
|
+
cs.ready.then(() => console.log(cs.getCountry()));
|
|
89
89
|
```
|
|
90
90
|
|
|
91
|
+
Elements with the `country-select` class are initialized automatically; calling `new CountrySelect()` with options on such an element re-creates it with those options.
|
|
92
|
+
|
|
91
93
|
---
|
|
92
94
|
|
|
93
|
-
|
|
95
|
+
## โ๏ธ Options
|
|
96
|
+
|
|
97
|
+
Every option can be set as a `data-*` attribute or as a JavaScript option (JS wins).
|
|
98
|
+
|
|
99
|
+
| Attribute | JS option | Default | Description |
|
|
100
|
+
| --- | --- | --- | --- |
|
|
101
|
+
| `data-value-type` | `valueType` | `code` | Value written to the input: `code` (ISO), `phone` (`+30`) or `name` |
|
|
102
|
+
| `data-schema` | `schema` | `{img} {name}` | Template of the list items: `{img}` `{name}` `{code}` `{phone}` |
|
|
103
|
+
| `data-schema-return` | `schemaReturn` | = schema | Template of the selected value |
|
|
104
|
+
| `data-preferred` | `preferredCountries` | โ | Codes shown first, e.g. `"GR,CY"` |
|
|
105
|
+
| `data-only-countries` | `onlyCountries` | โ | Show only these codes |
|
|
106
|
+
| `data-exclude-countries` | `excludeCountries` | โ | Hide these codes |
|
|
107
|
+
| `data-default-country` | `defaultCountry` | โ | ISO code selected when the input is empty |
|
|
108
|
+
| `data-placeholder` | `placeholder` | `Select` | Text when nothing is selected |
|
|
109
|
+
| `data-country-search` | `search` | `true` | Search box in the list |
|
|
110
|
+
| `data-search-placeholder` | `searchPlaceholder` | `Search...` | |
|
|
111
|
+
| `data-no-results` | `noResults` | `Not found` | |
|
|
112
|
+
| `data-country-row-values` | `rows` | `5` | Visible rows |
|
|
113
|
+
| `data-dropdown-width` | `dropdownWidth` | `auto` | e.g. `300px` |
|
|
114
|
+
| `data-lang` | `lang` | โ | Country names & texts language (`el`, `de`, โฆ or `auto` = `<html lang>`) |
|
|
115
|
+
| `data-flags` | `flags` | `true` | `false` = no flags |
|
|
116
|
+
| `data-sort` | `sort` | `true` | `false` = keep the order of your list |
|
|
117
|
+
| `data-countries` | `countries` | โ | Your list (see below) |
|
|
118
|
+
| `data-src` | `dataUrl` | โ | URL of your list (JSON) |
|
|
119
|
+
| `data-base-url` | `baseUrl` | jsDelivr | Where `dist/data.json` and `dist/flags/` live (self-hosting) |
|
|
120
|
+
| `data-flag-path` | `flagPath` | `{baseUrl}dist/flags/4x3/{code}.svg` | Flag URL template (`{code}` = `gr`, `{CODE}` = `GR`) |
|
|
121
|
+
| โ | `fetchOptions` | โ | `fetch()` options for `dataUrl` |
|
|
122
|
+
| โ | `merge` | `true` | Fill missing names / phone codes of your list from the built-in list |
|
|
94
123
|
|
|
95
|
-
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## ๐ Custom lists
|
|
96
127
|
|
|
97
128
|
```html
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
129
|
+
<!-- codes only: names, flags and phone codes come from the built-in list -->
|
|
130
|
+
<input class="country-select" data-countries='["GR","CY","IT"]'>
|
|
131
|
+
|
|
132
|
+
<!-- your own items -->
|
|
133
|
+
<input class="country-select" data-countries='[{"code":"GR","name":"Greece โ 2 days"},{"value":"PICKUP","name":"Store pickup","flag":false}]'>
|
|
134
|
+
|
|
135
|
+
<!-- from your server (ajax) -->
|
|
136
|
+
<input class="country-select" data-src="/api/shipping-countries.json">
|
|
102
137
|
```
|
|
103
138
|
|
|
104
139
|
```javascript
|
|
105
|
-
new CountrySelect(
|
|
140
|
+
new CountrySelect(el, {countries: () => fetch('/api/countries').then(r => r.json())});
|
|
141
|
+
cs.setCountries(['GR', 'CY']); // replace the list later
|
|
106
142
|
```
|
|
107
143
|
|
|
144
|
+
Accepted formats: an array of codes or objects, `{countries: [...]}`, `{data: [...]}`, `{"GR": "Greece", ...}` or the built-in `data.json` format. Item fields: `code` (`iso2`, `alpha2`), `name` (`label`, `text`), `phone` (`dial_code`), `flag` (URL or `false`), `value` (custom value), `keywords` (extra search words).
|
|
145
|
+
|
|
108
146
|
---
|
|
109
147
|
|
|
110
|
-
|
|
148
|
+
## ๐ก Events & methods
|
|
111
149
|
|
|
112
|
-
|
|
150
|
+
```javascript
|
|
151
|
+
input.addEventListener('country-change', e => {
|
|
152
|
+
console.log(e.detail.value, e.detail.country); // {code, name, localName, phone, value}
|
|
153
|
+
});
|
|
154
|
+
```
|
|
113
155
|
|
|
114
|
-
|
|
115
|
-
* Firefox
|
|
116
|
-
* Safari
|
|
117
|
-
* Edge
|
|
118
|
-
* Mobile browsers (iOS / Android)
|
|
156
|
+
`change` and `input` are dispatched too, so `hx-trigger="change"` / `hx-trigger="country-change"` work.
|
|
119
157
|
|
|
120
|
-
|
|
158
|
+
| Method | |
|
|
159
|
+
| --- | --- |
|
|
160
|
+
| `CountrySelect.getInstance(el)` | Instance of an element |
|
|
161
|
+
| `CountrySelect.init(root)` | Initialize `.country-select` inside `root` |
|
|
162
|
+
| `cs.setValue('CY', {silent})` | Select by value (sends events unless `silent`) |
|
|
163
|
+
| `cs.getValue()` / `cs.getCountry()` | Current value / country |
|
|
164
|
+
| `cs.open()` / `cs.close()` / `cs.toggle()` | |
|
|
165
|
+
| `cs.setCountries(list)` | New list (array, function, JSON, URL) |
|
|
166
|
+
| `cs.refresh()` | Re-read the input value after `input.value = ...` |
|
|
167
|
+
| `cs.destroy()` | Remove the widget, restore the input |
|
|
168
|
+
| `cs.ready` | Promise, resolved when the list is loaded |
|
|
121
169
|
|
|
122
170
|
---
|
|
123
171
|
|
|
124
|
-
|
|
172
|
+
## ๐จ Styling
|
|
125
173
|
|
|
174
|
+
**Bootstrap 5:** nothing to do โ colors, radius, focus ring and dark mode (`data-bs-theme="dark"`) follow Bootstrap's variables. Works inside `.input-group`, with `.is-invalid` and `.was-validated`.
|
|
175
|
+
|
|
176
|
+
**Without a framework:** set the `--cs-*` variables on any parent (or `:root`):
|
|
177
|
+
|
|
178
|
+
```css
|
|
179
|
+
.my-form {
|
|
180
|
+
--cs-bg: #fff; --cs-color: #222; --cs-border-color: #ccc; --cs-radius: 4px;
|
|
181
|
+
--cs-active-bg: #e07a2f; --cs-active-color: #fff; --cs-hover-bg: #f3f3f3;
|
|
182
|
+
--cs-focus-border: #e07a2f; --cs-focus-ring: rgba(224, 122, 47, .3);
|
|
183
|
+
/* also: --cs-font-size --cs-muted --cs-search-bg --cs-dropdown-bg --cs-dropdown-border-color
|
|
184
|
+
--cs-divider-color --cs-invalid --cs-disabled-bg --cs-shadow --cs-z-index */
|
|
185
|
+
}
|
|
126
186
|
```
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
โโ README.md
|
|
187
|
+
|
|
188
|
+
**Your own CSS only:** `window.CountrySelectConfig = {css: false}` before the script โ class names: `.cs-wrapper .cs-trigger .cs-selected-content .cs-dropdown .cs-search-input .cs-list .cs-option(.active) .cs-divider`.
|
|
189
|
+
|
|
190
|
+
### Global configuration
|
|
191
|
+
|
|
192
|
+
```html
|
|
193
|
+
<script>window.CountrySelectConfig = {lang: 'el', preferredCountries: ['GR', 'CY'], baseUrl: '/vendor/country-select-pro/'};</script>
|
|
194
|
+
<script src=".../country-select.min.js" defer></script>
|
|
136
195
|
```
|
|
137
196
|
|
|
197
|
+
Any option can be a default here, plus `css: false` and `autoInit: false`.
|
|
198
|
+
|
|
138
199
|
---
|
|
139
200
|
|
|
140
|
-
|
|
201
|
+
## โฟ Accessibility
|
|
141
202
|
|
|
142
|
-
|
|
203
|
+
* ARIA 1.2 combobox / listbox pattern (`aria-expanded`, `aria-activedescendant`, `aria-selected`), label taken from `aria-label`, `aria-labelledby` or `<label for>`.
|
|
204
|
+
* Keyboard: <kbd>โ</kbd> <kbd>โ</kbd> <kbd>Enter</kbd> <kbd>Space</kbd> open, <kbd>Home</kbd> <kbd>End</kbd> <kbd>PgUp</kbd> <kbd>PgDn</kbd> move, typing a letter opens the search, <kbd>Esc</kbd> closes (without closing a surrounding modal), <kbd>Tab</kbd> leaves.
|
|
205
|
+
* Result count announced to screen readers, WCAG AA contrast, `forced-colors` (Windows high contrast) and `prefers-reduced-motion` support, disabled / readonly states.
|
|
143
206
|
|
|
144
|
-
|
|
207
|
+
## ๐ SEO & performance
|
|
145
208
|
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
209
|
+
* Use a `<select>` (or an input with a value) โ the content is in the HTML and works without JavaScript.
|
|
210
|
+
* One request for the built-in list per page, no matter how many selectors.
|
|
211
|
+
* Flags are lazy-loaded, async-decoded and have a fixed size (no layout shift / CLS).
|
|
212
|
+
* Self-host the data and flags with `baseUrl` / `flagPath` to avoid third-party requests.
|
|
150
213
|
|
|
151
|
-
|
|
214
|
+
---
|
|
152
215
|
|
|
153
|
-
|
|
154
|
-
* registration forms
|
|
155
|
-
* shipping address forms
|
|
156
|
-
* phone number inputs
|
|
157
|
-
* international forms
|
|
216
|
+
## ๐ Browser support
|
|
158
217
|
|
|
159
|
-
|
|
218
|
+
All modern browsers (Chrome, Edge, Firefox, Safari, iOS, Android). No polyfills required.
|
|
160
219
|
|
|
161
|
-
|
|
220
|
+
## ๐ Project structure
|
|
162
221
|
|
|
163
|
-
|
|
222
|
+
```
|
|
223
|
+
dist/
|
|
224
|
+
โโ country-select.js source (also the unminified build)
|
|
225
|
+
โโ country-select.min.js built by `npm run build` (published to npm, not in git)
|
|
226
|
+
โโ data.json countries: names, ISO codes, phone codes
|
|
227
|
+
โโ flags/ SVG flags (4x3, 1x1)
|
|
228
|
+
docs/index.html live demo
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## ๐ค Contributing
|
|
164
232
|
|
|
165
233
|
1. Fork the repository
|
|
166
234
|
2. Create a feature branch
|
|
167
235
|
3. Submit a Pull Request
|
|
168
236
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
# ๐จโ๐ป Author
|
|
172
|
-
|
|
173
|
-
Developed by **Nikolaos Mavroeidis**
|
|
174
|
-
GitHub: https://github.com/nikmauro
|
|
175
|
-
|
|
176
|
-
With collaborative assistance from **Gemini (Google AI)**.
|
|
177
|
-
|
|
178
|
-
---
|
|
237
|
+
## ๐จโ๐ป Author
|
|
179
238
|
|
|
180
|
-
|
|
239
|
+
Developed by **Nikolaos Mavroeidis** โ https://github.com/nikmauro
|
|
181
240
|
|
|
182
|
-
|
|
241
|
+
## ๐ License
|
|
183
242
|
|
|
184
|
-
|
|
243
|
+
MIT โ free for personal and commercial projects.
|