-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(icon-button): Implement isRTL helper library, and refactor icon-…
…button to use it. PiperOrigin-RevId: 471352106
- Loading branch information
1 parent
ce24805
commit 5dd43fa
Showing
4 changed files
with
62 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,18 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* Returns `true` if the given element is in a right-to-left direction. | ||
* | ||
* @param el Element to determine direction from | ||
* @param shouldCheck Optional. If `false`, return `false` without checking | ||
* direction. Determining the direction of `el` is somewhat expensive, so | ||
* this parameter can be used as a conditional guard. Defaults to `true`. | ||
*/ | ||
export function isRtl(el: HTMLElement, shouldCheck = true) { | ||
return shouldCheck && | ||
getComputedStyle(el).getPropertyValue('direction').trim() === '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,34 @@ | ||
import 'jasmine'; | ||
|
||
import {isRtl} from './is-rtl.js'; | ||
|
||
function setDirection(node: HTMLElement, rtl: boolean) { | ||
node.dir = rtl ? 'rtl' : 'ltr'; | ||
} | ||
|
||
describe('isRtl', () => { | ||
let testDiv: HTMLElement; | ||
|
||
beforeEach(() => { | ||
// reset document direction | ||
setDirection(document.documentElement, false); | ||
testDiv = document.createElement('div'); | ||
document.body.appendChild(testDiv); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(testDiv); | ||
}); | ||
|
||
it('returns the direction of a given node', () => { | ||
expect(isRtl(testDiv)).toEqual(false); | ||
setDirection(testDiv, true); | ||
expect(isRtl(testDiv)).toEqual(true); | ||
}); | ||
|
||
it('does not check direction if `shouldCheck` is false', () => { | ||
expect(isRtl(testDiv, false)).toEqual(false); | ||
setDirection(testDiv, true); | ||
expect(isRtl(testDiv, false)).toEqual(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
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