Giter Site home page Giter Site logo

path-selector's Introduction

path-selector

This package uses by default the world map svg from mapsvg

Create a modal that can listen for any selected path inside it

(Example)[https://codesandbox.io/s/path-selector-2j4trz?file=/src/App.vue]

Install

npm i path-selector

Then inside your main.js (ts)

import pathSelector from "path-selector";

import "path-selector/dist/style.css";

createApp(App).use(pathSelector).mount("#app");

How to use

importing the component you´ll see the trigger button and the default SVG

<path-selector />

add styles to the buttons

openButton slot

For editing the trigger button you could use the openButton named slot

<path-selector>
  <template #openButton="{ showModal }">
    <button @click="showModal">Open my SVG</button>
  </template>
</path-selector>

The showModal function opens the modal

closeButton

For editing the close button you could use the closeButton named slot

<path-selector>
  <template #closeButton="{ hideModal }">
    <button @click="hideModal">Close modal</button>
  </template>
</path-selector>

The hideModal function close the modal Additionally if you don't want to show this button you could pass the props: "hasCloseButton" by default is true

add your own SVG

For use your own SVG you could use the svg named slot

<path-selector>
  <template #svg>
    <my-super-svg />
  </template>
</path-selector>

Note that here we pass a vue component as a SVG not an image or a inline svg

Todo this is as simple as create a new vue component an inside the template tag add the path without groups tags

<!-- my-super-svg -->
<template>
  <path id="CL" />
  <path id="UR" />
  <path id="AR" />
  // ...
</template>

This will display the new SVG and also apply hover styles when you open the modal, but you need one more step to make it selectable

Note that in the last example we added the IDs this is important because this is the way the plugin will differentiate the paths

Now you just need to pass a JSON with an ID properties, that match the IDs of SVG, Example:

...
{
  "name": "Chile",
  "dialCode": "+56",
  "id": "CL",
  "flag": "https://www.countryflags.io/CL/flat/64.png"
},
...

In this example the id="CL" will match with the path that has ID="CL", so, when you press will return all the JSON data

now we just need to listen the selectedPath event, and we're done, you have your own svg with selectable paths

<template>
  <path-selector
    @selectedPath="(path) => MYFUNC(path)"
    hoverColor="red"
    :svgData="MYJSON"
  >
    <template #svg>
      <my-super-svg />
    </template>
  </path-selector>
</template>

how it works

By default this plugin will recognized each path, then when you select a path will search the id attribute in the JSON and return all the data.

You could pass virtually any SVG that you want (with the right format)

Note: This package works great with the mapsvg maps.

Props

Props Description Default
svgData Json to map in every path available on the svg world-map
viewBox Custom viewBox values for an svg "0 0 1025 650"
pathColor Color of the map "#333"
hoverColor Color of the map on hover state "#c7c7c7"
hasCloseButton To hidden the close button true
width width of the viewPort of the svg "100%"
height height of the viewPort of the svg "75vh"

path-selector's People

Contributors

jaimetorrealba avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.