@xyd-js/ui 0.1.0-xyd.0 → 0.1.0-xyd.10

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,153 +0,0 @@
1
- import React, {useState, useEffect, useContext} from "react"
2
- import {Link} from "react-router";
3
-
4
- import {$toc, $scroller} from "./Toc.styles";
5
-
6
- export interface TocProps {
7
- children: React.ReactNode;
8
- defaultValue?: string
9
- }
10
-
11
- const Context = React.createContext({
12
- value: "",
13
- onChange: (v: string) => {
14
- }
15
- })
16
-
17
- // TODO: based on scroller?
18
- export function Toc({children, defaultValue}: TocProps) {
19
- const [activeTrackHeight, setActiveTrackHeight] = useState(0)
20
- const [activeTrackTop, setActiveTrackTop] = useState(0)
21
-
22
- const [value, setValue] = useState(defaultValue || "")
23
-
24
- // TODO: more reactish implt?
25
- function handleScroll() {
26
- const activeElement = document.querySelector(`.${$toc.link$$active}`);
27
- if (!activeElement) {
28
- return;
29
- }
30
-
31
- const {offsetHeight} = activeElement as HTMLElement;
32
- setActiveTrackHeight(offsetHeight);
33
-
34
- if (!activeElement?.parentElement) {
35
- return
36
- }
37
-
38
- const {offsetTop} = activeElement.parentElement as HTMLElement;
39
- setActiveTrackTop(offsetTop);
40
- }
41
-
42
- function onChange(v: string) {
43
- setValue(v)
44
- }
45
-
46
-
47
- // TODO: more reactish
48
- useEffect(() => {
49
- const observer = new IntersectionObserver(
50
- (entries) => {
51
- let set = false
52
- entries.forEach(entry => {
53
- if (set) {
54
- return
55
- }
56
- if (!entry.isIntersecting) {
57
- return
58
- }
59
-
60
- if (entry.target instanceof HTMLHeadingElement) {
61
- const rect = entry.target.getBoundingClientRect();
62
- const isVisible = rect.top >= 0 && rect.bottom <= window.innerHeight;
63
-
64
- if (isVisible) {
65
- set = true
66
- setValue(entry.target.innerText);
67
- }
68
- }
69
- });
70
- },
71
- {threshold: 0.3}
72
- );
73
-
74
- document.querySelectorAll("h2").forEach(ref => {
75
- if (ref) observer.observe(ref);
76
- });
77
-
78
- return () => {
79
- observer.disconnect();
80
- };
81
- }, []);
82
-
83
- useEffect(() => {
84
- handleScroll(); // Initial call to set the values
85
- }, [value]);
86
-
87
- return <Context.Provider value={{
88
- value: value,
89
- onChange
90
- }}>
91
- <div className={$toc.host}>
92
- <div className={$scroller.host}>
93
- <div
94
- style={{
95
- // @ts-ignore
96
- "--active-track-height": `${activeTrackHeight}px`,
97
- "--active-track-top": `${activeTrackTop}px`,
98
- }}
99
- className={$scroller.scroll}
100
- />
101
- </div>
102
- <ul className={$toc.ul}>
103
- {children}
104
- </ul>
105
- </div>
106
- </Context.Provider>
107
- }
108
-
109
- export interface TocItemProps {
110
- children: React.ReactNode;
111
- value: string;
112
- }
113
-
114
- Toc.Item = function TocItem({
115
- children,
116
- value = "#",
117
- }: TocItemProps) {
118
- const {
119
- value: rootValue,
120
- onChange
121
- } = useContext(Context);
122
-
123
- const href = "#" + value
124
- const active = rootValue === value;
125
-
126
- return <li className={$toc.li}>
127
- <a
128
- className={`${$toc.link} ${active && $toc.link$$active}`}
129
- href={href}
130
- onClick={(e) => {
131
- // TODO: use react-router but for some reason does not work
132
- e.preventDefault()
133
- onChange(value)
134
-
135
- let found = false
136
-
137
- // TODO: below is only a temporary solution
138
- document.querySelectorAll("h2").forEach(e => {
139
- if (found) {
140
- return
141
- }
142
-
143
- if (e.innerText === value) {
144
- found = true
145
- e.scrollIntoView()
146
- }
147
- })
148
- }}
149
- >
150
- {children}
151
- </a>
152
- </li>
153
- }
@@ -1 +0,0 @@
1
- export {Toc} from "./Toc";
@@ -1,13 +0,0 @@
1
- export * from './Nav';
2
-
3
- export * from './Sidebar';
4
-
5
- export * from './SubNav';
6
-
7
- export * from './Toc';
8
-
9
-
10
-
11
-
12
-
13
-
@@ -1,23 +0,0 @@
1
- export interface ITOC {
2
- depth: number
3
- value: string
4
- children: ITOC[]
5
- }
6
-
7
- export interface IBreadcrumb {
8
- title: string,
9
- href: string
10
- }
11
-
12
- export interface INavLinks {
13
- prev?: {
14
- title: string,
15
- href: string
16
- }
17
-
18
- next?: {
19
- title: string,
20
- href: string
21
- }
22
- }
23
-
package/tsconfig.json DELETED
@@ -1,36 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "esnext",
4
- "esModuleInterop": true,
5
- "moduleResolution": "node",
6
- "target": "ES6",
7
- "lib": [
8
- "dom",
9
- "dom.iterable",
10
- "esnext"
11
- ],
12
- "allowJs": true,
13
- "skipLibCheck": true,
14
- "strict": false,
15
- "noEmit": true,
16
- "incremental": false,
17
- "resolveJsonModule": true,
18
- "isolatedModules": true,
19
- "jsx": "react",
20
- "plugins": [
21
- {
22
- "name": "next"
23
- }
24
- ],
25
- "strictNullChecks": true
26
- },
27
- "include": [
28
- "next-env.d.ts",
29
- "**/*.ts",
30
- "**/*.tsx",
31
- ".next/types/**/*.ts"
32
- ],
33
- "exclude": [
34
- "node_modules"
35
- ]
36
- }