Svelte component
A Svelte 5 component for the intl-tel-input JavaScript plugin. View the source code.
Contents
- Demo
- Getting started
- Props
- Initialisation options
- Accessing instance methods
- Accessing static methods
Demo
You can see a live demo and example code on the Svelte component example page.
Try it for yourself by downloading and building the project yourself in 3 simple steps. You just need to initialise the submodules with git submodule update --init --recursive, then run npm install, and then npm run build. You can then run npm run svelte:demo and copy the given URL into your browser. This defaults to the validation demo — to run a different one, set the DEMO env var, e.g. DEMO=simple npm run svelte:demo. View a list of available demos.
Getting started
First, install the package:
npm install intl-tel-input
Then, add something like this to your code:
<script>
import IntlTelInput from "intl-tel-input/svelteWithUtils";
import "intl-tel-input/styles";
</script>
<IntlTelInput
initialCountry="us"
/>
See the Validation demo for a more fleshed-out example of how to handle validation.
[!NOTE] The package ships the raw
.sveltesource file rather than a pre-built.mjsbundle, so your project’s existing Svelte tooling compiles it alongside your own components. Any standard Svelte setup (SvelteKit, Vite +@sveltejs/vite-plugin-svelte, etc.) handles this out of the box.
A note on the utils script (~260KB): if you’re lazy loading the IntlTelInput chunk (and so less worried about filesize) then you can just import IntlTelInput from "intl-tel-input/svelteWithUtils", to include the utils script. Alternatively, if you use the main "intl-tel-input/svelte" import, then you should couple this with the loadUtils initialisation option - you will need to host the utils.js file, and then set the loadUtils option to that URL, or just point it to a CDN-hosted version, e.g. "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/utils.js".
Props
Here’s a list of all of the current props you can pass to the IntlTelInput Svelte component.
Initialisation options
All of the plugin’s initialisation options are supported as individual Svelte component props using the same option name.
For example, if you’re migrating from older usage like:
<IntlTelInput initOptions={{ initialCountry: "us" }} />
Use:
<IntlTelInput initialCountry="us" />
Accessing instance methods
You can access all of the plugin’s instance methods (setNumber, setCountry, setPlaceholderNumberType, etc.) by passing a ref into the IntlTelInput component (using bind:this), and then calling the getInstance() method, e.g. ref.getInstance().setNumber(...);. See the Set Number demo for a full example. You can also access the input DOM element via: ref.getInput().
Accessing static methods
You can access all of the plugin’s static methods by importing intlTelInput from the same file as the Svelte component, e.g. import { intlTelInput } from "intl-tel-input/svelte" (note the lower case “i” in “intlTelInput”). You can then use this as you would with the main plugin, e.g. intlTelInput.getCountryData() or intlTelInput.utils.numberType etc.