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

mapbox-svelte

v0.4.22

Published

A simple, lightweight, and flexible Svelte wrapper for MapBox. It's designed to be as simple as possible to use, and to be as flexible as possible to use.

Downloads

133

Readme

Svelte MapBox

A simple, lightweight, and flexible Svelte wrapper for MapBox. It's designed to be as simple as possible to use, and to be as flexible as possible to use.

It includes mapbox-gl-geocoder and mapbox-gl-draw as built-in components.

Installing

npm i mapbox-svelte

Basic Usage (Map)

The container component is the map, and there are a variety of components which go on the map.

<script>
import {env} from "$env/dynamic/public";
import {Map} from "mapbox-svelte";


let token = env.PUBLIC_MAPBOX_TOKEN as string;
let configuration = {
    container: "map-demo",
    style: "mapbox://styles/mapbox/satellite-streets-v11",
    center: [0.7051700188074221, 44.403806782279986],
    zoom: 7,
    attributionControl: false
}

let map;
const click = ({detail}) => {console.log(detail)}

</script>

<Map bind:map {token} {configuration} class="map-wrapper map-size" on:click={click}>

    //components go here

</Map>

Sources

Sources are the data that you want to display on the map. They can be a variety of types, including GeoJSON, images, and vector tiles.

<script>
    import {MapSource} from "$lib";

    let autofit = false;
    let data = {"type": "FeatureCollection", "features": []}
    
</script>

<MapSource name="city" type="geojson" bind:data {autofit} fitBoundsOptions={{"padding": 180}}>
    //Layers goes here
</MapSource>

//Control and custom controls goes here

Layers

Layers are the visual representation of the data from the source. They can be a variety of types, including fill, line, symbol, circle, and raster.

<script>
    import {MapLayer} from "mapbox-svelte";

    let type = "line";
    let paint = {"line-color": "#f00", "line-width": 2};

</script>

<MapLayer {type} {paint}/>

All events are passed to the svelte component for easy binding.

<script>
    import {MapLayer} from "mapbox-svelte";

    export let selected = null;

    let type = "fill";
    let paint = {"fill-color": "#088","fill-opacity": 0.8};
    let onmouseenter = ({detail}) => (detail.event.target.getCanvas().style.cursor = "pointer");
    let onmouseleave = ({detail}) => (detail.event.target.getCanvas().style.cursor = "");
    let onclick = ({detail}) => {selected = detail.event.features[0].properties};

</script>

<MapLayer {type} {paint} on:mouseenter = {onmouseenter} on:click = {onclick} on:mouseleave = {onmouseleave}/>

Markers & Popups

By default, markers are typical map pins to which you can pass an options object.

<Marker lng={0.7551700188074221} lat={44.403806782279986} markerOptions={{color: "#546"}}/>

You may also create a marker with a popup insert as a slot.

<Marker lng={0.7051700188074226} lat={44.403806782279986}>
    <svelte:fragment slot="popup">
        <div class="p-6">
            <h1 class="text-xl font-bold">Popup</h1>
            <p class="text-sm">This is a popup</p>
        </div>
    </svelte:fragment>
</Marker>

Controls

Controls are the UI elements that allow the user to interact with the map. They can be a variety of types, including navigation, geolocation, and scale. You can also create custom controls.

All control implements their position, and some of the options that mapbox provide. Events are also mapped.

<script>

    import {
    MapDrawControl,
    MapFullscreenControl,
    MapGeocoderControl,
    MapGeolocateControl,
    MapNavigationControl
} from "mapbox-svelte";

</script>

<MapGeolocateControl/>
<MapFullscreenControl/>
<MapNavigationControl/>
<MapDrawControl/>
<MapGeocoderControl countries={'fr'}/>

Custom Controls

You can also create custom controls.

<script>
    import {MapCustomControl} from "$lib";
    let value = "";
    
</script>

<MapCustomControl name="search" position="top-right" class="control-size">
    <div class="text-center p-6">
        <label for="Label">Label</label>
        <input type="text" bind:value placeholder="value"/>
    </div>
</MapCustomControl>

Demo

To see the basic demo:

Make sure you copy the .env file to .env.local and then populate it with your mapbox key.

npm run dev