-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from bprusinowski/fix/async-font-load
fix: Async font load
- Loading branch information
Showing
8 changed files
with
103 additions
and
37 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,46 @@ | ||
import { select } from "d3-selection"; | ||
|
||
export const prepareDiv = (div: HTMLDivElement): HTMLDivElement => { | ||
return select(div).style("position", "relative").node() as HTMLDivElement; | ||
}; | ||
|
||
/** Watches font family changes. | ||
* | ||
* Super important as it impacts the text dimensions calculations (and fonts | ||
* are often loaded asynchronously), so we need to wait for them to load. | ||
*/ | ||
export const createFontLoadObserver = ( | ||
div: HTMLDivElement, | ||
callback: () => void | ||
): ResizeObserver => { | ||
const node = select(div) | ||
.append("div") | ||
.attr("aria-hidden", "true") | ||
.style("z-index", -1) | ||
.style("position", "absolute") | ||
.style("top", 0) | ||
.style("left", 0) | ||
.style("width", "fit-content") | ||
.style("height", "fit-content") | ||
.style("opacity", 0) | ||
.style("pointer-events", "none") | ||
.style("white-space", "nowrap") | ||
.style("overflow", "hidden") | ||
.text("Font load trigger") | ||
.node() as HTMLDivElement; | ||
|
||
const observer = new ResizeObserver(callback); | ||
observer.observe(node); | ||
|
||
return observer; | ||
}; | ||
|
||
export const createResizeObserver = ( | ||
div: HTMLDivElement, | ||
callback: () => void | ||
): ResizeObserver => { | ||
const observer = new ResizeObserver(callback); | ||
observer.observe(div); | ||
|
||
return observer; | ||
}; |
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
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