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: usePrefersReducedData hook #340

Merged
merged 1 commit into from
Dec 3, 2020
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
7 changes: 7 additions & 0 deletions packages/fuselage-hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ yarn add @rocket.chat/fuselage-hooks
- [Parameters](#parameters-10)
- [usePosition](#useposition)
- [Parameters](#parameters-11)
- [usePrefersReducedData](#useprefersreduceddata)
- [usePrefersReducedMotion](#useprefersreducedmotion)
- [useResizeObserver](#useresizeobserver)
- [Parameters](#parameters-12)
Expand Down Expand Up @@ -227,6 +228,12 @@ Hook to deal and position an element using an anchor

Returns **PositionResult** The style containing top and left position

### usePrefersReducedData

Hook to get the prefers-reduce-data value.

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `true` if the prefers-reduce-data is set reduce in the media queries that matches

### usePrefersReducedMotion

Hook to get the prefers-reduce-motion value.
Expand Down
1 change: 1 addition & 0 deletions packages/fuselage-hooks/docs/fuselage-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
| [useMergedRefs](./fuselage-hooks.usemergedrefs.md) | Hook to merge refs and callbacks refs into a single callback ref. Useful when your component need a internal ref while receiving a forwared ref. |
| [useMutableCallback](./fuselage-hooks.usemutablecallback.md) | Hook to create a stable callback from a mutable one. |
| [usePosition](./fuselage-hooks.useposition.md) | Hook to deal and position an element using an anchor |
| [usePrefersReducedData](./fuselage-hooks.useprefersreduceddata.md) | Hook to get the prefers-reduce-data value. |
| [usePrefersReducedMotion](./fuselage-hooks.useprefersreducedmotion.md) | Hook to get the prefers-reduce-motion value. |
| [useResizeObserver](./fuselage-hooks.useresizeobserver.md) | Hook to track dimension changes in a DOM element using the ResizeObserver API. |
| [useSafely](./fuselage-hooks.usesafely.md) | Hook that wraps pairs of state and dispatcher to provide a new dispatcher which can be safe and asynchronically called even after the component unmounted. |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@rocket.chat/fuselage-hooks](./fuselage-hooks.md) &gt; [usePrefersReducedData](./fuselage-hooks.useprefersreduceddata.md)

## usePrefersReducedData variable

Hook to get the prefers-reduce-data value.

<b>Signature:</b>

```typescript
usePrefersReducedData: () => boolean
```
1 change: 1 addition & 0 deletions packages/fuselage-hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './useMediaQuery';
export * from './useMergedRefs';
export * from './useMutableCallback';
export * from './usePosition';
export * from './usePrefersReducedData';
export * from './usePrefersReducedMotion';
export * from './useResizeObserver';
export * from './useSafely';
Expand Down
34 changes: 34 additions & 0 deletions packages/fuselage-hooks/src/usePrefersReducedData.server.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @jest-environment node
*/

import { FunctionComponent, createElement, StrictMode } from 'react';
import { renderToString } from 'react-dom/server';

import { usePrefersReducedData } from '.';

describe('usePrefersReducedData hook on server', () => {
it('should return false without matchMedia mocked', () => {
let matches: boolean;
const TestComponent: FunctionComponent = () => {
matches = usePrefersReducedData();
return null;
};

renderToString(createElement(StrictMode, {}, createElement(TestComponent)));

expect(matches).toBe(false);
});

it('should return false with matchMedia mocked', () => {
let matches: boolean;
const TestComponent: FunctionComponent = () => {
matches = usePrefersReducedData();
return null;
};

renderToString(createElement(StrictMode, {}, createElement(TestComponent)));

expect(matches).toBe(false);
});
});
59 changes: 59 additions & 0 deletions packages/fuselage-hooks/src/usePrefersReducedData.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { createElement, FunctionComponent, StrictMode } from 'react';
import { render } from 'react-dom';
import { act } from 'react-dom/test-utils';

import { usePrefersReducedData } from '.';
import matchMediaMock from './__mocks__/matchMedia';

describe('usePrefersReducedData hook', () => {
let container: HTMLDivElement;

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
container.remove();
container = null;
});

it('should return false on the initial call', () => {
let matches: boolean;

const TestComponent: FunctionComponent = () => {
matches = usePrefersReducedData();
return null;
};

act(() => {
render(
createElement(StrictMode, {}, createElement(TestComponent)),
container
);
});

expect(matches).toBe(false);
});

it('should returns true with matchMedia mocked', () => {
window.matchMedia = jest.fn((media) =>
matchMediaMock(media, '(prefers-reduced-data: reduce)')
);

let matches: boolean;
const TestComponent: FunctionComponent = () => {
matches = usePrefersReducedData();
return null;
};

act(() => {
render(
createElement(StrictMode, {}, createElement(TestComponent)),
document.createElement('div')
);
});

expect(matches).toBe(true);
});
});
11 changes: 11 additions & 0 deletions packages/fuselage-hooks/src/usePrefersReducedData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useMediaQuery } from '.';

/**
* Hook to get the prefers-reduce-data value.
*
* @returns `true` if the prefers-reduce-data is set reduce in the media queries that matches
* @public
*/

export const usePrefersReducedData = (): boolean =>
useMediaQuery('(prefers-reduced-data: reduce)');