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,79 @@
|
|
|
1
|
+
export class CarouselScroll {
|
|
2
|
+
private frame?: number;
|
|
3
|
+
|
|
4
|
+
private clone?: HTMLElement;
|
|
5
|
+
|
|
6
|
+
private track?: HTMLElement;
|
|
7
|
+
|
|
8
|
+
private snap = '';
|
|
9
|
+
|
|
10
|
+
get looping() {
|
|
11
|
+
return Boolean(this.clone) || this.frame !== undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
navigate = (track: HTMLElement, left: number, smooth: boolean) => {
|
|
15
|
+
this.dispose();
|
|
16
|
+
if (!smooth || track.scrollLeft === left) {
|
|
17
|
+
track.scrollTo({ left, behavior: 'instant' });
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
this.track = track;
|
|
21
|
+
this.snap = track.style.scrollSnapType;
|
|
22
|
+
track.style.scrollSnapType = 'none';
|
|
23
|
+
this.animate(track, track.scrollLeft, left);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
loop = (track: HTMLElement, from: HTMLElement, destination: HTMLElement, forward: boolean) => {
|
|
27
|
+
this.dispose();
|
|
28
|
+
this.track = track;
|
|
29
|
+
this.snap = track.style.scrollSnapType;
|
|
30
|
+
|
|
31
|
+
const clone = destination.cloneNode(true) as HTMLElement;
|
|
32
|
+
clone.removeAttribute('id');
|
|
33
|
+
|
|
34
|
+
for (const element of clone.querySelectorAll('[id]')) element.removeAttribute('id');
|
|
35
|
+
|
|
36
|
+
clone.setAttribute('aria-hidden', 'true');
|
|
37
|
+
clone.inert = true;
|
|
38
|
+
clone.dataset.carouselClone = 'true';
|
|
39
|
+
clone.classList.remove('active');
|
|
40
|
+
this.clone = clone;
|
|
41
|
+
track.style.scrollSnapType = 'none';
|
|
42
|
+
|
|
43
|
+
if (forward) track.append(clone);
|
|
44
|
+
else track.prepend(clone);
|
|
45
|
+
|
|
46
|
+
const start = from.offsetLeft;
|
|
47
|
+
const end = clone.offsetLeft;
|
|
48
|
+
|
|
49
|
+
track.scrollTo({ left: start, behavior: 'instant' });
|
|
50
|
+
this.animate(track, start, end, () => {
|
|
51
|
+
this.dispose();
|
|
52
|
+
track.scrollTo({ left: destination.offsetLeft, behavior: 'instant' });
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
private animate = (track: HTMLElement, start: number, end: number, complete = this.dispose) => {
|
|
57
|
+
const startedAt = window.performance.now();
|
|
58
|
+
|
|
59
|
+
const animate = (time: number) => {
|
|
60
|
+
const progress = Math.min(1, (time - startedAt) / 400);
|
|
61
|
+
const eased = 1 - (1 - progress) ** 3;
|
|
62
|
+
|
|
63
|
+
track.scrollTo({ left: start + (end - start) * eased, behavior: 'instant' });
|
|
64
|
+
|
|
65
|
+
if (progress < 1) this.frame = window.requestAnimationFrame(animate);
|
|
66
|
+
else complete();
|
|
67
|
+
};
|
|
68
|
+
this.frame = window.requestAnimationFrame(animate);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
dispose = () => {
|
|
72
|
+
if (this.frame !== undefined) window.cancelAnimationFrame(this.frame);
|
|
73
|
+
this.frame = undefined;
|
|
74
|
+
this.clone?.remove();
|
|
75
|
+
this.clone = undefined;
|
|
76
|
+
if (this.track) this.track.style.scrollSnapType = this.snap;
|
|
77
|
+
this.track = undefined;
|
|
78
|
+
};
|
|
79
|
+
}
|
package/source/CloseButton.tsx
CHANGED
|
@@ -1,23 +1,28 @@
|
|
|
1
1
|
import classNames from 'classnames';
|
|
2
|
-
import { ComponentPropsWithRef
|
|
2
|
+
import { ComponentPropsWithRef } from 'react';
|
|
3
|
+
|
|
4
|
+
import { polymorphic } from './Polymorphic';
|
|
3
5
|
|
|
4
6
|
export interface CloseButtonProps extends ComponentPropsWithRef<'button'> {
|
|
5
7
|
dismiss?: string;
|
|
6
8
|
}
|
|
7
9
|
|
|
8
|
-
export const CloseButton
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
10
|
+
export const CloseButton = polymorphic<CloseButtonProps>(
|
|
11
|
+
({
|
|
12
|
+
as: Root = 'button',
|
|
13
|
+
dismiss,
|
|
14
|
+
className,
|
|
15
|
+
type = 'button',
|
|
16
|
+
'aria-label': ariaLabel = 'Close',
|
|
17
|
+
...props
|
|
18
|
+
}) => (
|
|
19
|
+
<Root
|
|
20
|
+
{...props}
|
|
21
|
+
type={type}
|
|
22
|
+
className={classNames('btn-close', className)}
|
|
23
|
+
data-bs-dismiss={dismiss}
|
|
24
|
+
aria-label={ariaLabel}
|
|
25
|
+
/>
|
|
26
|
+
)
|
|
22
27
|
);
|
|
23
28
|
CloseButton.displayName = 'CloseButton';
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import classNames from 'classnames';
|
|
4
|
+
import { action, observable } from 'mobx';
|
|
5
|
+
import { observer } from 'mobx-react';
|
|
6
|
+
import {
|
|
7
|
+
Component,
|
|
8
|
+
ComponentPropsWithoutRef,
|
|
9
|
+
createRef,
|
|
10
|
+
ElementType,
|
|
11
|
+
HTMLAttributes,
|
|
12
|
+
MouseEvent
|
|
13
|
+
} from 'react';
|
|
14
|
+
|
|
15
|
+
import { ButtonProps } from './Button';
|
|
16
|
+
import { AsProps, PolymorphicClassProps } from './Polymorphic';
|
|
17
|
+
|
|
18
|
+
const TOGGLE = 'idea-react:collapse-toggle';
|
|
19
|
+
const CHANGE = 'idea-react:collapse-change';
|
|
20
|
+
const OPEN = 'idea-react:collapse-open';
|
|
21
|
+
|
|
22
|
+
export interface CollapseProps extends ComponentPropsWithoutRef<'div'> {
|
|
23
|
+
horizontal?: boolean;
|
|
24
|
+
open?: boolean;
|
|
25
|
+
defaultOpen?: boolean;
|
|
26
|
+
parent?: string;
|
|
27
|
+
onOpenChange?: (open: boolean) => unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@observer
|
|
31
|
+
export class Collapse<T extends ElementType | undefined = 'div'> extends Component<
|
|
32
|
+
PolymorphicClassProps<T, CollapseProps>
|
|
33
|
+
> {
|
|
34
|
+
declare readonly props: PolymorphicClassProps<T, CollapseProps>;
|
|
35
|
+
|
|
36
|
+
protected get componentProps(): Readonly<CollapseProps & AsProps> {
|
|
37
|
+
return this.props;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static displayName = 'Collapse';
|
|
41
|
+
|
|
42
|
+
@observable accessor expanded = this.componentProps.defaultOpen ?? false;
|
|
43
|
+
|
|
44
|
+
root = createRef<HTMLDivElement>();
|
|
45
|
+
|
|
46
|
+
get isOpen() {
|
|
47
|
+
return this.componentProps.open ?? this.expanded;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
componentDidMount() {
|
|
51
|
+
this.root.current!.addEventListener(TOGGLE, this.handleToggle);
|
|
52
|
+
document.addEventListener(OPEN, this.handlePeerOpen);
|
|
53
|
+
this.notify();
|
|
54
|
+
if (this.isOpen) this.notifyOpen();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
componentDidUpdate(previous: CollapseProps) {
|
|
58
|
+
if (previous.open !== this.componentProps.open && this.isOpen) this.notifyOpen();
|
|
59
|
+
|
|
60
|
+
this.notify();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
componentWillUnmount() {
|
|
64
|
+
this.root.current!.removeEventListener(TOGGLE, this.handleToggle);
|
|
65
|
+
document.removeEventListener(OPEN, this.handlePeerOpen);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
notify = () => this.root.current!.dispatchEvent(new Event(CHANGE, { bubbles: true }));
|
|
69
|
+
|
|
70
|
+
notifyOpen = () => this.root.current!.dispatchEvent(new Event(OPEN, { bubbles: true }));
|
|
71
|
+
|
|
72
|
+
setOpen = action((open: boolean) => {
|
|
73
|
+
if (open === this.isOpen) return;
|
|
74
|
+
|
|
75
|
+
this.componentProps.onOpenChange?.(open);
|
|
76
|
+
|
|
77
|
+
if (this.componentProps.open === undefined) {
|
|
78
|
+
this.expanded = open;
|
|
79
|
+
if (open) this.notifyOpen();
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
handleToggle = (event: Event) => {
|
|
84
|
+
if (event.target === this.root.current) this.setOpen(!this.isOpen);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
handlePeerOpen = ({ target }: Event) => {
|
|
88
|
+
const { parent } = this.componentProps;
|
|
89
|
+
const root = this.root.current;
|
|
90
|
+
const group = parent ? root?.closest(parent) : null;
|
|
91
|
+
|
|
92
|
+
if (
|
|
93
|
+
parent &&
|
|
94
|
+
group &&
|
|
95
|
+
target instanceof Element &&
|
|
96
|
+
target !== root &&
|
|
97
|
+
target.closest(parent) === group
|
|
98
|
+
)
|
|
99
|
+
this.setOpen(false);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
render() {
|
|
103
|
+
const Root: ElementType = this.componentProps.as ?? 'div';
|
|
104
|
+
|
|
105
|
+
const {
|
|
106
|
+
as: _as,
|
|
107
|
+
open: _open,
|
|
108
|
+
defaultOpen: _defaultOpen,
|
|
109
|
+
parent: _parent,
|
|
110
|
+
onOpenChange: _onOpenChange,
|
|
111
|
+
horizontal,
|
|
112
|
+
className,
|
|
113
|
+
...props
|
|
114
|
+
} = this.componentProps;
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<Root
|
|
118
|
+
{...props}
|
|
119
|
+
ref={this.root}
|
|
120
|
+
className={classNames(
|
|
121
|
+
'collapse',
|
|
122
|
+
{ 'collapse-horizontal': horizontal, show: this.isOpen },
|
|
123
|
+
className
|
|
124
|
+
)}
|
|
125
|
+
/>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface CollapseToggleProps
|
|
131
|
+
extends
|
|
132
|
+
HTMLAttributes<HTMLElement>,
|
|
133
|
+
Pick<ButtonProps, 'variant' | 'theme' | 'size' | 'disabled' | 'icon' | 'styled'> {
|
|
134
|
+
target: string;
|
|
135
|
+
href?: string;
|
|
136
|
+
type?: 'button' | 'submit' | 'reset';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@observer
|
|
140
|
+
export class CollapseToggle<T extends ElementType | undefined = 'button'> extends Component<
|
|
141
|
+
PolymorphicClassProps<T, CollapseToggleProps>
|
|
142
|
+
> {
|
|
143
|
+
declare readonly props: PolymorphicClassProps<T, CollapseToggleProps>;
|
|
144
|
+
|
|
145
|
+
protected get componentProps(): Readonly<PolymorphicClassProps<T, CollapseToggleProps>> {
|
|
146
|
+
return this.props;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
static displayName = 'CollapseToggle';
|
|
150
|
+
|
|
151
|
+
@observable accessor expanded = false;
|
|
152
|
+
@observable accessor controls = '';
|
|
153
|
+
|
|
154
|
+
get targets() {
|
|
155
|
+
return [...document.querySelectorAll<HTMLElement>(this.componentProps.target)];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
componentDidMount() {
|
|
159
|
+
document.addEventListener(CHANGE, this.refresh);
|
|
160
|
+
this.refresh();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
componentDidUpdate(previous: CollapseToggleProps) {
|
|
164
|
+
if (previous.target !== this.componentProps.target) this.refresh();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
componentWillUnmount() {
|
|
168
|
+
document.removeEventListener(CHANGE, this.refresh);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
refresh = action(() => {
|
|
172
|
+
const { targets } = this;
|
|
173
|
+
|
|
174
|
+
this.expanded = targets.some(element => element.classList.contains('show'));
|
|
175
|
+
this.controls = targets
|
|
176
|
+
.map(({ id }) => id)
|
|
177
|
+
.filter(Boolean)
|
|
178
|
+
.join(' ');
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
handleClick = (event: MouseEvent<HTMLElement>) => {
|
|
182
|
+
this.componentProps.onClick?.(event);
|
|
183
|
+
|
|
184
|
+
if (event.defaultPrevented) return;
|
|
185
|
+
|
|
186
|
+
event.preventDefault();
|
|
187
|
+
if (this.componentProps.disabled) return;
|
|
188
|
+
|
|
189
|
+
for (const target of this.targets) target.dispatchEvent(new Event(TOGGLE));
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
render() {
|
|
193
|
+
const {
|
|
194
|
+
as: RootAs,
|
|
195
|
+
target: _target,
|
|
196
|
+
href,
|
|
197
|
+
type = 'button',
|
|
198
|
+
variant,
|
|
199
|
+
theme,
|
|
200
|
+
size,
|
|
201
|
+
disabled,
|
|
202
|
+
icon,
|
|
203
|
+
styled,
|
|
204
|
+
className,
|
|
205
|
+
...props
|
|
206
|
+
} = this.componentProps;
|
|
207
|
+
const Root: ElementType = RootAs ?? (href ? 'a' : 'button');
|
|
208
|
+
const nativeButton = Root === 'button';
|
|
209
|
+
const rootClassName = classNames(
|
|
210
|
+
variant === false ? undefined : variant ? `btn-${variant}` : icon ? undefined : 'btn',
|
|
211
|
+
theme && `theme-${theme}`,
|
|
212
|
+
size && `btn-${size}`,
|
|
213
|
+
{ 'btn-icon': icon, 'btn-styled': styled, disabled },
|
|
214
|
+
className
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
return (
|
|
218
|
+
<Root
|
|
219
|
+
{...props}
|
|
220
|
+
type={nativeButton ? type : undefined}
|
|
221
|
+
href={disabled ? undefined : href}
|
|
222
|
+
disabled={nativeButton || typeof Root !== 'string' ? disabled : undefined}
|
|
223
|
+
aria-disabled={(!nativeButton && disabled) || undefined}
|
|
224
|
+
tabIndex={!nativeButton && disabled ? -1 : props.tabIndex}
|
|
225
|
+
className={rootClassName}
|
|
226
|
+
role={props.role ?? 'button'}
|
|
227
|
+
aria-controls={this.controls || undefined}
|
|
228
|
+
aria-expanded={this.expanded}
|
|
229
|
+
onClick={this.handleClick}
|
|
230
|
+
/>
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
}
|
package/source/Drawer.tsx
CHANGED
|
@@ -1,62 +1,182 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
import classNames from 'classnames';
|
|
4
|
-
import {
|
|
4
|
+
import { action, observable } from 'mobx';
|
|
5
|
+
import { observer } from 'mobx-react';
|
|
6
|
+
import { Component, ComponentPropsWithRef, createRef, ElementType, ReactNode } from 'react';
|
|
5
7
|
|
|
6
8
|
import { CloseButton } from './CloseButton';
|
|
9
|
+
import { Breakpoint } from './Grid';
|
|
7
10
|
import { NativeDialog, NativeDialogProps } from './NativeDialog';
|
|
11
|
+
import { AsProps, polymorphic, PolymorphicClassProps } from './Polymorphic';
|
|
8
12
|
|
|
9
13
|
export type DrawerPlacement = 'start' | 'end' | 'top' | 'bottom';
|
|
10
14
|
|
|
11
|
-
export const DrawerHeader
|
|
12
|
-
|
|
15
|
+
export const DrawerHeader = polymorphic<ComponentPropsWithRef<'div'>>(
|
|
16
|
+
({ as: Root = 'div', className, ...props }) => (
|
|
17
|
+
<Root {...props} className={classNames('drawer-header', className)} />
|
|
18
|
+
)
|
|
13
19
|
);
|
|
20
|
+
DrawerHeader.displayName = 'DrawerHeader';
|
|
14
21
|
|
|
15
|
-
export const DrawerTitle
|
|
16
|
-
|
|
22
|
+
export const DrawerTitle = polymorphic<ComponentPropsWithRef<'h5'>>(
|
|
23
|
+
({ as: Root = 'h5', className, ...props }) => (
|
|
24
|
+
<Root {...props} className={classNames('drawer-title', className)} />
|
|
25
|
+
)
|
|
17
26
|
);
|
|
27
|
+
DrawerTitle.displayName = 'DrawerTitle';
|
|
18
28
|
|
|
19
|
-
export const DrawerBody
|
|
20
|
-
|
|
29
|
+
export const DrawerBody = polymorphic<ComponentPropsWithRef<'div'>>(
|
|
30
|
+
({ as: Root = 'div', className, ...props }) => (
|
|
31
|
+
<Root {...props} className={classNames('drawer-body', className)} />
|
|
32
|
+
)
|
|
21
33
|
);
|
|
34
|
+
DrawerBody.displayName = 'DrawerBody';
|
|
22
35
|
|
|
23
|
-
export const DrawerFooter
|
|
24
|
-
|
|
36
|
+
export const DrawerFooter = polymorphic<ComponentPropsWithRef<'div'>>(
|
|
37
|
+
({ as: Root = 'div', className, ...props }) => (
|
|
38
|
+
<Root {...props} className={classNames('drawer-footer', className)} />
|
|
39
|
+
)
|
|
25
40
|
);
|
|
41
|
+
DrawerFooter.displayName = 'DrawerFooter';
|
|
26
42
|
|
|
27
|
-
export interface DrawerProps extends Omit<
|
|
43
|
+
export interface DrawerProps extends Omit<
|
|
44
|
+
NativeDialogProps,
|
|
45
|
+
'dialog' | 'onClose' | 'title' | 'animation' | 'fullscreen'
|
|
46
|
+
> {
|
|
28
47
|
title?: ReactNode;
|
|
29
48
|
placement?: DrawerPlacement;
|
|
30
49
|
footer?: ReactNode;
|
|
31
50
|
bodyClassName?: string;
|
|
32
|
-
|
|
51
|
+
sheet?: boolean;
|
|
52
|
+
translucent?: boolean;
|
|
53
|
+
responsive?: Breakpoint;
|
|
54
|
+
fullscreen?: boolean;
|
|
55
|
+
animation?: 'slide' | 'instant';
|
|
56
|
+
onDismiss?: () => unknown;
|
|
33
57
|
}
|
|
34
58
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
<
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
const BREAKPOINT_WIDTHS: Record<Breakpoint, number> = {
|
|
60
|
+
sm: 576,
|
|
61
|
+
md: 768,
|
|
62
|
+
lg: 1024,
|
|
63
|
+
xl: 1280,
|
|
64
|
+
'2xl': 1536
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
@observer
|
|
68
|
+
export class Drawer<T extends ElementType | undefined = 'dialog'> extends Component<
|
|
69
|
+
PolymorphicClassProps<T, DrawerProps>
|
|
70
|
+
> {
|
|
71
|
+
declare readonly props: PolymorphicClassProps<T, DrawerProps>;
|
|
72
|
+
|
|
73
|
+
protected get componentProps(): Readonly<DrawerProps & AsProps> {
|
|
74
|
+
return this.props;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static displayName = 'Drawer';
|
|
78
|
+
|
|
79
|
+
@observable accessor expanded = false;
|
|
80
|
+
|
|
81
|
+
dialog = createRef<NativeDialog<ElementType>>();
|
|
82
|
+
media?: MediaQueryList;
|
|
83
|
+
dismissed = false;
|
|
84
|
+
|
|
85
|
+
componentDidMount() {
|
|
86
|
+
this.observeBreakpoint();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
componentDidUpdate({ responsive, open }: DrawerProps) {
|
|
90
|
+
if (responsive !== this.componentProps.responsive) this.observeBreakpoint();
|
|
91
|
+
|
|
92
|
+
if (open !== this.componentProps.open && this.componentProps.open) this.dismissed = false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
componentWillUnmount() {
|
|
96
|
+
this.media?.removeEventListener('change', this.handleBreakpoint);
|
|
97
|
+
}
|
|
61
98
|
|
|
62
|
-
|
|
99
|
+
observeBreakpoint = () => {
|
|
100
|
+
this.media?.removeEventListener('change', this.handleBreakpoint);
|
|
101
|
+
|
|
102
|
+
const { responsive } = this.componentProps;
|
|
103
|
+
this.media = responsive
|
|
104
|
+
? window.matchMedia(`(min-width: ${BREAKPOINT_WIDTHS[responsive]}px)`)
|
|
105
|
+
: undefined;
|
|
106
|
+
this.handleBreakpoint();
|
|
107
|
+
this.media?.addEventListener('change', this.handleBreakpoint);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
@action
|
|
111
|
+
handleBreakpoint = () => (this.expanded = this.media?.matches ?? false);
|
|
112
|
+
|
|
113
|
+
handleDismiss = () => {
|
|
114
|
+
this.componentProps.onDismiss?.();
|
|
115
|
+
|
|
116
|
+
this.dismissed = true;
|
|
117
|
+
this.dialog.current?.close();
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
handleClose = () => {
|
|
121
|
+
if (!this.dismissed && !this.expanded) this.componentProps.onDismiss?.();
|
|
122
|
+
|
|
123
|
+
this.dismissed = false;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
render() {
|
|
127
|
+
const {
|
|
128
|
+
as: _as,
|
|
129
|
+
title,
|
|
130
|
+
placement = 'end',
|
|
131
|
+
footer,
|
|
132
|
+
bodyClassName,
|
|
133
|
+
onDismiss: _onDismiss,
|
|
134
|
+
sheet,
|
|
135
|
+
translucent,
|
|
136
|
+
responsive,
|
|
137
|
+
animation = 'slide',
|
|
138
|
+
fullscreen,
|
|
139
|
+
open,
|
|
140
|
+
modal,
|
|
141
|
+
scroll,
|
|
142
|
+
backdrop,
|
|
143
|
+
className,
|
|
144
|
+
children,
|
|
145
|
+
...props
|
|
146
|
+
} = this.componentProps;
|
|
147
|
+
|
|
148
|
+
return (
|
|
149
|
+
<NativeDialog
|
|
150
|
+
as={this.componentProps.as}
|
|
151
|
+
{...props}
|
|
152
|
+
ref={this.dialog}
|
|
153
|
+
open={this.expanded || open}
|
|
154
|
+
modal={this.expanded ? false : (modal ?? !(scroll && backdrop === false))}
|
|
155
|
+
scroll={scroll}
|
|
156
|
+
backdrop={backdrop}
|
|
157
|
+
dialog={false}
|
|
158
|
+
tabIndex={-1}
|
|
159
|
+
aria-label={typeof title === 'string' ? title : props['aria-label']}
|
|
160
|
+
className={classNames(
|
|
161
|
+
responsive ? `${responsive}:drawer` : 'drawer',
|
|
162
|
+
`drawer-${placement}`,
|
|
163
|
+
{
|
|
164
|
+
'drawer-sheet': sheet,
|
|
165
|
+
'drawer-translucent': translucent,
|
|
166
|
+
'drawer-instant': animation === 'instant',
|
|
167
|
+
'drawer-fullscreen': fullscreen === true
|
|
168
|
+
},
|
|
169
|
+
className
|
|
170
|
+
)}
|
|
171
|
+
onClose={this.handleClose}
|
|
172
|
+
>
|
|
173
|
+
<DrawerHeader>
|
|
174
|
+
<DrawerTitle>{title}</DrawerTitle>
|
|
175
|
+
<CloseButton onClick={this.handleDismiss} />
|
|
176
|
+
</DrawerHeader>
|
|
177
|
+
<DrawerBody className={bodyClassName}>{children}</DrawerBody>
|
|
178
|
+
{footer && <DrawerFooter>{footer}</DrawerFooter>}
|
|
179
|
+
</NativeDialog>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -3,11 +3,14 @@
|
|
|
3
3
|
import {
|
|
4
4
|
createRef,
|
|
5
5
|
DetailedHTMLProps,
|
|
6
|
+
ElementType,
|
|
6
7
|
HTMLAttributes,
|
|
7
8
|
PropsWithChildren,
|
|
8
9
|
PureComponent
|
|
9
10
|
} from 'react';
|
|
10
11
|
|
|
12
|
+
import { AsProps, PolymorphicClassProps } from '../Polymorphic';
|
|
13
|
+
|
|
11
14
|
export type ClickBoundaryProps = PropsWithChildren<
|
|
12
15
|
{
|
|
13
16
|
onInnerClick?: (event: MouseEvent) => unknown;
|
|
@@ -15,11 +18,19 @@ export type ClickBoundaryProps = PropsWithChildren<
|
|
|
15
18
|
} & DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
|
|
16
19
|
>;
|
|
17
20
|
|
|
18
|
-
export class ClickBoundary extends PureComponent<
|
|
21
|
+
export class ClickBoundary<T extends ElementType | undefined = 'div'> extends PureComponent<
|
|
22
|
+
PolymorphicClassProps<T, ClickBoundaryProps>
|
|
23
|
+
> {
|
|
24
|
+
declare readonly props: PolymorphicClassProps<T, ClickBoundaryProps>;
|
|
25
|
+
|
|
26
|
+
protected get componentProps(): Readonly<ClickBoundaryProps & AsProps> {
|
|
27
|
+
return this.props;
|
|
28
|
+
}
|
|
29
|
+
|
|
19
30
|
root = createRef<HTMLDivElement>();
|
|
20
31
|
|
|
21
32
|
switchClick = (event: MouseEvent) => {
|
|
22
|
-
const { onInnerClick, onOuterClick } = this.
|
|
33
|
+
const { onInnerClick, onOuterClick } = this.componentProps;
|
|
23
34
|
|
|
24
35
|
if (this.root.current?.contains(event.target as Node)) onInnerClick?.(event);
|
|
25
36
|
else onOuterClick?.(event);
|
|
@@ -34,12 +45,20 @@ export class ClickBoundary extends PureComponent<ClickBoundaryProps> {
|
|
|
34
45
|
}
|
|
35
46
|
|
|
36
47
|
render() {
|
|
37
|
-
const
|
|
48
|
+
const Root: ElementType = this.componentProps.as ?? 'div';
|
|
49
|
+
|
|
50
|
+
const {
|
|
51
|
+
as: _as,
|
|
52
|
+
children,
|
|
53
|
+
onInnerClick: _,
|
|
54
|
+
onOuterClick: __,
|
|
55
|
+
...props
|
|
56
|
+
} = this.componentProps;
|
|
38
57
|
|
|
39
58
|
return (
|
|
40
|
-
<
|
|
59
|
+
<Root ref={this.root} {...props}>
|
|
41
60
|
{children}
|
|
42
|
-
</
|
|
61
|
+
</Root>
|
|
43
62
|
);
|
|
44
63
|
}
|
|
45
64
|
}
|