Examples
React and Vue. Each section has a live preview and the code next to it.
Country select
Independent countries for a select. Store the alpha-2 code; display commonName.
getCountrySelectOptions ยท getCountry
Preview
import { useMemo, useState } from 'react';
import { getCountry, getCountrySelectOptions } from 'country-kit';
export function ResidenceSelect() {
const options = useMemo(
() => getCountrySelectOptions({ independent: true }),
[],
);
const [code, setCode] = useState('FR');
const country = getCountry(code);
return (
<select value={code} onChange={(e) => setCode(e.target.value)}>
{options.map((o) => (
<option key={o.value} value={o.value}>{o.label}</option>
))}
</select>
);
// persist country.code โ not the label
} <script setup lang="ts">
import { computed, ref } from 'vue';
import { getCountry, getCountrySelectOptions } from 'country-kit';
const options = getCountrySelectOptions({ independent: true });
const code = ref('FR');
const country = computed(() => getCountry(code.value));
</script>
<template>
<select v-model="code">
<option v-for="o in options" :key="o.value" :value="o.value">
{{ o.label }}
</option>
</select>
</template> Phone prefix
Show dialCode in the picker (+1264 for Anguilla). callingCode is the E.164 country code (+1 for NANP).
searchCountries ยท getDialCode ยท getCallingCode
Preview
import { getCallingCode, getDialCode, searchCountries } from 'country-kit';
const [match] = searchCountries('Anguilla', { limit: 1 });
getDialCode(match.code); // '+1264' show in the picker
getCallingCode(match.code); // '+1' ITU-T E.164 country code <script setup lang="ts">
import { getCallingCode, getDialCode, searchCountries } from 'country-kit';
const [match] = searchCountries('Anguilla', { limit: 1 });
getDialCode(match.code); // '+1264'
getCallingCode(match.code); // '+1'
</script> Search
searchCountries ranks official names, common names, aliases (UK, Vietnam), codes, TLDs, currencies, and capitals.
searchCountries
Preview
import { searchCountries } from 'country-kit';
const results = searchCountries(query, { limit: 6 });
// 'uk' / '.uk' โ United Kingdom
// 'Vietnam' โ Viet Nam
order.shipTo = results[0].code;
order.label = results[0].commonName; <script setup lang="ts">
import { computed, ref } from 'vue';
import { searchCountries } from 'country-kit';
const query = ref('uk');
const results = computed(() => searchCountries(query.value, { limit: 6 }));
</script> Flags
getFlagSvgUrl returns a CDN URL (core package). Import country-kit/flags only for inline SVG markup.
getFlagSvgUrl ยท getCountryFlag ยท getCountry
Preview
import { getCountryFlag, getFlagSvgUrl } from 'country-kit';
<img src={getFlagSvgUrl('JP')} alt="Japan" />
<img src={getFlagSvgUrl('JP', { ratio: '1x1' })} alt="" />
getCountryFlag('JP'); // '๐ฏ๐ต' <script setup lang="ts">
import { getCountryFlag, getFlagSvgUrl } from 'country-kit';
</script>
<template>
<img :src="getFlagSvgUrl('JP')" alt="Japan" />
<img :src="getFlagSvgUrl('JP', { ratio: '1x1' })" alt="" />
</template> TLD lookup
getCountryByTld maps IANA ccTLDs to alpha-2. gov.uk is GB, not .gb. .com is not a country TLD.
getCountryByTld
Preview
import { getCountryByTld } from 'country-kit';
function countryFromHost(value: string) {
const host = value.includes('@')
? value.split('@').pop()!
: value.replace(/^https?:\/\//, '').split(/[/?#]/)[0];
return getCountryByTld('.' + host.split('.').pop());
}
countryFromHost('https://www.gov.uk'); // GB
countryFromHost('https://npmjs.com'); // undefined <script setup lang="ts">
import { getCountryByTld } from 'country-kit';
const country = getCountryByTld('.uk'); // GB, not .gb
</script> Validation
isValidCountryCode is a type guard for assigned alpha-2 codes. getCountry also accepts alpha-3 and numeric. XK is not assigned.
isValidCountryCode ยท getCountry ยท isValidCallingCode
Preview
import { getCountry, isValidCountryCode, type CountryCode } from 'country-kit';
function parseCountry(raw: string): CountryCode | undefined {
if (isValidCountryCode(raw)) return raw;
return getCountry(raw)?.code; // 'USA' / '840' โ 'US'
}
parseCountry('XK'); // undefined (not ISO assigned) <script setup lang="ts">
import { getCountry, isValidCallingCode, isValidCountryCode } from 'country-kit';
isValidCountryCode('GB'); // true
isValidCountryCode('XK'); // false
getCountry('USA')?.code; // 'US'
isValidCallingCode('+44'); // true
isValidCallingCode('+1264'); // false
</script>