idea-react 3.0.0-alpha.0 → 3.0.0-beta.0
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 +31 -10
- package/ReadMe.md +75 -41
- package/dist/index.d.mts +451 -100
- package/dist/index.d.ts +451 -100
- package/dist/index.js +16 -16
- package/dist/index.mjs +15 -15
- package/dist/style.css +5 -5
- package/package.json +7 -8
- package/source/Avatar.tsx +65 -12
- package/source/Button.tsx +13 -4
- package/source/ButtonGroup.tsx +28 -0
- package/source/Card.tsx +34 -0
- package/source/ClickBoundary.tsx +6 -5
- package/source/CloseButton.tsx +23 -0
- package/source/CodeBlock.tsx +5 -3
- package/source/Countdown.tsx +2 -0
- package/source/Drawer.tsx +62 -0
- package/source/Editor.tsx +5 -8
- package/source/Form/Adorn.tsx +38 -0
- package/source/Form/Checkbox.tsx +73 -0
- package/source/Form/Chips.tsx +51 -0
- package/source/Form/Combobox.tsx +109 -0
- package/source/Form/Control.tsx +59 -0
- package/source/Form/DatePicker.tsx +89 -0
- package/source/Form/Feedback.tsx +29 -0
- package/source/Form/FloatingLabel.tsx +15 -0
- package/source/Form/InputGroup.tsx +23 -0
- package/source/Form/OTP.tsx +48 -0
- package/source/Form/PasswordStrength.tsx +157 -0
- package/source/Form/Primitives.tsx +46 -0
- package/source/Form/Radio.tsx +51 -0
- package/source/Form/Range.tsx +65 -0
- package/source/Form/Switch.tsx +35 -0
- package/source/Form/index.ts +15 -0
- package/source/Grid.tsx +92 -0
- package/source/HorizontalMarquee/index.tsx +4 -10
- package/source/Image.tsx +15 -0
- package/source/ListGroup.tsx +103 -0
- package/source/LiveTSX.tsx +1 -1
- package/source/Loading.tsx +4 -3
- package/source/Menu.tsx +99 -1
- package/source/MonthCalendar.tsx +60 -22
- package/source/NativeDialog/index.tsx +37 -1
- package/source/Nav.tsx +52 -0
- package/source/Navbar.tsx +58 -1
- package/source/Overlay.ts +22 -0
- package/source/OverlayBox.tsx +200 -32
- package/source/PageNav.tsx +56 -32
- package/source/Popover.tsx +36 -0
- package/source/ScrollNav/Scroller.tsx +107 -0
- package/source/ScrollNav/index.tsx +12 -99
- package/source/Select.tsx +66 -55
- package/source/ShareBox.tsx +5 -11
- package/source/Spinner.tsx +30 -0
- package/source/SpinnerButton.tsx +8 -10
- package/source/Table.tsx +59 -0
- package/source/TableSpinner.tsx +7 -5
- package/source/{TextTruncate.tsx → TextTruncate/Content.tsx} +24 -20
- package/source/TextTruncate/index.tsx +25 -0
- package/source/Time.tsx +7 -33
- package/source/TimeDistance.tsx +1 -1
- package/source/Timeline/index.tsx +7 -7
- package/source/Toast.tsx +120 -29
- package/source/ToggleButton.tsx +47 -0
- package/source/Tooltip.tsx +32 -0
- package/source/TypeEcho/Content.tsx +58 -0
- package/source/TypeEcho/index.tsx +14 -0
- package/source/UserRank/index.module.less +9 -20
- package/source/UserRank/index.tsx +35 -32
- package/source/VerticalMarquee/index.module.less +0 -1
- package/source/VerticalMarquee/index.tsx +3 -5
- package/source/ZodiacBar.tsx +1 -1
- package/source/animate.ts +4 -2
- package/source/color.ts +3 -5
- package/source/index.ts +14 -0
- package/source/TypeEcho.tsx +0 -49
package/source/OverlayBox.tsx
CHANGED
|
@@ -1,40 +1,208 @@
|
|
|
1
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import classNames from 'classnames';
|
|
4
|
+
import { action, observable } from 'mobx';
|
|
5
|
+
import { observer } from 'mobx-react';
|
|
2
6
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
cloneElement,
|
|
8
|
+
Component,
|
|
9
|
+
createRef,
|
|
10
|
+
FocusEvent,
|
|
11
|
+
HTMLAttributes,
|
|
12
|
+
isValidElement,
|
|
13
|
+
KeyboardEvent,
|
|
14
|
+
MouseEvent,
|
|
15
|
+
ReactElement,
|
|
16
|
+
ReactNode
|
|
17
|
+
} from 'react';
|
|
18
|
+
import { uniqueID } from 'web-utility';
|
|
19
|
+
|
|
20
|
+
import { OverlayPlacement } from './Overlay';
|
|
21
|
+
import { Popover } from './Popover';
|
|
22
|
+
import { Tooltip } from './Tooltip';
|
|
23
|
+
|
|
24
|
+
export type OverlayTrigger = 'click' | 'focus' | 'hover';
|
|
8
25
|
|
|
9
|
-
export interface OverlayBoxProps extends Omit<
|
|
26
|
+
export interface OverlayBoxProps extends Omit<
|
|
27
|
+
HTMLAttributes<HTMLSpanElement>,
|
|
28
|
+
'children' | 'title'
|
|
29
|
+
> {
|
|
10
30
|
title: ReactNode;
|
|
11
31
|
detail?: ReactNode;
|
|
32
|
+
placement?: OverlayPlacement;
|
|
33
|
+
trigger?: OverlayTrigger | OverlayTrigger[];
|
|
34
|
+
delay?: number | { show: number; hide: number };
|
|
35
|
+
overlayClassName?: string;
|
|
36
|
+
children: ReactElement<{ 'aria-describedby'?: string }>;
|
|
12
37
|
}
|
|
13
38
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
39
|
+
const PLACEMENT_CLASSES: Record<OverlayPlacement, string> = {
|
|
40
|
+
top: 'bottom-100 start-50 translate-middle-x',
|
|
41
|
+
right: 'top-50 start-100 translate-middle-y',
|
|
42
|
+
bottom: 'top-100 start-50 translate-middle-x',
|
|
43
|
+
left: 'top-50 end-100 translate-middle-y'
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const OVERLAY_GAPS: Record<OverlayPlacement, Record<'popover' | 'tooltip', string>> = {
|
|
47
|
+
top: { popover: 'mb-3', tooltip: 'mb-2' },
|
|
48
|
+
right: { popover: 'ms-3', tooltip: 'ms-2' },
|
|
49
|
+
bottom: { popover: 'mt-3', tooltip: 'mt-2' },
|
|
50
|
+
left: { popover: 'me-3', tooltip: 'me-2' }
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
@observer
|
|
54
|
+
export class OverlayBox extends Component<OverlayBoxProps> {
|
|
55
|
+
static displayName = 'OverlayBox';
|
|
56
|
+
|
|
57
|
+
@observable accessor open = false;
|
|
58
|
+
|
|
59
|
+
id = uniqueID();
|
|
60
|
+
root = createRef<HTMLSpanElement>();
|
|
61
|
+
timer?: ReturnType<typeof setTimeout>;
|
|
62
|
+
|
|
63
|
+
@action
|
|
64
|
+
setOpen = (open: boolean) => (this.open = open);
|
|
65
|
+
|
|
66
|
+
hide = () => {
|
|
67
|
+
clearTimeout(this.timer);
|
|
68
|
+
this.setOpen(false);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
setOpenAfterDelay = (open: boolean) => {
|
|
72
|
+
clearTimeout(this.timer);
|
|
73
|
+
|
|
74
|
+
const { delay } = this.props;
|
|
75
|
+
const timeout = typeof delay === 'number' ? delay : delay?.[open ? 'show' : 'hide'];
|
|
76
|
+
|
|
77
|
+
if (!timeout) return this.setOpen(open);
|
|
78
|
+
|
|
79
|
+
this.timer = setTimeout(() => this.setOpen(open), timeout);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
componentWillUnmount() {
|
|
83
|
+
document.removeEventListener('pointerdown', this.handleOutside);
|
|
84
|
+
clearTimeout(this.timer);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
componentDidMount() {
|
|
88
|
+
document.addEventListener('pointerdown', this.handleOutside);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
hasTrigger = (trigger: OverlayTrigger) => {
|
|
92
|
+
const triggers: OverlayTrigger[] = this.props.trigger
|
|
93
|
+
? Array.isArray(this.props.trigger)
|
|
94
|
+
? this.props.trigger
|
|
95
|
+
: [this.props.trigger]
|
|
96
|
+
: ['hover', 'focus'];
|
|
97
|
+
|
|
98
|
+
return triggers.includes(trigger);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
handleMouseEnter = (event: MouseEvent<HTMLSpanElement>) => {
|
|
102
|
+
this.props.onMouseEnter?.(event);
|
|
103
|
+
|
|
104
|
+
if (this.hasTrigger('hover')) this.setOpenAfterDelay(true);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
handleMouseLeave = (event: MouseEvent<HTMLSpanElement>) => {
|
|
108
|
+
this.props.onMouseLeave?.(event);
|
|
109
|
+
|
|
110
|
+
if (this.hasTrigger('hover') && !this.root.current?.contains(document.activeElement))
|
|
111
|
+
this.setOpenAfterDelay(false);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
handleFocus = (event: FocusEvent<HTMLSpanElement>) => {
|
|
115
|
+
this.props.onFocus?.(event);
|
|
116
|
+
|
|
117
|
+
if (this.hasTrigger('focus')) this.setOpenAfterDelay(true);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
handleBlur = (event: FocusEvent<HTMLSpanElement>) => {
|
|
121
|
+
this.props.onBlur?.(event);
|
|
122
|
+
|
|
123
|
+
if (this.hasTrigger('focus') && !event.currentTarget.contains(event.relatedTarget))
|
|
124
|
+
this.setOpenAfterDelay(false);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
handleClick = (event: MouseEvent<HTMLSpanElement>) => {
|
|
128
|
+
this.props.onClick?.(event);
|
|
129
|
+
|
|
130
|
+
if (!event.defaultPrevented && this.hasTrigger('click')) this.setOpenAfterDelay(!this.open);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
handleOutside = ({ target }: PointerEvent) => {
|
|
134
|
+
if (target instanceof Node && !this.root.current?.contains(target)) this.hide();
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
handleKeyDown = (event: KeyboardEvent<HTMLSpanElement>) => {
|
|
138
|
+
this.props.onKeyDown?.(event);
|
|
139
|
+
|
|
140
|
+
if (event.key === 'Escape' && this.open) {
|
|
141
|
+
event.preventDefault();
|
|
142
|
+
this.hide();
|
|
34
143
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
render() {
|
|
147
|
+
const {
|
|
148
|
+
title,
|
|
149
|
+
detail,
|
|
150
|
+
placement = 'top',
|
|
151
|
+
trigger: _trigger,
|
|
152
|
+
delay: _delay,
|
|
153
|
+
overlayClassName,
|
|
154
|
+
children,
|
|
155
|
+
className = '',
|
|
156
|
+
onMouseEnter: _onMouseEnter,
|
|
157
|
+
onMouseLeave: _onMouseLeave,
|
|
158
|
+
onFocus: _onFocus,
|
|
159
|
+
onBlur: _onBlur,
|
|
160
|
+
onClick: _onClick,
|
|
161
|
+
onKeyDown: _onKeyDown,
|
|
162
|
+
...props
|
|
163
|
+
} = this.props;
|
|
164
|
+
const id = this.open ? this.id : undefined;
|
|
165
|
+
const position = PLACEMENT_CLASSES[placement];
|
|
166
|
+
const gap = OVERLAY_GAPS[placement][detail ? 'popover' : 'tooltip'];
|
|
167
|
+
const overlayClass = classNames('position-absolute w-max', position, gap, overlayClassName);
|
|
168
|
+
const trigger = isValidElement(children)
|
|
169
|
+
? cloneElement(children, {
|
|
170
|
+
'aria-describedby':
|
|
171
|
+
[children.props['aria-describedby'], id].filter(Boolean).join(' ') ||
|
|
172
|
+
undefined
|
|
173
|
+
})
|
|
174
|
+
: children;
|
|
39
175
|
|
|
40
|
-
|
|
176
|
+
return (
|
|
177
|
+
<span
|
|
178
|
+
{...props}
|
|
179
|
+
ref={this.root}
|
|
180
|
+
className={`position-relative d-inline-block ${className}`}
|
|
181
|
+
onMouseEnter={this.handleMouseEnter}
|
|
182
|
+
onMouseLeave={this.handleMouseLeave}
|
|
183
|
+
onFocus={this.handleFocus}
|
|
184
|
+
onBlur={this.handleBlur}
|
|
185
|
+
onClick={this.handleClick}
|
|
186
|
+
onKeyDown={this.handleKeyDown}
|
|
187
|
+
>
|
|
188
|
+
{trigger}
|
|
189
|
+
|
|
190
|
+
{this.open &&
|
|
191
|
+
(detail ? (
|
|
192
|
+
<Popover
|
|
193
|
+
id={id}
|
|
194
|
+
className={overlayClass}
|
|
195
|
+
title={title}
|
|
196
|
+
placement={placement}
|
|
197
|
+
>
|
|
198
|
+
{detail}
|
|
199
|
+
</Popover>
|
|
200
|
+
) : (
|
|
201
|
+
<Tooltip id={id} className={overlayClass} placement={placement}>
|
|
202
|
+
{title}
|
|
203
|
+
</Tooltip>
|
|
204
|
+
))}
|
|
205
|
+
</span>
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
}
|
package/source/PageNav.tsx
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { action, computed, observable } from 'mobx';
|
|
2
4
|
import { observer } from 'mobx-react';
|
|
3
5
|
import { Component } from 'react';
|
|
4
|
-
import { Nav, NavProps } from 'react-bootstrap';
|
|
5
6
|
import { scrollTo, uniqueID } from 'web-utility';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
import { Nav, NavLink, NavProps } from './Nav';
|
|
9
|
+
|
|
10
|
+
const HeadingSelector = Array.from({ length: 6 }, (_, index) => `h${index + 1}`).join(',');
|
|
9
11
|
|
|
10
|
-
type HeadingMeta = Record<'id' | 'text', string> &
|
|
11
|
-
Record<'level' | 'top', number>;
|
|
12
|
+
type HeadingMeta = Record<'id' | 'text', string> & Record<'level' | 'top', number>;
|
|
12
13
|
|
|
13
14
|
export interface PageNavProps extends NavProps {
|
|
14
15
|
depth?: number;
|
|
15
|
-
|
|
16
|
+
minDepth?: number;
|
|
17
|
+
rootSelector?: string;
|
|
18
|
+
onItemClick?: (item: HeadingMeta) => void;
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
@observer
|
|
@@ -33,26 +36,42 @@ export class PageNav extends Component<PageNavProps> {
|
|
|
33
36
|
return list[index - 1]?.id;
|
|
34
37
|
}
|
|
35
38
|
|
|
36
|
-
updateScrollY = () => (this.scrollY = window.scrollY);
|
|
39
|
+
updateScrollY = action(() => (this.scrollY = window.scrollY));
|
|
37
40
|
|
|
38
41
|
componentDidMount() {
|
|
39
42
|
if (!globalThis.document) return;
|
|
40
43
|
|
|
41
44
|
window.addEventListener('scroll', this.updateScrollY);
|
|
42
45
|
|
|
46
|
+
this.initList();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static slugOf(text: string) {
|
|
50
|
+
const slug = text
|
|
51
|
+
.toLowerCase()
|
|
52
|
+
.replace(/\W+/g, '-')
|
|
53
|
+
.replace(/^-+|-+$/g, '');
|
|
54
|
+
|
|
55
|
+
return !slug || document.getElementById(slug) ? uniqueID() : slug;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@action
|
|
59
|
+
initList() {
|
|
60
|
+
const { rootSelector } = this.props;
|
|
61
|
+
const root = rootSelector ? document.querySelector(rootSelector) : document;
|
|
62
|
+
|
|
63
|
+
if (!root) return;
|
|
64
|
+
|
|
43
65
|
this.list = Array.from(
|
|
44
|
-
|
|
66
|
+
root.querySelectorAll<HTMLHeadingElement>(HeadingSelector),
|
|
45
67
|
element => {
|
|
46
|
-
element.
|
|
68
|
+
const text = element.textContent.trim();
|
|
69
|
+
|
|
70
|
+
element.id ||= PageNav.slugOf(text);
|
|
47
71
|
|
|
48
|
-
const { id, tagName,
|
|
72
|
+
const { id, tagName, offsetTop } = element;
|
|
49
73
|
|
|
50
|
-
return {
|
|
51
|
-
id,
|
|
52
|
-
level: +tagName[1],
|
|
53
|
-
text: textContent.trim(),
|
|
54
|
-
top: offsetTop
|
|
55
|
-
};
|
|
74
|
+
return { id, level: +tagName[1], text, top: offsetTop };
|
|
56
75
|
}
|
|
57
76
|
);
|
|
58
77
|
}
|
|
@@ -69,32 +88,37 @@ export class PageNav extends Component<PageNavProps> {
|
|
|
69
88
|
|
|
70
89
|
renderLink = ({ id, level, text, top }: HeadingMeta) => {
|
|
71
90
|
const { currentActiveId } = this,
|
|
72
|
-
{ depth = Infinity } = this.props;
|
|
91
|
+
{ depth = Infinity, minDepth = 1 } = this.props;
|
|
73
92
|
|
|
74
93
|
return (
|
|
94
|
+
minDepth <= level &&
|
|
75
95
|
level <= depth && (
|
|
76
|
-
<
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
96
|
+
<NavLink
|
|
97
|
+
key={id}
|
|
98
|
+
className={currentActiveId === id ? ' active' : ''}
|
|
99
|
+
style={{ textIndent: `${level - minDepth}rem` }}
|
|
100
|
+
href={`#${id}`}
|
|
101
|
+
aria-current={currentActiveId === id ? 'location' : undefined}
|
|
102
|
+
onClick={this.scrollTo({ id, level, text, top })}
|
|
103
|
+
>
|
|
104
|
+
{text}
|
|
105
|
+
</NavLink>
|
|
85
106
|
)
|
|
86
107
|
);
|
|
87
108
|
};
|
|
88
109
|
|
|
89
110
|
render() {
|
|
90
111
|
const { list } = this,
|
|
91
|
-
{
|
|
112
|
+
{
|
|
113
|
+
depth: _depth,
|
|
114
|
+
minDepth: _minDepth,
|
|
115
|
+
rootSelector: _rootSelector,
|
|
116
|
+
onItemClick: _onItemClick,
|
|
117
|
+
...props
|
|
118
|
+
} = this.props;
|
|
92
119
|
|
|
93
120
|
return (
|
|
94
|
-
<Nav
|
|
95
|
-
{...{ ...props, variant }}
|
|
96
|
-
style={{ flexDirection: 'column', ...style }}
|
|
97
|
-
>
|
|
121
|
+
<Nav {...props} variant="underline" vertical>
|
|
98
122
|
{list.map(this.renderLink)}
|
|
99
123
|
</Nav>
|
|
100
124
|
);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import { FC, ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
import { OVERLAY_ARROW_POSITIONS, OVERLAY_DIRECTIONS, OverlayTriggerProps } from './Overlay';
|
|
5
|
+
|
|
6
|
+
export interface PopoverProps extends Omit<OverlayTriggerProps, 'title'> {
|
|
7
|
+
title?: ReactNode;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const Popover: FC<PopoverProps> = ({
|
|
11
|
+
title,
|
|
12
|
+
placement = 'top',
|
|
13
|
+
show = true,
|
|
14
|
+
role = 'tooltip',
|
|
15
|
+
className,
|
|
16
|
+
children,
|
|
17
|
+
...props
|
|
18
|
+
}) => (
|
|
19
|
+
<div
|
|
20
|
+
{...props}
|
|
21
|
+
className={classNames(
|
|
22
|
+
'popover',
|
|
23
|
+
{ show },
|
|
24
|
+
`bs-popover-${OVERLAY_DIRECTIONS[placement]}`,
|
|
25
|
+
className
|
|
26
|
+
)}
|
|
27
|
+
role={role}
|
|
28
|
+
data-bs-placement={placement}
|
|
29
|
+
>
|
|
30
|
+
<div className={classNames('popover-arrow', OVERLAY_ARROW_POSITIONS[placement])} />
|
|
31
|
+
<h3 className="popover-header">{title}</h3>
|
|
32
|
+
<div className="popover-body">{children}</div>
|
|
33
|
+
</div>
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
Popover.displayName = 'Popover';
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { action, computed, observable } from 'mobx';
|
|
4
|
+
import { observer } from 'mobx-react';
|
|
5
|
+
import { Component, createRef, ReactNode } from 'react';
|
|
6
|
+
|
|
7
|
+
import { Icon } from '../Icon';
|
|
8
|
+
import * as style from './index.module.less';
|
|
9
|
+
|
|
10
|
+
export interface ScrollNavScrollerProps {
|
|
11
|
+
className?: string;
|
|
12
|
+
children: ReactNode;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@observer
|
|
16
|
+
export class ScrollNavScroller extends Component<ScrollNavScrollerProps> {
|
|
17
|
+
scrollerRef = createRef<HTMLDivElement>();
|
|
18
|
+
|
|
19
|
+
@observable
|
|
20
|
+
accessor scrollLeft = 0;
|
|
21
|
+
|
|
22
|
+
@observable
|
|
23
|
+
accessor scrollWidth = 0;
|
|
24
|
+
|
|
25
|
+
@observable
|
|
26
|
+
accessor clientWidth = 0;
|
|
27
|
+
|
|
28
|
+
@computed
|
|
29
|
+
get canScrollLeft() {
|
|
30
|
+
return this.scrollLeft > 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@computed
|
|
34
|
+
get canScrollRight() {
|
|
35
|
+
return this.scrollLeft + this.clientWidth < this.scrollWidth;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@action
|
|
39
|
+
updateScroll = () => {
|
|
40
|
+
const scroller = this.scrollerRef.current;
|
|
41
|
+
if (scroller) {
|
|
42
|
+
this.scrollLeft = scroller.scrollLeft;
|
|
43
|
+
this.scrollWidth = scroller.scrollWidth;
|
|
44
|
+
this.clientWidth = scroller.clientWidth;
|
|
45
|
+
}
|
|
46
|
+
return scroller;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
private resizeObserver?: ResizeObserver;
|
|
50
|
+
|
|
51
|
+
componentDidMount() {
|
|
52
|
+
const scroller = this.updateScroll();
|
|
53
|
+
if (!scroller) return;
|
|
54
|
+
|
|
55
|
+
scroller.addEventListener('scroll', this.updateScroll);
|
|
56
|
+
this.resizeObserver = new ResizeObserver(this.updateScroll);
|
|
57
|
+
this.resizeObserver.observe(scroller);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
componentWillUnmount() {
|
|
61
|
+
this.resizeObserver?.disconnect();
|
|
62
|
+
this.scrollerRef.current?.removeEventListener('scroll', this.updateScroll);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
scrollBy = (direction: -1 | 1) => {
|
|
66
|
+
const scroller = this.scrollerRef.current;
|
|
67
|
+
|
|
68
|
+
scroller?.scrollBy({ left: direction * scroller.clientWidth * 0.8, behavior: 'smooth' });
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
scrollBackward = () => this.scrollBy(-1);
|
|
72
|
+
|
|
73
|
+
scrollForward = () => this.scrollBy(1);
|
|
74
|
+
|
|
75
|
+
render() {
|
|
76
|
+
const { canScrollLeft, canScrollRight } = this;
|
|
77
|
+
const { className = '', children } = this.props;
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<div className={`position-relative overflow-y-hidden ${className}`}>
|
|
81
|
+
{canScrollLeft && (
|
|
82
|
+
<button
|
|
83
|
+
type="button"
|
|
84
|
+
className={`position-absolute top-0 bottom-0 start-0 z-1 border-0 px-2 ${style.scrollButtonLeft}`}
|
|
85
|
+
aria-label="Scroll left"
|
|
86
|
+
onClick={this.scrollBackward}
|
|
87
|
+
>
|
|
88
|
+
<Icon name="caret-left-fill" />
|
|
89
|
+
</button>
|
|
90
|
+
)}
|
|
91
|
+
<div ref={this.scrollerRef} className={`overflow-x-auto ${style.scrollerInner}`}>
|
|
92
|
+
{children}
|
|
93
|
+
</div>
|
|
94
|
+
{canScrollRight && (
|
|
95
|
+
<button
|
|
96
|
+
type="button"
|
|
97
|
+
className={`position-absolute top-0 bottom-0 end-0 z-1 border-0 px-2 ${style.scrollButtonRight}`}
|
|
98
|
+
aria-label="Scroll right"
|
|
99
|
+
onClick={this.scrollForward}
|
|
100
|
+
>
|
|
101
|
+
<Icon name="caret-right-fill" />
|
|
102
|
+
</button>
|
|
103
|
+
)}
|
|
104
|
+
</div>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { observer } from 'mobx-react';
|
|
3
|
-
import { Component, createRef } from 'react';
|
|
4
|
-
import { Nav, NavProps } from 'react-bootstrap';
|
|
1
|
+
import { FC } from 'react';
|
|
5
2
|
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
3
|
+
import { Nav, NavProps } from '../Nav';
|
|
4
|
+
import { ScrollNavScroller } from './Scroller';
|
|
8
5
|
|
|
9
6
|
export type ScrollNavProps = NavProps;
|
|
10
7
|
|
|
@@ -14,96 +11,12 @@ export type ScrollNavProps = NavProps;
|
|
|
14
11
|
*
|
|
15
12
|
* @see {@link https://github.com/twbs/bootstrap/blob/main/site/src/assets/examples/offcanvas-navbar/index.astro#L50-L65}
|
|
16
13
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
@observable
|
|
27
|
-
accessor scrollWidth = 0;
|
|
28
|
-
|
|
29
|
-
@observable
|
|
30
|
-
accessor clientWidth = 0;
|
|
31
|
-
|
|
32
|
-
@computed
|
|
33
|
-
get canScrollLeft() {
|
|
34
|
-
return this.scrollLeft > 0;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
@computed
|
|
38
|
-
get canScrollRight() {
|
|
39
|
-
return this.scrollLeft + this.clientWidth < this.scrollWidth;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
updateScroll = () => {
|
|
43
|
-
const scroller = this.scrollerRef.current;
|
|
44
|
-
if (scroller) {
|
|
45
|
-
this.scrollLeft = scroller.scrollLeft;
|
|
46
|
-
this.scrollWidth = scroller.scrollWidth;
|
|
47
|
-
this.clientWidth = scroller.clientWidth;
|
|
48
|
-
}
|
|
49
|
-
return scroller;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
private resizeObserver?: ResizeObserver;
|
|
53
|
-
|
|
54
|
-
componentDidMount() {
|
|
55
|
-
const scroller = this.updateScroll();
|
|
56
|
-
if (!scroller) return;
|
|
57
|
-
|
|
58
|
-
scroller.addEventListener('scroll', this.updateScroll);
|
|
59
|
-
this.resizeObserver = new ResizeObserver(this.updateScroll);
|
|
60
|
-
this.resizeObserver.observe(scroller);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
componentWillUnmount() {
|
|
64
|
-
this.resizeObserver?.disconnect();
|
|
65
|
-
this.scrollerRef.current?.removeEventListener('scroll', this.updateScroll);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
scrollBy = (direction: -1 | 1) => () => {
|
|
69
|
-
const scroller = this.scrollerRef.current;
|
|
70
|
-
if (!scroller) return;
|
|
71
|
-
|
|
72
|
-
scroller.scrollBy({ left: direction * scroller.clientWidth * 0.8, behavior: 'smooth' });
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
render() {
|
|
76
|
-
const { canScrollLeft, canScrollRight } = this;
|
|
77
|
-
const { className = '', children, ...props } = this.props;
|
|
78
|
-
|
|
79
|
-
return (
|
|
80
|
-
<div className={`position-relative overflow-y-hidden ${className}`}>
|
|
81
|
-
{canScrollLeft && (
|
|
82
|
-
<button
|
|
83
|
-
type="button"
|
|
84
|
-
className={`position-absolute top-0 bottom-0 start-0 z-1 border-0 px-2 ${style.scrollButtonLeft}`}
|
|
85
|
-
aria-label="Scroll left"
|
|
86
|
-
onClick={this.scrollBy(-1)}
|
|
87
|
-
>
|
|
88
|
-
<Icon name="caret-left-fill" />
|
|
89
|
-
</button>
|
|
90
|
-
)}
|
|
91
|
-
<div ref={this.scrollerRef} className={`overflow-x-auto ${style.scrollerInner}`}>
|
|
92
|
-
<Nav {...props} className="flex-nowrap">
|
|
93
|
-
{children}
|
|
94
|
-
</Nav>
|
|
95
|
-
</div>
|
|
96
|
-
{canScrollRight && (
|
|
97
|
-
<button
|
|
98
|
-
type="button"
|
|
99
|
-
className={`position-absolute top-0 bottom-0 end-0 z-1 border-0 px-2 ${style.scrollButtonRight}`}
|
|
100
|
-
aria-label="Scroll right"
|
|
101
|
-
onClick={this.scrollBy(1)}
|
|
102
|
-
>
|
|
103
|
-
<Icon name="caret-right-fill" />
|
|
104
|
-
</button>
|
|
105
|
-
)}
|
|
106
|
-
</div>
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
14
|
+
export const ScrollNav: FC<ScrollNavProps> = ({ className = '', children, ...props }) => (
|
|
15
|
+
<ScrollNavScroller className={className}>
|
|
16
|
+
<Nav {...props} className="flex-nowrap">
|
|
17
|
+
{children}
|
|
18
|
+
</Nav>
|
|
19
|
+
</ScrollNavScroller>
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
ScrollNav.displayName = 'ScrollNav';
|