cs-country-selector
Version:
<h1>ð cs-country-selector</h1>
173 lines (128 loc) âĒ 5.34 kB
Markdown
<h1>ð cs-country-selector</h1>
<p>A lightweight, fully customizable country selector component for Angular, with flag support, keyboard accessibility, dynamic filtering, and zero dependencies.</p>
<blockquote>
<p>ðĄ Built with modern Angular (standalone components) â works with all Angular versions and styles (SCSS, CSS, Tailwind, etc.)</p>
</blockquote>
<hr />
<h2>âĻ Features</h2>
<ul>
<li>â
Country list with flags, codes, and names</li>
<li>ðŊ <code>onlyCountries</code> and <code>excludeCountries</code> filters</li>
<li>ð Search with <code>startsWith</code> behavior</li>
<li>ð§ Keyboard accessibility (arrow keys + enter)</li>
<li>ðŠķ Small size, zero dependencies</li>
<li>ðĻ Fully customizable layout and appearance</li>
<li>ð§ Compatible with ReactiveForms and ngModel</li>
<li>âŋ Accessible with ARIA attributes</li>
</ul>
<hr />
<h2>âĻ Demo</h2>
<img src="https://raw.githubusercontent.com/HoussemEs/cs-country-selector/master/projects/country-selector-demo/public/demo.png" width="400" alt="cs-country-selector demo">
<hr />
<h2>ð Installation</h2>
<pre><code>npm install cs-country-selector
</code></pre>
<hr />
<h2>ð§ Usage</h2>
<pre><code>// inside ___.module.ts
//import CountrySelectorComponent and add it to your imports
import { CountrySelectorComponent } from 'cs-country-selector';
imports:[CountrySelectorComponent]
</code></pre>
<pre><code>//inside ___.component.ts
import { Country } from 'cs-country-selector';
selectedCountry: Country | null = null;
onCountryChange(country: Country | null) {
console.log('Selected:', country);
}
</code></pre>
<pre><code>//inside ___.component.html
<cs-country-selector
[(ngModel)]="selectedCountry"
[placeholder]="'Choose your country...'"
[includeFlags]="true"
[onlyCountries]="['US', 'FR', 'DE']"
[excludeCountries]="['KP']"
[containerClass]="'custom-container'"
[inputClass]="'custom-input'"
[dropdownClass]="'custom-dropdown'"
[itemClass]="'custom-item'"
[activeItemClass]="'highlighted'"
[flagClass]="'flag-icon'"
[selectedFlagClass]="'flag-icon-selected'"
(countrySelected)="onCountryChange($event)"
></cs-country-selector>
</code></pre>
<hr />
<h2>ðĶ Country Object Format</h2>
<pre><code>interface Country {
code: string; // e.g. "FR"
name: string; // e.g. "France"
flagUrl: string; // e.g. "https://.../fr.svg"
}
</code></pre>
<hr />
<h2>ðĻ Full Customizability</h2>
<p>The <code>cs-country-selector</code> is built to be styled your way â without forcing a design system or theme. You can pass your own classes or override CSS variables.</p>
<h3>â
CSS Class Bindings</h3>
<p>Use these <code>[Input()]</code> bindings to add your own classes while retaining the internal ones:</p>
<table>
<thead>
<tr>
<th>Input</th>
<th>Target Element</th>
<th>Default Class</th>
</tr>
</thead>
<tbody>
<tr><td>containerClass</td><td><div> root wrapper</td><td><code>country-selector-container</code></td></tr>
<tr><td>inputClass</td><td><input> search field</td><td><code>country-input</code></td></tr>
<tr><td>dropdownClass</td><td>Dropdown container</td><td><code>country-dropdown</code></td></tr>
<tr><td>itemClass</td><td>Each country <li></td><td><code>country-item</code></td></tr>
<tr><td>activeItemClass</td><td>Active country (hover/arrow)</td><td><code>active</code></td></tr>
<tr><td>flagClass</td><td>All flags</td><td><code>country-flag</code></td></tr>
<tr><td>selectedFlagClass</td><td>Flag inside input field</td><td><code>selected-country-flag</code></td></tr>
</tbody>
</table>
<p>You can override any style using global CSS or component-level styles.</p>
<h4>ðĄ Example</h4>
<pre><code>.custom-input {
border-radius: 8px;
background-color: #f0f0f0;
padding: 10px;
font-size: 14px;
}
</code></pre>
<h3>ðĻ Custom Scrollbar (via CSS Variables)</h3>
<p>Scrollbar design is fully themeable via CSS variables â no need to override deeply nested elements.</p>
<h4>Available CSS Variables</h4>
<ul>
<li><code>--cs-scrollbar-thumb-color</code></li>
<li><code>--cs-scrollbar-thumb-hover-color</code></li>
<li><code>--cs-scrollbar-width</code></li>
<li><code>--cs-scrollbar-radius</code></li>
</ul>
<pre><code>cs-country-selector {
--scrollbar-thumb-color: #00bcd4;
--scrollbar-thumb-hover-color: #0097a7;
--scrollbar-width: 10px;
--scrollbar-radius: 10px;
}
</code></pre>
<hr />
<h2>âïļ Filtering Countries</h2>
<h3><code>onlyCountries: string[]</code></h3>
<p>Displays <strong>only</strong> the countries listed (by ISO alpha-2 code):</p>
<pre><code><cs-country-selector [onlyCountries]="['FR', 'DE']" />
</code></pre>
<h3><code>excludeCountries: string[]</code></h3>
<p>Hides the countries listed:</p>
<pre><code><cs-country-selector [excludeCountries]="['KP']" />
</code></pre>
<p>You can combine both as needed.</p>
<hr />
<h2>ð License</h2>
<p>MIT ÂĐ Houssem Hosni</p>
<hr />
<h2>ð Contribute</h2>
<p>Found a bug? Want to suggest a feature? Open a PR or issue on GitHub â contributions welcome!</p>