-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: usePrefersColorScheme hook (#341)
Co-authored-by: Guilherme Gazzo <guilherme@gazzo.xyz>
- Loading branch information
Showing
7 changed files
with
280 additions
and
6 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
13 changes: 13 additions & 0 deletions
13
packages/fuselage-hooks/docs/fuselage-hooks.usepreferscolorscheme.md
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,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@rocket.chat/fuselage-hooks](./fuselage-hooks.md) > [usePrefersColorScheme](./fuselage-hooks.usepreferscolorscheme.md) | ||
|
||
## usePrefersColorScheme variable | ||
|
||
Hook to get the prefers-color-scheme value. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
usePrefersColorScheme: (scheme?: string) => boolean | ||
``` |
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
34 changes: 34 additions & 0 deletions
34
packages/fuselage-hooks/src/usePrefersColorScheme.server.spec.ts
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,34 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import { FunctionComponent, createElement, StrictMode } from 'react'; | ||
import { renderToString } from 'react-dom/server'; | ||
|
||
import { usePrefersColorScheme } from '.'; | ||
|
||
describe('usePrefersColorScheme hook on server', () => { | ||
it('should return false without matchMedia mocked', () => { | ||
let matches: boolean; | ||
const TestComponent: FunctionComponent = () => { | ||
matches = usePrefersColorScheme(); | ||
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 = usePrefersColorScheme(); | ||
return null; | ||
}; | ||
|
||
renderToString(createElement(StrictMode, {}, createElement(TestComponent))); | ||
|
||
expect(matches).toBe(false); | ||
}); | ||
}); |
101 changes: 101 additions & 0 deletions
101
packages/fuselage-hooks/src/usePrefersColorScheme.spec.ts
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,101 @@ | ||
import { createElement, FunctionComponent, StrictMode } from 'react'; | ||
import { render } from 'react-dom'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
import { usePrefersColorScheme } from '.'; | ||
import matchMediaMock from './__mocks__/matchMedia'; | ||
|
||
describe('usePrefersColorScheme 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 = usePrefersColorScheme(); | ||
return null; | ||
}; | ||
|
||
act(() => { | ||
render( | ||
createElement(StrictMode, {}, createElement(TestComponent)), | ||
container | ||
); | ||
}); | ||
|
||
expect(matches).toBe(false); | ||
}); | ||
|
||
it('should return true with matchMedia mocked and anything set', () => { | ||
window.matchMedia = jest.fn((media) => | ||
matchMediaMock(media, '(prefers-color-scheme: light)') | ||
); | ||
|
||
let matches: boolean; | ||
const TestComponent: FunctionComponent = () => { | ||
matches = usePrefersColorScheme(); | ||
return null; | ||
}; | ||
|
||
act(() => { | ||
render( | ||
createElement(StrictMode, {}, createElement(TestComponent)), | ||
document.createElement('div') | ||
); | ||
}); | ||
|
||
expect(matches).toBe(true); | ||
}); | ||
|
||
it('should return true with matchMedia mocked and light scheme set', () => { | ||
window.matchMedia = jest.fn((media) => | ||
matchMediaMock(media, '(prefers-color-scheme: light)') | ||
); | ||
|
||
let matches: boolean; | ||
const TestComponent: FunctionComponent = () => { | ||
matches = usePrefersColorScheme('light'); | ||
return null; | ||
}; | ||
|
||
act(() => { | ||
render( | ||
createElement(StrictMode, {}, createElement(TestComponent)), | ||
document.createElement('div') | ||
); | ||
}); | ||
|
||
expect(matches).toBe(true); | ||
}); | ||
|
||
it('should return true with matchMedia mocked and dark scheme set', () => { | ||
window.matchMedia = jest.fn((media) => | ||
matchMediaMock(media, '(prefers-color-scheme: dark)') | ||
); | ||
|
||
let matches: boolean; | ||
const TestComponent: FunctionComponent = () => { | ||
matches = usePrefersColorScheme('dark'); | ||
return null; | ||
}; | ||
|
||
act(() => { | ||
render( | ||
createElement(StrictMode, {}, createElement(TestComponent)), | ||
document.createElement('div') | ||
); | ||
}); | ||
|
||
expect(matches).toBe(true); | ||
}); | ||
}); |
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,11 @@ | ||
import { useMediaQuery } from '.'; | ||
|
||
/** | ||
* Hook to get the prefers-color-scheme value. | ||
* | ||
* @returns `true` if the prefers-color-scheme matches | ||
* @public | ||
*/ | ||
|
||
export const usePrefersColorScheme = (scheme?: string): boolean => | ||
useMediaQuery(`(prefers-color-scheme: ${!scheme ? 'light' : scheme})`); |