idea-react 3.0.0-beta.0 → 3.0.0-beta.2
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/AGENTS.md +19 -2
- package/ReadMe.md +95 -54
- package/dist/DatePickerPlugin-CpGuQRsi.js +1 -0
- package/dist/DatePickerPlugin-DB3nnslN.mjs +1 -0
- package/dist/index.d.mts +1232 -474
- package/dist/index.d.ts +1232 -474
- package/dist/index.js +16 -16
- package/dist/index.mjs +15 -15
- package/dist/rolldown-runtime-5iZMYShA.js +1 -0
- package/dist/style.css +66 -66
- package/package.json +1 -1
- package/source/Accordion.tsx +161 -0
- package/source/Alert.tsx +153 -0
- package/source/Avatar.tsx +57 -46
- package/source/Badge.tsx +17 -15
- package/source/Breadcrumb.tsx +50 -0
- package/source/Button.tsx +69 -40
- package/source/ButtonGroup.tsx +23 -18
- package/source/Card.tsx +86 -17
- package/source/Carousel/index.tsx +392 -0
- package/source/Carousel/model.ts +113 -0
- package/source/Carousel/scroll.ts +79 -0
- package/source/CloseButton.tsx +20 -15
- package/source/Collapse.tsx +233 -0
- package/source/Drawer.tsx +158 -38
- package/source/{ClickBoundary.tsx → Extension/ClickBoundary.tsx} +24 -5
- package/source/Extension/CodeBlock.tsx +64 -0
- package/source/{Countdown.tsx → Extension/Countdown.tsx} +21 -9
- package/source/{Dialog.tsx → Extension/Dialog.tsx} +10 -6
- package/source/Extension/Editor.tsx +78 -0
- package/source/Extension/EditorHTML.tsx +20 -0
- package/source/{HorizontalMarquee → Extension/HorizontalMarquee}/index.module.less +1 -3
- package/source/Extension/HorizontalMarquee/index.tsx +85 -0
- package/source/Extension/Icon.tsx +19 -0
- package/source/Extension/Loading.tsx +30 -0
- package/source/{MonthCalendar.tsx → Extension/MonthCalendar.tsx} +29 -13
- package/source/{Nameplate.tsx → Extension/Nameplate.tsx} +1 -1
- package/source/Extension/OverlayBox.tsx +293 -0
- package/source/{PageNav.tsx → Extension/PageNav.tsx} +43 -16
- package/source/{ScrollNav → Extension/ScrollNav}/Scroller.tsx +21 -5
- package/source/Extension/ScrollNav/index.tsx +47 -0
- package/source/Extension/Select.tsx +128 -0
- package/source/{ShareBox.tsx → Extension/ShareBox.tsx} +1 -1
- package/source/Extension/SpinnerButton.tsx +30 -0
- package/source/Extension/TableSpinner.tsx +22 -0
- package/source/Extension/TextTruncate/index.tsx +29 -0
- package/source/Extension/Time.tsx +17 -0
- package/source/Extension/TimeDistance.tsx +45 -0
- package/source/Extension/Timeline/index.tsx +63 -0
- package/source/Extension/TypeEcho/Content.tsx +123 -0
- package/source/Extension/TypeEcho/index.tsx +22 -0
- package/source/Extension/UserRank/Address.tsx +22 -0
- package/source/Extension/UserRank/index.tsx +106 -0
- package/source/Extension/VerticalMarquee/index.tsx +23 -0
- package/source/Extension/ZodiacBar.tsx +59 -0
- package/source/Extension/index.ts +27 -0
- package/source/Form/Adorn.tsx +45 -26
- package/source/Form/Checkbox.tsx +20 -11
- package/source/Form/ChipInput.tsx +129 -0
- package/source/Form/Chips.tsx +57 -37
- package/source/Form/Combobox.tsx +362 -78
- package/source/Form/Control.tsx +72 -42
- package/source/Form/DatePicker.tsx +245 -45
- package/source/Form/DatePickerPlugin.ts +37 -0
- package/source/Form/Feedback.tsx +21 -16
- package/source/Form/FloatingLabel.tsx +21 -7
- package/source/Form/InputGroup.tsx +162 -11
- package/source/Form/OTP.tsx +156 -45
- package/source/Form/PasswordStrength.tsx +43 -17
- package/source/Form/Plugin.tsx +53 -0
- package/source/Form/Primitives.tsx +70 -20
- package/source/Form/Radio.tsx +35 -40
- package/source/Form/Range.tsx +125 -56
- package/source/Form/Switch.tsx +53 -27
- package/source/Form/index.ts +1 -0
- package/source/Grid.tsx +68 -56
- package/source/Image.tsx +10 -6
- package/source/ListGroup.tsx +59 -58
- package/source/Menu.tsx +317 -46
- package/source/NativeDialog/index.tsx +148 -22
- package/source/Nav.tsx +49 -33
- package/source/NavOverflow/index.tsx +323 -0
- package/source/NavOverflow/model.ts +21 -0
- package/source/Navbar.tsx +65 -45
- package/source/Overlay.ts +163 -1
- package/source/Pagination.tsx +86 -0
- package/source/Placeholder.tsx +70 -0
- package/source/Polymorphic.ts +57 -0
- package/source/Popover.tsx +34 -26
- package/source/Progress/index.tsx +81 -0
- package/source/Progress/style.module.css +15 -0
- package/source/ScrollSpy/index.tsx +380 -0
- package/source/ScrollSpy/model.ts +17 -0
- package/source/Spinner.tsx +31 -17
- package/source/Stack.tsx +25 -0
- package/source/Stepper.tsx +91 -0
- package/source/Tab.tsx +261 -0
- package/source/Table.tsx +53 -42
- package/source/Toast.tsx +93 -46
- package/source/ToggleButton.tsx +47 -30
- package/source/Toggler.tsx +117 -0
- package/source/Tooltip.tsx +31 -24
- package/source/index.ts +17 -27
- package/source/CodeBlock.tsx +0 -40
- package/source/Editor.tsx +0 -43
- package/source/EditorHTML.tsx +0 -16
- package/source/HorizontalMarquee/index.tsx +0 -61
- package/source/Icon.tsx +0 -24
- package/source/Loading.tsx +0 -22
- package/source/OverlayBox.tsx +0 -208
- package/source/ScrollNav/index.tsx +0 -22
- package/source/Select.tsx +0 -95
- package/source/SpinnerButton.tsx +0 -35
- package/source/TableSpinner.tsx +0 -26
- package/source/TextTruncate/index.tsx +0 -25
- package/source/Time.tsx +0 -15
- package/source/TimeDistance.tsx +0 -41
- package/source/Timeline/index.tsx +0 -60
- package/source/TypeEcho/Content.tsx +0 -58
- package/source/TypeEcho/index.tsx +0 -14
- package/source/UserRank/Address.tsx +0 -20
- package/source/UserRank/index.tsx +0 -111
- package/source/VerticalMarquee/index.tsx +0 -25
- package/source/ZodiacBar.tsx +0 -51
- /package/source/{LiveTSX.tsx → Extension/LiveTSX.tsx} +0 -0
- /package/source/{ScrollNav → Extension/ScrollNav}/index.module.less +0 -0
- /package/source/{TextTruncate → Extension/TextTruncate}/Content.tsx +0 -0
- /package/source/{Timeline → Extension/Timeline}/index.module.less +0 -0
- /package/source/{UserRank → Extension/UserRank}/index.module.less +0 -0
- /package/source/{VerticalMarquee → Extension/VerticalMarquee}/index.module.less +0 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import classNames from 'classnames';
|
|
4
|
+
import { action, observable } from 'mobx';
|
|
5
|
+
import { observer } from 'mobx-react';
|
|
6
|
+
import {
|
|
7
|
+
cloneElement,
|
|
8
|
+
Component,
|
|
9
|
+
createRef,
|
|
10
|
+
ElementType,
|
|
11
|
+
FocusEvent,
|
|
12
|
+
HTMLAttributes,
|
|
13
|
+
isValidElement,
|
|
14
|
+
KeyboardEvent,
|
|
15
|
+
MouseEvent,
|
|
16
|
+
ReactElement,
|
|
17
|
+
ReactNode
|
|
18
|
+
} from 'react';
|
|
19
|
+
import { createPortal } from 'react-dom';
|
|
20
|
+
import { uniqueID } from 'web-utility';
|
|
21
|
+
|
|
22
|
+
import { FloatingOptions, FloatingPosition, OverlayContainer, OverlayPlacement } from '../Overlay';
|
|
23
|
+
import { AsProps, PolymorphicClassProps } from '../Polymorphic';
|
|
24
|
+
import { Popover } from '../Popover';
|
|
25
|
+
import { Tooltip } from '../Tooltip';
|
|
26
|
+
|
|
27
|
+
export type OverlayTrigger = 'click' | 'focus' | 'hover';
|
|
28
|
+
|
|
29
|
+
export interface OverlayBoxProps extends Omit<
|
|
30
|
+
HTMLAttributes<HTMLSpanElement>,
|
|
31
|
+
'children' | 'title'
|
|
32
|
+
> {
|
|
33
|
+
title: ReactNode;
|
|
34
|
+
detail?: ReactNode;
|
|
35
|
+
placement?: OverlayPlacement;
|
|
36
|
+
responsivePlacement?: FloatingOptions['responsivePlacement'];
|
|
37
|
+
container?: OverlayContainer;
|
|
38
|
+
offset?: FloatingOptions['offset'];
|
|
39
|
+
padding?: number;
|
|
40
|
+
flip?: boolean;
|
|
41
|
+
trigger?: OverlayTrigger | OverlayTrigger[];
|
|
42
|
+
delay?: number | { show: number; hide: number };
|
|
43
|
+
overlayClassName?: string;
|
|
44
|
+
children: ReactElement<{ 'aria-describedby'?: string }>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@observer
|
|
48
|
+
export class OverlayBox<T extends ElementType | undefined = 'span'> extends Component<
|
|
49
|
+
PolymorphicClassProps<T, OverlayBoxProps>
|
|
50
|
+
> {
|
|
51
|
+
declare readonly props: PolymorphicClassProps<T, OverlayBoxProps>;
|
|
52
|
+
|
|
53
|
+
protected get componentProps(): Readonly<OverlayBoxProps & AsProps> {
|
|
54
|
+
return this.props;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static displayName = 'OverlayBox';
|
|
58
|
+
|
|
59
|
+
@observable accessor open = false;
|
|
60
|
+
|
|
61
|
+
id = uniqueID();
|
|
62
|
+
root = createRef<HTMLSpanElement>();
|
|
63
|
+
overlay = createRef<HTMLDivElement>();
|
|
64
|
+
position = new FloatingPosition();
|
|
65
|
+
timer?: ReturnType<typeof setTimeout>;
|
|
66
|
+
|
|
67
|
+
@action
|
|
68
|
+
setOpen = (open: boolean) => (this.open = open);
|
|
69
|
+
|
|
70
|
+
hide = () => {
|
|
71
|
+
clearTimeout(this.timer);
|
|
72
|
+
this.setOpen(false);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
setOpenAfterDelay = (open: boolean) => {
|
|
76
|
+
clearTimeout(this.timer);
|
|
77
|
+
|
|
78
|
+
const { delay } = this.componentProps;
|
|
79
|
+
const timeout = typeof delay === 'number' ? delay : delay?.[open ? 'show' : 'hide'];
|
|
80
|
+
|
|
81
|
+
if (!timeout) return this.setOpen(open);
|
|
82
|
+
|
|
83
|
+
this.timer = setTimeout(() => this.setOpen(open), timeout);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
componentWillUnmount() {
|
|
87
|
+
document.removeEventListener('pointerdown', this.handleOutside);
|
|
88
|
+
clearTimeout(this.timer);
|
|
89
|
+
this.position.dispose();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
componentDidMount() {
|
|
93
|
+
document.addEventListener('pointerdown', this.handleOutside);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
componentDidUpdate(previous: OverlayBoxProps) {
|
|
97
|
+
const { open, root, overlay, props, position } = this;
|
|
98
|
+
const {
|
|
99
|
+
placement = 'top',
|
|
100
|
+
responsivePlacement,
|
|
101
|
+
offset = props.detail == null ? 8 : 12,
|
|
102
|
+
padding,
|
|
103
|
+
flip,
|
|
104
|
+
container
|
|
105
|
+
} = props;
|
|
106
|
+
|
|
107
|
+
if (!open) return position.dispose();
|
|
108
|
+
if (!root.current || !overlay.current) return;
|
|
109
|
+
if (
|
|
110
|
+
position.element !== overlay.current ||
|
|
111
|
+
previous.placement !== props.placement ||
|
|
112
|
+
previous.responsivePlacement !== responsivePlacement ||
|
|
113
|
+
previous.offset !== props.offset ||
|
|
114
|
+
previous.padding !== padding ||
|
|
115
|
+
previous.flip !== flip ||
|
|
116
|
+
previous.container !== container
|
|
117
|
+
)
|
|
118
|
+
position.attach(root.current, overlay.current, {
|
|
119
|
+
placement,
|
|
120
|
+
responsivePlacement,
|
|
121
|
+
offset,
|
|
122
|
+
padding,
|
|
123
|
+
flip
|
|
124
|
+
});
|
|
125
|
+
else position.update();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
contains = (target: EventTarget | null) =>
|
|
129
|
+
target instanceof Node &&
|
|
130
|
+
(!!this.root.current?.contains(target) || !!this.overlay.current?.contains(target));
|
|
131
|
+
|
|
132
|
+
hasTrigger = (trigger: OverlayTrigger) => {
|
|
133
|
+
const triggers: OverlayTrigger[] = this.componentProps.trigger
|
|
134
|
+
? Array.isArray(this.componentProps.trigger)
|
|
135
|
+
? this.componentProps.trigger
|
|
136
|
+
: [this.componentProps.trigger]
|
|
137
|
+
: ['hover', 'focus'];
|
|
138
|
+
|
|
139
|
+
return triggers.includes(trigger);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
handleMouseEnter = (event: MouseEvent<HTMLSpanElement>) => {
|
|
143
|
+
this.componentProps.onMouseEnter?.(event);
|
|
144
|
+
|
|
145
|
+
if (!event.defaultPrevented && this.hasTrigger('hover')) this.setOpenAfterDelay(true);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
handleMouseLeave = (event: MouseEvent<HTMLSpanElement>) => {
|
|
149
|
+
this.componentProps.onMouseLeave?.(event);
|
|
150
|
+
|
|
151
|
+
if (
|
|
152
|
+
!event.defaultPrevented &&
|
|
153
|
+
this.hasTrigger('hover') &&
|
|
154
|
+
!this.contains(event.relatedTarget) &&
|
|
155
|
+
!this.contains(document.activeElement)
|
|
156
|
+
)
|
|
157
|
+
this.setOpenAfterDelay(false);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
handleFocus = (event: FocusEvent<HTMLSpanElement>) => {
|
|
161
|
+
this.componentProps.onFocus?.(event);
|
|
162
|
+
|
|
163
|
+
if (!event.defaultPrevented && this.hasTrigger('focus')) this.setOpenAfterDelay(true);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
handleBlur = (event: FocusEvent<HTMLSpanElement>) => {
|
|
167
|
+
this.componentProps.onBlur?.(event);
|
|
168
|
+
|
|
169
|
+
if (
|
|
170
|
+
!event.defaultPrevented &&
|
|
171
|
+
this.hasTrigger('focus') &&
|
|
172
|
+
!this.contains(event.relatedTarget)
|
|
173
|
+
)
|
|
174
|
+
this.setOpenAfterDelay(false);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
handleClick = (event: MouseEvent<HTMLSpanElement>) => {
|
|
178
|
+
this.componentProps.onClick?.(event);
|
|
179
|
+
|
|
180
|
+
if (
|
|
181
|
+
!event.defaultPrevented &&
|
|
182
|
+
this.hasTrigger('click') &&
|
|
183
|
+
!(event.target instanceof Node && this.overlay.current?.contains(event.target))
|
|
184
|
+
)
|
|
185
|
+
this.setOpenAfterDelay(!this.open);
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
handleOutside = ({ target }: PointerEvent) => {
|
|
189
|
+
if (target instanceof Node && !this.contains(target)) this.hide();
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
handleKeyDown = (event: KeyboardEvent<HTMLSpanElement>) => {
|
|
193
|
+
this.componentProps.onKeyDown?.(event);
|
|
194
|
+
|
|
195
|
+
if (!event.defaultPrevented && event.key === 'Escape' && this.open) {
|
|
196
|
+
event.preventDefault();
|
|
197
|
+
this.hide();
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
render() {
|
|
202
|
+
const Root: ElementType = this.componentProps.as ?? 'span';
|
|
203
|
+
|
|
204
|
+
const {
|
|
205
|
+
as: _as,
|
|
206
|
+
title,
|
|
207
|
+
detail,
|
|
208
|
+
placement = 'top',
|
|
209
|
+
responsivePlacement: _responsivePlacement,
|
|
210
|
+
container,
|
|
211
|
+
offset: _offset,
|
|
212
|
+
padding: _padding,
|
|
213
|
+
flip: _flip,
|
|
214
|
+
trigger: _trigger,
|
|
215
|
+
delay: _delay,
|
|
216
|
+
overlayClassName,
|
|
217
|
+
children,
|
|
218
|
+
className = '',
|
|
219
|
+
onMouseEnter: _onMouseEnter,
|
|
220
|
+
onMouseLeave: _onMouseLeave,
|
|
221
|
+
onFocus: _onFocus,
|
|
222
|
+
onBlur: _onBlur,
|
|
223
|
+
onClick: _onClick,
|
|
224
|
+
onKeyDown: _onKeyDown,
|
|
225
|
+
...props
|
|
226
|
+
} = this.componentProps;
|
|
227
|
+
const id = this.open ? this.id : undefined;
|
|
228
|
+
const direction = this.position.element
|
|
229
|
+
? (this.position.placement.split('-')[0] as OverlayPlacement)
|
|
230
|
+
: placement;
|
|
231
|
+
const overlayClass = classNames('w-max', overlayClassName);
|
|
232
|
+
const overlay =
|
|
233
|
+
detail != null ? (
|
|
234
|
+
<Popover
|
|
235
|
+
ref={this.overlay}
|
|
236
|
+
id={id}
|
|
237
|
+
style={{ ...this.position.style }}
|
|
238
|
+
arrowStyle={{ ...this.position.arrowStyle }}
|
|
239
|
+
className={overlayClass}
|
|
240
|
+
title={title}
|
|
241
|
+
placement={direction}
|
|
242
|
+
>
|
|
243
|
+
{detail}
|
|
244
|
+
</Popover>
|
|
245
|
+
) : (
|
|
246
|
+
<Tooltip
|
|
247
|
+
ref={this.overlay}
|
|
248
|
+
id={id}
|
|
249
|
+
style={{ ...this.position.style }}
|
|
250
|
+
arrowStyle={{ ...this.position.arrowStyle }}
|
|
251
|
+
className={overlayClass}
|
|
252
|
+
placement={direction}
|
|
253
|
+
>
|
|
254
|
+
{title}
|
|
255
|
+
</Tooltip>
|
|
256
|
+
);
|
|
257
|
+
const target =
|
|
258
|
+
this.open && container
|
|
259
|
+
? typeof container === 'string'
|
|
260
|
+
? document.querySelector<HTMLElement>(container)
|
|
261
|
+
: typeof container === 'function'
|
|
262
|
+
? container()
|
|
263
|
+
: container
|
|
264
|
+
: undefined;
|
|
265
|
+
if (this.open && container && !target) throw new Error('Overlay container was not found');
|
|
266
|
+
|
|
267
|
+
const trigger = isValidElement(children)
|
|
268
|
+
? cloneElement(children, {
|
|
269
|
+
'aria-describedby':
|
|
270
|
+
[children.props['aria-describedby'], id].filter(Boolean).join(' ') ||
|
|
271
|
+
undefined
|
|
272
|
+
})
|
|
273
|
+
: children;
|
|
274
|
+
|
|
275
|
+
return (
|
|
276
|
+
<Root
|
|
277
|
+
{...props}
|
|
278
|
+
ref={this.root}
|
|
279
|
+
className={`position-relative d-inline-block ${className}`}
|
|
280
|
+
onMouseEnter={this.handleMouseEnter}
|
|
281
|
+
onMouseLeave={this.handleMouseLeave}
|
|
282
|
+
onFocus={this.handleFocus}
|
|
283
|
+
onBlur={this.handleBlur}
|
|
284
|
+
onClick={this.handleClick}
|
|
285
|
+
onKeyDown={this.handleKeyDown}
|
|
286
|
+
>
|
|
287
|
+
{trigger}
|
|
288
|
+
|
|
289
|
+
{this.open && (target ? createPortal(overlay, target) : overlay)}
|
|
290
|
+
</Root>
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
+
import classNames from 'classnames';
|
|
3
4
|
import { action, computed, observable } from 'mobx';
|
|
4
5
|
import { observer } from 'mobx-react';
|
|
5
|
-
import { Component } from 'react';
|
|
6
|
+
import { Component, ElementType } from 'react';
|
|
6
7
|
import { scrollTo, uniqueID } from 'web-utility';
|
|
7
8
|
|
|
8
|
-
import { Nav, NavLink, NavProps } from '
|
|
9
|
+
import { Nav, NavLink, NavProps } from '../Nav';
|
|
10
|
+
import { AsProps, PolymorphicClassProps } from '../Polymorphic';
|
|
9
11
|
|
|
10
12
|
const HeadingSelector = Array.from({ length: 6 }, (_, index) => `h${index + 1}`).join(',');
|
|
11
13
|
|
|
12
14
|
type HeadingMeta = Record<'id' | 'text', string> & Record<'level' | 'top', number>;
|
|
13
15
|
|
|
14
|
-
export interface PageNavProps extends NavProps {
|
|
16
|
+
export interface PageNavProps extends Omit<NavProps, 'ref'> {
|
|
15
17
|
depth?: number;
|
|
16
18
|
minDepth?: number;
|
|
17
19
|
rootSelector?: string;
|
|
@@ -19,7 +21,15 @@ export interface PageNavProps extends NavProps {
|
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
@observer
|
|
22
|
-
export class PageNav extends Component<
|
|
24
|
+
export class PageNav<T extends ElementType | undefined = 'nav'> extends Component<
|
|
25
|
+
PolymorphicClassProps<T, PageNavProps>
|
|
26
|
+
> {
|
|
27
|
+
declare readonly props: PolymorphicClassProps<T, PageNavProps>;
|
|
28
|
+
|
|
29
|
+
protected get componentProps(): Readonly<PageNavProps & AsProps> {
|
|
30
|
+
return this.props;
|
|
31
|
+
}
|
|
32
|
+
|
|
23
33
|
static displayName = 'PageNav';
|
|
24
34
|
|
|
25
35
|
@observable
|
|
@@ -33,17 +43,29 @@ export class PageNav extends Component<PageNavProps> {
|
|
|
33
43
|
const { list, scrollY } = this;
|
|
34
44
|
const index = list.findIndex(({ top }) => top > scrollY);
|
|
35
45
|
|
|
36
|
-
return list[index - 1]?.id;
|
|
46
|
+
return list[index < 0 ? list.length - 1 : index - 1]?.id;
|
|
37
47
|
}
|
|
38
48
|
|
|
39
|
-
updateScrollY = action(() =>
|
|
49
|
+
updateScrollY = action(() => {
|
|
50
|
+
this.scrollY = window.scrollY;
|
|
51
|
+
|
|
52
|
+
this.list = this.list.map(item => {
|
|
53
|
+
const element = document.getElementById(item.id);
|
|
54
|
+
|
|
55
|
+
return element
|
|
56
|
+
? { ...item, top: element.getBoundingClientRect().top + this.scrollY }
|
|
57
|
+
: item;
|
|
58
|
+
});
|
|
59
|
+
});
|
|
40
60
|
|
|
41
61
|
componentDidMount() {
|
|
42
62
|
if (!globalThis.document) return;
|
|
43
63
|
|
|
44
64
|
window.addEventListener('scroll', this.updateScrollY);
|
|
65
|
+
window.addEventListener('resize', this.updateScrollY);
|
|
45
66
|
|
|
46
67
|
this.initList();
|
|
68
|
+
this.updateScrollY();
|
|
47
69
|
}
|
|
48
70
|
|
|
49
71
|
static slugOf(text: string) {
|
|
@@ -57,7 +79,7 @@ export class PageNav extends Component<PageNavProps> {
|
|
|
57
79
|
|
|
58
80
|
@action
|
|
59
81
|
initList() {
|
|
60
|
-
const { rootSelector } = this.
|
|
82
|
+
const { rootSelector } = this.componentProps;
|
|
61
83
|
const root = rootSelector ? document.querySelector(rootSelector) : document;
|
|
62
84
|
|
|
63
85
|
if (!root) return;
|
|
@@ -69,34 +91,39 @@ export class PageNav extends Component<PageNavProps> {
|
|
|
69
91
|
|
|
70
92
|
element.id ||= PageNav.slugOf(text);
|
|
71
93
|
|
|
72
|
-
const { id, tagName
|
|
94
|
+
const { id, tagName } = element;
|
|
95
|
+
const top = element.getBoundingClientRect().top + window.scrollY;
|
|
73
96
|
|
|
74
|
-
return { id, level: +tagName[1], text, top
|
|
97
|
+
return { id, level: +tagName[1], text, top };
|
|
75
98
|
}
|
|
76
99
|
);
|
|
77
100
|
}
|
|
78
101
|
|
|
79
102
|
componentWillUnmount(): void {
|
|
80
|
-
|
|
103
|
+
window.removeEventListener('scroll', this.updateScrollY);
|
|
104
|
+
window.removeEventListener('resize', this.updateScrollY);
|
|
81
105
|
}
|
|
82
106
|
|
|
83
107
|
scrollTo = (meta: HeadingMeta) => () => {
|
|
84
108
|
setTimeout(() => scrollTo(`[id="${meta.id}"]`));
|
|
85
109
|
|
|
86
|
-
this.
|
|
110
|
+
this.componentProps.onItemClick?.(meta);
|
|
87
111
|
};
|
|
88
112
|
|
|
89
113
|
renderLink = ({ id, level, text, top }: HeadingMeta) => {
|
|
90
114
|
const { currentActiveId } = this,
|
|
91
|
-
{ depth = Infinity, minDepth = 1 } = this.
|
|
115
|
+
{ depth = Infinity, minDepth = 1 } = this.componentProps;
|
|
92
116
|
|
|
93
117
|
return (
|
|
94
118
|
minDepth <= level &&
|
|
95
119
|
level <= depth && (
|
|
96
120
|
<NavLink
|
|
97
121
|
key={id}
|
|
98
|
-
className={
|
|
99
|
-
|
|
122
|
+
className={classNames(
|
|
123
|
+
'justify-content-start text-wrap min-w-0',
|
|
124
|
+
currentActiveId === id && 'active'
|
|
125
|
+
)}
|
|
126
|
+
style={{ paddingInlineStart: `${level - minDepth + 0.75}rem` }}
|
|
100
127
|
href={`#${id}`}
|
|
101
128
|
aria-current={currentActiveId === id ? 'location' : undefined}
|
|
102
129
|
onClick={this.scrollTo({ id, level, text, top })}
|
|
@@ -115,10 +142,10 @@ export class PageNav extends Component<PageNavProps> {
|
|
|
115
142
|
rootSelector: _rootSelector,
|
|
116
143
|
onItemClick: _onItemClick,
|
|
117
144
|
...props
|
|
118
|
-
} = this.
|
|
145
|
+
} = this.componentProps;
|
|
119
146
|
|
|
120
147
|
return (
|
|
121
|
-
<Nav {...props} variant="underline" vertical>
|
|
148
|
+
<Nav as={this.componentProps.as} {...props} variant="underline" vertical>
|
|
122
149
|
{list.map(this.renderLink)}
|
|
123
150
|
</Nav>
|
|
124
151
|
);
|
|
@@ -2,18 +2,28 @@
|
|
|
2
2
|
|
|
3
3
|
import { action, computed, observable } from 'mobx';
|
|
4
4
|
import { observer } from 'mobx-react';
|
|
5
|
-
import { Component, createRef, ReactNode } from 'react';
|
|
5
|
+
import { Component, createRef, ElementType, ReactNode, Ref } from 'react';
|
|
6
6
|
|
|
7
|
+
import { AsProps, PolymorphicClassProps } from '../../Polymorphic';
|
|
7
8
|
import { Icon } from '../Icon';
|
|
8
9
|
import * as style from './index.module.less';
|
|
9
10
|
|
|
10
11
|
export interface ScrollNavScrollerProps {
|
|
11
12
|
className?: string;
|
|
12
13
|
children: ReactNode;
|
|
14
|
+
rootRef?: Ref<HTMLElement>;
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
@observer
|
|
16
|
-
export class ScrollNavScroller extends Component<
|
|
18
|
+
export class ScrollNavScroller<T extends ElementType | undefined = 'div'> extends Component<
|
|
19
|
+
PolymorphicClassProps<T, ScrollNavScrollerProps>
|
|
20
|
+
> {
|
|
21
|
+
declare readonly props: PolymorphicClassProps<T, ScrollNavScrollerProps>;
|
|
22
|
+
|
|
23
|
+
protected get componentProps(): Readonly<ScrollNavScrollerProps & AsProps> {
|
|
24
|
+
return this.props;
|
|
25
|
+
}
|
|
26
|
+
|
|
17
27
|
scrollerRef = createRef<HTMLDivElement>();
|
|
18
28
|
|
|
19
29
|
@observable
|
|
@@ -73,11 +83,17 @@ export class ScrollNavScroller extends Component<ScrollNavScrollerProps> {
|
|
|
73
83
|
scrollForward = () => this.scrollBy(1);
|
|
74
84
|
|
|
75
85
|
render() {
|
|
86
|
+
const Root: ElementType = this.componentProps.as ?? 'div';
|
|
87
|
+
|
|
76
88
|
const { canScrollLeft, canScrollRight } = this;
|
|
77
|
-
const { className = '', children } = this.
|
|
89
|
+
const { as: _as, className = '', children, rootRef, ...props } = this.componentProps;
|
|
78
90
|
|
|
79
91
|
return (
|
|
80
|
-
<
|
|
92
|
+
<Root
|
|
93
|
+
{...props}
|
|
94
|
+
ref={rootRef}
|
|
95
|
+
className={`position-relative overflow-y-hidden ${className}`}
|
|
96
|
+
>
|
|
81
97
|
{canScrollLeft && (
|
|
82
98
|
<button
|
|
83
99
|
type="button"
|
|
@@ -101,7 +117,7 @@ export class ScrollNavScroller extends Component<ScrollNavScrollerProps> {
|
|
|
101
117
|
<Icon name="caret-right-fill" />
|
|
102
118
|
</button>
|
|
103
119
|
)}
|
|
104
|
-
</
|
|
120
|
+
</Root>
|
|
105
121
|
);
|
|
106
122
|
}
|
|
107
123
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ElementType } from 'react';
|
|
2
|
+
|
|
3
|
+
import { Nav, NavProps } from '../../Nav';
|
|
4
|
+
import { polymorphic } from '../../Polymorphic';
|
|
5
|
+
import { ScrollNavScroller } from './Scroller';
|
|
6
|
+
|
|
7
|
+
export type ScrollNavProps = NavProps;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A horizontal scrolling navigation bar based on Bootstrap's `nav-scroller` pattern.
|
|
11
|
+
* Automatically shows scroll arrow buttons when nav items overflow the container width.
|
|
12
|
+
*
|
|
13
|
+
* @see {@link https://github.com/twbs/bootstrap/blob/main/site/src/assets/examples/offcanvas-navbar/index.astro#L50-L65}
|
|
14
|
+
*/
|
|
15
|
+
export const ScrollNav = polymorphic<ScrollNavProps>(
|
|
16
|
+
({
|
|
17
|
+
as: RootAs,
|
|
18
|
+
className = '',
|
|
19
|
+
children,
|
|
20
|
+
ref,
|
|
21
|
+
variant,
|
|
22
|
+
vertical,
|
|
23
|
+
fill,
|
|
24
|
+
justified,
|
|
25
|
+
cardHeader,
|
|
26
|
+
navbar,
|
|
27
|
+
...props
|
|
28
|
+
}) => (
|
|
29
|
+
<ScrollNavScroller<ElementType>
|
|
30
|
+
as={RootAs}
|
|
31
|
+
{...(RootAs ? props : {})}
|
|
32
|
+
rootRef={RootAs ? ref : undefined}
|
|
33
|
+
className={className}
|
|
34
|
+
>
|
|
35
|
+
<Nav
|
|
36
|
+
{...(RootAs ? {} : props)}
|
|
37
|
+
ref={RootAs ? undefined : ref}
|
|
38
|
+
className="flex-nowrap"
|
|
39
|
+
{...{ variant, vertical, fill, justified, cardHeader, navbar }}
|
|
40
|
+
>
|
|
41
|
+
{children}
|
|
42
|
+
</Nav>
|
|
43
|
+
</ScrollNavScroller>
|
|
44
|
+
)
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
ScrollNav.displayName = 'ScrollNav';
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import classNames from 'classnames';
|
|
4
|
+
import {
|
|
5
|
+
ButtonHTMLAttributes,
|
|
6
|
+
Children,
|
|
7
|
+
cloneElement,
|
|
8
|
+
Component,
|
|
9
|
+
isValidElement,
|
|
10
|
+
MouseEvent,
|
|
11
|
+
ReactElement,
|
|
12
|
+
ReactNode
|
|
13
|
+
} from 'react';
|
|
14
|
+
|
|
15
|
+
import { ButtonProps } from '../Button';
|
|
16
|
+
import { Menu } from '../Menu';
|
|
17
|
+
import { polymorphic } from '../Polymorphic';
|
|
18
|
+
|
|
19
|
+
export interface OptionProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
|
20
|
+
value?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const Option = polymorphic<OptionProps>(
|
|
24
|
+
({ as: Root = 'button', value, className, children, ...props }) => (
|
|
25
|
+
<Root
|
|
26
|
+
{...props}
|
|
27
|
+
type="button"
|
|
28
|
+
className={classNames('menu-item', className)}
|
|
29
|
+
data-value={value}
|
|
30
|
+
>
|
|
31
|
+
{children}
|
|
32
|
+
</Root>
|
|
33
|
+
)
|
|
34
|
+
);
|
|
35
|
+
Option.displayName = 'Option';
|
|
36
|
+
|
|
37
|
+
export type SelectProps = Pick<
|
|
38
|
+
ButtonProps,
|
|
39
|
+
'className' | 'style' | 'size' | 'theme' | 'variant' | 'disabled' | 'children'
|
|
40
|
+
> & {
|
|
41
|
+
align?: 'start' | 'end';
|
|
42
|
+
menuClassName?: string;
|
|
43
|
+
value?: string;
|
|
44
|
+
name?: string;
|
|
45
|
+
placeholder?: ReactNode;
|
|
46
|
+
'aria-label'?: string;
|
|
47
|
+
onChange?: (value: string) => unknown;
|
|
48
|
+
valueRender?: (value: string) => ReactNode;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export class Select extends Component<SelectProps> {
|
|
52
|
+
static displayName = 'Select';
|
|
53
|
+
|
|
54
|
+
private handleSelect = ({ target, defaultPrevented }: MouseEvent<HTMLDivElement>) => {
|
|
55
|
+
if (defaultPrevented || this.props.disabled || !(target instanceof Element)) return;
|
|
56
|
+
|
|
57
|
+
const option = target.closest<HTMLButtonElement>('[data-value]');
|
|
58
|
+
|
|
59
|
+
if (option && !option.disabled && option.getAttribute('aria-disabled') !== 'true')
|
|
60
|
+
this.props.onChange?.(option.dataset.value ?? '');
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
private renderOption = (child: ReactNode) =>
|
|
64
|
+
isValidElement<OptionProps>(child) && child.type === Option
|
|
65
|
+
? cloneElement(child, {
|
|
66
|
+
className: classNames(
|
|
67
|
+
child.props.className,
|
|
68
|
+
child.props.value === this.props.value && 'selected'
|
|
69
|
+
),
|
|
70
|
+
'aria-selected': child.props.value === this.props.value
|
|
71
|
+
})
|
|
72
|
+
: child;
|
|
73
|
+
|
|
74
|
+
render() {
|
|
75
|
+
const {
|
|
76
|
+
align,
|
|
77
|
+
className,
|
|
78
|
+
menuClassName,
|
|
79
|
+
children,
|
|
80
|
+
theme = 'primary',
|
|
81
|
+
variant = 'solid',
|
|
82
|
+
size,
|
|
83
|
+
disabled,
|
|
84
|
+
style,
|
|
85
|
+
value,
|
|
86
|
+
onChange: _onChange,
|
|
87
|
+
valueRender,
|
|
88
|
+
name,
|
|
89
|
+
placeholder,
|
|
90
|
+
'aria-label': label
|
|
91
|
+
} = this.props;
|
|
92
|
+
const current = Children.toArray(children).find(
|
|
93
|
+
node =>
|
|
94
|
+
isValidElement<OptionProps>(node) &&
|
|
95
|
+
node.type === Option &&
|
|
96
|
+
node.props.value === value
|
|
97
|
+
) as ReactElement<OptionProps> | undefined;
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<>
|
|
101
|
+
<Menu
|
|
102
|
+
align={align}
|
|
103
|
+
className="w-100"
|
|
104
|
+
menuClassName={menuClassName}
|
|
105
|
+
label={valueRender?.(value ?? '') ?? current?.props.children ?? placeholder}
|
|
106
|
+
toggleProps={{
|
|
107
|
+
className: classNames(
|
|
108
|
+
'w-100 d-flex justify-content-between align-items-center',
|
|
109
|
+
className
|
|
110
|
+
),
|
|
111
|
+
theme,
|
|
112
|
+
variant,
|
|
113
|
+
size,
|
|
114
|
+
disabled,
|
|
115
|
+
style,
|
|
116
|
+
'aria-label': label
|
|
117
|
+
}}
|
|
118
|
+
onClick={this.handleSelect}
|
|
119
|
+
>
|
|
120
|
+
{Children.map(children, this.renderOption)}
|
|
121
|
+
</Menu>
|
|
122
|
+
{name && (
|
|
123
|
+
<input type="hidden" name={name} value={value ?? ''} disabled={disabled} />
|
|
124
|
+
)}
|
|
125
|
+
</>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -6,7 +6,7 @@ import { observer } from 'mobx-react';
|
|
|
6
6
|
import { Component, createRef, HTMLAttributes, MouseEvent } from 'react';
|
|
7
7
|
import { blobOf } from 'web-utility';
|
|
8
8
|
|
|
9
|
-
import { Image } from '
|
|
9
|
+
import { Image } from '../Image';
|
|
10
10
|
import { Loading } from './Loading';
|
|
11
11
|
|
|
12
12
|
export async function elementToImage(element: HTMLElement, type?: string, quality?: number) {
|