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

First draft useScreenOrientation #325

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/hooks/useMediaQuery/useMediaQuery.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Hook that returns true when the media query matches.
## Reference

```ts
function useMediaQuery(mediaQueryOrVariableName: string, defaultValue = false): boolean;
function useMediaQuery(mediaQueryOrVariableName: string, defaultValue?: boolean): boolean;
```

## Usage
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useMediaQuery/useMediaQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export function getMediaQueryList(
*/
export function useMediaQuery(
mediaQueryOrVariableName: MediaQueryValues,
defaultValue = false,
): boolean {
defaultValue?: boolean,
): boolean | undefined {
Copy link
Member

Choose a reason for hiding this comment

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

in what scenario would you want undefined to be returned ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be helpful for SSR, and only on client side return a boolean 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Interesting. I wonder what you would do with this then... not render something at all?
You always get a hydration error then? And this value on the client should only be used after the first render?

const [mediaQueryList, setMediaQueryList] = useState<MediaQueryList | undefined>(() =>
getMediaQueryList(mediaQueryOrVariableName),
);
Expand Down
30 changes: 30 additions & 0 deletions src/hooks/useScreenOrientation/useScreenOrientation.mdx
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 src/hooks/useScreenOrientation/useScreenOrientation.stories.tsx
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 />;
},
};
19 changes: 19 additions & 0 deletions src/hooks/useScreenOrientation/useScreenOrientation.ts
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 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;
Comment on lines +10 to +11
Copy link
Member

Choose a reason for hiding this comment

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

These are mutually exclusive, so why not return 'landscape' | 'portrait'?

} {
const isLandscape = useMediaQuery('(orientation: landscape)');

return {
isLandscape,
isPortrait: !isLandscape,
};
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from './hooks/useRefs/utils/unwrapRefs/unwrapRefs.js';
export * from './hooks/useRefs/utils/unwrapRefs/unwrapRefs.types.js';
export * from './hooks/useRefs/utils/validateAndUnwrapRefs/validateAndUnwrapRefs.js';
export * from './hooks/useResizeObserver/useResizeObserver.js';
export * from './hooks/useScreenOrientation/useScreenOrientation.js';
export * from './hooks/useStaticValue/useStaticValue.js';
export * from './hooks/useToggle/useToggle.js';
export * from './lifecycle/components/CrossFlow/CrossFlow.js';
Expand Down
Loading