generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 100
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): Add helper components #154
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useWindowDimensions } from './windowSize'; |
56 changes: 56 additions & 0 deletions
56
packages/components/src/custom/Helpers/Dimension/windowSize.tsx
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,56 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* Returns the width and height of the window. | ||
* | ||
* @returns {WindowDimensions} { width, height } | ||
*/ | ||
function getWindowDimensions(): WindowDimensions { | ||
const { innerWidth: width, innerHeight: height } = window; | ||
return { | ||
width, | ||
height | ||
}; | ||
} | ||
|
||
/** | ||
* Custom hook for getting window dimensions. | ||
* | ||
* @returns {WindowDimensions} { width, height } | ||
*/ | ||
export function useWindowDimensions(): WindowDimensions { | ||
const [windowDimensions, setWindowDimensions] = useState(getWindowDimensions()); | ||
|
||
useEffect(() => { | ||
let resizeTimeout: number; | ||
|
||
function handleResize() { | ||
if (resizeTimeout) { | ||
clearTimeout(resizeTimeout); | ||
} | ||
resizeTimeout = window.setTimeout(() => { | ||
setWindowDimensions(getWindowDimensions()); | ||
}, 500); | ||
} | ||
|
||
window.addEventListener('resize', handleResize); | ||
|
||
// Clean up the event listener on unmount | ||
return () => { | ||
window.removeEventListener('resize', handleResize); | ||
if (resizeTimeout) { | ||
clearTimeout(resizeTimeout); | ||
} | ||
}; | ||
}, []); | ||
|
||
return windowDimensions; | ||
} | ||
|
||
/** | ||
* Represents the width and height of the window. | ||
*/ | ||
interface WindowDimensions { | ||
width: number; | ||
height: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Helper Components | ||
|
||
This directory contains a collection of utility and helper components that you can use across your project. These components are designed to simplify common tasks, enhance reusability, and improve code organization. | ||
|
||
## Table of Contents | ||
|
||
- [Available Helper Components](#available-helper-components) | ||
- [How to Use](#how-to-use) | ||
- [Examples](#examples) | ||
|
||
## Available Helper Components | ||
|
||
1. **Window Dimensions Hook**: A custom React hook for tracking changes in window dimensions. | ||
|
||
- **File**: `Dimension` | ||
- **Usage**: Provides the `useWindowDimensions` hook, which allows you to get the current window dimensions and react to changes in window size. | ||
- **Returns**: An object containing the current window dimensions and a boolean value indicating whether the window is currently in landscape mode. | ||
|
||
## How to Use | ||
|
||
To use these helper components in your project, follow these steps: | ||
|
||
1. Navigate to the specific helper component directory (e.g., `dimension.ts`) to find details about its usage. | ||
|
||
2. Import the required helper component into your code: | ||
|
||
```javascript | ||
import { useWindowDimensions } from './helpers'; | ||
``` |
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.
Thanks for providing a README.md on this.
However I am a bit confused, is this hook expected to be used in Meshery and Meshery Cloud? Or just for internal use in the
components
package.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.
This hook basically will focus initially on internal use for our custom components but we can also export this
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.
Okay, I made a note to add this to the
hooks
library once I have this merged in. For now, looks good to me.