forked from Workday/canvas-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): Add useIsRTL hook to common module (Workday#971)
- Loading branch information
1 parent
771a215
commit 75e909f
Showing
5 changed files
with
64 additions
and
3 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
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,20 @@ | ||
import * as React from 'react'; | ||
import {ThemeContext} from '@emotion/core'; | ||
import {EmotionCanvasTheme, ContentDirection, PartialEmotionCanvasTheme} from './types'; | ||
|
||
function useDefaultTheme<T, C>(theme: T | undefined, config: C, fn: (config: C) => T) { | ||
return theme || fn(config); | ||
} | ||
|
||
/** | ||
* This is a small hook to support right-to-left logic. | ||
* It returns a boolean | ||
* @example | ||
* const isRTL = useIsRTL(); | ||
*/ | ||
|
||
export const useIsRTL = (partialTheme?: PartialEmotionCanvasTheme) => { | ||
const theme = useDefaultTheme(partialTheme, ThemeContext, React.useContext) as EmotionCanvasTheme; | ||
|
||
return (theme && theme.canvas && theme.canvas.direction) === ContentDirection.RTL; | ||
}; |
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,18 @@ | ||
import {renderHook} from '@testing-library/react-hooks'; | ||
import {ContentDirection, useIsRTL} from '../lib/theming'; | ||
|
||
describe('useIsRTL hook', () => { | ||
it("should return true when the theme's content direction is RTL", () => { | ||
const partialTheme = {canvas: {direction: ContentDirection.RTL}}; | ||
const {result} = renderHook(() => useIsRTL(partialTheme)); | ||
|
||
expect(result.current).toBe(true); | ||
}); | ||
|
||
it("should return false when the theme's content direction is LTR", () => { | ||
const partialTheme = {canvas: {direction: ContentDirection.LTR}}; | ||
const {result} = renderHook(() => useIsRTL(partialTheme)); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
}); |
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