idea-react 3.0.0-alpha.0 → 3.0.0-alpha.1
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 +42 -35
- package/dist/index.d.mts +301 -100
- package/dist/index.d.ts +301 -100
- package/dist/index.js +16 -16
- package/dist/index.mjs +15 -15
- package/dist/style.css +5 -5
- package/package.json +4 -7
- package/source/Avatar.tsx +65 -12
- package/source/Button.tsx +13 -4
- package/source/ButtonGroup.tsx +28 -0
- package/source/Card.tsx +26 -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/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 +48 -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 +114 -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 +13 -0
- package/source/TypeEcho.tsx +0 -49
package/source/MonthCalendar.tsx
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
1
3
|
import classNames from 'classnames';
|
|
2
|
-
import { computed, observable } from 'mobx';
|
|
4
|
+
import { action, computed, observable } from 'mobx';
|
|
3
5
|
import { observer } from 'mobx-react';
|
|
4
6
|
import { ObservedComponent } from 'mobx-react-helper';
|
|
5
|
-
import { ReactNode } from 'react';
|
|
6
|
-
import { Badge, Button, ButtonProps, Table, TableProps } from 'react-bootstrap';
|
|
7
|
+
import { HTMLAttributes, MouseEvent, ReactNode } from 'react';
|
|
7
8
|
import { changeMonth, Day, formatDate, splitArray, TimeData } from 'web-utility';
|
|
8
9
|
|
|
10
|
+
import { Badge } from './Badge';
|
|
11
|
+
import { Button, type ComponentTheme } from './Button';
|
|
9
12
|
import { text2color } from './color';
|
|
10
13
|
import { OverlayBox } from './OverlayBox';
|
|
11
14
|
|
|
@@ -15,13 +18,15 @@ export interface DateData {
|
|
|
15
18
|
link?: string;
|
|
16
19
|
}
|
|
17
20
|
|
|
18
|
-
export interface MonthCalendarProps
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
export interface MonthCalendarProps extends Omit<
|
|
22
|
+
HTMLAttributes<HTMLTableElement>,
|
|
23
|
+
'onChange' | 'onSelect'
|
|
24
|
+
> {
|
|
21
25
|
locale?: Navigator['language'];
|
|
26
|
+
theme?: ComponentTheme;
|
|
22
27
|
value?: DateData[];
|
|
23
|
-
onSelect?: (value: DateData) =>
|
|
24
|
-
onChange?: (date: Date) =>
|
|
28
|
+
onSelect?: (value: DateData) => unknown;
|
|
29
|
+
onChange?: (date: Date) => void;
|
|
25
30
|
}
|
|
26
31
|
|
|
27
32
|
/**
|
|
@@ -54,14 +59,34 @@ export class MonthCalendar extends ObservedComponent<MonthCalendarProps> {
|
|
|
54
59
|
return splitArray(dateList, 7);
|
|
55
60
|
}
|
|
56
61
|
|
|
62
|
+
@action
|
|
57
63
|
changeMonth(delta: number) {
|
|
58
64
|
this.currentDate = changeMonth(this.currentDate, delta);
|
|
59
65
|
|
|
60
66
|
this.props.onChange?.(this.currentDate);
|
|
61
67
|
}
|
|
62
68
|
|
|
69
|
+
changePreviousMonth = () => this.changeMonth(-1);
|
|
70
|
+
changeNextMonth = () => this.changeMonth(1);
|
|
71
|
+
|
|
72
|
+
handleCellClick = ({ target, currentTarget }: MouseEvent<HTMLTableCellElement>) => {
|
|
73
|
+
if (!(target instanceof Element)) return;
|
|
74
|
+
|
|
75
|
+
const targetItem = target.closest<HTMLElement>('[data-calendar-index]');
|
|
76
|
+
if (!targetItem || !currentTarget.contains(targetItem)) return;
|
|
77
|
+
|
|
78
|
+
const date = targetItem.dataset.calendarDate;
|
|
79
|
+
const index = Number(targetItem.dataset.calendarIndex);
|
|
80
|
+
const items = this.props.value?.filter(
|
|
81
|
+
({ date: itemDate }) => formatDate(itemDate, 'YYYY-MM-DD') === date
|
|
82
|
+
);
|
|
83
|
+
const item = items?.[index];
|
|
84
|
+
|
|
85
|
+
if (item) this.props.onSelect?.(item);
|
|
86
|
+
};
|
|
87
|
+
|
|
63
88
|
renderDate = (date: Date) => {
|
|
64
|
-
const {
|
|
89
|
+
const { value } = this.observedProps,
|
|
65
90
|
dateText = formatDate(date, 'YYYY-MM-DD');
|
|
66
91
|
const list = value?.filter(({ date }) => formatDate(date, 'YYYY-MM-DD') === dateText);
|
|
67
92
|
|
|
@@ -72,22 +97,23 @@ export class MonthCalendar extends ObservedComponent<MonthCalendarProps> {
|
|
|
72
97
|
'opacity-50': date.getMonth() !== this.currentDate.getMonth(),
|
|
73
98
|
'fw-bold': dateText === formatDate(new Date(), 'YYYY-MM-DD')
|
|
74
99
|
})}
|
|
100
|
+
onClick={this.handleCellClick}
|
|
75
101
|
>
|
|
76
102
|
<time className="d-block" dateTime={date.toJSON()}>
|
|
77
103
|
{date.getDate()}
|
|
78
104
|
</time>
|
|
79
105
|
|
|
80
|
-
{list?.map(item =>
|
|
106
|
+
{list?.map((item, index) =>
|
|
81
107
|
typeof item.content === 'object' ? (
|
|
82
108
|
item.content
|
|
83
109
|
) : (
|
|
84
|
-
<OverlayBox key={
|
|
110
|
+
<OverlayBox key={`${dateText}-${index}`} title={item.content}>
|
|
85
111
|
<Badge
|
|
86
|
-
as="a"
|
|
87
112
|
className="d-inline-block text-decoration-none w-100 text-truncate"
|
|
88
|
-
|
|
113
|
+
theme={text2color(item.content + '', ['light'])}
|
|
114
|
+
data-calendar-date={dateText}
|
|
115
|
+
data-calendar-index={index}
|
|
89
116
|
href={item.link}
|
|
90
|
-
onClick={() => onSelect?.(item)}
|
|
91
117
|
>
|
|
92
118
|
{item.content}
|
|
93
119
|
</Badge>
|
|
@@ -100,19 +126,30 @@ export class MonthCalendar extends ObservedComponent<MonthCalendarProps> {
|
|
|
100
126
|
|
|
101
127
|
render() {
|
|
102
128
|
const { weekFormatter, currentDate, dateGrid } = this,
|
|
103
|
-
{
|
|
129
|
+
{
|
|
130
|
+
className = '',
|
|
131
|
+
style,
|
|
132
|
+
theme = 'primary',
|
|
133
|
+
onChange: _onChange,
|
|
134
|
+
onSelect: _onSelect,
|
|
135
|
+
...props
|
|
136
|
+
} = this.observedProps;
|
|
104
137
|
|
|
105
138
|
return (
|
|
106
|
-
<
|
|
139
|
+
<table
|
|
140
|
+
{...props}
|
|
141
|
+
className={`table ${className}`}
|
|
142
|
+
style={{ tableLayout: 'fixed', ...style }}
|
|
143
|
+
>
|
|
107
144
|
<caption>
|
|
108
145
|
<div className="d-flex justify-content-between align-items-center">
|
|
109
|
-
<Button
|
|
146
|
+
<Button theme={theme} onClick={this.changePreviousMonth}>
|
|
110
147
|
<
|
|
111
148
|
</Button>
|
|
112
149
|
|
|
113
150
|
{formatDate(currentDate, 'YYYY-MM')}
|
|
114
151
|
|
|
115
|
-
<Button
|
|
152
|
+
<Button theme={theme} onClick={this.changeNextMonth}>
|
|
116
153
|
>
|
|
117
154
|
</Button>
|
|
118
155
|
</div>
|
|
@@ -120,13 +157,14 @@ export class MonthCalendar extends ObservedComponent<MonthCalendarProps> {
|
|
|
120
157
|
<thead>
|
|
121
158
|
<tr>
|
|
122
159
|
{dateGrid[0].map((date, index, { length }) => (
|
|
123
|
-
<
|
|
160
|
+
<th
|
|
124
161
|
key={index}
|
|
125
|
-
className={`
|
|
162
|
+
className={`theme-${theme} theme-contrast`}
|
|
126
163
|
style={{ width: `calc(100% / ${length})` }}
|
|
164
|
+
scope="col"
|
|
127
165
|
>
|
|
128
166
|
{weekFormatter.format(date)}
|
|
129
|
-
</
|
|
167
|
+
</th>
|
|
130
168
|
))}
|
|
131
169
|
</tr>
|
|
132
170
|
</thead>
|
|
@@ -135,7 +173,7 @@ export class MonthCalendar extends ObservedComponent<MonthCalendarProps> {
|
|
|
135
173
|
<tr key={days[0] + ''}>{days.map(this.renderDate)}</tr>
|
|
136
174
|
))}
|
|
137
175
|
</tbody>
|
|
138
|
-
</
|
|
176
|
+
</table>
|
|
139
177
|
);
|
|
140
178
|
}
|
|
141
179
|
}
|
|
@@ -3,8 +3,10 @@
|
|
|
3
3
|
import classNames from 'classnames';
|
|
4
4
|
import {
|
|
5
5
|
Component,
|
|
6
|
+
ComponentPropsWithRef,
|
|
6
7
|
createRef,
|
|
7
8
|
DialogHTMLAttributes,
|
|
9
|
+
FC,
|
|
8
10
|
KeyboardEvent,
|
|
9
11
|
MouseEvent,
|
|
10
12
|
SyntheticEvent
|
|
@@ -14,10 +16,29 @@ import styles from './style.module.css';
|
|
|
14
16
|
|
|
15
17
|
export interface NativeDialogProps extends Omit<DialogHTMLAttributes<HTMLDialogElement>, 'open'> {
|
|
16
18
|
modal?: boolean;
|
|
19
|
+
dialog?: boolean;
|
|
20
|
+
theme?: 'light' | 'dark';
|
|
17
21
|
keyboard?: boolean;
|
|
18
22
|
backdrop?: boolean | 'static';
|
|
23
|
+
open?: boolean;
|
|
19
24
|
}
|
|
20
25
|
|
|
26
|
+
export const DialogHeader: FC<ComponentPropsWithRef<'div'>> = ({ className, ...props }) => (
|
|
27
|
+
<div {...props} className={classNames('dialog-header', className)} />
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
export const DialogTitle: FC<ComponentPropsWithRef<'h1'>> = ({ className, ...props }) => (
|
|
31
|
+
<h1 {...props} className={classNames('dialog-title', className)} />
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
export const DialogBody: FC<ComponentPropsWithRef<'div'>> = ({ className, ...props }) => (
|
|
35
|
+
<div {...props} className={classNames('dialog-body', className)} />
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
export const DialogFooter: FC<ComponentPropsWithRef<'div'>> = ({ className, ...props }) => (
|
|
39
|
+
<div {...props} className={classNames('dialog-footer', className)} />
|
|
40
|
+
);
|
|
41
|
+
|
|
21
42
|
export class NativeDialog extends Component<NativeDialogProps> {
|
|
22
43
|
element = createRef<HTMLDialogElement>();
|
|
23
44
|
returnFocus?: HTMLElement;
|
|
@@ -26,6 +47,17 @@ export class NativeDialog extends Component<NativeDialogProps> {
|
|
|
26
47
|
return this.element.current?.open ?? false;
|
|
27
48
|
}
|
|
28
49
|
|
|
50
|
+
componentDidMount() {
|
|
51
|
+
if (this.props.open) this.show();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
componentDidUpdate({ open }: NativeDialogProps) {
|
|
55
|
+
if (this.props.open === open) return;
|
|
56
|
+
|
|
57
|
+
if (this.props.open) this.show();
|
|
58
|
+
else this.close();
|
|
59
|
+
}
|
|
60
|
+
|
|
29
61
|
show = () => {
|
|
30
62
|
if (this.open) return;
|
|
31
63
|
const element = this.element.current;
|
|
@@ -74,9 +106,12 @@ export class NativeDialog extends Component<NativeDialogProps> {
|
|
|
74
106
|
|
|
75
107
|
render() {
|
|
76
108
|
const {
|
|
109
|
+
open: _open,
|
|
77
110
|
modal: _modal,
|
|
78
111
|
keyboard: _keyboard,
|
|
79
112
|
backdrop = true,
|
|
113
|
+
theme,
|
|
114
|
+
dialog = true,
|
|
80
115
|
className,
|
|
81
116
|
onCancel: _onCancel,
|
|
82
117
|
onClick: _onClick,
|
|
@@ -89,11 +124,12 @@ export class NativeDialog extends Component<NativeDialogProps> {
|
|
|
89
124
|
{...props}
|
|
90
125
|
ref={this.element}
|
|
91
126
|
className={classNames(
|
|
92
|
-
'dialog',
|
|
127
|
+
dialog && 'dialog',
|
|
93
128
|
backdrop === false && styles.hiddenBackdrop,
|
|
94
129
|
className
|
|
95
130
|
)}
|
|
96
131
|
data-backdrop={backdrop === false ? 'false' : undefined}
|
|
132
|
+
data-bs-theme={theme}
|
|
97
133
|
onCancel={this.handleCancel}
|
|
98
134
|
onKeyDown={this.handleKeyDown}
|
|
99
135
|
onClick={this.handleClick}
|
package/source/Nav.tsx
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import { ComponentPropsWithRef, FC } from 'react';
|
|
3
|
+
|
|
4
|
+
export interface NavProps extends ComponentPropsWithRef<'nav'> {
|
|
5
|
+
variant?: 'tabs' | 'pills' | 'underline';
|
|
6
|
+
vertical?: boolean;
|
|
7
|
+
fill?: boolean;
|
|
8
|
+
justified?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const Nav: FC<NavProps> = ({ variant, vertical, fill, justified, className, ...props }) => (
|
|
12
|
+
<nav
|
|
13
|
+
{...props}
|
|
14
|
+
className={classNames(
|
|
15
|
+
'nav',
|
|
16
|
+
variant && `nav-${variant}`,
|
|
17
|
+
vertical && (variant === 'pills' ? 'nav-pills-vertical' : 'flex-column'),
|
|
18
|
+
{ 'nav-fill': fill, 'nav-justified': justified },
|
|
19
|
+
className
|
|
20
|
+
)}
|
|
21
|
+
/>
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
export const NavItem: FC<ComponentPropsWithRef<'li'>> = ({ className, ...props }) => (
|
|
25
|
+
<li {...props} className={classNames('nav-item', className)} />
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
export interface NavLinkProps extends ComponentPropsWithRef<'a'> {
|
|
29
|
+
disabled?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const NavLink: FC<NavLinkProps> = ({
|
|
33
|
+
disabled,
|
|
34
|
+
className,
|
|
35
|
+
href,
|
|
36
|
+
onClick,
|
|
37
|
+
tabIndex,
|
|
38
|
+
...props
|
|
39
|
+
}) => (
|
|
40
|
+
<a
|
|
41
|
+
{...props}
|
|
42
|
+
href={disabled ? undefined : href}
|
|
43
|
+
tabIndex={disabled ? -1 : tabIndex}
|
|
44
|
+
aria-disabled={disabled || undefined}
|
|
45
|
+
className={classNames('nav-link', { disabled, 'pe-none': disabled }, className)}
|
|
46
|
+
onClick={disabled ? undefined : onClick}
|
|
47
|
+
/>
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
Nav.displayName = 'Nav';
|
|
51
|
+
NavItem.displayName = 'NavItem';
|
|
52
|
+
NavLink.displayName = 'NavLink';
|
package/source/Navbar.tsx
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import classNames from 'classnames';
|
|
2
2
|
import { ComponentPropsWithRef, FC } from 'react';
|
|
3
3
|
|
|
4
|
+
import { Button } from './Button';
|
|
5
|
+
|
|
4
6
|
export interface NavbarProps extends ComponentPropsWithRef<'nav'> {
|
|
7
|
+
theme?: 'light' | 'dark';
|
|
5
8
|
expand?: boolean | 'sm' | 'md' | 'lg' | 'xl' | '2xl';
|
|
6
9
|
}
|
|
7
10
|
|
|
8
|
-
export const Navbar: FC<NavbarProps> = ({ expand, className, ...props }) => (
|
|
11
|
+
export const Navbar: FC<NavbarProps> = ({ expand, theme, className, ...props }) => (
|
|
9
12
|
<nav
|
|
10
13
|
{...props}
|
|
11
14
|
className={classNames(
|
|
@@ -13,5 +16,49 @@ export const Navbar: FC<NavbarProps> = ({ expand, className, ...props }) => (
|
|
|
13
16
|
expand && (expand === true ? 'navbar-expand' : `${expand}:navbar-expand`),
|
|
14
17
|
className
|
|
15
18
|
)}
|
|
19
|
+
data-bs-theme={theme}
|
|
16
20
|
/>
|
|
17
21
|
);
|
|
22
|
+
|
|
23
|
+
export const NavbarBrand: FC<ComponentPropsWithRef<'a'>> = ({ className, ...props }) => (
|
|
24
|
+
<a {...props} className={classNames('navbar-brand', className)} />
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
export const NavbarNav: FC<ComponentPropsWithRef<'ul'>> = ({ className, ...props }) => (
|
|
28
|
+
<ul {...props} className={classNames('navbar-nav', className)} />
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
export const NavbarText: FC<ComponentPropsWithRef<'span'>> = ({ className, ...props }) => (
|
|
32
|
+
<span {...props} className={classNames('navbar-text', className)} />
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
export interface NavbarTogglerProps extends Omit<
|
|
36
|
+
ComponentPropsWithRef<'button'>,
|
|
37
|
+
'children' | 'type'
|
|
38
|
+
> {
|
|
39
|
+
expanded?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const NavbarToggler: FC<NavbarTogglerProps> = ({
|
|
43
|
+
expanded,
|
|
44
|
+
className,
|
|
45
|
+
'aria-label': ariaLabel = 'Toggle navigation',
|
|
46
|
+
...props
|
|
47
|
+
}) => (
|
|
48
|
+
<Button
|
|
49
|
+
{...props}
|
|
50
|
+
type="button"
|
|
51
|
+
className={classNames('navbar-toggler', className)}
|
|
52
|
+
aria-label={ariaLabel}
|
|
53
|
+
aria-expanded={expanded}
|
|
54
|
+
icon
|
|
55
|
+
>
|
|
56
|
+
<span className="navbar-toggler-icon" aria-hidden="true" />
|
|
57
|
+
</Button>
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
Navbar.displayName = 'Navbar';
|
|
61
|
+
NavbarBrand.displayName = 'NavbarBrand';
|
|
62
|
+
NavbarNav.displayName = 'NavbarNav';
|
|
63
|
+
NavbarText.displayName = 'NavbarText';
|
|
64
|
+
NavbarToggler.displayName = 'NavbarToggler';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ComponentPropsWithRef } from 'react';
|
|
2
|
+
|
|
3
|
+
export type OverlayPlacement = 'top' | 'right' | 'bottom' | 'left';
|
|
4
|
+
|
|
5
|
+
export interface OverlayTriggerProps extends ComponentPropsWithRef<'div'> {
|
|
6
|
+
placement?: OverlayPlacement;
|
|
7
|
+
show?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const OVERLAY_DIRECTIONS: Record<OverlayPlacement, string> = {
|
|
11
|
+
top: 'top',
|
|
12
|
+
right: 'end',
|
|
13
|
+
bottom: 'bottom',
|
|
14
|
+
left: 'start'
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const OVERLAY_ARROW_POSITIONS: Record<OverlayPlacement, string> = {
|
|
18
|
+
top: 'position-absolute start-50 translate-middle-x',
|
|
19
|
+
right: 'position-absolute top-50 translate-middle-y',
|
|
20
|
+
bottom: 'position-absolute start-50 translate-middle-x',
|
|
21
|
+
left: 'position-absolute top-50 translate-middle-y'
|
|
22
|
+
};
|
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
|
+
}
|