Skip to content

Commit fc0aeed

Browse files
alamenaiAla
andauthored
feat: add useGeolocation documentation (#30)
Co-authored-by: Ala <[email protected]>
1 parent 652e38a commit fc0aeed

File tree

3 files changed

+118
-14
lines changed

3 files changed

+118
-14
lines changed

pages/hooks/_meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"use-cookie": "useCookie",
55
"use-env": "useEnv",
66
"use-fetch": "useFetch",
7+
"use-geolocation": "useGeolocation",
78
"use-history": "useHistory",
89
"use-loading": "useLoading",
910
"use-media-query": "useMediaQuery",

pages/hooks/use-geolocation.mdx

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { Steps } from 'nextra/components'
2+
import AvatarCircles from "@/components/magic-ui/avatar.circles.tsx";
3+
4+
# useGeolocation
5+
6+
<h3 className="mt-4 text-xl font-normal text-gray-400">A custom hook that provides functionalities for accessing and managing geolocation data.</h3>
7+
8+
### Add hook
9+
10+
Create a file `use-geolocation.ts` and copy & paste the code from [useGeolocation](/hooks/use-geolocation#hook).
11+
12+
### Hook
13+
14+
```ts copy
15+
import { useState, useEffect, useCallback } from 'react'
16+
17+
type Coordinates = {
18+
latitude: number | null
19+
longitude: number | null
20+
}
21+
22+
type GeolocationError = {
23+
code: number
24+
message: string
25+
} | null
26+
27+
type GeolocationOptions = {
28+
enableHighAccuracy?: boolean
29+
timeout?: number
30+
maximumAge?: number
31+
}
32+
33+
export const useGeolocation = (options?: GeolocationOptions) => {
34+
const [coordinates, setCoordinates] = useState<Coordinates>({
35+
latitude: null,
36+
longitude: null,
37+
})
38+
39+
const [error, setError] = useState<GeolocationError>(null)
40+
41+
const handleSuccess = (position: GeolocationPosition) => {
42+
const { latitude, longitude } = position.coords
43+
setCoordinates({ latitude, longitude })
44+
setError(null)
45+
}
46+
47+
const handleError = (error: GeolocationPositionError) => {
48+
setError({
49+
code: error.code,
50+
message: error.message,
51+
})
52+
}
53+
54+
const getCurrentPosition = useCallback(() => {
55+
if (navigator.geolocation) {
56+
navigator.geolocation.getCurrentPosition(handleSuccess, handleError, options)
57+
} else {
58+
setError({
59+
code: 0,
60+
message: 'Geolocation is not supported by your browser.',
61+
})
62+
}
63+
}, [options])
64+
65+
useEffect(() => {
66+
getCurrentPosition()
67+
}, [getCurrentPosition])
68+
69+
return { coordinates, error, refresh: getCurrentPosition }
70+
}
71+
```
72+
73+
### Usage
74+
75+
```jsx copy
76+
import React from 'react';
77+
import { useGeolocation } from './hooks/use-geolocation';
78+
79+
function GeolocationComponent() {
80+
const { coordinates, error, refresh } = useGeolocation();
81+
82+
if (error) {
83+
return <div>Error: {error.message}</div>;
84+
}
85+
86+
return (
87+
<div>
88+
<h1>Geolocation Demo</h1>
89+
{coordinates.latitude && coordinates.longitude ? (
90+
<p>
91+
Latitude: {coordinates.latitude}, Longitude: {coordinates.longitude}
92+
</p>
93+
) : (
94+
<p>Loading location...</p>
95+
)}
96+
<button onClick={refresh}>Refresh Location</button>
97+
</div>
98+
);
99+
}
100+
101+
export default GeolocationComponent;
102+
```
103+
104+
## API
105+
106+
<h1 className='text-xl font-medium mt-6'>Returns</h1>
107+
108+
| Name | Description |
109+
|------|-------------|
110+
| `coordinates` | An object containing the current latitude and longitude. |
111+
| `error` | An object containing error information if geolocation fails. |
112+
| `refresh` | A function to manually refresh the geolocation. |
113+
114+
## Contributors
115+
116+
<AvatarCircles numPeople={1} avatarUrls={["https://avatars.githubusercontent.com/u/20143684?v=4"]} className="mt-6"/>

theme.config.jsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,11 @@ const config = {
8383
return (
8484
<div className="flex items-center justify-between relative w-full">
8585
<div>{title}</div>
86-
{(title === "useLoading" ||
87-
title === "useBattery" ||
88-
title === "useUrl" ||
89-
title === "useEnv" ||
90-
title === "useMouse" ||
91-
title === "useUnsavedFormChanges" ||
92-
title === "useCookie" ||
93-
title === "useSession" ||
94-
title === "useMediaQuery") && (
86+
{title === "useGeolocation" && (
9587
<Badge className=" absolute -right-[0.5em] bg-transparent border-rose-500 text-rose-500 px-[0.5em] hover:bg-transparent">
9688
New
9789
</Badge>
9890
)}
99-
{title === "useFetch" && (
100-
<Badge className=" absolute -right-[0.5em] bg-transparent border-blue-500 text-blue-500 px-[0.5em] hover:bg-transparent">
101-
Updated
102-
</Badge>
103-
)}
10491
</div>
10592
);
10693
},

0 commit comments

Comments
 (0)