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.
Files changed (130) hide show
  1. package/AGENTS.md +19 -2
  2. package/ReadMe.md +95 -54
  3. package/dist/DatePickerPlugin-CpGuQRsi.js +1 -0
  4. package/dist/DatePickerPlugin-DB3nnslN.mjs +1 -0
  5. package/dist/index.d.mts +1232 -474
  6. package/dist/index.d.ts +1232 -474
  7. package/dist/index.js +16 -16
  8. package/dist/index.mjs +15 -15
  9. package/dist/rolldown-runtime-5iZMYShA.js +1 -0
  10. package/dist/style.css +66 -66
  11. package/package.json +1 -1
  12. package/source/Accordion.tsx +161 -0
  13. package/source/Alert.tsx +153 -0
  14. package/source/Avatar.tsx +57 -46
  15. package/source/Badge.tsx +17 -15
  16. package/source/Breadcrumb.tsx +50 -0
  17. package/source/Button.tsx +69 -40
  18. package/source/ButtonGroup.tsx +23 -18
  19. package/source/Card.tsx +86 -17
  20. package/source/Carousel/index.tsx +392 -0
  21. package/source/Carousel/model.ts +113 -0
  22. package/source/Carousel/scroll.ts +79 -0
  23. package/source/CloseButton.tsx +20 -15
  24. package/source/Collapse.tsx +233 -0
  25. package/source/Drawer.tsx +158 -38
  26. package/source/{ClickBoundary.tsx → Extension/ClickBoundary.tsx} +24 -5
  27. package/source/Extension/CodeBlock.tsx +64 -0
  28. package/source/{Countdown.tsx → Extension/Countdown.tsx} +21 -9
  29. package/source/{Dialog.tsx → Extension/Dialog.tsx} +10 -6
  30. package/source/Extension/Editor.tsx +78 -0
  31. package/source/Extension/EditorHTML.tsx +20 -0
  32. package/source/{HorizontalMarquee → Extension/HorizontalMarquee}/index.module.less +1 -3
  33. package/source/Extension/HorizontalMarquee/index.tsx +85 -0
  34. package/source/Extension/Icon.tsx +19 -0
  35. package/source/Extension/Loading.tsx +30 -0
  36. package/source/{MonthCalendar.tsx → Extension/MonthCalendar.tsx} +29 -13
  37. package/source/{Nameplate.tsx → Extension/Nameplate.tsx} +1 -1
  38. package/source/Extension/OverlayBox.tsx +293 -0
  39. package/source/{PageNav.tsx → Extension/PageNav.tsx} +43 -16
  40. package/source/{ScrollNav → Extension/ScrollNav}/Scroller.tsx +21 -5
  41. package/source/Extension/ScrollNav/index.tsx +47 -0
  42. package/source/Extension/Select.tsx +128 -0
  43. package/source/{ShareBox.tsx → Extension/ShareBox.tsx} +1 -1
  44. package/source/Extension/SpinnerButton.tsx +30 -0
  45. package/source/Extension/TableSpinner.tsx +22 -0
  46. package/source/Extension/TextTruncate/index.tsx +29 -0
  47. package/source/Extension/Time.tsx +17 -0
  48. package/source/Extension/TimeDistance.tsx +45 -0
  49. package/source/Extension/Timeline/index.tsx +63 -0
  50. package/source/Extension/TypeEcho/Content.tsx +123 -0
  51. package/source/Extension/TypeEcho/index.tsx +22 -0
  52. package/source/Extension/UserRank/Address.tsx +22 -0
  53. package/source/Extension/UserRank/index.tsx +106 -0
  54. package/source/Extension/VerticalMarquee/index.tsx +23 -0
  55. package/source/Extension/ZodiacBar.tsx +59 -0
  56. package/source/Extension/index.ts +27 -0
  57. package/source/Form/Adorn.tsx +45 -26
  58. package/source/Form/Checkbox.tsx +20 -11
  59. package/source/Form/ChipInput.tsx +129 -0
  60. package/source/Form/Chips.tsx +57 -37
  61. package/source/Form/Combobox.tsx +362 -78
  62. package/source/Form/Control.tsx +72 -42
  63. package/source/Form/DatePicker.tsx +245 -45
  64. package/source/Form/DatePickerPlugin.ts +37 -0
  65. package/source/Form/Feedback.tsx +21 -16
  66. package/source/Form/FloatingLabel.tsx +21 -7
  67. package/source/Form/InputGroup.tsx +162 -11
  68. package/source/Form/OTP.tsx +156 -45
  69. package/source/Form/PasswordStrength.tsx +43 -17
  70. package/source/Form/Plugin.tsx +53 -0
  71. package/source/Form/Primitives.tsx +70 -20
  72. package/source/Form/Radio.tsx +35 -40
  73. package/source/Form/Range.tsx +125 -56
  74. package/source/Form/Switch.tsx +53 -27
  75. package/source/Form/index.ts +1 -0
  76. package/source/Grid.tsx +68 -56
  77. package/source/Image.tsx +10 -6
  78. package/source/ListGroup.tsx +59 -58
  79. package/source/Menu.tsx +317 -46
  80. package/source/NativeDialog/index.tsx +148 -22
  81. package/source/Nav.tsx +49 -33
  82. package/source/NavOverflow/index.tsx +323 -0
  83. package/source/NavOverflow/model.ts +21 -0
  84. package/source/Navbar.tsx +65 -45
  85. package/source/Overlay.ts +163 -1
  86. package/source/Pagination.tsx +86 -0
  87. package/source/Placeholder.tsx +70 -0
  88. package/source/Polymorphic.ts +57 -0
  89. package/source/Popover.tsx +34 -26
  90. package/source/Progress/index.tsx +81 -0
  91. package/source/Progress/style.module.css +15 -0
  92. package/source/ScrollSpy/index.tsx +380 -0
  93. package/source/ScrollSpy/model.ts +17 -0
  94. package/source/Spinner.tsx +31 -17
  95. package/source/Stack.tsx +25 -0
  96. package/source/Stepper.tsx +91 -0
  97. package/source/Tab.tsx +261 -0
  98. package/source/Table.tsx +53 -42
  99. package/source/Toast.tsx +93 -46
  100. package/source/ToggleButton.tsx +47 -30
  101. package/source/Toggler.tsx +117 -0
  102. package/source/Tooltip.tsx +31 -24
  103. package/source/index.ts +17 -27
  104. package/source/CodeBlock.tsx +0 -40
  105. package/source/Editor.tsx +0 -43
  106. package/source/EditorHTML.tsx +0 -16
  107. package/source/HorizontalMarquee/index.tsx +0 -61
  108. package/source/Icon.tsx +0 -24
  109. package/source/Loading.tsx +0 -22
  110. package/source/OverlayBox.tsx +0 -208
  111. package/source/ScrollNav/index.tsx +0 -22
  112. package/source/Select.tsx +0 -95
  113. package/source/SpinnerButton.tsx +0 -35
  114. package/source/TableSpinner.tsx +0 -26
  115. package/source/TextTruncate/index.tsx +0 -25
  116. package/source/Time.tsx +0 -15
  117. package/source/TimeDistance.tsx +0 -41
  118. package/source/Timeline/index.tsx +0 -60
  119. package/source/TypeEcho/Content.tsx +0 -58
  120. package/source/TypeEcho/index.tsx +0 -14
  121. package/source/UserRank/Address.tsx +0 -20
  122. package/source/UserRank/index.tsx +0 -111
  123. package/source/VerticalMarquee/index.tsx +0 -25
  124. package/source/ZodiacBar.tsx +0 -51
  125. /package/source/{LiveTSX.tsx → Extension/LiveTSX.tsx} +0 -0
  126. /package/source/{ScrollNav → Extension/ScrollNav}/index.module.less +0 -0
  127. /package/source/{TextTruncate → Extension/TextTruncate}/Content.tsx +0 -0
  128. /package/source/{Timeline → Extension/Timeline}/index.module.less +0 -0
  129. /package/source/{UserRank → Extension/UserRank}/index.module.less +0 -0
  130. /package/source/{VerticalMarquee → Extension/VerticalMarquee}/index.module.less +0 -0
package/source/Tab.tsx ADDED
@@ -0,0 +1,261 @@
1
+ 'use client';
2
+
3
+ import classNames from 'classnames';
4
+ import { action, observable } from 'mobx';
5
+ import { observer } from 'mobx-react';
6
+ import {
7
+ Children,
8
+ cloneElement,
9
+ Component,
10
+ ComponentPropsWithRef,
11
+ createRef,
12
+ ElementType,
13
+ isValidElement,
14
+ KeyboardEvent,
15
+ MouseEvent,
16
+ ReactNode
17
+ } from 'react';
18
+
19
+ import { AsProps, polymorphic, PolymorphicClassProps } from './Polymorphic';
20
+
21
+ export interface TabProps extends ComponentPropsWithRef<'div'> {
22
+ eventKey: string;
23
+ label: ReactNode;
24
+ disabled?: boolean;
25
+ }
26
+
27
+ export const Tab = polymorphic<TabProps>(
28
+ ({ as: Root = 'div', eventKey: _eventKey, label: _label, disabled: _disabled, ...props }) => (
29
+ <Root {...props} />
30
+ )
31
+ );
32
+ Tab.displayName = 'Tab';
33
+
34
+ export interface TabsProps extends Omit<ComponentPropsWithRef<'div'>, 'onChange'> {
35
+ activeKey?: string;
36
+ defaultActiveKey?: string;
37
+ fade?: boolean;
38
+ id: string;
39
+ onChange?: (key: string) => unknown;
40
+ orientation?: 'horizontal' | 'vertical';
41
+ variant?: 'tabs' | 'pills' | 'underline' | 'list-group';
42
+ }
43
+
44
+ @observer
45
+ export class Tabs<T extends ElementType | undefined = 'div'> extends Component<
46
+ PolymorphicClassProps<T, TabsProps>
47
+ > {
48
+ static displayName = 'Tabs';
49
+
50
+ declare readonly props: PolymorphicClassProps<T, TabsProps>;
51
+
52
+ protected get componentProps(): Readonly<TabsProps & AsProps> {
53
+ return this.props;
54
+ }
55
+
56
+ get tabs() {
57
+ return Children.toArray(this.componentProps.children)
58
+ .filter(child => typeof child !== 'string' || child.trim())
59
+ .map(child => {
60
+ // Server component boundaries may wrap Tab in a lazy client reference.
61
+ if (
62
+ !isValidElement<TabProps>(child) ||
63
+ typeof child.props.eventKey !== 'string' ||
64
+ !('label' in child.props)
65
+ )
66
+ throw new TypeError('Tabs children must be Tab elements.');
67
+
68
+ return child;
69
+ });
70
+ }
71
+
72
+ root = createRef<HTMLDivElement>();
73
+
74
+ @observable accessor selectedKey =
75
+ this.componentProps.defaultActiveKey ??
76
+ this.tabs.find(({ props }) => !props.disabled)?.props.eventKey ??
77
+ this.tabs[0]?.props.eventKey ??
78
+ '';
79
+
80
+ get activeKey() {
81
+ const key = this.componentProps.activeKey ?? this.selectedKey;
82
+
83
+ return this.tabs.some(({ props }) => props.eventKey === key && !props.disabled)
84
+ ? key
85
+ : (this.tabs.find(({ props }) => !props.disabled)?.props.eventKey ?? '');
86
+ }
87
+
88
+ handleClick = (event: MouseEvent<HTMLDivElement>) => {
89
+ this.componentProps.onClick?.(event);
90
+
91
+ if (event.defaultPrevented) return;
92
+
93
+ const button = (event.target as Element).closest<HTMLButtonElement>('[data-tab-index]');
94
+ const tab = button && this.tabs[Number(button.dataset.tabIndex)]?.props;
95
+
96
+ if (!tab || tab.disabled) return;
97
+
98
+ this.activate(tab.eventKey);
99
+ };
100
+
101
+ handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
102
+ this.componentProps.onKeyDown?.(event);
103
+
104
+ if (event.defaultPrevented) return;
105
+
106
+ const button = (event.target as Element).closest<HTMLButtonElement>('[data-tab-index]');
107
+ const index = Number(button?.dataset.tabIndex);
108
+ const { tabs } = this;
109
+ if (!button || Number.isNaN(index)) return;
110
+
111
+ const horizontal = this.componentProps.orientation !== 'vertical';
112
+ const previous = horizontal ? event.key === 'ArrowLeft' : event.key === 'ArrowUp';
113
+ const next = horizontal ? event.key === 'ArrowRight' : event.key === 'ArrowDown';
114
+ const enabled = tabs
115
+ .map(({ props }, tabIndex) => (props.disabled ? -1 : tabIndex))
116
+ .filter(tabIndex => tabIndex >= 0);
117
+ const currentPosition = enabled.indexOf(index);
118
+ const destination =
119
+ event.key === 'Home'
120
+ ? enabled[0]
121
+ : event.key === 'End'
122
+ ? enabled.at(-1)
123
+ : previous
124
+ ? enabled[(currentPosition - 1 + enabled.length) % enabled.length]
125
+ : next
126
+ ? enabled[(currentPosition + 1) % enabled.length]
127
+ : undefined;
128
+ if (destination === undefined) return;
129
+
130
+ event.preventDefault();
131
+ const tab = tabs[destination];
132
+
133
+ this.activate(tab.props.eventKey);
134
+ this.root.current
135
+ ?.querySelector<HTMLButtonElement>(`[data-tab-index="${destination}"]`)
136
+ ?.focus();
137
+ };
138
+
139
+ @action
140
+ activate = (key: string) => {
141
+ this.componentProps.onChange?.(key);
142
+
143
+ if (this.componentProps.activeKey === undefined) this.selectedKey = key;
144
+ };
145
+
146
+ tabId = (key: string) => `${this.componentProps.id}-tab-${encodeURIComponent(key)}`;
147
+
148
+ panelId = (key: string) => `${this.componentProps.id}-panel-${encodeURIComponent(key)}`;
149
+
150
+ renderTabs = () => {
151
+ const { orientation = 'horizontal', variant = 'tabs' } = this.componentProps;
152
+ const listGroup = variant === 'list-group';
153
+
154
+ return (
155
+ <div
156
+ className={classNames(
157
+ listGroup ? 'list-group' : 'nav',
158
+ !listGroup && `nav-${variant}`,
159
+ orientation === 'vertical' &&
160
+ (listGroup
161
+ ? 'flex-column'
162
+ : variant === 'pills'
163
+ ? 'nav-pills-vertical'
164
+ : 'flex-column')
165
+ )}
166
+ role="tablist"
167
+ aria-label={this.componentProps['aria-label'] ?? 'Tabs'}
168
+ aria-orientation={orientation}
169
+ >
170
+ {this.tabs.map(({ props: tab }, index) => {
171
+ const active = tab.eventKey === this.activeKey;
172
+
173
+ return (
174
+ <button
175
+ key={tab.eventKey}
176
+ id={this.tabId(tab.eventKey)}
177
+ type="button"
178
+ role="tab"
179
+ data-tab-index={index}
180
+ aria-controls={this.panelId(tab.eventKey)}
181
+ aria-selected={active}
182
+ aria-disabled={tab.disabled || undefined}
183
+ tabIndex={active ? 0 : -1}
184
+ disabled={tab.disabled}
185
+ className={classNames(
186
+ listGroup ? 'list-group-item list-group-item-action' : 'nav-link',
187
+ { active },
188
+ tab.disabled && 'disabled'
189
+ )}
190
+ >
191
+ {tab.label}
192
+ </button>
193
+ );
194
+ })}
195
+ </div>
196
+ );
197
+ };
198
+
199
+ renderPanels = () => {
200
+ const { fade = true } = this.componentProps;
201
+
202
+ return (
203
+ <div className="tab-content">
204
+ {this.tabs.map(tab => {
205
+ const { eventKey, className } = tab.props;
206
+ const active = eventKey === this.activeKey;
207
+
208
+ return cloneElement(tab, {
209
+ key: eventKey,
210
+ id: this.panelId(eventKey),
211
+ role: 'tabpanel',
212
+ 'aria-labelledby': this.tabId(eventKey),
213
+ 'aria-hidden': !active,
214
+ tabIndex: tab.props.tabIndex ?? 0,
215
+ className: classNames(
216
+ 'tab-pane',
217
+ { active, show: active && fade },
218
+ !fade && 'tab-pane-instant',
219
+ className
220
+ ),
221
+ hidden: !active
222
+ });
223
+ })}
224
+ </div>
225
+ );
226
+ };
227
+
228
+ render() {
229
+ const Root: ElementType = this.componentProps.as ?? 'div';
230
+
231
+ const {
232
+ as: _as,
233
+ activeKey: _activeKey,
234
+ className,
235
+ defaultActiveKey: _defaultActiveKey,
236
+ fade: _fade,
237
+ onChange: _onChange,
238
+ onClick: _onClick,
239
+ onKeyDown: _onKeyDown,
240
+ id: _id,
241
+ orientation: _orientation,
242
+ children: _children,
243
+ variant: _variant,
244
+ ...props
245
+ } = this.componentProps;
246
+
247
+ return (
248
+ <Root
249
+ {...props}
250
+ ref={this.root}
251
+ id={this.componentProps.id}
252
+ className={className}
253
+ onClick={this.handleClick}
254
+ onKeyDown={this.handleKeyDown}
255
+ >
256
+ {this.renderTabs()}
257
+ {this.renderPanels()}
258
+ </Root>
259
+ );
260
+ }
261
+ }
package/source/Table.tsx CHANGED
@@ -1,7 +1,8 @@
1
1
  import classNames from 'classnames';
2
- import { ComponentPropsWithRef, FC } from 'react';
2
+ import { ComponentPropsWithRef } from 'react';
3
3
 
4
4
  import type { Breakpoint } from './Grid';
5
+ import { polymorphic } from './Polymorphic';
5
6
 
6
7
  export interface TableProps extends ComponentPropsWithRef<'table'> {
7
8
  striped?: boolean;
@@ -13,47 +14,57 @@ export interface TableProps extends ComponentPropsWithRef<'table'> {
13
14
  responsive?: boolean | Breakpoint;
14
15
  }
15
16
 
16
- export const Table: FC<TableProps> = ({
17
- striped,
18
- stripedColumns,
19
- hover,
20
- bordered,
21
- borderless,
22
- small,
23
- responsive,
24
- className,
25
- ...props
26
- }) => {
27
- const table = (
28
- <table
29
- {...props}
30
- className={classNames(
31
- 'table',
32
- {
33
- 'table-striped': striped,
34
- 'table-striped-columns': stripedColumns,
35
- 'table-hover': hover,
36
- 'table-bordered': bordered,
37
- 'table-borderless': borderless,
38
- 'table-sm': small
39
- },
40
- className
41
- )}
42
- />
43
- );
17
+ export const Table = polymorphic<TableProps>(
18
+ ({
19
+ as: RootAs,
20
+ striped,
21
+ stripedColumns,
22
+ hover,
23
+ bordered,
24
+ borderless,
25
+ small,
26
+ responsive,
27
+ className,
28
+ children,
29
+ ...props
30
+ }) => {
31
+ const Root = RootAs ?? 'div';
32
+ const TableRoot = !responsive && RootAs ? RootAs : 'table';
44
33
 
45
- return !responsive ? (
46
- table
47
- ) : (
48
- <div
49
- className={classNames(
50
- 'table-responsive',
51
- responsive !== true && `${responsive}:table-responsive`
52
- )}
53
- >
54
- {table}
55
- </div>
56
- );
57
- };
34
+ const table = (
35
+ <TableRoot
36
+ {...(responsive && RootAs ? {} : props)}
37
+ className={classNames(
38
+ 'table',
39
+ {
40
+ 'table-striped': striped,
41
+ 'table-striped-columns': stripedColumns,
42
+ 'table-hover': hover,
43
+ 'table-bordered': bordered,
44
+ 'table-borderless': borderless,
45
+ 'table-sm': small
46
+ },
47
+ responsive && RootAs ? undefined : className
48
+ )}
49
+ >
50
+ {children}
51
+ </TableRoot>
52
+ );
58
53
 
54
+ return !responsive ? (
55
+ table
56
+ ) : (
57
+ <Root
58
+ {...(RootAs ? props : {})}
59
+ className={classNames(
60
+ 'table-responsive',
61
+ responsive !== true && `${responsive}:table-responsive`,
62
+ RootAs && className
63
+ )}
64
+ >
65
+ {table}
66
+ </Root>
67
+ );
68
+ }
69
+ );
59
70
  Table.displayName = 'Table';
package/source/Toast.tsx CHANGED
@@ -7,6 +7,8 @@ import { ComponentPropsWithRef, FC, MouseEvent, ReactNode } from 'react';
7
7
  import { Timestamp, uniqueID } from 'web-utility';
8
8
 
9
9
  import type { ComponentTheme } from './Button';
10
+ import { CloseButton } from './CloseButton';
11
+ import { polymorphic } from './Polymorphic';
10
12
 
11
13
  export type ToastPosition =
12
14
  | 'top-start'
@@ -31,11 +33,13 @@ const TOAST_POSITION_CLASSES: Record<ToastPosition, string> = {
31
33
  'bottom-end': 'bottom-0 end-0'
32
34
  };
33
35
 
34
- export interface ToastMessage {
36
+ export interface ToastMessage extends Pick<
37
+ ToastItemProps,
38
+ 'theme' | 'translucent' | 'instant' | 'role'
39
+ > {
35
40
  id?: string;
36
41
  title?: ReactNode;
37
42
  body: ReactNode;
38
- theme?: ComponentTheme;
39
43
  delay?: number;
40
44
  }
41
45
 
@@ -50,50 +54,80 @@ export interface ToastContainerProps extends ComponentPropsWithRef<'div'> {
50
54
  position?: ToastPosition | 'static';
51
55
  }
52
56
 
53
- export const ToastContainer: FC<ToastContainerProps> = ({
54
- position = 'top-end',
55
- className,
56
- 'aria-live': ariaLive = 'polite',
57
- 'aria-atomic': ariaAtomic = true,
58
- ...props
59
- }) => {
60
- const isStatic = position === 'static';
61
-
62
- return (
63
- <div
64
- {...props}
65
- className={classNames(
66
- 'toast-container',
67
- !isStatic && 'position-fixed',
68
- !isStatic && 'p-3',
69
- isStatic ? 'position-static' : TOAST_POSITION_CLASSES[position],
70
- className
71
- )}
72
- aria-live={ariaLive}
73
- aria-atomic={ariaAtomic}
74
- />
75
- );
76
- };
57
+ export const ToastContainer = polymorphic<ToastContainerProps>(
58
+ ({
59
+ as: Root = 'div',
60
+ position = 'top-end',
61
+ className,
62
+ 'aria-live': ariaLive = 'polite',
63
+ 'aria-atomic': ariaAtomic = true,
64
+ ...props
65
+ }) => {
66
+ const isStatic = position === 'static';
67
+
68
+ return (
69
+ <Root
70
+ {...props}
71
+ className={classNames(
72
+ 'toast-container',
73
+ !isStatic && 'position-fixed',
74
+ !isStatic && 'p-3',
75
+ isStatic ? 'position-static' : TOAST_POSITION_CLASSES[position],
76
+ className
77
+ )}
78
+ aria-live={ariaLive}
79
+ aria-atomic={ariaAtomic}
80
+ />
81
+ );
82
+ }
83
+ );
84
+ ToastContainer.displayName = 'ToastContainer';
77
85
 
78
86
  export interface ToastItemProps extends ComponentPropsWithRef<'div'> {
79
87
  theme?: ComponentTheme;
88
+ show?: boolean;
89
+ translucent?: boolean;
90
+ instant?: boolean;
80
91
  }
81
92
 
82
- export const ToastItem: FC<ToastItemProps> = ({ theme, role = 'status', className, ...props }) => (
83
- <div
84
- {...props}
85
- className={classNames('toast show', theme && `theme-${theme}`, className)}
86
- role={role}
87
- />
93
+ export const ToastItem = polymorphic<ToastItemProps>(
94
+ ({
95
+ as: Root = 'div',
96
+ theme,
97
+ show = true,
98
+ translucent,
99
+ instant,
100
+ role = 'status',
101
+ className,
102
+ ...props
103
+ }) => (
104
+ <Root
105
+ {...props}
106
+ className={classNames(
107
+ 'toast',
108
+ { show, 'toast-translucent': translucent, 'toast-instant': instant },
109
+ theme && `theme-${theme}`,
110
+ className
111
+ )}
112
+ role={role}
113
+ />
114
+ )
88
115
  );
116
+ ToastItem.displayName = 'ToastItem';
89
117
 
90
- export const ToastHeader: FC<ComponentPropsWithRef<'div'>> = ({ className, ...props }) => (
91
- <div {...props} className={classNames('toast-header', className)} />
118
+ export const ToastHeader = polymorphic<ComponentPropsWithRef<'div'>>(
119
+ ({ as: Root = 'div', className, ...props }) => (
120
+ <Root {...props} className={classNames('toast-header', className)} />
121
+ )
92
122
  );
123
+ ToastHeader.displayName = 'ToastHeader';
93
124
 
94
- export const ToastBody: FC<ComponentPropsWithRef<'div'>> = ({ className, ...props }) => (
95
- <div {...props} className={classNames('toast-body', className)} />
125
+ export const ToastBody = polymorphic<ComponentPropsWithRef<'div'>>(
126
+ ({ as: Root = 'div', className, ...props }) => (
127
+ <Root {...props} className={classNames('toast-body', className)} />
128
+ )
96
129
  );
130
+ ToastBody.displayName = 'ToastBody';
97
131
 
98
132
  export class Toast {
99
133
  @observable
@@ -105,22 +139,33 @@ export class Toast {
105
139
  </ToastContainer>
106
140
  ));
107
141
 
108
- renderMessage = ({ id, openedAt, title, body, theme, delay = 3000 }: ToastEntry) => {
142
+ renderMessage = ({
143
+ id,
144
+ openedAt,
145
+ title,
146
+ body,
147
+ theme,
148
+ translucent,
149
+ instant,
150
+ role,
151
+ delay = 3000
152
+ }: ToastEntry) => {
109
153
  const showHeader = !!title || delay === 0;
110
154
  const elapsed = this.TimeScalar.distanceOf<Timestamp>(Date.now(), openedAt).toShortString();
111
155
 
112
156
  return (
113
- <ToastItem key={id} theme={theme}>
157
+ <ToastItem
158
+ key={id}
159
+ theme={theme}
160
+ translucent={translucent}
161
+ instant={instant}
162
+ role={role}
163
+ >
114
164
  {showHeader && (
115
165
  <ToastHeader>
116
166
  <strong className="me-auto">{title}</strong>
117
167
  <small>{elapsed}</small>
118
- <button
119
- type="button"
120
- className="btn-close"
121
- aria-label="Close"
122
- data-toast-dismiss={id}
123
- />
168
+ <CloseButton data-toast-dismiss={id} />
124
169
  </ToastHeader>
125
170
  )}
126
171
  <ToastBody>{body}</ToastBody>
@@ -141,7 +186,9 @@ export class Toast {
141
186
 
142
187
  @action
143
188
  open(message: ToastMessage) {
144
- const id = uniqueID();
189
+ const id = message.id ?? uniqueID();
190
+ this.close(id);
191
+
145
192
  const openedAt = Date.now();
146
193
  const { promise, resolve } = Promise.withResolvers<ToastEntry>();
147
194
  const timer =
@@ -149,7 +196,7 @@ export class Toast {
149
196
  ? undefined
150
197
  : setTimeout(() => this.close(id), message.delay ?? 3000);
151
198
 
152
- const item: ToastEntry = { id, ...message, openedAt, resolve, timer };
199
+ const item: ToastEntry = { ...message, id, openedAt, resolve, timer };
153
200
 
154
201
  this.messages = [...this.messages, item];
155
202
 
@@ -1,7 +1,8 @@
1
1
  import classNames from 'classnames';
2
- import { ComponentPropsWithRef, FC } from 'react';
2
+ import { ComponentPropsWithRef } from 'react';
3
3
 
4
4
  import type { ComponentTheme } from './Button';
5
+ import { polymorphic } from './Polymorphic';
5
6
 
6
7
  export interface ToggleButtonProps extends Omit<ComponentPropsWithRef<'label'>, 'onChange'> {
7
8
  type?: 'checkbox' | 'radio';
@@ -12,36 +13,52 @@ export interface ToggleButtonProps extends Omit<ComponentPropsWithRef<'label'>,
12
13
  checked?: boolean;
13
14
  defaultChecked?: boolean;
14
15
  disabled?: boolean;
16
+ required?: boolean;
17
+ size?: 'xs' | 'sm' | 'lg';
18
+ inputProps?: Omit<
19
+ ComponentPropsWithRef<'input'>,
20
+ 'type' | 'name' | 'value' | 'checked' | 'defaultChecked' | 'disabled' | 'required'
21
+ >;
22
+ onChange?: ComponentPropsWithRef<'input'>['onChange'];
15
23
  }
16
24
 
17
- export const ToggleButton: FC<ToggleButtonProps> = ({
18
- type = 'checkbox',
19
- variant = 'solid',
20
- theme,
21
- name,
22
- value,
23
- checked,
24
- defaultChecked,
25
- disabled,
26
- className,
27
- children,
28
- ...props
29
- }) => (
30
- <label
31
- {...props}
32
- className={classNames('btn-check', `btn-${variant}`, theme && `theme-${theme}`, className)}
33
- >
34
- <input
35
- type={type}
36
- name={name}
37
- value={value}
38
- checked={checked}
39
- defaultChecked={defaultChecked}
40
- disabled={disabled}
41
- autoComplete="off"
42
- />
43
- {children}
44
- </label>
25
+ export const ToggleButton = polymorphic<ToggleButtonProps>(
26
+ ({
27
+ as: Root = 'label',
28
+ type = 'checkbox',
29
+ variant = 'solid',
30
+ theme,
31
+ name,
32
+ value,
33
+ checked,
34
+ defaultChecked,
35
+ disabled,
36
+ required,
37
+ size,
38
+ inputProps,
39
+ onChange,
40
+ className,
41
+ children,
42
+ ...props
43
+ }) => (
44
+ <Root
45
+ {...props}
46
+ className={classNames(
47
+ 'btn-check',
48
+ `btn-${variant}`,
49
+ theme && `theme-${theme}`,
50
+ size && `btn-${size}`,
51
+ className
52
+ )}
53
+ >
54
+ <input
55
+ {...inputProps}
56
+ {...{ type, name, value, checked, defaultChecked, disabled, required }}
57
+ autoComplete="off"
58
+ onChange={onChange}
59
+ />
60
+ {children}
61
+ </Root>
62
+ )
45
63
  );
46
-
47
64
  ToggleButton.displayName = 'ToggleButton';