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.
- package/dist/hide-if-empty.d.ts +8 -4
- package/dist/hide-if-empty.js +56 -38
- package/package.json +1 -1
package/dist/hide-if-empty.d.ts
CHANGED
|
@@ -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({
|
|
11
|
-
|
|
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 };
|
package/dist/hide-if-empty.js
CHANGED
|
@@ -2,9 +2,28 @@
|
|
|
2
2
|
import "./chunk-JSBRDJBE.js";
|
|
3
3
|
|
|
4
4
|
// src/hide-if-empty.tsx
|
|
5
|
-
import
|
|
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
|
|
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({
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
};
|