generated from gravity-ui/package-example
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(Stories): add hook for sync tabs #138
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -3,8 +3,8 @@ import React from 'react'; | |
import {Button} from '@gravity-ui/uikit'; | ||
import type {Meta, StoryFn} from '@storybook/react'; | ||
|
||
import {Stories} from '../Stories'; | ||
import type {StoriesProps} from '../Stories'; | ||
import {Stories} from '../Stories'; | ||
import type {StoriesItem} from '../types'; | ||
|
||
export default { | ||
|
@@ -62,6 +62,7 @@ const DefaultTemplate: StoryFn<StoriesProps> = (props: StoriesProps) => { | |
<Stories | ||
{...props} | ||
open={visible} | ||
syncInTabs={props.syncInTabs} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Above, with the restructuring of props, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, right. Fixed. |
||
onClose={() => { | ||
setVisible(false); | ||
}} | ||
|
@@ -94,3 +95,10 @@ WithCustomAction.args = { | |
children: 'View examples', | ||
}, | ||
}; | ||
|
||
export const WithSyncInTabs = DefaultTemplate.bind({}); | ||
WithSyncInTabs.args = { | ||
open: true, | ||
syncInTabs: true, | ||
items: [items[0]], | ||
}; |
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'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to check
syncInTabs
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tnx, fixed.