generated from gravity-ui/package-example
-
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
7 changed files
with
86 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
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 @@ | ||
export * from './useSyncWithLS'; |
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,19 @@ | ||
<!--GITHUB_BLOCK--> | ||
|
||
# useSyncWithLS | ||
|
||
<!--/GITHUB_BLOCK--> | ||
|
||
```tsx | ||
import {useSyncWithLS} from '@gravity-ui/components'; | ||
``` | ||
|
||
The `useSyncWithLS` hook executes callback when value changed in Local Storage | ||
|
||
## Properties | ||
|
||
| Name | Description | Type | Default | | ||
| :------------- | :----------------------------------------------------------- | :------------: | :---------: | | ||
| callback | Callback function called when key in local storage triggered | `VoidFunction` | | | ||
| dataSourceName | Name for data source of keys | `string` | 'sync-tabs' | | ||
| uniqueKey | Key in local storage for handle | `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,2 @@ | ||
export {useSyncWithLS} from './useSyncWithLS'; | ||
export type {UseSyncWithLSInputProps, UseSyncWithLSOutputProps} from './useSyncWithLS'; |
40 changes: 40 additions & 0 deletions
40
src/components/Stories/hooks/useSyncWithLS/useSyncWithLS.ts
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,40 @@ | ||
import React from 'react'; | ||
|
||
export type UseSyncWithLSInputProps<T> = { | ||
callback: T; | ||
uniqueKey: string; | ||
dataSourceName?: string; | ||
}; | ||
export type UseSyncWithLSOutputProps = {callback: (...args: unknown[]) => void}; | ||
|
||
export const useSyncWithLS = <T extends Function>({ | ||
dataSourceName = 'sync-tabs', | ||
callback, | ||
uniqueKey, | ||
}: UseSyncWithLSInputProps<T>): UseSyncWithLSOutputProps => { | ||
const key = `${dataSourceName}.${uniqueKey}`; | ||
|
||
const handler = (event: StorageEvent) => { | ||
if (event.key === key && event.newValue) { | ||
return callback(); | ||
} | ||
return undefined; | ||
}; | ||
|
||
React.useEffect(() => { | ||
// Action in non-active tab | ||
window.addEventListener('storage', handler); | ||
|
||
return () => { | ||
window.removeEventListener('storage', handler); | ||
localStorage.removeItem(key); | ||
}; | ||
}); | ||
|
||
return { | ||
callback: React.useCallback(() => { | ||
localStorage.setItem(key, String(Number(localStorage.getItem(key) || '0') + 1)); | ||
return callback(); | ||
}, [key, callback]), | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './Stories'; | ||
export * from './hooks'; | ||
export * from './types'; |