-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-components): release useViewport hook (#631)
- Loading branch information
Showing
4 changed files
with
79 additions
and
56 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,62 @@ | ||
# useViewport react hook | ||
The `useViewport` react hook is a function which can be used within react components to utilize and update a shared time frame within a group of | ||
IoT App Kit widgets. | ||
|
||
`useViewport` should be used within the context of a `<TimeSync />`. [Learn more about Time Sync](https://github.com/awslabs/iot-app-kit/blob/main/docs/TimeSync.md) | ||
|
||
NOTE: This documentation assumes you are familiar with react and react-hooks. If you need to brush up, learn more at https://reactjs.org/docs/hooks-intro.html | ||
|
||
# Example usage | ||
Below is an example of a simple component which displays the current viewport, and provides a button to set the viewport | ||
to the last 5 minutes: | ||
``` | ||
const BasicComponent = () => { | ||
const { viewport, setViewport } = useViewport(); | ||
return ( | ||
<div> | ||
{JSON.stringify(viewport)} | ||
<button onClick={() => setViewport({ duration: '5m' })}>Set viewport</button> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
This component can then be used as follows in junction with the `<TimeSync />` component: | ||
|
||
``` | ||
import { TimeSync } from '@iot-app-kit/react-components'; | ||
<TimeSync initialViewport={{ duration: '10m' }}> | ||
<BasicComponent /> | ||
</TimeSync> | ||
``` | ||
|
||
To demonstrate the synchronizing of viewports, we can utilize multiple of the newly created `<BasicComponent />`, which will all display the same viewport: | ||
|
||
``` | ||
import { TimeSync } from '@iot-app-kit/react-components'; | ||
<TimeSync initialViewport={{ duration: '10m' }}> | ||
<BasicComponent /> | ||
<BasicComponent /> | ||
<BasicComponent /> | ||
</TimeSync> | ||
``` | ||
|
||
### API Summary | ||
|
||
The `useViewport` takes no inputs, and returns an object containing the following fields: | ||
|
||
### `viewport` | ||
|
||
The current viewport for the viewport group subscribed to. Will be undefined if the hook is used outside of the context of a `<TimeSync />`. | ||
Will also be undefined if the associated viewport group has no associated viewport. | ||
|
||
Type: Object or undefined | ||
|
||
### `setViewport: (viewport) => void` | ||
|
||
A function which you pass a viewport to set the current viewport group to. When called, the viewport group will update and all consumers of the viewport group will immediately receive the updated viewport provided. | ||
|
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