-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Renna-Labs/v1.2.0
v1.2.0
- Loading branch information
Showing
15 changed files
with
340 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,11 @@ yarn-error.log | |
node_modules | ||
temp | ||
dist | ||
lib | ||
coverage | ||
esm | ||
cjs | ||
buildcache | ||
types | ||
*.log | ||
example/.cache | ||
|
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 |
---|---|---|
|
@@ -7,4 +7,6 @@ node_modules | |
*.map | ||
tests | ||
.circleci | ||
tsconfig.json | ||
tsconfig.json | ||
tsconfig.tsbuildinfo | ||
buildcache |
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,23 @@ | ||
import { useCallback, useState } from 'react'; | ||
import Cookies from 'js-cookie'; | ||
|
||
export const useCookie = ( | ||
cookieName: string, | ||
): [string | null, (newValue: string, options?: Cookies.CookieAttributes) => void, () => void] => { | ||
const [value, setValue] = useState<string | null>(() => Cookies.get(cookieName) || null); | ||
|
||
const updateCookie = useCallback( | ||
(newValue: string, options?: Cookies.CookieAttributes) => { | ||
Cookies.set(cookieName, newValue, options); | ||
setValue(newValue); | ||
}, | ||
[cookieName], | ||
); | ||
|
||
const deleteCookie = useCallback(() => { | ||
Cookies.remove(cookieName); | ||
setValue(null); | ||
}, [cookieName]); | ||
|
||
return [value, updateCookie, deleteCookie]; | ||
}; |
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 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { useDocumentTitle } from './useDocumentTitle'; | ||
|
||
describe('useDocumentTitle', () => { | ||
it('sets given value as document.title', () => { | ||
renderHook(() => useDocumentTitle('test-title')); | ||
expect(document.title).toBe('test-title'); | ||
}); | ||
|
||
it('does not change document.title if called with empty string', () => { | ||
document.title = 'test-title'; | ||
renderHook(() => useDocumentTitle('')); | ||
expect(document.title).toBe('test-title'); | ||
renderHook(() => useDocumentTitle(' \t\n')); | ||
expect(document.title).toBe('test-title'); | ||
}); | ||
|
||
it('trims value before setting to document.title', () => { | ||
renderHook(() => useDocumentTitle(' test-title\t\n ')); | ||
expect(document.title).toBe('test-title'); | ||
}); | ||
}); |
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,9 @@ | ||
import { useIsomorphicEffect } from '../useIsomorphicEffect/useIsomorphicEffect'; | ||
|
||
export function useDocumentTitle(title: string) { | ||
useIsomorphicEffect(() => { | ||
if (typeof title === 'string' && title.trim().length > 0) { | ||
document.title = title.trim(); | ||
} | ||
}, [title]); | ||
} |
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,36 @@ | ||
import { renderHook, fireEvent } from '@testing-library/react'; | ||
import { useDocumentVisibility } from './useDocumentVisibility'; | ||
|
||
describe('useDocumentVisibility', () => { | ||
beforeAll(() => { | ||
Object.defineProperty(document, 'visibilityState', { | ||
value: 'visible', | ||
writable: true, | ||
}); | ||
}); | ||
|
||
it('return default visibility state', () => { | ||
const { result } = renderHook(() => useDocumentVisibility()); | ||
|
||
expect(result.current).toBe('visible'); | ||
}); | ||
|
||
it('should update return value when visibilityState changes', () => { | ||
const { result } = renderHook(() => useDocumentVisibility()); | ||
|
||
expect(result.current).toBe('visible'); | ||
|
||
// @ts-ignore | ||
document.visibilityState = 'hidden'; | ||
|
||
fireEvent(document, new Event('visibilitychange')); | ||
|
||
expect(result.current).toBe('hidden'); | ||
// @ts-ignore | ||
document.visibilityState = 'visible'; | ||
|
||
fireEvent(document, new Event('visibilitychange')); | ||
|
||
expect(result.current).toBe('visible'); | ||
}); | ||
}); |
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 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
export function useDocumentVisibility(): DocumentVisibilityState { | ||
const [documentVisibility, setDocumentVisibility] = useState<DocumentVisibilityState>('visible'); | ||
|
||
useEffect(() => { | ||
const listener = () => setDocumentVisibility(document.visibilityState); | ||
document.addEventListener('visibilitychange', listener); | ||
return () => document.removeEventListener('visibilitychange', listener); | ||
}, []); | ||
|
||
return documentVisibility; | ||
} |
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,73 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* @desc Made compatible with {GeolocationPositionError} and {PositionError} cause | ||
* PositionError been renamed to GeolocationPositionError in typescript 4.1.x and making | ||
* own compatible interface is most easiest way to avoid errors. | ||
*/ | ||
export interface IGeolocationPositionError { | ||
readonly code: number; | ||
readonly message: string; | ||
readonly PERMISSION_DENIED: number; | ||
readonly POSITION_UNAVAILABLE: number; | ||
readonly TIMEOUT: number; | ||
} | ||
|
||
export interface GeoLocationSensorState { | ||
loading: boolean; | ||
accuracy: number | null; | ||
altitude: number | null; | ||
altitudeAccuracy: number | null; | ||
heading: number | null; | ||
latitude: number | null; | ||
longitude: number | null; | ||
speed: number | null; | ||
timestamp: number | null; | ||
error?: Error | IGeolocationPositionError; | ||
} | ||
|
||
export const useGeolocation = (options?: PositionOptions): GeoLocationSensorState => { | ||
const [state, setState] = useState<GeoLocationSensorState>({ | ||
loading: true, | ||
accuracy: null, | ||
altitude: null, | ||
altitudeAccuracy: null, | ||
heading: null, | ||
latitude: null, | ||
longitude: null, | ||
speed: null, | ||
timestamp: Date.now(), | ||
}); | ||
let mounted = true; | ||
let watchId: any; | ||
|
||
const onEvent = (event: any) => { | ||
if (mounted) { | ||
setState({ | ||
loading: false, | ||
accuracy: event.coords.accuracy, | ||
altitude: event.coords.altitude, | ||
altitudeAccuracy: event.coords.altitudeAccuracy, | ||
heading: event.coords.heading, | ||
latitude: event.coords.latitude, | ||
longitude: event.coords.longitude, | ||
speed: event.coords.speed, | ||
timestamp: event.timestamp, | ||
}); | ||
} | ||
}; | ||
const onEventError = (error: IGeolocationPositionError) => | ||
mounted && setState(oldState => ({ ...oldState, loading: false, error })); | ||
|
||
useEffect(() => { | ||
navigator.geolocation.getCurrentPosition(onEvent, onEventError, options); | ||
watchId = navigator.geolocation.watchPosition(onEvent, onEventError, options); | ||
|
||
return () => { | ||
mounted = false; | ||
navigator.geolocation.clearWatch(watchId); | ||
}; | ||
}, []); | ||
|
||
return state; | ||
}; |
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,5 @@ | ||
import { useEffect, useLayoutEffect } from 'react'; | ||
|
||
// useLayoutEffect will show warning if used during ssr, e.g. with Next.js | ||
// useIsomorphicEffect removes it by replacing useLayoutEffect with useEffect during ssr | ||
export const useIsomorphicEffect = typeof document !== 'undefined' ? useLayoutEffect : useEffect; |
Oops, something went wrong.