-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a372ee8
commit 69d271b
Showing
6 changed files
with
88 additions
and
3 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,30 @@ | ||
import { Meta } from '@storybook/blocks'; | ||
|
||
<Meta title="Hooks / useScreenOrientation" /> | ||
|
||
# useScreenOrientation | ||
|
||
`useScreenOrientation` is a custom React hook for handling screen orientation changes. It listens | ||
for window resizing events, determining the screen's current orientation (portrait or landscape). | ||
This hook is useful for building UIs that adapt based on device orientation, enhancing user | ||
experience across various devices. | ||
|
||
## Reference | ||
|
||
```ts | ||
function useScreenOrientation(): { isLandscape: boolean; isPortait: boolean }; | ||
``` | ||
|
||
### Returns | ||
|
||
- An object with 2 values: isLandscape and isPortrait, both booleans where the later is the opposite | ||
of the first. | ||
|
||
## Usage | ||
|
||
```tsx | ||
function DemoComponent() { | ||
const { isLandscape } = useScreenOrientation(); | ||
return <div>Orientation: {isLandscape ? 'Landscape' : 'Portrait'}</div>; | ||
} | ||
``` |
35 changes: 35 additions & 0 deletions
35
src/hooks/useScreenOrientation/useScreenOrientation.stories.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,35 @@ | ||
/* eslint-disable react/jsx-no-literals */ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import type { ReactElement } from 'react'; | ||
import { useScreenOrientation } from './useScreenOrientation.js'; | ||
|
||
const meta = { | ||
title: 'Hooks / useScreenOrientation', | ||
} satisfies Meta; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
function DemoComponent(): ReactElement { | ||
const { isLandscape } = useScreenOrientation(); | ||
|
||
return ( | ||
<div> | ||
<div className="alert alert-primary"> | ||
<h4 className="alert-heading">Instructions</h4> | ||
<p className="mb-0">Resize the viewport to see the useScreenOrientation hook in action.</p> | ||
</div> | ||
|
||
<div> | ||
<div>Orientation: {isLandscape ? 'Landscape' : 'Portrait'}</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export const Demo: Story = { | ||
render() { | ||
return <DemoComponent />; | ||
}, | ||
}; |
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 @@ | ||
import { useMediaQuery } from '../useMediaQuery/useMediaQuery.js'; | ||
|
||
/** | ||
* This hook observes the orientation of the of the screen. | ||
* | ||
* @returns An object with 2 values: isLandscape and isPortrait, both booleans where the later is the opposite of the first. | ||
*/ | ||
|
||
export function useScreenOrientation(): { | ||
isLandscape: boolean | undefined; | ||
isPortrait: boolean | undefined; | ||
} { | ||
const isLandscape = useMediaQuery('(orientation: landscape)'); | ||
|
||
return { | ||
isLandscape, | ||
isPortrait: !isLandscape, | ||
}; | ||
} |
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