Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/.vuepress/components/GeolocationExample.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<template>
<div>
<h6>
Supported: <b>{{ supported }}</b>
</h6>
<button @click="refresh">Request location</button>
<div>
coords:
<ul v-if="coords">
<li>latitude: {{ coords.latitude }}</li>
<li>longitude: {{ coords.longitude }}</li>
<li>altitude : {{ coords.altitude }}</li>
<li>accuracy: {{ coords.accuracy }}</li>
<li>altitudeAccuracy: {{ coords.altitudeAccuracy }}</li>
<li>heading : {{ coords.heading }}</li>
<li>speed : {{ coords.speed }}</li>
</ul>
<div v-else-if="error">
<p><b>Error:</b> {{ error }}</p>
</div>
<div v-else>
No coords
</div>
</div>
</div>
</template>

<script>
import { useGeolocation } from "vue-composable";
export default {
setup() {
return useGeolocation({ immediate: true });
}
};
</script>
3 changes: 2 additions & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ module.exports = {
["composable/web/online", "Navigator.onLine"],
["composable/web/pageVisibility", "PageVisibilityAPI"],
["composable/web/language", "Language"],
["composable/web/broadcastChannel", "BroadcastChannel API"]
["composable/web/broadcastChannel", "BroadcastChannel API"],
["composable/web/geolocation", "Geolocation API"]
]
},
{
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Check out the [examples folder](examples) or start hacking on [codesandbox](http
- [PageVisibility](composable/web/pageVisibility) - reactive `Page Visibility API`
- [Language](composable/web/language) - reactive `NavigatorLanguage`
- [BroadcastChannel](composable/web/broadcastChannel) - reactive `BroadcastChannel API`
- [Geolocation](composable/web/geolocation) - reactive `Geolocation API`

### External

Expand Down
93 changes: 93 additions & 0 deletions docs/composable/web/geolocation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Geolocation API

> [Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API).

## Parameters

```js
import { useGeolocation } from "vue-composable";

const geolocation = useGeolocation(options?);
```

| Parameters | Type | Required | Default | Description |
| ---------- | ------------------------------------------- | -------- | --------------------- | ----------------------------------------------------------------------------------- |
| options | `PositionOptions` & `{immediate?: boolean}` | | `{ immediate: true }` | Options to handle geoLocation, `immediate` will trigger `watchPosition` on mounting |

## State

The `useGeolocation` function exposes the following reactive state:

```js
import { useGeolocation } from "vue-composable";

const { supported, coords, highAccuracy, error, timestamp } = useGeolocation();
```

| State | Type | Description |
| ------------ | -------------------- | ------------------------------------------ |
| supported | `Boolean` | Checks if the browser supports Geolocation |
| coords | `Ref<Position>` | Position object |
| highAccuracy | `Ref<Boolean>` | enable or disable highAccuracy mode |
| error | `Ref<PositionError>` | last position error |
| timestamp | `Ref<Number>` | Timestamp of the last position or error |

## Methods

The `useGeolocation` function exposes the following methods:

```js
import { useGeolocation } from "vue-composable";

const { refresh } = useGeolocation();
```

| Signature | Description |
| --------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `refresh` | Refreshes the location. If `immediate:false` it will add a watch on `watchPosition` and if called multiple times will call `getCurrentPosition` |

## Example

<ClientOnly>
<geolocation-example/>
</ClientOnly>

### Code

```vue
<template>
<div>
<h6>
Supported: <b>{{ supported }}</b>
</h6>
<button @click="refresh">Request location</button>
<div>
coords:
<ul v-if="coords">
<li>latitude: {{ coords.latitude }}</li>
<li>longitude: {{ coords.longitude }}</li>
<li>altitude : {{ coords.altitude }}</li>
<li>accuracy: {{ coords.accuracy }}</li>
<li>altitudeAccuracy: {{ coords.altitudeAccuracy }}</li>
<li>heading : {{ coords.heading }}</li>
<li>speed : {{ coords.speed }}</li>
</ul>
<div v-else-if="error">
<p><b>Error:</b> {{ error }}</p>
</div>
<div v-else>
No coords
</div>
</div>
</div>
</template>

<script>
import { useGeolocation } from "vue-composable";
export default {
setup() {
return useGeolocation({ immediate: true });
}
};
</script>
```
12 changes: 11 additions & 1 deletion packages/web/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ export const Vue: any = require('vue/dist/vue.common');
Vue.config.productionTip = false;
Vue.config.devtools = false;


Vue.config.warnHandler = (err: any) => {
throw err;
}

Vue.config.erroHandler = (err: any) => {
throw err;
}


export function nextTick<T>(callback: (this: T) => void, context?: T): void;
export function nextTick(): Promise<void>;
export function nextTick<T>(callback?: (this: T) => void, context?: T) {
if(!callback) {
if (!callback) {
return Vue.nextTick();
}
return Vue.nextTick(callback, context);
Expand Down
Loading