fumadocs-core 16.0.5 → 16.0.6

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.
@@ -12,6 +12,7 @@ var Link2 = forwardRef(
12
12
  external = href.match(/^\w+:/) || // protocol relative URL
13
13
  href.startsWith("//"),
14
14
  prefetch,
15
+ children,
15
16
  ...props
16
17
  }, ref) => {
17
18
  if (external) {
@@ -23,11 +24,11 @@ var Link2 = forwardRef(
23
24
  rel: "noreferrer noopener",
24
25
  target: "_blank",
25
26
  ...props,
26
- children: props.children
27
+ children
27
28
  }
28
29
  );
29
30
  }
30
- return /* @__PURE__ */ jsx(Link, { ref, href, prefetch, ...props });
31
+ return /* @__PURE__ */ jsx(Link, { ref, href, prefetch, ...props, children });
31
32
  }
32
33
  );
33
34
  Link2.displayName = "Link";
@@ -1,8 +1,8 @@
1
1
  import { PluggableList } from 'unified';
2
2
  import { Compatible } from 'vfile';
3
3
  import { TOCItemType } from '../toc.js';
4
- import 'react';
5
4
  import 'react/jsx-runtime';
5
+ import 'react';
6
6
 
7
7
  /**
8
8
  * Get Table of Contents from markdown/mdx document (using remark)
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  import {
3
3
  Link
4
- } from "./chunk-H2GMUTQG.js";
4
+ } from "./chunk-N2ZQXKIX.js";
5
5
  import {
6
6
  useParams
7
7
  } from "./chunk-BBP7MIO4.js";
package/dist/link.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  import {
3
3
  Link
4
- } from "./chunk-H2GMUTQG.js";
4
+ } from "./chunk-N2ZQXKIX.js";
5
5
  import "./chunk-BBP7MIO4.js";
6
6
  import "./chunk-U67V476Y.js";
7
7
  export {
@@ -1,11 +1,11 @@
1
- import {
2
- defaultThemes,
3
- getHighlighter
4
- } from "../chunk-XN2LKXFZ.js";
5
1
  import {
6
2
  flattenNode,
7
3
  remarkHeading
8
4
  } from "../chunk-QMATWJ5F.js";
5
+ import {
6
+ defaultThemes,
7
+ getHighlighter
8
+ } from "../chunk-XN2LKXFZ.js";
9
9
  import "../chunk-U67V476Y.js";
10
10
 
11
11
  // src/mdx-plugins/index.ts
package/dist/toc.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import * as react from 'react';
2
- import { ReactNode, RefObject, AnchorHTMLAttributes } from 'react';
3
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode, RefObject, ComponentProps } from 'react';
4
3
 
5
4
  interface TOCItemType {
6
5
  title: ReactNode;
@@ -35,10 +34,10 @@ interface ScrollProviderProps {
35
34
  }
36
35
  declare function ScrollProvider({ containerRef, children, }: ScrollProviderProps): react_jsx_runtime.JSX.Element;
37
36
  declare function AnchorProvider({ toc, single, children, }: AnchorProviderProps): react_jsx_runtime.JSX.Element;
38
- interface TOCItemProps extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> {
37
+ interface TOCItemProps extends Omit<ComponentProps<'a'>, 'href'> {
39
38
  href: string;
40
39
  onActiveChange?: (v: boolean) => void;
41
40
  }
42
- declare const TOCItem: react.ForwardRefExoticComponent<TOCItemProps & react.RefAttributes<HTMLAnchorElement>>;
41
+ declare function TOCItem({ ref, onActiveChange, ...props }: TOCItemProps): react_jsx_runtime.JSX.Element;
43
42
 
44
43
  export { AnchorProvider, type AnchorProviderProps, ScrollProvider, type ScrollProviderProps, TOCItem, type TOCItemProps, type TOCItemType, type TableOfContents, useActiveAnchor, useActiveAnchors };
package/dist/toc.js CHANGED
@@ -1,11 +1,17 @@
1
1
  "use client";
2
- import {
3
- useOnChange
4
- } from "./chunk-EMWGTXSW.js";
5
2
  import "./chunk-U67V476Y.js";
6
3
 
7
4
  // src/toc.tsx
8
- import { createContext, forwardRef, useContext, useMemo, useRef } from "react";
5
+ import {
6
+ createContext,
7
+ useContext,
8
+ useEffect,
9
+ useEffectEvent,
10
+ useLayoutEffect,
11
+ useMemo,
12
+ useRef,
13
+ useState
14
+ } from "react";
9
15
  import scrollIntoView from "scroll-into-view-if-needed";
10
16
 
11
17
  // src/utils/merge-refs.ts
@@ -14,60 +20,13 @@ function mergeRefs(...refs) {
14
20
  refs.forEach((ref) => {
15
21
  if (typeof ref === "function") {
16
22
  ref(value);
17
- } else if (ref !== null) {
23
+ } else if (ref != null) {
18
24
  ref.current = value;
19
25
  }
20
26
  });
21
27
  };
22
28
  }
23
29
 
24
- // src/utils/use-anchor-observer.ts
25
- import { useEffect, useState } from "react";
26
- function useAnchorObserver(watch, single) {
27
- const [activeAnchor, setActiveAnchor] = useState([]);
28
- useEffect(() => {
29
- let visible = [];
30
- const observer = new IntersectionObserver(
31
- (entries) => {
32
- for (const entry of entries) {
33
- if (entry.isIntersecting && !visible.includes(entry.target.id)) {
34
- visible = [...visible, entry.target.id];
35
- } else if (!entry.isIntersecting && visible.includes(entry.target.id)) {
36
- visible = visible.filter((v) => v !== entry.target.id);
37
- }
38
- }
39
- if (visible.length > 0) setActiveAnchor(visible);
40
- },
41
- {
42
- rootMargin: single ? "-80px 0% -70% 0%" : `-20px 0% -40% 0%`,
43
- threshold: 1
44
- }
45
- );
46
- function onScroll() {
47
- const element = document.scrollingElement;
48
- if (!element) return;
49
- const top = element.scrollTop;
50
- if (top <= 0 && single) setActiveAnchor(watch.slice(0, 1));
51
- else if (top + element.clientHeight >= element.scrollHeight - 6) {
52
- setActiveAnchor((active) => {
53
- return active.length > 0 && !single ? watch.slice(watch.indexOf(active[0])) : watch.slice(-1);
54
- });
55
- }
56
- }
57
- for (const heading of watch) {
58
- const element = document.getElementById(heading);
59
- if (element) observer.observe(element);
60
- }
61
- onScroll();
62
- window.addEventListener("scroll", onScroll);
63
- return () => {
64
- window.removeEventListener("scroll", onScroll);
65
- observer.disconnect();
66
- };
67
- }, [single, watch]);
68
- return single ? activeAnchor.slice(0, 1) : activeAnchor;
69
- }
70
-
71
30
  // src/toc.tsx
72
31
  import { jsx } from "react/jsx-runtime";
73
32
  var ActiveAnchorContext = createContext([]);
@@ -75,7 +34,7 @@ var ScrollContext = createContext({
75
34
  current: null
76
35
  });
77
36
  function useActiveAnchor() {
78
- return useContext(ActiveAnchorContext).at(-1);
37
+ return useContext(ActiveAnchorContext)[0];
79
38
  }
80
39
  function useActiveAnchors() {
81
40
  return useContext(ActiveAnchorContext);
@@ -96,31 +55,93 @@ function AnchorProvider({
96
55
  }, [toc]);
97
56
  return /* @__PURE__ */ jsx(ActiveAnchorContext.Provider, { value: useAnchorObserver(headings, single), children });
98
57
  }
99
- var TOCItem = forwardRef(
100
- ({ onActiveChange, ...props }, ref) => {
101
- const containerRef = useContext(ScrollContext);
102
- const anchors = useActiveAnchors();
103
- const anchorRef = useRef(null);
104
- const mergedRef = mergeRefs(anchorRef, ref);
105
- const isActive = anchors.includes(props.href.slice(1));
106
- useOnChange(isActive, (v) => {
107
- const element = anchorRef.current;
108
- if (!element) return;
109
- if (v && containerRef.current) {
110
- scrollIntoView(element, {
111
- behavior: "smooth",
112
- block: "center",
113
- inline: "center",
114
- scrollMode: "always",
115
- boundary: containerRef.current
116
- });
58
+ function TOCItem({
59
+ ref,
60
+ onActiveChange = () => null,
61
+ ...props
62
+ }) {
63
+ const containerRef = useContext(ScrollContext);
64
+ const anchorRef = useRef(null);
65
+ const activeOrder = useActiveAnchors().indexOf(props.href.slice(1));
66
+ const isActive = activeOrder !== -1;
67
+ const shouldScroll = activeOrder === 0;
68
+ const onActiveChangeEvent = useEffectEvent(onActiveChange);
69
+ useLayoutEffect(() => {
70
+ const anchor = anchorRef.current;
71
+ const container = containerRef.current;
72
+ if (container && anchor && shouldScroll)
73
+ scrollIntoView(anchor, {
74
+ behavior: "smooth",
75
+ block: "center",
76
+ inline: "center",
77
+ scrollMode: "always",
78
+ boundary: container
79
+ });
80
+ }, [containerRef, shouldScroll]);
81
+ useEffect(() => {
82
+ return () => onActiveChangeEvent(isActive);
83
+ }, [isActive]);
84
+ return /* @__PURE__ */ jsx("a", { ref: mergeRefs(anchorRef, ref), "data-active": isActive, ...props, children: props.children });
85
+ }
86
+ function useAnchorObserver(watch, single) {
87
+ const observerRef = useRef(null);
88
+ const [activeAnchor, setActiveAnchor] = useState(() => []);
89
+ const stateRef = useRef(null);
90
+ const onChange = useEffectEvent((entries) => {
91
+ stateRef.current ??= {
92
+ visible: /* @__PURE__ */ new Set()
93
+ };
94
+ const state = stateRef.current;
95
+ for (const entry of entries) {
96
+ if (entry.isIntersecting) {
97
+ state.visible.add(entry.target.id);
98
+ } else {
99
+ state.visible.delete(entry.target.id);
100
+ }
101
+ }
102
+ if (state.visible.size === 0) {
103
+ const viewTop = entries[0].rootBounds.top;
104
+ let fallback;
105
+ let min = -1;
106
+ for (const id of watch) {
107
+ const element = document.getElementById(id);
108
+ if (!element) continue;
109
+ const d = Math.abs(viewTop - element.getBoundingClientRect().top);
110
+ if (min === -1 || d < min) {
111
+ fallback = element;
112
+ min = d;
113
+ }
117
114
  }
118
- onActiveChange?.(v);
115
+ setActiveAnchor(fallback ? [fallback.id] : []);
116
+ } else {
117
+ const items = watch.filter((item) => state.visible.has(item));
118
+ setActiveAnchor(single ? items.slice(0, 1) : items);
119
+ }
120
+ });
121
+ useEffect(() => {
122
+ if (observerRef.current) return;
123
+ observerRef.current = new IntersectionObserver(onChange, {
124
+ rootMargin: "0px",
125
+ threshold: 0.98
119
126
  });
120
- return /* @__PURE__ */ jsx("a", { ref: mergedRef, "data-active": isActive, ...props, children: props.children });
121
- }
122
- );
123
- TOCItem.displayName = "TOCItem";
127
+ return () => {
128
+ observerRef.current?.disconnect();
129
+ observerRef.current = null;
130
+ };
131
+ }, []);
132
+ useEffect(() => {
133
+ const observer = observerRef.current;
134
+ if (!observer) return;
135
+ const elements = watch.flatMap(
136
+ (heading) => document.getElementById(heading) ?? []
137
+ );
138
+ for (const element of elements) observer.observe(element);
139
+ return () => {
140
+ for (const element of elements) observer.unobserve(element);
141
+ };
142
+ }, [watch]);
143
+ return activeAnchor;
144
+ }
124
145
  export {
125
146
  AnchorProvider,
126
147
  ScrollProvider,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fumadocs-core",
3
- "version": "16.0.5",
3
+ "version": "16.0.6",
4
4
  "description": "The library for building a documentation website in any React.js framework",
5
5
  "keywords": [
6
6
  "Fumadocs",
@@ -123,19 +123,19 @@
123
123
  "@mdx-js/mdx": "^3.1.1",
124
124
  "@mixedbread/sdk": "^0.35.1",
125
125
  "@orama/core": "^1.2.13",
126
- "@tanstack/react-router": "^1.133.32",
126
+ "@tanstack/react-router": "^1.133.36",
127
127
  "@types/estree-jsx": "^1.0.5",
128
128
  "@types/hast": "^3.0.4",
129
129
  "@types/mdast": "^4.0.4",
130
130
  "@types/negotiator": "^0.6.4",
131
- "@types/node": "24.9.1",
131
+ "@types/node": "24.9.2",
132
132
  "@types/react": "^19.2.2",
133
133
  "@types/react-dom": "^19.2.2",
134
134
  "algoliasearch": "5.41.0",
135
135
  "lucide-react": "^0.548.0",
136
136
  "mdast-util-mdx-jsx": "^3.2.0",
137
137
  "mdast-util-mdxjs-esm": "^2.0.1",
138
- "next": "16.0.0",
138
+ "next": "16.0.1",
139
139
  "react-router": "^7.9.4",
140
140
  "remark-mdx": "^3.1.1",
141
141
  "remove-markdown": "^0.6.2",