@san-siva/blogkit 1.1.19 → 1.1.21
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/cjs/dynamicComponents/BlogDynamic.js +31 -181
- package/dist/cjs/dynamicComponents/BlogDynamic.js.map +1 -1
- package/dist/cjs/dynamicComponents/BlogSectionDynamic.js +11 -2
- package/dist/cjs/dynamicComponents/BlogSectionDynamic.js.map +1 -1
- package/dist/cjs/hooks/useCategoryTitles.js +104 -0
- package/dist/cjs/hooks/useCategoryTitles.js.map +1 -0
- package/dist/cjs/hooks/useSectionObserver.js +89 -0
- package/dist/cjs/hooks/useSectionObserver.js.map +1 -0
- package/dist/cjs/index.css +1 -1
- package/dist/cjs/index.css.map +1 -1
- package/dist/cjs/staticComponents/TocNodeStatic.js +16 -0
- package/dist/cjs/staticComponents/TocNodeStatic.js.map +1 -0
- package/dist/cjs/styles/Blog.module.scss.js +1 -1
- package/dist/cjs/styles/Callout.module.scss.js +1 -1
- package/dist/cjs/styles/TocNode.module.scss.js +8 -0
- package/dist/cjs/styles/TocNode.module.scss.js.map +1 -0
- package/dist/esm/dynamicComponents/BlogDynamic.js +32 -182
- package/dist/esm/dynamicComponents/BlogDynamic.js.map +1 -1
- package/dist/esm/dynamicComponents/BlogSectionDynamic.js +12 -3
- package/dist/esm/dynamicComponents/BlogSectionDynamic.js.map +1 -1
- package/dist/esm/hooks/useCategoryTitles.js +102 -0
- package/dist/esm/hooks/useCategoryTitles.js.map +1 -0
- package/dist/esm/hooks/useSectionObserver.js +87 -0
- package/dist/esm/hooks/useSectionObserver.js.map +1 -0
- package/dist/esm/index.css +1 -1
- package/dist/esm/index.css.map +1 -1
- package/dist/esm/staticComponents/TocNodeStatic.js +12 -0
- package/dist/esm/staticComponents/TocNodeStatic.js.map +1 -0
- package/dist/esm/styles/Blog.module.scss.js +1 -1
- package/dist/esm/styles/Callout.module.scss.js +1 -1
- package/dist/esm/styles/TocNode.module.scss.js +4 -0
- package/dist/esm/styles/TocNode.module.scss.js.map +1 -0
- package/dist/types/dynamicComponents/BlogDynamic.d.ts +1 -1
- package/dist/types/dynamicComponents/BlogDynamic.d.ts.map +1 -1
- package/dist/types/dynamicComponents/BlogSectionDynamic.d.ts.map +1 -1
- package/dist/types/hooks/useCategoryTitles.d.ts +22 -0
- package/dist/types/hooks/useCategoryTitles.d.ts.map +1 -0
- package/dist/types/hooks/useSectionObserver.d.ts +11 -0
- package/dist/types/hooks/useSectionObserver.d.ts.map +1 -0
- package/dist/types/staticComponents/TocNodeStatic.d.ts +16 -0
- package/dist/types/staticComponents/TocNodeStatic.d.ts.map +1 -0
- package/package.json +5 -3
- package/src/dynamicComponents/BlogDynamic.tsx +42 -253
- package/src/dynamicComponents/BlogSectionDynamic.tsx +16 -2
- package/src/hooks/useCategoryTitles.ts +148 -0
- package/src/hooks/useSectionObserver.ts +102 -0
- package/src/staticComponents/TocNodeStatic.tsx +52 -0
- package/src/styles/Blog.module.scss +0 -30
- package/src/styles/Blog.module.scss.d.ts +0 -4
- package/src/styles/BlogLink.module.scss +1 -1
- package/src/styles/BlogSection.module.scss +36 -13
- package/src/styles/CodeBlock.module.scss +2 -2
- package/src/styles/Table.module.scss +1 -1
- package/src/styles/TocNode.module.scss +49 -0
- package/src/styles/TocNode.module.scss.d.ts +11 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useRef, useEffect, useCallback } from 'react';
|
|
3
|
+
import lockScrollUpdates from '../utils/lockScrollUpdates.js';
|
|
4
|
+
|
|
5
|
+
const getSectionFromUrl = () => {
|
|
6
|
+
const url = new URL(window.location.href);
|
|
7
|
+
return url.searchParams.get('section');
|
|
8
|
+
};
|
|
9
|
+
const updateUrl = (id) => {
|
|
10
|
+
const url = new URL(window.location.href);
|
|
11
|
+
url.searchParams.set('section', id);
|
|
12
|
+
window.history.replaceState({}, '', url.toString());
|
|
13
|
+
};
|
|
14
|
+
const scrollIntoView = (element) => {
|
|
15
|
+
if (!element)
|
|
16
|
+
return;
|
|
17
|
+
const top = element.getBoundingClientRect().top + document.body.scrollTop - 100;
|
|
18
|
+
document.body.scrollTo({ top, behavior: 'smooth' });
|
|
19
|
+
};
|
|
20
|
+
function useSectionObserver({ categoryTitles, setVisibleTitle }) {
|
|
21
|
+
const isClickScrolling = useRef(false);
|
|
22
|
+
const scrollEndHandlerRef = useRef(null);
|
|
23
|
+
const intersectionObserversRef = useRef(new Map());
|
|
24
|
+
// Set up IntersectionObservers whenever the set of sections changes
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
for (const [id, { el }] of categoryTitles) {
|
|
27
|
+
const observer = new IntersectionObserver(([entry]) => {
|
|
28
|
+
if (!entry.isIntersecting)
|
|
29
|
+
return;
|
|
30
|
+
if (isClickScrolling.current)
|
|
31
|
+
return;
|
|
32
|
+
if (document.body.scrollTop === 0)
|
|
33
|
+
return;
|
|
34
|
+
setVisibleTitle(visibleId => {
|
|
35
|
+
if (visibleId === id && !entry.isIntersecting)
|
|
36
|
+
return null;
|
|
37
|
+
if (entry.isIntersecting)
|
|
38
|
+
return id;
|
|
39
|
+
return visibleId;
|
|
40
|
+
});
|
|
41
|
+
updateUrl(id);
|
|
42
|
+
}, { threshold: 0.1 });
|
|
43
|
+
intersectionObserversRef.current.set(id, observer);
|
|
44
|
+
observer.observe(el);
|
|
45
|
+
}
|
|
46
|
+
}, [categoryTitles.size, setVisibleTitle]);
|
|
47
|
+
// On initial load, scroll to section specified in URL
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (categoryTitles.size === 0)
|
|
50
|
+
return;
|
|
51
|
+
const section = getSectionFromUrl();
|
|
52
|
+
if (!section)
|
|
53
|
+
return;
|
|
54
|
+
const entry = categoryTitles.get(section);
|
|
55
|
+
if (!entry)
|
|
56
|
+
return;
|
|
57
|
+
scrollIntoView(entry.el);
|
|
58
|
+
lockScrollUpdates(section, isClickScrolling, scrollEndHandlerRef, setVisibleTitle);
|
|
59
|
+
}, [categoryTitles.size, setVisibleTitle]);
|
|
60
|
+
// Cleanup observers on unmount
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
return () => {
|
|
63
|
+
if (scrollEndHandlerRef.current) {
|
|
64
|
+
document.body.removeEventListener('scrollend', scrollEndHandlerRef.current);
|
|
65
|
+
}
|
|
66
|
+
for (const observer of intersectionObserversRef.current.values()) {
|
|
67
|
+
observer.disconnect();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}, []);
|
|
71
|
+
const handleClickCategoryTitle = useCallback((event) => {
|
|
72
|
+
const id = event.currentTarget.dataset.id;
|
|
73
|
+
const index = event.currentTarget.dataset.idx;
|
|
74
|
+
if (!id || !index)
|
|
75
|
+
return;
|
|
76
|
+
const { el } = categoryTitles.get(id) || {};
|
|
77
|
+
if (!el)
|
|
78
|
+
return;
|
|
79
|
+
updateUrl(id);
|
|
80
|
+
scrollIntoView(el);
|
|
81
|
+
lockScrollUpdates(id, isClickScrolling, scrollEndHandlerRef, setVisibleTitle);
|
|
82
|
+
}, [categoryTitles, setVisibleTitle]);
|
|
83
|
+
return { handleClickCategoryTitle };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export { useSectionObserver };
|
|
87
|
+
//# sourceMappingURL=useSectionObserver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSectionObserver.js","sources":["../../../src/hooks/useSectionObserver.ts"],"sourcesContent":["'use client';\n\nimport { useCallback, useEffect, useRef } from 'react';\n\nimport type { Dispatch, MouseEvent, SetStateAction } from 'react';\n\nimport lockScrollUpdates from '../utils/lockScrollUpdates';\nimport type { CategoryTitle } from './useCategoryTitles';\n\nconst getSectionFromUrl = () => {\n\tconst url = new URL(window.location.href);\n\treturn url.searchParams.get('section');\n};\n\nconst updateUrl = (id: string) => {\n\tconst url = new URL(window.location.href);\n\turl.searchParams.set('section', id);\n\twindow.history.replaceState({}, '', url.toString());\n};\n\nconst scrollIntoView = (element: HTMLElement) => {\n\tif (!element) return;\n\tconst top =\n\t\telement.getBoundingClientRect().top + document.body.scrollTop - 100;\n\tdocument.body.scrollTo({ top, behavior: 'smooth' });\n};\n\ninterface Options {\n\tcategoryTitles: CategoryTitle;\n\tsetVisibleTitle: Dispatch<SetStateAction<string | null>>;\n}\n\nexport function useSectionObserver({ categoryTitles, setVisibleTitle }: Options) {\n\tconst isClickScrolling = useRef(false);\n\tconst scrollEndHandlerRef = useRef<(() => void) | null>(null);\n\tconst intersectionObserversRef = useRef<Map<string, IntersectionObserver>>(\n\t\tnew Map()\n\t);\n\n\t// Set up IntersectionObservers whenever the set of sections changes\n\tuseEffect(() => {\n\t\tfor (const [id, { el }] of categoryTitles) {\n\t\t\tconst observer = new IntersectionObserver(\n\t\t\t\t([entry]) => {\n\t\t\t\t\tif (!entry.isIntersecting) return;\n\t\t\t\t\tif (isClickScrolling.current) return;\n\t\t\t\t\tif (document.body.scrollTop === 0) return;\n\t\t\t\t\tsetVisibleTitle(visibleId => {\n\t\t\t\t\t\tif (visibleId === id && !entry.isIntersecting) return null;\n\t\t\t\t\t\tif (entry.isIntersecting) return id;\n\t\t\t\t\t\treturn visibleId;\n\t\t\t\t\t});\n\t\t\t\t\tupdateUrl(id);\n\t\t\t\t},\n\t\t\t\t{ threshold: 0.1 }\n\t\t\t);\n\t\t\tintersectionObserversRef.current.set(id, observer);\n\t\t\tobserver.observe(el as HTMLElement);\n\t\t}\n\t}, [categoryTitles.size, setVisibleTitle]);\n\n\t// On initial load, scroll to section specified in URL\n\tuseEffect(() => {\n\t\tif (categoryTitles.size === 0) return;\n\t\tconst section = getSectionFromUrl();\n\t\tif (!section) return;\n\t\tconst entry = categoryTitles.get(section);\n\t\tif (!entry) return;\n\t\tscrollIntoView(entry.el);\n\t\tlockScrollUpdates(section, isClickScrolling, scrollEndHandlerRef, setVisibleTitle);\n\t}, [categoryTitles.size, setVisibleTitle]);\n\n\t// Cleanup observers on unmount\n\tuseEffect(() => {\n\t\treturn () => {\n\t\t\tif (scrollEndHandlerRef.current) {\n\t\t\t\tdocument.body.removeEventListener('scrollend', scrollEndHandlerRef.current);\n\t\t\t}\n\t\t\tfor (const observer of intersectionObserversRef.current.values()) {\n\t\t\t\tobserver.disconnect();\n\t\t\t}\n\t\t};\n\t}, []);\n\n\tconst handleClickCategoryTitle = useCallback(\n\t\t(event: MouseEvent<HTMLParagraphElement>) => {\n\t\t\tconst id = event.currentTarget.dataset.id;\n\t\t\tconst index = event.currentTarget.dataset.idx;\n\t\t\tif (!id || !index) return;\n\n\t\t\tconst { el } = categoryTitles.get(id) || {};\n\t\t\tif (!el) return;\n\n\t\t\tupdateUrl(id);\n\t\t\tscrollIntoView(el);\n\t\t\tlockScrollUpdates(id, isClickScrolling, scrollEndHandlerRef, setVisibleTitle);\n\t\t},\n\t\t[categoryTitles, setVisibleTitle]\n\t);\n\n\treturn { handleClickCategoryTitle };\n}\n"],"names":[],"mappings":";;;AASA,MAAM,iBAAiB,GAAG,MAAK;IAC9B,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;IACzC,OAAO,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,SAAS,CAAC;AACvC,CAAC;AAED,MAAM,SAAS,GAAG,CAAC,EAAU,KAAI;IAChC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;IACzC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,SAAS,EAAE,EAAE,CAAC;AACnC,IAAA,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE,EAAE,EAAE,EAAE,GAAG,CAAC,QAAQ,EAAE,CAAC;AACpD,CAAC;AAED,MAAM,cAAc,GAAG,CAAC,OAAoB,KAAI;AAC/C,IAAA,IAAI,CAAC,OAAO;QAAE;AACd,IAAA,MAAM,GAAG,GACR,OAAO,CAAC,qBAAqB,EAAE,CAAC,GAAG,GAAG,QAAQ,CAAC,IAAI,CAAC,SA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