-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add responsive module (#7716)
**Related Issue:** #6670 ## Summary Adds support module for component responsiveness. ### Notes * Spec test is skipped until the following JSDOM issues are addressed: * jsdom/jsdom#3563 * jsdom/jsdom#2160. * Moves existing custom matchers from the color-picker E2E test to the test utils module for reuse.
- Loading branch information
Showing
4 changed files
with
112 additions
and
26 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,28 @@ | ||
import { getBreakpoints } from "./responsive"; | ||
import { toBeInteger } from "../tests/utils"; | ||
|
||
describe("getBreakpoints()", () => { | ||
// skipped due to JSDOM bugs with inheritance/getComputedStyle | ||
// see https://github.com/jsdom/jsdom/issues/2160 and https://github.com/jsdom/jsdom/issues/3563 | ||
it.skip("returns breakpoints lookup object", async () => { | ||
document.head.innerHTML = ` | ||
<style> | ||
:root { | ||
--calcite-app-breakpoint-width-lg: 1000px; | ||
--calcite-app-breakpoint-width-md: 100px; | ||
--calcite-app-breakpoint-width-sm: 10px; | ||
--calcite-app-breakpoint-width-xs: 1px; | ||
} | ||
</style> | ||
`; | ||
|
||
expect(await getBreakpoints()).toMatchObject({ | ||
width: { | ||
large: toBeInteger(), | ||
medium: toBeInteger(), | ||
small: toBeInteger(), | ||
xsmall: toBeInteger(), | ||
}, | ||
}); | ||
}); | ||
}); |
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,42 @@ | ||
interface Breakpoints { | ||
width: { | ||
large: number; | ||
medium: number; | ||
small: number; | ||
xsmall: number; | ||
}; | ||
} | ||
|
||
let getBreakpointsPromise: Promise<Breakpoints>; | ||
|
||
function breakpointTokenToNumericalValue(style: CSSStyleDeclaration, tokenName: string): number { | ||
return parseInt(style.getPropertyValue(tokenName)); | ||
} | ||
|
||
/** | ||
* This util will return a breakpoints lookup object. | ||
* | ||
* Note that the breakpoints will be evaluated at the root and cached for reuse. | ||
*/ | ||
export async function getBreakpoints(): Promise<Breakpoints> { | ||
if (getBreakpointsPromise) { | ||
return getBreakpointsPromise; | ||
} | ||
|
||
getBreakpointsPromise = new Promise<Breakpoints>((resolve) => { | ||
requestAnimationFrame(() => { | ||
const rootStyles = getComputedStyle(document.body); | ||
|
||
resolve({ | ||
width: { | ||
large: breakpointTokenToNumericalValue(rootStyles, "--calcite-app-breakpoint-width-lg"), | ||
medium: breakpointTokenToNumericalValue(rootStyles, "--calcite-app-breakpoint-width-md"), | ||
small: breakpointTokenToNumericalValue(rootStyles, "--calcite-app-breakpoint-width-sm"), | ||
xsmall: breakpointTokenToNumericalValue(rootStyles, "--calcite-app-breakpoint-width-xs"), | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
return getBreakpointsPromise; | ||
} |