Skip to content

Commit 41b12e8

Browse files
authored
fix: observe slidev-layout for updated font size (#21)
* Watching changes in .slidev-layout * Add debug msg to useInjectStyle.ts * Only read font size when layout loaded * Inject base font sizes through config provider * Add a comment * Remove useBaseFontSizes from naive.ts * Directly import base font size * Use isDark as a singleton * Rename base font hook * Add a comment in useBaseFontSize * Bump version * Update latest changes * Extract observeSlidevLayout * Use ResizeObserver for font sizes * Bump version to 0.8.1
1 parent 5c31c77 commit 41b12e8

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "slidev-addon-naive",
3-
"version": "0.8.0",
3+
"version": "0.8.1",
44
"description": "An addon that brings Naive UI components to Slidev.",
55
"keywords": [
66
"naive-ui",

src/useBaseFontSize.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,50 @@ const setBaseFontSize = () => {
1616
}
1717
};
1818

19-
if (typeof window === "undefined") {
20-
console.error(
21-
`[Naive] Window is undefined, can't observe DOM to set base font size`,
22-
);
23-
} else {
24-
// Setup observer for DOM changes
25-
const observer = new MutationObserver(() => {
26-
const slidevLayout = document.querySelector(".slidev-layout");
27-
28-
if (slidevLayout) {
29-
console.debug("[Naive] Found .slidev-layout");
30-
observer.disconnect(); // stop observing the document body
31-
setBaseFontSize();
19+
const observeBaseFontSize = () => {
20+
// Setup observer for DOM changes and font size changes
21+
const layoutObserver = new MutationObserver(() => {
22+
const layout = document.querySelector(".slidev-layout");
23+
if (!layout) {
24+
return;
3225
}
26+
27+
console.debug("[Naive] Found .slidev-layout, switching to style observer");
28+
layoutObserver.disconnect(); // stop observing the document body
29+
30+
// Create new observers to get updated font size
31+
const sizeObserver = new ResizeObserver(setBaseFontSize);
32+
// somehow needed to get styles from UnoCSS @apply directives
33+
sizeObserver.observe(layout);
34+
35+
const styleObserver = new MutationObserver(setBaseFontSize);
36+
// needed to get styles update in runtime
37+
styleObserver.observe(layout, {
38+
attributes: true,
39+
attributeFilter: ["style"],
40+
});
41+
42+
// TODO: this won't change font size in runtime. a polling approach can be
43+
// used, but I'm hesitating because of performance concerns
44+
45+
setBaseFontSize(); // make an attempt to set the font size
3346
});
3447

3548
// Start observing the document body for layout appearance
36-
observer.observe(document.body, {
49+
layoutObserver.observe(document.body, {
3750
childList: true,
3851
subtree: true,
3952
});
4053

4154
console.debug("[Naive] Watching for .slidev-layout element to show up");
55+
};
56+
57+
if (typeof window === "undefined") {
58+
console.error(
59+
`[Naive] Window is undefined, can't observe DOM to set base font size`,
60+
);
61+
} else {
62+
observeBaseFontSize();
4263
}
4364

4465
export default fontSize;

0 commit comments

Comments
 (0)