-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kerosene-ui: Add measureCSSProperty function (#48)
* kerosene-ui: Add measureCSSProperty function * kerosene-ui: Bump version to 0.0.12
- Loading branch information
1 parent
a17adca
commit 441e968
Showing
6 changed files
with
99 additions
and
18 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
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,54 @@ | ||
import createSandbox from "jest-sandbox"; | ||
import { when } from "jest-when"; | ||
import measureCSSProperty from "./measureCSSProperty"; | ||
import { JestMock } from "../../../kerosene-test/src"; | ||
|
||
describe("measureCSSProperty", () => { | ||
let sandbox: JestSandbox; | ||
let createElement: JestMock<Document["createElement"]>; | ||
let getComputedStyle: JestMock<Window["getComputedStyle"]>; | ||
beforeEach(() => { | ||
sandbox = createSandbox(); | ||
createElement = jest.fn(); | ||
document.createElement = createElement; | ||
getComputedStyle = jest.fn(); | ||
window.getComputedStyle = getComputedStyle; | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it("should inject a noscript tag and return the computed style", () => { | ||
const el = { | ||
appendChild: jest.fn(), | ||
removeChild: jest.fn(), | ||
}; | ||
|
||
const noscript = { tagName: "NOSCRIPT", style: {} } as HTMLElement; | ||
when(createElement) | ||
.calledWith("noscript") | ||
.mockReturnValue(noscript); | ||
|
||
when(getComputedStyle) | ||
.calledWith({ | ||
...noscript, | ||
style: { ...noscript.style, paddingTop: "var(--header-height, 0px)" }, | ||
} as HTMLElement) | ||
.mockReturnValue({ paddingTop: "48px" } as CSSStyleDeclaration); | ||
|
||
expect( | ||
measureCSSProperty( | ||
(el as Partial<Element>) as Element, | ||
"paddingTop", | ||
"var(--header-height, 0px)", | ||
), | ||
).toBe("48px"); | ||
expect(el.appendChild).toBeCalledWith(noscript); | ||
expect(noscript.style).toHaveProperty( | ||
["paddingTop"], | ||
"var(--header-height, 0px)", | ||
); | ||
expect(el.removeChild).toBeCalledWith(noscript); | ||
}); | ||
}); |
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,38 @@ | ||
/** | ||
* Measures a CSS `property` at the provided `element` by injecting a `<noscript />` element and setting the style | ||
* `property` to `value` and measuring with `window.getComputedStyle`. | ||
* | ||
* Useful for measuring CSS Custom Properties that contain calc expressions, e.g. | ||
* | ||
* ```css | ||
* :root { | ||
* --header-height: 48px; | ||
* } | ||
* | ||
* @media only screen and (min-width: 768px) { | ||
* :root { | ||
* --header-height: 72px; | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* ```typescript | ||
* const headerHeight = measureCSSProperty(document.body, "paddingTop", "var(--header-height, 0px)"); | ||
* ``` | ||
* | ||
* @param element | ||
* @param property | ||
* @param value | ||
*/ | ||
export default function measureCSSProperty<P extends keyof CSSStyleDeclaration>( | ||
element: Element, | ||
property: P, | ||
value: CSSStyleDeclaration[P], | ||
): CSSStyleDeclaration[P] { | ||
const noscript = document.createElement("noscript"); | ||
noscript.style[property] = value; | ||
element.appendChild(noscript); | ||
const computed = window.getComputedStyle(noscript)[property]; | ||
element.removeChild(noscript); | ||
return computed; | ||
} |
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