idea-react 3.0.0-beta.1 → 3.0.0-beta.3

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 (89) hide show
  1. package/AGENTS.md +12 -3
  2. package/ReadMe.md +34 -6
  3. package/dist/index.d.mts +313 -230
  4. package/dist/index.d.ts +313 -230
  5. package/dist/index.js +16 -16
  6. package/dist/index.mjs +15 -15
  7. package/dist/style.css +0 -3
  8. package/package.json +13 -13
  9. package/source/Accordion.tsx +61 -37
  10. package/source/Alert.tsx +30 -15
  11. package/source/Avatar.tsx +55 -50
  12. package/source/Badge.tsx +16 -16
  13. package/source/Breadcrumb.tsx +36 -22
  14. package/source/Button.tsx +63 -69
  15. package/source/ButtonGroup.tsx +21 -25
  16. package/source/Card.tsx +67 -59
  17. package/source/Carousel/index.tsx +22 -21
  18. package/source/CloseButton.tsx +20 -15
  19. package/source/Collapse.tsx +74 -19
  20. package/source/Drawer.tsx +40 -17
  21. package/source/Extension/ClickBoundary.tsx +24 -5
  22. package/source/Extension/CodeBlock.tsx +64 -0
  23. package/source/Extension/Countdown.tsx +21 -9
  24. package/source/Extension/Editor.tsx +53 -18
  25. package/source/Extension/EditorHTML.tsx +7 -3
  26. package/source/Extension/HorizontalMarquee/index.tsx +69 -45
  27. package/source/Extension/Icon.tsx +15 -14
  28. package/source/Extension/Loading.tsx +20 -17
  29. package/source/Extension/MonthCalendar.tsx +26 -10
  30. package/source/Extension/OverlayBox.tsx +28 -15
  31. package/source/Extension/PageNav.tsx +16 -7
  32. package/source/Extension/ScrollNav/Scroller.tsx +21 -5
  33. package/source/Extension/ScrollNav/index.tsx +32 -7
  34. package/source/Extension/Select.tsx +12 -11
  35. package/source/Extension/SpinnerButton.tsx +19 -24
  36. package/source/Extension/TableSpinner.tsx +10 -14
  37. package/source/Extension/TextTruncate/index.tsx +17 -13
  38. package/source/Extension/Time.tsx +7 -5
  39. package/source/Extension/TimeDistance.tsx +22 -18
  40. package/source/Extension/Timeline/index.tsx +37 -34
  41. package/source/Extension/TypeEcho/Content.tsx +79 -14
  42. package/source/Extension/TypeEcho/index.tsx +13 -5
  43. package/source/Extension/UserRank/Address.tsx +17 -15
  44. package/source/Extension/UserRank/index.tsx +79 -84
  45. package/source/Extension/VerticalMarquee/index.tsx +14 -16
  46. package/source/Extension/ZodiacBar.tsx +40 -32
  47. package/source/Form/Adorn.tsx +30 -32
  48. package/source/Form/Checkbox.tsx +9 -5
  49. package/source/Form/ChipInput.tsx +9 -5
  50. package/source/Form/Chips.tsx +41 -39
  51. package/source/Form/Combobox.tsx +80 -61
  52. package/source/Form/Control.tsx +53 -58
  53. package/source/Form/DatePicker.tsx +74 -65
  54. package/source/Form/Feedback.tsx +18 -19
  55. package/source/Form/FloatingLabel.tsx +16 -19
  56. package/source/Form/InputGroup.tsx +27 -26
  57. package/source/Form/OTP.tsx +30 -12
  58. package/source/Form/Primitives.tsx +47 -39
  59. package/source/Form/Radio.tsx +25 -22
  60. package/source/Form/Range.tsx +30 -12
  61. package/source/Form/Switch.tsx +73 -48
  62. package/source/Form/inputProps.ts +184 -0
  63. package/source/Grid.tsx +67 -59
  64. package/source/Image.tsx +10 -6
  65. package/source/ListGroup.tsx +57 -69
  66. package/source/Menu.tsx +128 -66
  67. package/source/NativeDialog/index.tsx +51 -29
  68. package/source/Nav.tsx +46 -46
  69. package/source/NavOverflow/index.tsx +26 -10
  70. package/source/Navbar.tsx +60 -55
  71. package/source/Pagination.tsx +63 -69
  72. package/source/Placeholder.tsx +47 -43
  73. package/source/Polymorphic.ts +57 -0
  74. package/source/Popover.tsx +34 -31
  75. package/source/Progress/index.tsx +59 -54
  76. package/source/ScrollSpy/index.tsx +48 -33
  77. package/source/Spinner.tsx +27 -23
  78. package/source/Stack.tsx +25 -0
  79. package/source/Stepper.tsx +46 -40
  80. package/source/Tab.tsx +40 -27
  81. package/source/Table.tsx +53 -42
  82. package/source/Toast.tsx +62 -47
  83. package/source/ToggleButton.tsx +65 -54
  84. package/source/Toggler.tsx +17 -8
  85. package/source/Tooltip.tsx +31 -29
  86. package/source/index.ts +2 -0
  87. package/tsconfig.json +2 -2
  88. package/source/Extension/CodeBlock/index.tsx +0 -58
  89. package/source/Extension/CodeBlock/style.module.css +0 -3
@@ -1,61 +1,85 @@
1
- import { FC, HTMLAttributes } from 'react';
1
+ import { HTMLAttributes } from 'react';
2
2
 
3
+ import { polymorphic } from '../../Polymorphic';
3
4
  import * as style from './index.module.less';
4
5
 
5
6
  export type HorizontalMarqueeProps = HTMLAttributes<HTMLDivElement> &
6
7
  Partial<Record<'maxWidth' | 'duration' | 'height', string>>;
7
8
 
8
- export const HorizontalMarquee: FC<HorizontalMarqueeProps> = ({
9
- className = '',
10
- children,
11
- maxWidth = '100%',
12
- duration,
13
- height,
14
- ...props
15
- }) => (
16
- <div className={`overflow-hidden max-w-100 ${className}`} {...props}>
17
- <div
18
- className={`d-inline-block align-top text-nowrap ${style.scrollWrap}`}
19
- style={{
20
- maxWidth,
21
- animationDuration: duration,
22
- height,
23
- lineHeight: height
24
- }}
25
- >
9
+ export const HorizontalMarquee = polymorphic<HorizontalMarqueeProps>(
10
+ ({
11
+ as: Root = 'div',
12
+ className = '',
13
+ children,
14
+ maxWidth = '100%',
15
+ duration,
16
+ height,
17
+ ...props
18
+ }) => (
19
+ <Root className={`overflow-hidden max-w-100 ${className}`} {...props}>
26
20
  <div
27
- className={`d-inline-block ${style.scrollItem}`}
28
- style={{ animationDuration: duration }}
21
+ className={`d-inline-block align-top text-nowrap ${style.scrollWrap}`}
22
+ style={{
23
+ maxWidth,
24
+ animationDuration: duration,
25
+ height,
26
+ lineHeight: height
27
+ }}
29
28
  >
30
- {children}
29
+ <div
30
+ className={`d-inline-block ${style.scrollItem}`}
31
+ style={{ animationDuration: duration }}
32
+ >
33
+ {children}
34
+ </div>
31
35
  </div>
32
- </div>
33
- </div>
36
+ </Root>
37
+ )
34
38
  );
35
39
 
36
- export const HorizontalMarqueeBox: FC<HorizontalMarqueeProps> = ({
37
- children,
38
- maxWidth,
39
- duration,
40
- height
41
- }) =>
42
- height ? (
43
- <div
44
- className={`overflow-hidden ${style.scrollHeightWrap}`}
45
- // @ts-expect-error remove after React 19 released
46
- style={{ maxWidth: maxWidth, '--scroll-height': height }}
47
- >
48
- <div className={`d-inline-block overflow-hidden max-w-100 ${style.scrollContainer}`}>
40
+ export const HorizontalMarqueeBox = polymorphic<HorizontalMarqueeProps>(
41
+ ({
42
+ as: RootAs,
43
+ children,
44
+ className = '',
45
+ style: rootStyle,
46
+ maxWidth,
47
+ duration,
48
+ height,
49
+ ...props
50
+ }) => {
51
+ const Root = RootAs ?? 'div';
52
+ return height ? (
53
+ <Root
54
+ {...props}
55
+ className={`overflow-hidden ${style.scrollHeightWrap} ${className}`}
56
+ style={{ maxWidth, '--scroll-height': height, ...rootStyle }}
57
+ >
49
58
  <div
50
- className={`d-block overflow-hidden text-wrap text-break ${style.unScrollItem}`}
59
+ className={`d-inline-block overflow-hidden max-w-100 ${style.scrollContainer}`}
51
60
  >
52
- {children}
61
+ <div
62
+ className={`d-block overflow-hidden text-wrap text-break ${style.unScrollItem}`}
63
+ >
64
+ {children}
65
+ </div>
66
+ <HorizontalMarquee duration={duration}>{children}</HorizontalMarquee>
53
67
  </div>
54
- <HorizontalMarquee duration={duration}>{children}</HorizontalMarquee>
55
- </div>
56
- </div>
57
- ) : (
58
- <HorizontalMarquee {...{ maxWidth, duration, height }}>{children}</HorizontalMarquee>
59
- );
68
+ </Root>
69
+ ) : (
70
+ <HorizontalMarquee
71
+ as={RootAs}
72
+ {...props}
73
+ className={className}
74
+ style={rootStyle}
75
+ {...{ maxWidth, duration, height }}
76
+ >
77
+ {children}
78
+ </HorizontalMarquee>
79
+ );
80
+ }
81
+ );
60
82
 
61
83
  HorizontalMarquee.displayName = 'HorizontalMarquee';
84
+
85
+ HorizontalMarqueeBox.displayName = 'HorizontalMarqueeBox';
@@ -1,18 +1,19 @@
1
1
  import classNames from 'classnames';
2
- import { FC, HTMLAttributes, PropsWithRef } from 'react';
2
+ import { ComponentPropsWithRef } from 'react';
3
3
 
4
- export type IconProps = PropsWithRef<
5
- HTMLAttributes<HTMLSpanElement> & {
6
- name: string;
7
- size?: number;
8
- }
9
- >;
10
- export const Icon: FC<IconProps> = ({ className, style, name, size, ...props }) => (
11
- <span
12
- className={classNames(`bi-${name}`, className)}
13
- style={{ fontSize: size && `${size}rem`, ...style }}
14
- {...props}
15
- />
16
- );
4
+ import { polymorphic } from '../Polymorphic';
17
5
 
6
+ export type IconProps = ComponentPropsWithRef<'span'> & {
7
+ name: string;
8
+ size?: number;
9
+ };
10
+ export const Icon = polymorphic<IconProps>(
11
+ ({ as: Root = 'span', className, style, name, size, ...props }) => (
12
+ <Root
13
+ className={classNames(`bi-${name}`, className)}
14
+ style={{ fontSize: size && `${size}rem`, ...style }}
15
+ {...props}
16
+ />
17
+ )
18
+ );
18
19
  Icon.displayName = 'Icon';
@@ -1,27 +1,30 @@
1
- import { FC, HTMLAttributes } from 'react';
1
+ import { HTMLAttributes } from 'react';
2
2
 
3
+ import { polymorphic } from '../Polymorphic';
3
4
  import { Spinner } from '../Spinner';
4
5
 
5
6
  export interface LoadingProps extends HTMLAttributes<HTMLDivElement> {
6
7
  opacity?: number;
7
8
  }
8
9
 
9
- export const Loading: FC<LoadingProps> = ({
10
- className = '',
11
- children = 'Loading...',
12
- opacity = 25,
13
- style,
14
- ...props
15
- }) => (
16
- <div
17
- className={`fixed-top h-100 w-100 d-flex justify-content-center align-items-center bg-black ${className}`}
18
- style={{ ...style, opacity: opacity / 100 }}
19
- {...props}
20
- >
21
- <Spinner theme="success" />
10
+ export const Loading = polymorphic<LoadingProps>(
11
+ ({
12
+ as: Root = 'div',
13
+ className = '',
14
+ children = 'Loading...',
15
+ opacity = 25,
16
+ style,
17
+ ...props
18
+ }) => (
19
+ <Root
20
+ className={`fixed-top h-100 w-100 d-flex justify-content-center align-items-center bg-black ${className}`}
21
+ style={{ ...style, opacity: opacity / 100 }}
22
+ {...props}
23
+ >
24
+ <Spinner theme="success" />
22
25
 
23
- <span className="ms-3 fg-white">{children}</span>
24
- </div>
26
+ <span className="ms-3 fg-white">{children}</span>
27
+ </Root>
28
+ )
25
29
  );
26
-
27
30
  Loading.displayName = 'Loading';
@@ -4,12 +4,13 @@ import classNames from 'classnames';
4
4
  import { action, computed, observable } from 'mobx';
5
5
  import { observer } from 'mobx-react';
6
6
  import { ObservedComponent } from 'mobx-react-helper';
7
- import { HTMLAttributes, MouseEvent, ReactNode } from 'react';
7
+ import { ElementType, HTMLAttributes, MouseEvent, ReactNode } from 'react';
8
8
  import { changeMonth, Day, formatDate, splitArray, TimeData } from 'web-utility';
9
9
 
10
10
  import { Badge } from '../Badge';
11
11
  import { Button, type ComponentTheme } from '../Button';
12
12
  import { text2color } from '../color';
13
+ import { AsProps, PolymorphicClassProps } from '../Polymorphic';
13
14
  import { OverlayBox } from './OverlayBox';
14
15
 
15
16
  export interface DateData {
@@ -33,12 +34,24 @@ export interface MonthCalendarProps extends Omit<
33
34
  * Re-implement from https://github.com/EasyWebApp/BootCell/blob/3d30027a97fe0a8c4ab8fabc8dfef22aede04de7/source/Calendar/MonthCalendar.tsx
34
35
  */
35
36
  @observer
36
- export class MonthCalendar extends ObservedComponent<MonthCalendarProps> {
37
+ export class MonthCalendar<T extends ElementType | undefined = 'table'> extends ObservedComponent<
38
+ PolymorphicClassProps<T, MonthCalendarProps>
39
+ > {
40
+ declare readonly props: PolymorphicClassProps<T, MonthCalendarProps>;
41
+
42
+ protected get componentProps(): Readonly<MonthCalendarProps & AsProps> {
43
+ return this.props;
44
+ }
45
+
46
+ protected get observedComponentProps(): MonthCalendarProps & AsProps {
47
+ return this.observedProps;
48
+ }
49
+
37
50
  static displayName = 'MonthCalendar';
38
51
 
39
52
  @computed
40
53
  get weekFormatter() {
41
- const { locale = globalThis.navigator?.language } = this.observedProps;
54
+ const { locale = globalThis.navigator?.language } = this.observedComponentProps;
42
55
 
43
56
  return new Intl.DateTimeFormat(locale, { weekday: 'long' });
44
57
  }
@@ -63,7 +76,7 @@ export class MonthCalendar extends ObservedComponent<MonthCalendarProps> {
63
76
  changeMonth(delta: number) {
64
77
  this.currentDate = changeMonth(this.currentDate, delta);
65
78
 
66
- this.props.onChange?.(this.currentDate);
79
+ this.componentProps.onChange?.(this.currentDate);
67
80
  }
68
81
 
69
82
  changePreviousMonth = () => this.changeMonth(-1);
@@ -77,16 +90,16 @@ export class MonthCalendar extends ObservedComponent<MonthCalendarProps> {
77
90
 
78
91
  const date = targetItem.dataset.calendarDate;
79
92
  const index = Number(targetItem.dataset.calendarIndex);
80
- const items = this.props.value?.filter(
93
+ const items = this.componentProps.value?.filter(
81
94
  ({ date: itemDate }) => formatDate(itemDate, 'YYYY-MM-DD') === date
82
95
  );
83
96
  const item = items?.[index];
84
97
 
85
- if (item) this.props.onSelect?.(item);
98
+ if (item) this.componentProps.onSelect?.(item);
86
99
  };
87
100
 
88
101
  renderDate = (date: Date) => {
89
- const { value } = this.observedProps,
102
+ const { value } = this.observedComponentProps,
90
103
  dateText = formatDate(date, 'YYYY-MM-DD');
91
104
  const list = value?.filter(({ date }) => formatDate(date, 'YYYY-MM-DD') === dateText);
92
105
 
@@ -125,18 +138,21 @@ export class MonthCalendar extends ObservedComponent<MonthCalendarProps> {
125
138
  };
126
139
 
127
140
  render() {
141
+ const Root: ElementType = this.componentProps.as ?? 'table';
142
+
128
143
  const { weekFormatter, currentDate, dateGrid } = this,
129
144
  {
145
+ as: _as,
130
146
  className = '',
131
147
  style,
132
148
  theme = 'primary',
133
149
  onChange: _onChange,
134
150
  onSelect: _onSelect,
135
151
  ...props
136
- } = this.observedProps;
152
+ } = this.observedComponentProps;
137
153
 
138
154
  return (
139
- <table
155
+ <Root
140
156
  {...props}
141
157
  className={`table ${className}`}
142
158
  style={{ tableLayout: 'fixed', ...style }}
@@ -173,7 +189,7 @@ export class MonthCalendar extends ObservedComponent<MonthCalendarProps> {
173
189
  <tr key={days[0] + ''}>{days.map(this.renderDate)}</tr>
174
190
  ))}
175
191
  </tbody>
176
- </table>
192
+ </Root>
177
193
  );
178
194
  }
179
195
  }
@@ -7,6 +7,7 @@ import {
7
7
  cloneElement,
8
8
  Component,
9
9
  createRef,
10
+ ElementType,
10
11
  FocusEvent,
11
12
  HTMLAttributes,
12
13
  isValidElement,
@@ -19,6 +20,7 @@ import { createPortal } from 'react-dom';
19
20
  import { uniqueID } from 'web-utility';
20
21
 
21
22
  import { FloatingOptions, FloatingPosition, OverlayContainer, OverlayPlacement } from '../Overlay';
23
+ import { AsProps, PolymorphicClassProps } from '../Polymorphic';
22
24
  import { Popover } from '../Popover';
23
25
  import { Tooltip } from '../Tooltip';
24
26
 
@@ -43,7 +45,15 @@ export interface OverlayBoxProps extends Omit<
43
45
  }
44
46
 
45
47
  @observer
46
- export class OverlayBox extends Component<OverlayBoxProps> {
48
+ export class OverlayBox<T extends ElementType | undefined = 'span'> extends Component<
49
+ PolymorphicClassProps<T, OverlayBoxProps>
50
+ > {
51
+ declare readonly props: PolymorphicClassProps<T, OverlayBoxProps>;
52
+
53
+ protected get componentProps(): Readonly<OverlayBoxProps & AsProps> {
54
+ return this.props;
55
+ }
56
+
47
57
  static displayName = 'OverlayBox';
48
58
 
49
59
  @observable accessor open = false;
@@ -65,7 +75,7 @@ export class OverlayBox extends Component<OverlayBoxProps> {
65
75
  setOpenAfterDelay = (open: boolean) => {
66
76
  clearTimeout(this.timer);
67
77
 
68
- const { delay } = this.props;
78
+ const { delay } = this.componentProps;
69
79
  const timeout = typeof delay === 'number' ? delay : delay?.[open ? 'show' : 'hide'];
70
80
 
71
81
  if (!timeout) return this.setOpen(open);
@@ -120,23 +130,23 @@ export class OverlayBox extends Component<OverlayBoxProps> {
120
130
  (!!this.root.current?.contains(target) || !!this.overlay.current?.contains(target));
121
131
 
122
132
  hasTrigger = (trigger: OverlayTrigger) => {
123
- const triggers: OverlayTrigger[] = this.props.trigger
124
- ? Array.isArray(this.props.trigger)
125
- ? this.props.trigger
126
- : [this.props.trigger]
133
+ const triggers: OverlayTrigger[] = this.componentProps.trigger
134
+ ? Array.isArray(this.componentProps.trigger)
135
+ ? this.componentProps.trigger
136
+ : [this.componentProps.trigger]
127
137
  : ['hover', 'focus'];
128
138
 
129
139
  return triggers.includes(trigger);
130
140
  };
131
141
 
132
142
  handleMouseEnter = (event: MouseEvent<HTMLSpanElement>) => {
133
- this.props.onMouseEnter?.(event);
143
+ this.componentProps.onMouseEnter?.(event);
134
144
 
135
145
  if (!event.defaultPrevented && this.hasTrigger('hover')) this.setOpenAfterDelay(true);
136
146
  };
137
147
 
138
148
  handleMouseLeave = (event: MouseEvent<HTMLSpanElement>) => {
139
- this.props.onMouseLeave?.(event);
149
+ this.componentProps.onMouseLeave?.(event);
140
150
 
141
151
  if (
142
152
  !event.defaultPrevented &&
@@ -148,13 +158,13 @@ export class OverlayBox extends Component<OverlayBoxProps> {
148
158
  };
149
159
 
150
160
  handleFocus = (event: FocusEvent<HTMLSpanElement>) => {
151
- this.props.onFocus?.(event);
161
+ this.componentProps.onFocus?.(event);
152
162
 
153
163
  if (!event.defaultPrevented && this.hasTrigger('focus')) this.setOpenAfterDelay(true);
154
164
  };
155
165
 
156
166
  handleBlur = (event: FocusEvent<HTMLSpanElement>) => {
157
- this.props.onBlur?.(event);
167
+ this.componentProps.onBlur?.(event);
158
168
 
159
169
  if (
160
170
  !event.defaultPrevented &&
@@ -165,7 +175,7 @@ export class OverlayBox extends Component<OverlayBoxProps> {
165
175
  };
166
176
 
167
177
  handleClick = (event: MouseEvent<HTMLSpanElement>) => {
168
- this.props.onClick?.(event);
178
+ this.componentProps.onClick?.(event);
169
179
 
170
180
  if (
171
181
  !event.defaultPrevented &&
@@ -180,7 +190,7 @@ export class OverlayBox extends Component<OverlayBoxProps> {
180
190
  };
181
191
 
182
192
  handleKeyDown = (event: KeyboardEvent<HTMLSpanElement>) => {
183
- this.props.onKeyDown?.(event);
193
+ this.componentProps.onKeyDown?.(event);
184
194
 
185
195
  if (!event.defaultPrevented && event.key === 'Escape' && this.open) {
186
196
  event.preventDefault();
@@ -189,7 +199,10 @@ export class OverlayBox extends Component<OverlayBoxProps> {
189
199
  };
190
200
 
191
201
  render() {
202
+ const Root: ElementType = this.componentProps.as ?? 'span';
203
+
192
204
  const {
205
+ as: _as,
193
206
  title,
194
207
  detail,
195
208
  placement = 'top',
@@ -210,7 +223,7 @@ export class OverlayBox extends Component<OverlayBoxProps> {
210
223
  onClick: _onClick,
211
224
  onKeyDown: _onKeyDown,
212
225
  ...props
213
- } = this.props;
226
+ } = this.componentProps;
214
227
  const id = this.open ? this.id : undefined;
215
228
  const direction = this.position.element
216
229
  ? (this.position.placement.split('-')[0] as OverlayPlacement)
@@ -260,7 +273,7 @@ export class OverlayBox extends Component<OverlayBoxProps> {
260
273
  : children;
261
274
 
262
275
  return (
263
- <span
276
+ <Root
264
277
  {...props}
265
278
  ref={this.root}
266
279
  className={`position-relative d-inline-block ${className}`}
@@ -274,7 +287,7 @@ export class OverlayBox extends Component<OverlayBoxProps> {
274
287
  {trigger}
275
288
 
276
289
  {this.open && (target ? createPortal(overlay, target) : overlay)}
277
- </span>
290
+ </Root>
278
291
  );
279
292
  }
280
293
  }
@@ -3,10 +3,11 @@
3
3
  import classNames from 'classnames';
4
4
  import { action, computed, observable } from 'mobx';
5
5
  import { observer } from 'mobx-react';
6
- import { Component } from 'react';
6
+ import { Component, ElementType } from 'react';
7
7
  import { scrollTo, uniqueID } from 'web-utility';
8
8
 
9
9
  import { Nav, NavLink, NavProps } from '../Nav';
10
+ import { AsProps, PolymorphicClassProps } from '../Polymorphic';
10
11
 
11
12
  const HeadingSelector = Array.from({ length: 6 }, (_, index) => `h${index + 1}`).join(',');
12
13
 
@@ -20,7 +21,15 @@ export interface PageNavProps extends Omit<NavProps, 'ref'> {
20
21
  }
21
22
 
22
23
  @observer
23
- export class PageNav extends Component<PageNavProps> {
24
+ export class PageNav<T extends ElementType | undefined = 'nav'> extends Component<
25
+ PolymorphicClassProps<T, PageNavProps>
26
+ > {
27
+ declare readonly props: PolymorphicClassProps<T, PageNavProps>;
28
+
29
+ protected get componentProps(): Readonly<PageNavProps & AsProps> {
30
+ return this.props;
31
+ }
32
+
24
33
  static displayName = 'PageNav';
25
34
 
26
35
  @observable
@@ -70,7 +79,7 @@ export class PageNav extends Component<PageNavProps> {
70
79
 
71
80
  @action
72
81
  initList() {
73
- const { rootSelector } = this.props;
82
+ const { rootSelector } = this.componentProps;
74
83
  const root = rootSelector ? document.querySelector(rootSelector) : document;
75
84
 
76
85
  if (!root) return;
@@ -98,12 +107,12 @@ export class PageNav extends Component<PageNavProps> {
98
107
  scrollTo = (meta: HeadingMeta) => () => {
99
108
  setTimeout(() => scrollTo(`[id="${meta.id}"]`));
100
109
 
101
- this.props.onItemClick?.(meta);
110
+ this.componentProps.onItemClick?.(meta);
102
111
  };
103
112
 
104
113
  renderLink = ({ id, level, text, top }: HeadingMeta) => {
105
114
  const { currentActiveId } = this,
106
- { depth = Infinity, minDepth = 1 } = this.props;
115
+ { depth = Infinity, minDepth = 1 } = this.componentProps;
107
116
 
108
117
  return (
109
118
  minDepth <= level &&
@@ -133,10 +142,10 @@ export class PageNav extends Component<PageNavProps> {
133
142
  rootSelector: _rootSelector,
134
143
  onItemClick: _onItemClick,
135
144
  ...props
136
- } = this.props;
145
+ } = this.componentProps;
137
146
 
138
147
  return (
139
- <Nav {...props} variant="underline" vertical>
148
+ <Nav as={this.componentProps.as} {...props} variant="underline" vertical>
140
149
  {list.map(this.renderLink)}
141
150
  </Nav>
142
151
  );
@@ -2,18 +2,28 @@
2
2
 
3
3
  import { action, computed, observable } from 'mobx';
4
4
  import { observer } from 'mobx-react';
5
- import { Component, createRef, ReactNode } from 'react';
5
+ import { Component, createRef, ElementType, ReactNode, Ref } from 'react';
6
6
 
7
+ import { AsProps, PolymorphicClassProps } from '../../Polymorphic';
7
8
  import { Icon } from '../Icon';
8
9
  import * as style from './index.module.less';
9
10
 
10
11
  export interface ScrollNavScrollerProps {
11
12
  className?: string;
12
13
  children: ReactNode;
14
+ rootRef?: Ref<HTMLElement>;
13
15
  }
14
16
 
15
17
  @observer
16
- export class ScrollNavScroller extends Component<ScrollNavScrollerProps> {
18
+ export class ScrollNavScroller<T extends ElementType | undefined = 'div'> extends Component<
19
+ PolymorphicClassProps<T, ScrollNavScrollerProps>
20
+ > {
21
+ declare readonly props: PolymorphicClassProps<T, ScrollNavScrollerProps>;
22
+
23
+ protected get componentProps(): Readonly<ScrollNavScrollerProps & AsProps> {
24
+ return this.props;
25
+ }
26
+
17
27
  scrollerRef = createRef<HTMLDivElement>();
18
28
 
19
29
  @observable
@@ -73,11 +83,17 @@ export class ScrollNavScroller extends Component<ScrollNavScrollerProps> {
73
83
  scrollForward = () => this.scrollBy(1);
74
84
 
75
85
  render() {
86
+ const Root: ElementType = this.componentProps.as ?? 'div';
87
+
76
88
  const { canScrollLeft, canScrollRight } = this;
77
- const { className = '', children } = this.props;
89
+ const { as: _as, className = '', children, rootRef, ...props } = this.componentProps;
78
90
 
79
91
  return (
80
- <div className={`position-relative overflow-y-hidden ${className}`}>
92
+ <Root
93
+ {...props}
94
+ ref={rootRef}
95
+ className={`position-relative overflow-y-hidden ${className}`}
96
+ >
81
97
  {canScrollLeft && (
82
98
  <button
83
99
  type="button"
@@ -101,7 +117,7 @@ export class ScrollNavScroller extends Component<ScrollNavScrollerProps> {
101
117
  <Icon name="caret-right-fill" />
102
118
  </button>
103
119
  )}
104
- </div>
120
+ </Root>
105
121
  );
106
122
  }
107
123
  }
@@ -1,6 +1,7 @@
1
- import { FC } from 'react';
1
+ import { ElementType } from 'react';
2
2
 
3
3
  import { Nav, NavProps } from '../../Nav';
4
+ import { polymorphic } from '../../Polymorphic';
4
5
  import { ScrollNavScroller } from './Scroller';
5
6
 
6
7
  export type ScrollNavProps = NavProps;
@@ -11,12 +12,36 @@ export type ScrollNavProps = NavProps;
11
12
  *
12
13
  * @see {@link https://github.com/twbs/bootstrap/blob/main/site/src/assets/examples/offcanvas-navbar/index.astro#L50-L65}
13
14
  */
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>
15
+ export const ScrollNav = polymorphic<ScrollNavProps>(
16
+ ({
17
+ as: RootAs,
18
+ className = '',
19
+ children,
20
+ ref,
21
+ variant,
22
+ vertical,
23
+ fill,
24
+ justified,
25
+ cardHeader,
26
+ navbar,
27
+ ...props
28
+ }) => (
29
+ <ScrollNavScroller<ElementType>
30
+ as={RootAs}
31
+ {...(RootAs ? props : {})}
32
+ rootRef={RootAs ? ref : undefined}
33
+ className={className}
34
+ >
35
+ <Nav
36
+ {...(RootAs ? {} : props)}
37
+ ref={RootAs ? undefined : ref}
38
+ className="flex-nowrap"
39
+ {...{ variant, vertical, fill, justified, cardHeader, navbar }}
40
+ >
41
+ {children}
42
+ </Nav>
43
+ </ScrollNavScroller>
44
+ )
20
45
  );
21
46
 
22
47
  ScrollNav.displayName = 'ScrollNav';