Unverified Commit 475ae342 authored by Douglas Fabris's avatar Douglas Fabris Committed by GitHub

feat: usePrefersReducedMotion hook (#339)

Co-authored-by: default avatarGuilherme Gazzo <guilherme@gazzo.xyz>
parent 3311ee9b
......@@ -66,6 +66,7 @@ yarn add @rocket.chat/fuselage-hooks
- [Parameters](#parameters-10)
- [usePosition](#useposition)
- [Parameters](#parameters-11)
- [usePrefersReducedMotion](#useprefersreducedmotion)
- [useResizeObserver](#useresizeobserver)
- [Parameters](#parameters-12)
- [useSafely](#usesafely)
......@@ -226,6 +227,12 @@ Hook to deal and position an element using an anchor
Returns **PositionResult** The style containing top and left position
### usePrefersReducedMotion
Hook to get the prefers-reduce-motion value.
Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `true` if the prefers-reduce-motion is set reduce in the media queries that matches
### useResizeObserver
Hook to track dimension changes in a DOM element using the ResizeObserver API.
......
......@@ -7,7 +7,7 @@
<b>Signature:</b>
```typescript
getPositionStyle: ({ placement, container, targetBoundaries, variantStore, target }: {
getPositionStyle: ({ placement, container, targetBoundaries, variantStore, target, }: {
placement: Placements;
target: DOMRect;
container: DOMRect;
......
......@@ -7,7 +7,7 @@
<b>Signature:</b>
```typescript
getTargetBoundaries: ({ referenceBox, target, margin }: {
getTargetBoundaries: ({ referenceBox, target, margin, }: {
referenceBox?: DOMRect;
target?: DOMRect;
margin?: number;
......
......@@ -7,7 +7,7 @@
<b>Signature:</b>
```typescript
getVariantBoundaries: ({ referenceBox, target }: {
getVariantBoundaries: ({ referenceBox, target, }: {
referenceBox?: DOMRect;
target?: DOMRect;
}) => VariantBoundaries | null
......
......@@ -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 |
| [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. |
| [useSessionStorage](./fuselage-hooks.usesessionstorage.md) | Hook to deal with sessionStorage |
......
......@@ -7,10 +7,13 @@
<b>Signature:</b>
```typescript
export declare function useDebouncedReducer<S, R extends ReducerWithoutAction<S>>(reducer: R, initialArg: S, init: undefined, delay: number): [ReducerStateWithoutAction<R>, DispatchWithoutAction & {
flush: () => void;
cancel: () => void;
}];
export declare function useDebouncedReducer<S, R extends ReducerWithoutAction<S>>(reducer: R, initialArg: S, init: undefined, delay: number): [
ReducerStateWithoutAction<R>,
DispatchWithoutAction & {
flush: () => void;
cancel: () => void;
}
];
```
## Parameters
......@@ -24,5 +27,5 @@ export declare function useDebouncedReducer<S, R extends ReducerWithoutAction<S>
<b>Returns:</b>
\[ReducerStateWithoutAction&lt;R&gt;, DispatchWithoutAction &amp; { flush: () =&gt; void; cancel: () =&gt; void; }\]
\[ ReducerStateWithoutAction&lt;R&gt;, DispatchWithoutAction &amp; { flush: () =&gt; void; cancel: () =&gt; void; } \]
......@@ -7,10 +7,13 @@
<b>Signature:</b>
```typescript
export declare function useDebouncedReducer<S, R extends ReducerWithoutAction<S>, I>(reducer: R, initialArg: I, init: (arg: I) => ReducerStateWithoutAction<R>, delay: number): [ReducerStateWithoutAction<R>, DispatchWithoutAction & {
flush: () => void;
cancel: () => void;
}];
export declare function useDebouncedReducer<S, R extends ReducerWithoutAction<S>, I>(reducer: R, initialArg: I, init: (arg: I) => ReducerStateWithoutAction<R>, delay: number): [
ReducerStateWithoutAction<R>,
DispatchWithoutAction & {
flush: () => void;
cancel: () => void;
}
];
```
## Parameters
......@@ -24,5 +27,5 @@ export declare function useDebouncedReducer<S, R extends ReducerWithoutAction<S>
<b>Returns:</b>
\[ReducerStateWithoutAction&lt;R&gt;, DispatchWithoutAction &amp; { flush: () =&gt; void; cancel: () =&gt; void; }\]
\[ ReducerStateWithoutAction&lt;R&gt;, DispatchWithoutAction &amp; { flush: () =&gt; void; cancel: () =&gt; void; } \]
......@@ -7,10 +7,13 @@
<b>Signature:</b>
```typescript
export declare function useDebouncedReducer<S, A, R extends Reducer<S, A>>(reducer: R, initialArg: S, init: undefined, delay: number): [ReducerState<R>, Dispatch<A> & {
flush: () => void;
cancel: () => void;
}];
export declare function useDebouncedReducer<S, A, R extends Reducer<S, A>>(reducer: R, initialArg: S, init: undefined, delay: number): [
ReducerState<R>,
Dispatch<A> & {
flush: () => void;
cancel: () => void;
}
];
```
## Parameters
......@@ -24,5 +27,5 @@ export declare function useDebouncedReducer<S, A, R extends Reducer<S, A>>(reduc
<b>Returns:</b>
\[ReducerState&lt;R&gt;, Dispatch&lt;A&gt; &amp; { flush: () =&gt; void; cancel: () =&gt; void; }\]
\[ ReducerState&lt;R&gt;, Dispatch&lt;A&gt; &amp; { flush: () =&gt; void; cancel: () =&gt; void; } \]
......@@ -9,10 +9,13 @@ Hook to create a state with a debounced setter function.
<b>Signature:</b>
```typescript
export declare function useDebouncedState<S>(initialValue: S | (() => S), delay: number): [S, Dispatch<SetStateAction<S>> & {
flush: () => void;
cancel: () => void;
}];
export declare function useDebouncedState<S>(initialValue: S | (() => S), delay: number): [
S,
Dispatch<SetStateAction<S>> & {
flush: () => void;
cancel: () => void;
}
];
```
## Parameters
......@@ -24,7 +27,7 @@ export declare function useDebouncedState<S>(initialValue: S | (() => S), delay:
<b>Returns:</b>
\[S, Dispatch&lt;SetStateAction&lt;S&gt;&gt; &amp; { flush: () =&gt; void; cancel: () =&gt; void; }\]
\[ S, Dispatch&lt;SetStateAction&lt;S&gt;&gt; &amp; { flush: () =&gt; void; cancel: () =&gt; void; } \]
a state and debounced setter function
......@@ -9,10 +9,13 @@ Hook to debounce the state dispatcher function returned by hooks like `useState(
<b>Signature:</b>
```typescript
export declare function useDebouncedUpdates<S>([state, dispatch]: [S, DispatchWithoutAction], delay: number): [S, DispatchWithoutAction & {
flush: () => void;
cancel: () => void;
}];
export declare function useDebouncedUpdates<S>([state, dispatch]: [S, DispatchWithoutAction], delay: number): [
S,
DispatchWithoutAction & {
flush: () => void;
cancel: () => void;
}
];
```
## Parameters
......@@ -24,7 +27,7 @@ export declare function useDebouncedUpdates<S>([state, dispatch]: [S, DispatchWi
<b>Returns:</b>
\[S, DispatchWithoutAction &amp; { flush: () =&gt; void; cancel: () =&gt; void; }\]
\[ S, DispatchWithoutAction &amp; { flush: () =&gt; void; cancel: () =&gt; void; } \]
a state value and debounced dispatcher pair
......@@ -7,10 +7,13 @@
<b>Signature:</b>
```typescript
export declare function useDebouncedUpdates<S, A>([state, dispatch]: [S, Dispatch<A>], delay: number): [S, Dispatch<A> & {
flush: () => void;
cancel: () => void;
}];
export declare function useDebouncedUpdates<S, A>([state, dispatch]: [S, Dispatch<A>], delay: number): [
S,
Dispatch<A> & {
flush: () => void;
cancel: () => void;
}
];
```
## Parameters
......@@ -22,5 +25,5 @@ export declare function useDebouncedUpdates<S, A>([state, dispatch]: [S, Dispatc
<b>Returns:</b>
\[S, Dispatch&lt;A&gt; &amp; { flush: () =&gt; void; cancel: () =&gt; void; }\]
\[ S, Dispatch&lt;A&gt; &amp; { flush: () =&gt; void; cancel: () =&gt; void; } \]
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [@rocket.chat/fuselage-hooks](./fuselage-hooks.md) &gt; [usePrefersReducedMotion](./fuselage-hooks.useprefersreducedmotion.md)
## usePrefersReducedMotion variable
Hook to get the prefers-reduce-motion value.
<b>Signature:</b>
```typescript
usePrefersReducedMotion: () => boolean
```
......@@ -9,7 +9,7 @@ Hook to track dimension changes in a DOM element using the ResizeObserver API.
<b>Signature:</b>
```typescript
useResizeObserver: ({ debounceDelay }?: UseResizeObserverOptions) => {
useResizeObserver: ({ debounceDelay, }?: UseResizeObserverOptions) => {
ref: RefObject<Element>;
contentBoxSize: ResizeObserverSize;
borderBoxSize: ResizeObserverSize;
......
......@@ -9,5 +9,5 @@ Hook that wraps pairs of state and dispatcher to provide a new dispatcher which
<b>Signature:</b>
```typescript
useSafely: <S, A, D extends DispatchWithoutAction | Dispatch<A>>([state, dispatcher]: [S, DispatchWithoutAction | Dispatch<A>]) => [S, D]
useSafely: <S, A, D extends DispatchWithoutAction | Dispatch<A>>([state, dispatcher,]: [S, DispatchWithoutAction | Dispatch<A>]) => [S, D]
```
......@@ -9,7 +9,9 @@ class MediaQueryListMock implements MediaQueryList {
changeEventListeners: Set<EventListener>;
constructor(media: string) {
_matchString: string;
constructor(media: string, matchString?: string) {
this._media = media;
this._onchange = null;
this.changeEventListeners = new Set([
......@@ -17,6 +19,7 @@ class MediaQueryListMock implements MediaQueryList {
this._onchange && this._onchange.call(this, ev);
},
]);
this._matchString = matchString;
}
get matches(): boolean {
......@@ -32,7 +35,7 @@ class MediaQueryListMock implements MediaQueryList {
);
}
return false;
return this._matchString && this._matchString === this._media;
}
get media(): string {
......@@ -88,7 +91,7 @@ class MediaQueryListMock implements MediaQueryList {
}
}
const matchMediaMock = (media: string): MediaQueryList =>
new MediaQueryListMock(media);
const matchMediaMock = (media: string, matchString?: string): MediaQueryList =>
new MediaQueryListMock(media, matchString);
export default matchMediaMock;
......@@ -12,6 +12,7 @@ export * from './useMediaQuery';
export * from './useMergedRefs';
export * from './useMutableCallback';
export * from './usePosition';
export * from './usePrefersReducedMotion';
export * from './useResizeObserver';
export * from './useSafely';
export * from './useStableArray';
......
/**
* @jest-environment node
*/
import { FunctionComponent, createElement, StrictMode } from 'react';
import { renderToString } from 'react-dom/server';
import { usePrefersReducedMotion } from '.';
describe('usePrefersReducedMotion hook on server', () => {
it('should return false without matchMedia mocked', () => {
let matches: boolean;
const TestComponent: FunctionComponent = () => {
matches = usePrefersReducedMotion();
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 = usePrefersReducedMotion();
return null;
};
renderToString(createElement(StrictMode, {}, createElement(TestComponent)));
expect(matches).toBe(false);
});
});
import { createElement, FunctionComponent, StrictMode } from 'react';
import { render } from 'react-dom';
import { act } from 'react-dom/test-utils';
import { usePrefersReducedMotion } from '.';
import matchMediaMock from './__mocks__/matchMedia';
describe('usePrefersReducedMotion hook', () => {
let container: HTMLDivElement;
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
afterEach(() => {
container.remove();
container = null;
});
it('returns false on the initial call', () => {
let matches: boolean;
const TestComponent: FunctionComponent = () => {
matches = usePrefersReducedMotion();
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-motion: reduce)')
);
let matches: boolean;
const TestComponent: FunctionComponent = () => {
matches = usePrefersReducedMotion();
return null;
};
act(() => {
render(
createElement(StrictMode, {}, createElement(TestComponent)),
document.createElement('div')
);
});
expect(matches).toBe(true);
});
});
import { useMediaQuery } from '.';
/**
* Hook to get the prefers-reduce-motion value.
*
* @returns `true` if the prefers-reduce-motion is set reduce in the media queries that matches
* @public
*/
export const usePrefersReducedMotion = (): boolean =>
useMediaQuery('(prefers-reduced-motion: reduce)');
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment