@farming-labs/theme 0.2.109 → 0.2.110

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,11 +8,6 @@ type BrowserDocsLayoutProps = Omit<TanstackDocsLayoutProps, "browserRuntime">;
8
8
  /** Docs layout for framework-neutral browser adapters such as Farm.js. */
9
9
  declare function BrowserDocsLayout(props: BrowserDocsLayoutProps): react_jsx_runtime0.JSX.Element;
10
10
  type FumadocsProviderProps = ComponentPropsWithoutRef<typeof RootProvider>;
11
- declare global {
12
- interface Window {
13
- __fdBrowserHistoryPatched?: boolean;
14
- }
15
- }
16
11
  interface BrowserRootProviderProps extends FumadocsProviderProps {
17
12
  /** Path rendered by the server, used to keep hydration deterministic. */
18
13
  initialPathname?: string;
package/dist/browser.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  "use client";
2
2
 
3
+ import { subscribeToWindowLocation } from "./client-location.mjs";
3
4
  import { TanstackDocsLayout } from "./tanstack-layout.mjs";
4
5
  import { createContext, useCallback, useContext, useMemo, useSyncExternalStore } from "react";
5
6
  import { FrameworkProvider } from "fumadocs-core/framework";
@@ -23,28 +24,6 @@ const defaultBrowserNavigation = {
23
24
  }
24
25
  };
25
26
  const BrowserNavigationContext = createContext(defaultBrowserNavigation);
26
- function patchHistoryEvents() {
27
- if (typeof window === "undefined" || window.__fdBrowserHistoryPatched) return;
28
- for (const method of ["pushState", "replaceState"]) {
29
- const original = window.history[method];
30
- window.history[method] = function patchedHistoryMethod(...args) {
31
- const result = original.apply(this, args);
32
- window.dispatchEvent(new Event("fd-location-change"));
33
- return result;
34
- };
35
- }
36
- window.__fdBrowserHistoryPatched = true;
37
- }
38
- function subscribeToLocation(onStoreChange) {
39
- if (typeof window === "undefined") return () => {};
40
- patchHistoryEvents();
41
- window.addEventListener("popstate", onStoreChange);
42
- window.addEventListener("fd-location-change", onStoreChange);
43
- return () => {
44
- window.removeEventListener("popstate", onStoreChange);
45
- window.removeEventListener("fd-location-change", onStoreChange);
46
- };
47
- }
48
27
  function getBrowserPathname() {
49
28
  return typeof window === "undefined" ? "/" : window.location.pathname;
50
29
  }
@@ -95,7 +74,7 @@ function BrowserLink({ prefetch: _prefetch, href, target, download, onClick, ...
95
74
  }
96
75
  /** Framework-neutral provider for server-rendered React documentation adapters. */
97
76
  function BrowserRootProvider({ children, search, initialPathname = "/", navigation = defaultBrowserNavigation, ...props }) {
98
- const useBrowserPathname = () => useSyncExternalStore(subscribeToLocation, getBrowserPathname, () => initialPathname);
77
+ const useBrowserPathname = () => useSyncExternalStore(subscribeToWindowLocation, getBrowserPathname, () => initialPathname);
99
78
  return /* @__PURE__ */ jsx(BrowserNavigationContext.Provider, {
100
79
  value: navigation,
101
80
  children: /* @__PURE__ */ jsx(FrameworkProvider, {
@@ -17,14 +17,24 @@ function patchHistoryEvents() {
17
17
  wrap("replaceState");
18
18
  window.__fdHistoryPatched = true;
19
19
  }
20
- function subscribe(onStoreChange) {
20
+ function subscribeToWindowLocation(onStoreChange) {
21
21
  if (typeof window === "undefined") return () => {};
22
22
  patchHistoryEvents();
23
- const notify = () => onStoreChange();
23
+ let active = true;
24
+ let notificationQueued = false;
25
+ const notify = () => {
26
+ if (notificationQueued) return;
27
+ notificationQueued = true;
28
+ queueMicrotask(() => {
29
+ notificationQueued = false;
30
+ if (active) onStoreChange();
31
+ });
32
+ };
24
33
  window.addEventListener("popstate", notify);
25
34
  window.addEventListener("hashchange", notify);
26
35
  window.addEventListener("fd-location-change", notify);
27
36
  return () => {
37
+ active = false;
28
38
  window.removeEventListener("popstate", notify);
29
39
  window.removeEventListener("hashchange", notify);
30
40
  window.removeEventListener("fd-location-change", notify);
@@ -39,12 +49,12 @@ function getPathnameSnapshot() {
39
49
  return window.location.pathname;
40
50
  }
41
51
  function useWindowSearchParams() {
42
- const search = useSyncExternalStore(subscribe, getSearchSnapshot, () => "");
52
+ const search = useSyncExternalStore(subscribeToWindowLocation, getSearchSnapshot, () => "");
43
53
  return new URLSearchParams(search);
44
54
  }
45
55
  function useWindowPathname() {
46
- return useSyncExternalStore(subscribe, getPathnameSnapshot, () => "");
56
+ return useSyncExternalStore(subscribeToWindowLocation, getPathnameSnapshot, () => "");
47
57
  }
48
58
 
49
59
  //#endregion
50
- export { useWindowPathname, useWindowSearchParams };
60
+ export { subscribeToWindowLocation, useWindowPathname, useWindowSearchParams };
@@ -6,7 +6,7 @@ import { useWindowPathname, useWindowSearchParams } from "./client-location.mjs"
6
6
  import { DocsFeedback } from "./docs-feedback.mjs";
7
7
  import { resolveClientLocale, withLangInUrl } from "./i18n.mjs";
8
8
  import { escapeJsonLdForScript } from "./json-ld.mjs";
9
- import { Children, Fragment, useEffect, useState } from "react";
9
+ import { Children, Fragment, useEffect, useLayoutEffect, useState } from "react";
10
10
  import { DocsBody, DocsPage, EditOnGitHub } from "fumadocs-ui/layouts/docs/page";
11
11
  import { createPortal } from "react-dom";
12
12
  import { usePathname, useRouter } from "fumadocs-core/framework";
@@ -307,19 +307,16 @@ function DocsPageClient({ tocEnabled, showLlmsInHeader = false, tocStyle = "defa
307
307
  const resolvedReadingTime = !isChangelogRoute ? readingTimeProp !== void 0 ? readingTimeProp : readingTimeEnabled ? matchedReadingTime : void 0 : void 0;
308
308
  const effectiveTocEnabled = isChangelogRoute ? false : tocEnabled;
309
309
  const effectiveBreadcrumbEnabled = isChangelogRoute ? false : breadcrumbEnabled;
310
- useEffect(() => {
310
+ useLayoutEffect(() => {
311
311
  if (!effectiveTocEnabled) return;
312
- const timer = requestAnimationFrame(() => {
313
- const container = document.getElementById("nd-page");
314
- if (!container) return;
315
- const headings = container.querySelectorAll("h2[id], h3[id], h4[id]");
316
- setToc(Array.from(headings).map((el) => ({
317
- title: el.textContent?.replace(/^#\s*/, "") || "",
318
- url: `#${el.id}`,
319
- depth: parseInt(el.tagName[1], 10)
320
- })));
321
- });
322
- return () => cancelAnimationFrame(timer);
312
+ const container = document.getElementById("nd-page");
313
+ if (!container) return;
314
+ const headings = container.querySelectorAll("h2[id], h3[id], h4[id]");
315
+ setToc(Array.from(headings).map((el) => ({
316
+ title: el.textContent?.replace(/^#\s*/, "") || "",
317
+ url: `#${el.id}`,
318
+ depth: parseInt(el.tagName[1], 10)
319
+ })));
323
320
  }, [
324
321
  children,
325
322
  effectiveTocEnabled,
@@ -385,7 +382,7 @@ function DocsPageClient({ tocEnabled, showLlmsInHeader = false, tocStyle = "defa
385
382
  const showActionsAboveTitle = showActions && pageActionsPosition === "above-title";
386
383
  const showActionsInToc = showActions && pageActionsPosition === "toc";
387
384
  const githubFileUrl = editOnGithubUrl ?? (githubUrl ? buildGithubFileUrl(githubUrl, githubBranch, pathname, entry, activeLocale, githubDirectory, contentDir) : void 0);
388
- useEffect(() => {
385
+ useLayoutEffect(() => {
389
386
  if (!showActionsInToc) {
390
387
  setTocActionsPortalHost(null);
391
388
  return;
@@ -424,7 +421,7 @@ function DocsPageClient({ tocEnabled, showLlmsInHeader = false, tocStyle = "defa
424
421
  showActionsInToc,
425
422
  toc.length
426
423
  ]);
427
- useEffect(() => {
424
+ useLayoutEffect(() => {
428
425
  if (!showActionsInToc) {
429
426
  setTitleControlsPortalHost(null);
430
427
  return;
@@ -509,7 +506,7 @@ function DocsPageClient({ tocEnabled, showLlmsInHeader = false, tocStyle = "defa
509
506
  }, "below-title") : void 0;
510
507
  const decoratedChildren = children;
511
508
  const needsTitleDecorationsPortal = !pageTitle && (!!titleDescription || !!belowTitleBlock);
512
- useEffect(() => {
509
+ useLayoutEffect(() => {
513
510
  if (!needsTitleDecorationsPortal) {
514
511
  setTitlePortalHost(null);
515
512
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farming-labs/theme",
3
- "version": "0.2.109",
3
+ "version": "0.2.110",
4
4
  "description": "Theme package for @farming-labs/docs — layout, provider, MDX components, and styles",
5
5
  "keywords": [
6
6
  "docs",
@@ -161,7 +161,7 @@
161
161
  "tsdown": "^0.20.3",
162
162
  "typescript": "^5.9.3",
163
163
  "vitest": "^4.1.8",
164
- "@farming-labs/docs": "0.2.109"
164
+ "@farming-labs/docs": "0.2.110"
165
165
  },
166
166
  "peerDependencies": {
167
167
  "@farming-labs/docs": ">=0.0.1",