npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kreonovo/places-autocomplete-svelte

v2.0.9

Published

A lightweight and customizable Svelte component for easy integration of Google Maps Places Autocomplete (New API) in your Svelte/SvelteKit applications. Provides accessible autocomplete suggestions and detailed address retrieval.

Readme

Places Autocomplete Svelte

This Svelte component leverages the Google Maps Places Autocomplete API to provide a user-friendly way to search for and retrieve detailed address information within your SvelteKit applications.

Preview this package Demo

Features:

  • Seamless Integration: Easily integrate the component into your SvelteKit projects.
  • Autocomplete Suggestions: Provide users with real-time suggestions as they type, enhancing the search experience.
  • Detailed Address Retrieval: Retrieve comprehensive address information, including street address, city, region, postal code, and country.
  • Country/Region Selection: Allow users to specify a region for more targeted results.
  • Customizable: Tailor the component's appearance and behavior using language settings, placeholder text.
  • Accessible: Supports keyboard navigation for selecting suggestions.

Places Autocomplete Svelte

Requirements

  • Google Maps API Key: Create an API key with the Places API (New) enabled. Refer to Use API Keys for detailed instructions.

Installation Svelte 5

npm i places-autocomplete-svelte

Installation Svelte 4

npm i [email protected]

Basic Usage

  1. Provide your Google Maps API Key: Replace '___YOUR_API_KEY___' with your actual Google Maps API key.
  2. Handle the Response: Use the onResponse callback to receive the selected place details.
<script>
	import { PlaceAutocomplete } from 'places-autocomplete-svelte';

	const PUBLIC_GOOGLE_MAPS_API_KEY = '___YOUR_API_KEY___';

	let fullResponse = $state('')
	let onResponse = (response) => {
		console.log(response)
		fullResponse = response;
	};
</script>

<PlaceAutocomplete  {onResponse} {PUBLIC_GOOGLE_MAPS_API_KEY} />

<p>Response Object: {JSON.stringify(fullResponse, null, 2)}</p>

Countries/Regions

Use optional countries property to refine search by region:

<script>
	// ... other imports

	let countries = [
		{ name: 'United Kingdom', region: 'GB', language: 'en-GB' },
		{ name: 'United States', region: 'US', language: 'en-US' }
		// ... more countries
	];
</script>

<PlaceAutocomplete {onResponse} {PUBLIC_GOOGLE_MAPS_API_KEY} bind:countries/>

Error Handling

The component will throw an error if:

  • The Google Maps API key is invalid or missing.
  • There are network issues connecting to the Google Maps service.

Handle these errors gracefully in your application:

<script>
	// ... other imports

	// Error handler
	let pacError = '';
	let onError = (error: string) => {
		console.error(error);
		pacError = error;
	};
</script>

<PlaceAutocomplete {onError} {onResponse} {PUBLIC_GOOGLE_MAPS_API_KEY} />

{#if pacError}
	<p class="error">{pacError}</p>
{/if}

Customization

  • Placeholder: Use the placeholder property to customize the input field's placeholder text.
  • Language: Use the language property to set the language of the autocomplete results.
  • Region: Use the region property to bias the results toward a particular region. If the countries array is provided the region will be used from the selected country.
  • Autocomplete: Use to disable the input autocomplete.
<script>
	// ... other imports
	const placeholder = 'Search...';
	const language = 'en-GB';
	const region = 'GB';
	const autocompete = 'off';
</script>

<PlaceAutocomplete {onError} {onResponse} {PUBLIC_GOOGLE_MAPS_API_KEY} bind:countries {placeholder} {language} {region} {autocomplete}/>

Contributing

Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository.

License

MIT