-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): usePreferredColocScheme 신규 훅 추가 (#322)
* feat(react): usePreferredColocScheme 신규 훅 추가 * docs(react): usePreferredColorScheme 문서 수정 * docs: 문서 수정 * docs: 문서 수정
- Loading branch information
Showing
8 changed files
with
120 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@modern-kit/react': minor | ||
--- | ||
|
||
feat(react): usePreferredColocScheme 신규 훅 추가 - @ssi02014 |
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,66 @@ | ||
import { usePreferredColorScheme } from '@modern-kit/react'; | ||
|
||
# usePreferredColorScheme | ||
|
||
사용자의 색상 스킴 선호도(**[prefers-coloc-scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme)**) 에 따라 `dark`, `light`, 또는 `no-preference`를 반환합니다. | ||
|
||
- ligth: 사용자가 시스템에 `ligth` 테마를 사용하는 것을 선호하거나 선호하는 테마를 알리지 않았을 때 반환합니다. | ||
- dark: 사용자가 시스템에 `dark` 테마를 사용하는 것을 선호한다고 알렸을 때 반환합니다. | ||
|
||
사용자는 `운영체제 설정`이나 `사용자 에이전트 설정`에서 선호하는 테마를 지정 할 수 있습니다. | ||
|
||
<br /> | ||
|
||
## Code | ||
[🔗 실제 구현 코드 확인](https://github.com/modern-agile-team/modern-kit/blob/main/packages/react/src/hooks/usePreferredColorScheme/index.ts) | ||
|
||
## Interface | ||
|
||
```ts title="typescript" | ||
const usePreferredColorScheme: () => "dark" | "light" | ||
``` | ||
## Usage | ||
```tsx title="typescript" | ||
import { usePreferredColorScheme } from '@modern-kit/react'; | ||
|
||
const Example = () => { | ||
const colorScheme = usePreferredColorScheme(); | ||
|
||
return ( | ||
<div> | ||
<p>OS 시스템 설정에서 테마를 변경해보세요.</p> | ||
<p>ColorScheme: <b>{colorScheme}</b></p> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Example | ||
|
||
export const Example = () => { | ||
const colorScheme = usePreferredColorScheme(); | ||
|
||
return ( | ||
<div> | ||
<p>OS 시스템 설정에서 테마를 변경해보세요.</p> | ||
<p>ColorScheme: <b>{colorScheme}</b></p> | ||
</div> | ||
); | ||
}; | ||
|
||
<Example /> | ||
|
||
<br /> | ||
|
||
## Image | ||
### 1. Light Theme | ||
![스크린샷 2024-07-10 오후 8 09 33](https://github.com/modern-agile-team/modern-kit/assets/64779472/79c6298b-72f1-4f50-b644-93762d4d59e1) | ||
|
||
<br /> | ||
|
||
### 2. Dark Theme | ||
![스크린샷 2024-07-10 오후 8 09 39](https://github.com/modern-agile-team/modern-kit/assets/64779472/ab735906-9f8c-4e8e-9688-f6cde1786ec9) | ||
|
||
## Note | ||
- [Prefers Color Scheme - MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme) |
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
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,7 @@ | ||
import { useMediaQuery } from '../useMediaQuery'; | ||
|
||
export const usePreferredColorScheme = () => { | ||
const isDark = useMediaQuery('(prefers-color-scheme: dark)'); | ||
|
||
return isDark ? 'dark' : 'light'; | ||
}; |
28 changes: 28 additions & 0 deletions
28
packages/react/src/hooks/usePreferredColorScheme/usePreferredColorScheme.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,28 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { usePreferredColorScheme } from '.'; | ||
import { useMediaQuery } from '../useMediaQuery'; | ||
import { Mock } from 'vitest'; | ||
|
||
vi.mock('../useMediaQuery', () => ({ | ||
useMediaQuery: vi.fn(), | ||
})); | ||
|
||
describe('usePreferredColorScheme', () => { | ||
it('should return "dark" when prefers-color-scheme is dark', () => { | ||
(useMediaQuery as Mock).mockImplementation((query: string) => { | ||
return query === '(prefers-color-scheme: dark)'; | ||
}); | ||
|
||
const { result } = renderHook(() => usePreferredColorScheme()); | ||
expect(result.current).toBe('dark'); | ||
}); | ||
|
||
it('should return "light" when prefers-color-scheme is light', () => { | ||
(useMediaQuery as Mock).mockImplementation((query: string) => { | ||
return query === '(prefers-color-scheme: light)'; | ||
}); | ||
|
||
const { result } = renderHook(() => usePreferredColorScheme()); | ||
expect(result.current).toBe('light'); | ||
}); | ||
}); |