-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
640 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@yext/search-ui-react](./search-ui-react.md) > [Geolocation\_2](./search-ui-react.geolocation_2.md) | ||
|
||
## Geolocation\_2() function | ||
|
||
A React Component which collects location information to create a location filter and perform a new search. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export declare function Geolocation({ geolocationOptions, radius, label, GeolocationIcon, handleClick, customCssClasses, }: GeolocationProps): JSX.Element | null; | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| { geolocationOptions, radius, label, GeolocationIcon, handleClick, customCssClasses, } | [GeolocationProps](./search-ui-react.geolocationprops.md) | | | ||
|
||
<b>Returns:</b> | ||
|
||
JSX.Element \| null | ||
|
||
A react component for geolocation | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@yext/search-ui-react](./search-ui-react.md) > [GeolocationCssClasses](./search-ui-react.geolocationcssclasses.md) > [button](./search-ui-react.geolocationcssclasses.button.md) | ||
|
||
## GeolocationCssClasses.button property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
button?: string; | ||
``` |
11 changes: 11 additions & 0 deletions
11
docs/search-ui-react.geolocationcssclasses.geolocationcontainer.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@yext/search-ui-react](./search-ui-react.md) > [GeolocationCssClasses](./search-ui-react.geolocationcssclasses.md) > [geolocationContainer](./search-ui-react.geolocationcssclasses.geolocationcontainer.md) | ||
|
||
## GeolocationCssClasses.geolocationContainer property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
geolocationContainer?: string; | ||
``` |
11 changes: 11 additions & 0 deletions
11
docs/search-ui-react.geolocationcssclasses.iconcontainer.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@yext/search-ui-react](./search-ui-react.md) > [GeolocationCssClasses](./search-ui-react.geolocationcssclasses.md) > [iconContainer](./search-ui-react.geolocationcssclasses.iconcontainer.md) | ||
|
||
## GeolocationCssClasses.iconContainer property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
iconContainer?: string; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@yext/search-ui-react](./search-ui-react.md) > [GeolocationCssClasses](./search-ui-react.geolocationcssclasses.md) | ||
|
||
## GeolocationCssClasses interface | ||
|
||
The CSS class interface for the Geolocation component. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface GeolocationCssClasses | ||
``` | ||
|
||
## Properties | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| [button?](./search-ui-react.geolocationcssclasses.button.md) | string | <i>(Optional)</i> | | ||
| [geolocationContainer?](./search-ui-react.geolocationcssclasses.geolocationcontainer.md) | string | <i>(Optional)</i> | | ||
| [iconContainer?](./search-ui-react.geolocationcssclasses.iconcontainer.md) | string | <i>(Optional)</i> | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@yext/search-ui-react](./search-ui-react.md) > [GeolocationProps](./search-ui-react.geolocationprops.md) > [customCssClasses](./search-ui-react.geolocationprops.customcssclasses.md) | ||
|
||
## GeolocationProps.customCssClasses property | ||
|
||
CSS classes for customizing the component styling. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
customCssClasses?: GeolocationCssClasses; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@yext/search-ui-react](./search-ui-react.md) > [GeolocationProps](./search-ui-react.geolocationprops.md) > [GeolocationIcon](./search-ui-react.geolocationprops.geolocationicon.md) | ||
|
||
## GeolocationProps.GeolocationIcon property | ||
|
||
Custom icon component to display along with the button. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
GeolocationIcon?: React.FunctionComponent; | ||
``` |
13 changes: 13 additions & 0 deletions
13
docs/search-ui-react.geolocationprops.geolocationoptions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@yext/search-ui-react](./search-ui-react.md) > [GeolocationProps](./search-ui-react.geolocationprops.md) > [geolocationOptions](./search-ui-react.geolocationprops.geolocationoptions.md) | ||
|
||
## GeolocationProps.geolocationOptions property | ||
|
||
Configuration used when collecting the user's location. Definition: [https://w3c.github.io/geolocation-api/\#position\_options\_interface](https://w3c.github.io/geolocation-api/#position_options_interface)<!-- -->. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
geolocationOptions?: PositionOptions; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@yext/search-ui-react](./search-ui-react.md) > [GeolocationProps](./search-ui-react.geolocationprops.md) > [handleClick](./search-ui-react.geolocationprops.handleclick.md) | ||
|
||
## GeolocationProps.handleClick property | ||
|
||
A function which is called when the geolocation button is clicked. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
handleClick?: (position: GeolocationPosition) => void; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@yext/search-ui-react](./search-ui-react.md) > [GeolocationProps](./search-ui-react.geolocationprops.md) > [label](./search-ui-react.geolocationprops.label.md) | ||
|
||
## GeolocationProps.label property | ||
|
||
The label for the button. Defaults to 'Use my location'. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
label?: string; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@yext/search-ui-react](./search-ui-react.md) > [GeolocationProps](./search-ui-react.geolocationprops.md) | ||
|
||
## GeolocationProps interface | ||
|
||
The props for the Geolocation component. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface GeolocationProps | ||
``` | ||
|
||
## Properties | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| [customCssClasses?](./search-ui-react.geolocationprops.customcssclasses.md) | [GeolocationCssClasses](./search-ui-react.geolocationcssclasses.md) | <i>(Optional)</i> CSS classes for customizing the component styling. | | ||
| [GeolocationIcon?](./search-ui-react.geolocationprops.geolocationicon.md) | React.FunctionComponent | <i>(Optional)</i> Custom icon component to display along with the button. | | ||
| [geolocationOptions?](./search-ui-react.geolocationprops.geolocationoptions.md) | PositionOptions | <i>(Optional)</i> Configuration used when collecting the user's location. Definition: [https://w3c.github.io/geolocation-api/\#position\_options\_interface](https://w3c.github.io/geolocation-api/#position_options_interface)<!-- -->. | | ||
| [handleClick?](./search-ui-react.geolocationprops.handleclick.md) | (position: GeolocationPosition) => void | <i>(Optional)</i> A function which is called when the geolocation button is clicked. | | ||
| [label?](./search-ui-react.geolocationprops.label.md) | string | <i>(Optional)</i> The label for the button. Defaults to 'Use my location'. | | ||
| [radius?](./search-ui-react.geolocationprops.radius.md) | number | <i>(Optional)</i> The radius, in miles, around the user's location to find results. Defaults to 50. If location accuracy is low, a larger radius may be used automatically. | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@yext/search-ui-react](./search-ui-react.md) > [GeolocationProps](./search-ui-react.geolocationprops.md) > [radius](./search-ui-react.geolocationprops.radius.md) | ||
|
||
## GeolocationProps.radius property | ||
|
||
The radius, in miles, around the user's location to find results. Defaults to 50. If location accuracy is low, a larger radius may be used automatically. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
radius?: number; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { Matcher, SelectableStaticFilter, useSearchActions, useSearchState } from '@yext/search-headless-react'; | ||
import { executeSearch } from '../utils/search-operations'; | ||
import { getUserLocation } from '../utils/location-operations'; | ||
import { useComposedCssClasses } from '../hooks/useComposedCssClasses'; | ||
import { useCallback, useState } from 'react'; | ||
import LoadingIndicator from '../icons/LoadingIndicator'; | ||
import { YextIcon } from '../icons/YextIcon'; | ||
|
||
/** | ||
* The CSS class interface for the Geolocation component. | ||
* | ||
* @public | ||
*/ | ||
export interface GeolocationCssClasses { | ||
geolocationContainer?: string, | ||
button?: string, | ||
iconContainer?: string | ||
} | ||
|
||
const builtInCssClasses: Readonly<GeolocationCssClasses> = { | ||
geolocationContainer: 'text-sm text-neutral text-center justify-center items-center flex flex-row', | ||
button: 'text-primary font-semibold hover:underline focus:underline', | ||
iconContainer: 'w-4 ml-2' | ||
}; | ||
|
||
/** | ||
* The props for the Geolocation component. | ||
* | ||
* @public | ||
*/ | ||
export interface GeolocationProps { | ||
/** | ||
* Configuration used when collecting the user's location. | ||
* Definition: {@link https://w3c.github.io/geolocation-api/#position_options_interface}. | ||
*/ | ||
geolocationOptions?: PositionOptions, | ||
/** | ||
* The radius, in miles, around the user's location to find results. Defaults to 50. | ||
* If location accuracy is low, a larger radius may be used automatically. | ||
*/ | ||
radius?: number, | ||
/** The label for the button. Defaults to 'Use my location'. */ | ||
label?: string, | ||
/** Custom icon component to display along with the button. */ | ||
GeolocationIcon?: React.FunctionComponent, | ||
/** | ||
* A function which is called when the geolocation button is clicked, | ||
* after user's position is successfully determined. | ||
*/ | ||
handleClick?: (position: GeolocationPosition) => void, | ||
/** CSS classes for customizing the component styling. */ | ||
customCssClasses?: GeolocationCssClasses | ||
} | ||
|
||
const LOCATION_FIELD_ID = 'builtin.location'; | ||
const METERS_PER_MILE = 1609.344; | ||
|
||
/** | ||
* A React Component which collects location information to create a | ||
* location filter and perform a new search. | ||
* | ||
* @public | ||
* | ||
* @param props - {@link GeolocationProps} | ||
* @returns A react component for geolocation | ||
*/ | ||
export function Geolocation({ | ||
geolocationOptions, | ||
radius = 50, | ||
label = 'Use my location', | ||
//TODO: replace default icon with SVG create from design team | ||
GeolocationIcon = YextIcon, | ||
handleClick, | ||
customCssClasses, | ||
}: GeolocationProps): JSX.Element | null { | ||
const searchActions = useSearchActions(); | ||
const staticFilters = useSearchState(s => s.filters.static || []); | ||
const [isFetchingLocation, setIsFetchingLocation] = useState<boolean>(false); | ||
const cssClasses = useComposedCssClasses(builtInCssClasses, customCssClasses); | ||
|
||
const defaultHandleClick = useCallback((position: GeolocationPosition) => { | ||
const { latitude, longitude, accuracy } = position.coords; | ||
const locationFilter: SelectableStaticFilter = { | ||
displayName: 'Current Location', | ||
selected: true, | ||
filter: { | ||
kind: 'fieldValue', | ||
fieldId: LOCATION_FIELD_ID, | ||
matcher: Matcher.Near, | ||
value: { | ||
lat: latitude, | ||
lng: longitude, | ||
radius: Math.max(accuracy, radius * METERS_PER_MILE) | ||
}, | ||
} | ||
}; | ||
const nonLocationFilters = staticFilters.filter(filter => { | ||
return !(filter.filter.kind === 'fieldValue' | ||
&& filter.filter.fieldId === LOCATION_FIELD_ID); | ||
}); | ||
searchActions.setStaticFilters([...nonLocationFilters, locationFilter]); | ||
executeSearch(searchActions); | ||
}, [radius, searchActions, staticFilters]); | ||
|
||
const handleGeolocationClick = useCallback(async () => { | ||
setIsFetchingLocation(true); | ||
try { | ||
const position = await getUserLocation(geolocationOptions); | ||
(handleClick ?? defaultHandleClick)(position); | ||
} catch (e) { | ||
console.warn(e); | ||
} finally { | ||
setIsFetchingLocation(false); | ||
} | ||
}, [geolocationOptions, handleClick, defaultHandleClick]); | ||
|
||
return ( | ||
<div className={cssClasses.geolocationContainer}> | ||
<button className={cssClasses.button} onClick={handleGeolocationClick}> | ||
{label} | ||
</button> | ||
<div className={cssClasses.iconContainer}> | ||
{isFetchingLocation ? <LoadingIndicator /> : <GeolocationIcon />} | ||
</div> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.