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 CHANGED
@@ -1,184 +1,243 @@
1
1
  # CountrySelect Pro ๐ŸŒ
2
2
 
3
3
  [![MIT License](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
4
- [![jsDelivr](https://data.jsdelivr.com/v1/package/gh/nikmauro/country-select-pro/badge)](https://www.jsdelivr.com/package/gh/nikmauro/country-select-pro)
4
+ [![npm](https://img.shields.io/npm/v/country-select-pro)](https://www.npmjs.com/package/country-select-pro)
5
+ [![jsDelivr](https://data.jsdelivr.com/v1/package/npm/country-select-pro/badge)](https://www.jsdelivr.com/package/npm/country-select-pro)
5
6
  ![Vanilla JS](https://img.shields.io/badge/Vanilla-JavaScript-yellow)
6
- ![Size](https://img.shields.io/badge/size-%3C4KB-brightgreen)
7
+ ![Size](https://img.shields.io/badge/gzip-~7KB-brightgreen)
7
8
 
8
- A **lightweight, zero-dependency, vanilla JavaScript country selector** with localized browser validation and **Bootstrap 5 integration**.
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
- # โœจ Features
13
+ ## โœจ Features
15
14
 
16
- * ๐Ÿš€ **Zero Dependencies** โ€“ No jQuery, no frameworks.
17
- * โšก **Ultra Lightweight** โ€“ Less than **4KB minified**.
18
- * ๐ŸŽจ **Bootstrap 5 Ready** โ€“ Seamless integration with `input-group`.
19
- * ๐Ÿ” **Built-in Search** โ€“ Quickly filter countries and phone codes.
20
- * ๐Ÿ›ก๏ธ **Smart Validation** โ€“ Uses native browser localized validation messages.
21
- * ๐Ÿ“ฑ **Responsive UI** โ€“ Works perfectly on mobile and desktop.
22
- * โŒจ๏ธ **Keyboard Accessible** โ€“ Fully navigable with keyboard.
23
- * ๐ŸŒ **International Ready** โ€“ Supports ISO country codes.
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
- # ๐ŸŽฎ Live Demo
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
- # ๐Ÿ“ฆ Installation
36
-
37
- ## CDN (Recommended)
32
+ ## ๐Ÿ“ฆ Installation
38
33
 
39
- Include the script via **jsDelivr**:
34
+ ### CDN
40
35
 
41
36
  ```html
42
- <script src="https://cdn.jsdelivr.net/gh/nikmauro/country-select-pro@4.8.2/dist/country-select.min.js"></script>
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
- Download the repository and include:
42
+ ```bash
43
+ npm install country-select-pro
44
+ ```
50
45
 
51
- ```html
52
- <script src="dist/country-select.min.js"></script>
46
+ ```javascript
47
+ import 'country-select-pro'; // auto-init + window.CountrySelect
53
48
  ```
54
49
 
55
50
  ---
56
51
 
57
- # ๐Ÿš€ Basic Usage
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 id="country" class="form-control">
57
+ <input type="text" class="country-select" name="country" value="GR">
61
58
 
62
- <script>
63
- const countrySelect = new CountrySelect("#country", {
64
- defaultCountry: "gr"
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
- # โš™๏ธ Options
66
+ The list stays in your HTML: it works without JavaScript and the options are your own list.
72
67
 
73
- | Option | Type | Description |
74
- | -------------------- | ------- | ----------------------------------- |
75
- | `defaultCountry` | string | Default selected country (ISO code) |
76
- | `preferredCountries` | array | Countries displayed first |
77
- | `onlyCountries` | array | Restrict selectable countries |
78
- | `search` | boolean | Enable / disable search |
79
- | `placeholder` | string | Custom placeholder text |
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
- Example:
79
+ ### JavaScript
82
80
 
83
81
  ```javascript
84
- new CountrySelect("#country", {
85
- defaultCountry: "gr",
86
- preferredCountries: ["gr", "cy", "de"],
87
- search: true
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
- # ๐Ÿงฉ Bootstrap 5 Example
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
- CountrySelect works perfectly with Bootstrap input groups.
124
+ ---
125
+
126
+ ## ๐Ÿ“‹ Custom lists
96
127
 
97
128
  ```html
98
- <div class="input-group">
99
- <span class="input-group-text">๐ŸŒ</span>
100
- <input id="country" class="form-control">
101
- </div>
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("#country");
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
- # ๐ŸŒ Browser Support
148
+ ## ๐Ÿ“ก Events & methods
111
149
 
112
- Compatible with all modern browsers:
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
- * Chrome
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
- No polyfills required.
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
- # ๐Ÿ“‚ Project Structure
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
- country-select-pro
128
- โ”‚
129
- โ”œโ”€ dist
130
- โ”‚ โ”œโ”€ country-select.js
131
- โ”‚ โ””โ”€ country-select.min.js
132
- โ”‚
133
- โ”œโ”€ demo
134
- โ”œโ”€ src
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
- # ๐Ÿง  Why CountrySelect Pro?
201
+ ## โ™ฟ Accessibility
141
202
 
142
- Many country dropdown libraries rely on **large frameworks like jQuery or heavy UI components**.
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
- CountrySelect Pro was built to be:
207
+ ## ๐Ÿ”Ž SEO & performance
145
208
 
146
- * **Tiny**
147
- * **Fast**
148
- * **Dependency-free**
149
- * **Easy to integrate**
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
- Perfect for:
214
+ ---
152
215
 
153
- * checkout forms
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
- # ๐Ÿค Contributing
220
+ ## ๐Ÿ“‚ Project structure
162
221
 
163
- Contributions are welcome.
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
- # ๐Ÿ“„ License
239
+ Developed by **Nikolaos Mavroeidis** โ€“ https://github.com/nikmauro
181
240
 
182
- MIT License
241
+ ## ๐Ÿ“„ License
183
242
 
184
- Feel free to use it in personal and commercial projects.
243
+ MIT โ€“ free for personal and commercial projects.