Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
feat: ✨ add useGetScreenWidth hook (#112)
Browse files Browse the repository at this point in the history
* feature: ✨ add useGetScreenWidth hook

* fix: 🐛 get window width in server-side
  • Loading branch information
velenyx committed Sep 25, 2023
1 parent db4e68c commit d04a207
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
'use client';

import { Typography } from '@/shared/ui';
import { useGetScreenWidth } from '@/shared/lib';

export default function Home() {
const width = useGetScreenWidth();

return (
<>
<Typography size='heading_l' variant='h6'>
We are working hard to deliver teameights on NextJS/TS soon!
</Typography>

<div> The screen width is: {width} </div>

<a href='/login' style={{ color: 'green' }}>
Get to login
</a>
Expand Down
1 change: 1 addition & 0 deletions client/src/shared/lib/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { useClickOutside } from './use-click-outside/use-click-outside';
export { useGetScreenWidth } from './use-get-screen-width/use-get-screen-width';
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use client';

import { useEffect, useState } from 'react';

/**
* Custom hook to get the current screen width.
*
* This hook returns the current screen width, and will re-render the component
* using this hook whenever the screen is resized.
*
* @returns {number} The current screen width in pixels.
*
* @example
* import { useGetScreenWidth } from '@/shared/lib';
*
* export const ResponsiveComponent = () => {
* const screenWidth = useGetScreenWidth();
*
* return (
* <div>
* The screen width is: {screenWidth}
* </div>
* );
* };
*/
export const useGetScreenWidth = (): number => {
const [screenWidth, setScreenWidth] = useState<number>(0);

useEffect(() => {
// Set the screen width on the client-side after the component has mounted
setScreenWidth(window.innerWidth);

const handleResize = () => {
setScreenWidth(window.innerWidth);
};

// Event listener for window resize
window.addEventListener('resize', handleResize);

// Clean up the event listener on component unmount
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);

return screenWidth;
};

2 comments on commit d04a207

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for teameights ready!

✅ Preview
https://teameights-9rqo88xvn-exortme1ster.vercel.app

Built with commit d04a207.
This pull request is being automatically deployed with vercel-action

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for teameights-storybook ready!

✅ Preview
https://teameights-storybook-oy861k5d7-exortme1ster.vercel.app

Built with commit d04a207.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.