Skip to content
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 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/src/custom/Helpers/Dimension/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useWindowDimensions } from './windowSize';
56 changes: 56 additions & 0 deletions packages/components/src/custom/Helpers/Dimension/windowSize.tsx
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;
}
29 changes: 29 additions & 0 deletions packages/components/src/custom/Helpers/readme.md
Copy link
Contributor

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.

Copy link
Member Author

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

Copy link
Contributor

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.

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';
```