fumadocs-core 15.6.6 → 15.6.7

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.
@@ -1,14 +1,18 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import { ReactNode } from 'react';
2
+ import { ReactNode, HTMLAttributes, FC } from 'react';
3
3
 
4
+ declare function HideIfEmptyProvider({ nonce, children, }: {
5
+ nonce?: string;
6
+ children: ReactNode;
7
+ }): react_jsx_runtime.JSX.Element;
4
8
  /**
5
9
  * The built-in CSS `:empty` selector cannot detect if the children is hidden, classes such as `md:hidden` causes it to fail.
6
10
  * This component is an enhancement to `empty:hidden` to fix the issue described above.
7
11
  *
8
12
  * This can be expensive, please avoid this whenever possible.
9
13
  */
10
- declare function HideIfEmpty({ children }: {
11
- children: ReactNode;
14
+ declare function HideIfEmpty<Props extends HTMLAttributes<HTMLElement>>({ as: Comp, ...props }: Props & {
15
+ as: FC<Props>;
12
16
  }): react_jsx_runtime.JSX.Element;
13
17
 
14
- export { HideIfEmpty };
18
+ export { HideIfEmpty, HideIfEmptyProvider };
@@ -2,9 +2,28 @@
2
2
  import "./chunk-JSBRDJBE.js";
3
3
 
4
4
  // src/hide-if-empty.tsx
5
- import React from "react";
5
+ import {
6
+ createContext,
7
+ useContext,
8
+ useEffect,
9
+ useId,
10
+ useMemo,
11
+ useState
12
+ } from "react";
6
13
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
7
- var isEmpty = (node) => {
14
+ var Context = createContext({
15
+ nonce: void 0
16
+ });
17
+ function HideIfEmptyProvider({
18
+ nonce,
19
+ children
20
+ }) {
21
+ return /* @__PURE__ */ jsx(Context.Provider, { value: useMemo(() => ({ nonce }), [nonce]), children });
22
+ }
23
+ function getElement(id) {
24
+ return document.querySelector(`[data-fd-if-empty="${id}"]`);
25
+ }
26
+ function isEmpty(node) {
8
27
  for (let i = 0; i < node.childNodes.length; i++) {
9
28
  const child = node.childNodes.item(i);
10
29
  if (child.nodeType === Node.TEXT_NODE || child.nodeType === Node.ELEMENT_NODE && window.getComputedStyle(child).display !== "none") {
@@ -12,53 +31,52 @@ var isEmpty = (node) => {
12
31
  }
13
32
  }
14
33
  return true;
15
- };
16
- function HideIfEmpty({ children }) {
17
- const id = React.useId();
18
- const [empty, setEmpty] = React.useState();
19
- React.useEffect(() => {
20
- const element = document.querySelector(
21
- `[data-fdid="${id}"]`
22
- );
23
- if (!element) return;
24
- const onUpdate = () => {
25
- setEmpty(isEmpty(element));
26
- };
27
- const observer = new ResizeObserver(onUpdate);
28
- observer.observe(element);
29
- onUpdate();
30
- return () => {
31
- observer.disconnect();
34
+ }
35
+ function HideIfEmpty({
36
+ as: Comp,
37
+ ...props
38
+ }) {
39
+ const id = useId();
40
+ const { nonce } = useContext(Context);
41
+ const [empty, setEmpty] = useState(() => {
42
+ const element = typeof window !== "undefined" ? getElement(id) : null;
43
+ if (element) return isEmpty(element);
44
+ });
45
+ useEffect(() => {
46
+ const handleResize = () => {
47
+ const element = getElement(id);
48
+ if (element) setEmpty(isEmpty(element));
32
49
  };
50
+ window.addEventListener("resize", handleResize);
51
+ return () => window.removeEventListener("resize", handleResize);
33
52
  }, [id]);
34
- let child;
35
- if (React.isValidElement(children)) {
36
- child = React.cloneElement(children, {
37
- ...children.props,
38
- "data-fdid": id,
39
- "data-empty": empty,
40
- suppressHydrationWarning: true
41
- });
42
- } else {
43
- child = React.Children.count(children) > 1 ? React.Children.only(null) : null;
44
- }
53
+ const init = (id2) => {
54
+ const element = getElement(id2);
55
+ if (element) element.hidden = isEmpty(element);
56
+ const script = document.currentScript;
57
+ if (script) script.parentNode?.removeChild(script);
58
+ };
45
59
  return /* @__PURE__ */ jsxs(Fragment, { children: [
46
- child,
60
+ /* @__PURE__ */ jsx(
61
+ Comp,
62
+ {
63
+ ...props,
64
+ "data-fd-if-empty": id,
65
+ hidden: empty ?? false
66
+ }
67
+ ),
47
68
  empty === void 0 && /* @__PURE__ */ jsx(
48
69
  "script",
49
70
  {
50
- suppressHydrationWarning: true,
71
+ nonce,
51
72
  dangerouslySetInnerHTML: {
52
- __html: `{
53
- const element = document.querySelector('[data-fdid="${id}"]')
54
- if (element) {
55
- element.setAttribute('data-empty', String((${isEmpty.toString()})(element)))
56
- }}`
73
+ __html: `{${getElement};${isEmpty};(${init})("${id}")}`
57
74
  }
58
75
  }
59
76
  )
60
77
  ] });
61
78
  }
62
79
  export {
63
- HideIfEmpty
80
+ HideIfEmpty,
81
+ HideIfEmptyProvider
64
82
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fumadocs-core",
3
- "version": "15.6.6",
3
+ "version": "15.6.7",
4
4
  "description": "The library for building a documentation website in Next.js",
5
5
  "keywords": [
6
6
  "NextJs",