idea-react 3.0.0-beta.1 → 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 (87) hide show
  1. package/AGENTS.md +9 -1
  2. package/ReadMe.md +34 -6
  3. package/dist/index.d.mts +270 -186
  4. package/dist/index.d.ts +270 -186
  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 +1 -1
  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 +43 -40
  62. package/source/Grid.tsx +67 -59
  63. package/source/Image.tsx +10 -6
  64. package/source/ListGroup.tsx +57 -69
  65. package/source/Menu.tsx +128 -66
  66. package/source/NativeDialog/index.tsx +51 -29
  67. package/source/Nav.tsx +46 -46
  68. package/source/NavOverflow/index.tsx +26 -10
  69. package/source/Navbar.tsx +60 -55
  70. package/source/Pagination.tsx +63 -69
  71. package/source/Placeholder.tsx +47 -43
  72. package/source/Polymorphic.ts +57 -0
  73. package/source/Popover.tsx +34 -31
  74. package/source/Progress/index.tsx +59 -54
  75. package/source/ScrollSpy/index.tsx +48 -33
  76. package/source/Spinner.tsx +27 -23
  77. package/source/Stack.tsx +25 -0
  78. package/source/Stepper.tsx +46 -40
  79. package/source/Tab.tsx +40 -27
  80. package/source/Table.tsx +53 -42
  81. package/source/Toast.tsx +62 -47
  82. package/source/ToggleButton.tsx +40 -37
  83. package/source/Toggler.tsx +17 -8
  84. package/source/Tooltip.tsx +31 -29
  85. package/source/index.ts +2 -0
  86. package/source/Extension/CodeBlock/index.tsx +0 -58
  87. package/source/Extension/CodeBlock/style.module.css +0 -3
package/source/Menu.tsx CHANGED
@@ -6,12 +6,11 @@ import { observer } from 'mobx-react';
6
6
  import {
7
7
  Component,
8
8
  createRef,
9
- FC,
9
+ ElementType,
10
10
  FocusEvent,
11
11
  HTMLAttributes,
12
12
  KeyboardEvent,
13
13
  MouseEvent,
14
- PropsWithChildren,
15
14
  ReactNode
16
15
  } from 'react';
17
16
 
@@ -19,17 +18,22 @@ import { Button, ButtonProps } from './Button';
19
18
  import { Icon, IconProps } from './Extension/Icon';
20
19
  import { Breakpoint } from './Grid';
21
20
  import { FloatingOptions, FloatingPosition } from './Overlay';
21
+ import { AsProps, polymorphic, PolymorphicClassProps } from './Polymorphic';
22
22
 
23
23
  export type MenuItemProps = ButtonProps & { active?: boolean; selected?: boolean };
24
24
 
25
- export const MenuItem: FC<MenuItemProps> = ({ active, selected, className, ...props }) => (
26
- <Button
27
- {...props}
28
- aria-current={active || (selected && props.href) ? 'true' : props['aria-current']}
29
- aria-pressed={props.href ? props['aria-pressed'] : (selected ?? props['aria-pressed'])}
30
- className={classNames('menu-item', { active, selected }, className)}
31
- />
25
+ export const MenuItem = polymorphic<MenuItemProps>(
26
+ ({ as: RootAs, active, selected, className, ...props }) => (
27
+ <Button
28
+ as={RootAs}
29
+ {...props}
30
+ className={classNames('menu-item', { active, selected }, className)}
31
+ aria-current={active || (selected && props.href) ? 'true' : props['aria-current']}
32
+ aria-pressed={props.href ? props['aria-pressed'] : (selected ?? props['aria-pressed'])}
33
+ />
34
+ )
32
35
  );
36
+ MenuItem.displayName = 'MenuItem';
33
37
 
34
38
  export interface MenuPanelProps extends HTMLAttributes<HTMLDivElement> {
35
39
  theme?: 'light' | 'dark';
@@ -38,67 +42,98 @@ export interface MenuPanelProps extends HTMLAttributes<HTMLDivElement> {
38
42
  show?: boolean;
39
43
  }
40
44
 
41
- export const MenuPanel: FC<MenuPanelProps> = ({
42
- theme,
43
- translucent,
44
- scrollable,
45
- show,
46
- className,
47
- ...props
48
- }) => (
49
- <div
50
- {...props}
51
- data-bs-theme={theme}
52
- className={classNames(
53
- 'menu',
54
- { show },
55
- translucent && 'menu-translucent',
56
- scrollable && 'menu-scrollable',
57
- className
58
- )}
59
- />
45
+ export const MenuPanel = polymorphic<MenuPanelProps>(
46
+ ({ as: Root = 'div', theme, translucent, scrollable, show, className, ...props }) => (
47
+ <Root
48
+ {...props}
49
+ data-bs-theme={theme}
50
+ className={classNames(
51
+ 'menu',
52
+ { show },
53
+ translucent && 'menu-translucent',
54
+ scrollable && 'menu-scrollable',
55
+ className
56
+ )}
57
+ />
58
+ )
60
59
  );
60
+ MenuPanel.displayName = 'MenuPanel';
61
61
 
62
- export const MenuItemIcon: FC<IconProps> = ({ className, ...props }) => (
63
- <Icon aria-hidden="true" {...props} className={classNames('menu-item-icon', className)} />
64
- );
65
-
66
- export const MenuItemCheck: FC<Omit<IconProps, 'name'>> = ({ className, ...props }) => (
62
+ export const MenuItemIcon = polymorphic<IconProps>(({ as: RootAs, className, ...props }) => (
67
63
  <Icon
64
+ as={RootAs}
68
65
  aria-hidden="true"
69
66
  {...props}
70
- name="check"
71
- className={classNames('menu-item-check', className)}
67
+ className={classNames('menu-item-icon', className)}
72
68
  />
69
+ ));
70
+ MenuItemIcon.displayName = 'MenuItemIcon';
71
+
72
+ export const MenuItemCheck = polymorphic<Omit<IconProps, 'name'>>(
73
+ ({ as: RootAs, className, ...props }) => (
74
+ <Icon
75
+ as={RootAs}
76
+ aria-hidden="true"
77
+ {...props}
78
+ name="check"
79
+ className={classNames('menu-item-check', className)}
80
+ />
81
+ )
73
82
  );
83
+ MenuItemCheck.displayName = 'MenuItemCheck';
74
84
 
75
- export const MenuText: FC<HTMLAttributes<HTMLSpanElement>> = ({ className, ...props }) => (
76
- <span {...props} className={classNames('menu-text', className)} />
85
+ export const MenuText = polymorphic<HTMLAttributes<HTMLSpanElement>>(
86
+ ({ as: Root = 'span', className, ...props }) => (
87
+ <Root {...props} className={classNames('menu-text', className)} />
88
+ )
77
89
  );
90
+ MenuText.displayName = 'MenuText';
78
91
 
79
- export const MenuHeader: FC<HTMLAttributes<HTMLHeadingElement>> = ({ className, ...props }) => (
80
- <h6 {...props} className={classNames('menu-header', className)} />
92
+ export const MenuHeader = polymorphic<HTMLAttributes<HTMLHeadingElement>>(
93
+ ({ as: Root = 'h6', className, ...props }) => (
94
+ <Root {...props} className={classNames('menu-header', className)} />
95
+ )
81
96
  );
97
+ MenuHeader.displayName = 'MenuHeader';
82
98
 
83
- export const MenuDivider: FC<HTMLAttributes<HTMLHRElement>> = ({ className, ...props }) => (
84
- <hr {...props} className={classNames('menu-divider', className)} />
99
+ export const MenuDivider = polymorphic<HTMLAttributes<HTMLHRElement>>(
100
+ ({ as: Root = 'hr', className, ...props }) => (
101
+ <Root {...props} className={classNames('menu-divider', className)} />
102
+ )
85
103
  );
104
+ MenuDivider.displayName = 'MenuDivider';
86
105
 
87
- export const MenuItemContent: FC<HTMLAttributes<HTMLSpanElement>> = ({ className, ...props }) => (
88
- <span {...props} className={classNames('menu-item-content', className)} />
106
+ export const MenuItemContent = polymorphic<HTMLAttributes<HTMLSpanElement>>(
107
+ ({ as: Root = 'span', className, ...props }) => (
108
+ <Root {...props} className={classNames('menu-item-content', className)} />
109
+ )
89
110
  );
111
+ MenuItemContent.displayName = 'MenuItemContent';
90
112
 
91
- export const MenuItemDescription: FC<HTMLAttributes<HTMLElement>> = ({ className, ...props }) => (
92
- <small {...props} className={classNames('menu-item-description', className)} />
113
+ export const MenuItemDescription = polymorphic<HTMLAttributes<HTMLElement>>(
114
+ ({ as: Root = 'small', className, ...props }) => (
115
+ <Root {...props} className={classNames('menu-item-description', className)} />
116
+ )
93
117
  );
118
+ MenuItemDescription.displayName = 'MenuItemDescription';
94
119
 
95
- export interface MenuSubmenuProps extends PropsWithChildren {
120
+ export interface MenuSubmenuProps extends HTMLAttributes<HTMLDivElement> {
96
121
  label: ReactNode;
97
122
  className?: string;
98
123
  }
99
124
 
100
125
  @observer
101
- export class MenuSubmenu extends Component<MenuSubmenuProps> {
126
+ export class MenuSubmenu<T extends ElementType | undefined = 'div'> extends Component<
127
+ PolymorphicClassProps<T, MenuSubmenuProps>
128
+ > {
129
+ static displayName = 'MenuSubmenu';
130
+
131
+ declare readonly props: PolymorphicClassProps<T, MenuSubmenuProps>;
132
+
133
+ protected get componentProps(): Readonly<MenuSubmenuProps & AsProps> {
134
+ return this.props;
135
+ }
136
+
102
137
  @observable accessor open = false;
103
138
 
104
139
  panel = createRef<HTMLDivElement>();
@@ -128,11 +163,23 @@ export class MenuSubmenu extends Component<MenuSubmenuProps> {
128
163
 
129
164
  handleClick = () => this.setOpen(!this.open);
130
165
 
131
- handleMouseEnter = () => this.setOpen(true);
166
+ handleMouseEnter = (event: MouseEvent<HTMLDivElement>) => {
167
+ this.componentProps.onMouseEnter?.(event);
132
168
 
133
- handleMouseLeave = () => this.setOpen(false);
169
+ if (!event.defaultPrevented) this.setOpen(true);
170
+ };
171
+
172
+ handleMouseLeave = (event: MouseEvent<HTMLDivElement>) => {
173
+ this.componentProps.onMouseLeave?.(event);
174
+
175
+ if (!event.defaultPrevented) this.setOpen(false);
176
+ };
134
177
 
135
178
  handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
179
+ this.componentProps.onKeyDown?.(event);
180
+
181
+ if (event.defaultPrevented) return;
182
+
136
183
  const { key } = event;
137
184
 
138
185
  if (key === 'ArrowRight') {
@@ -156,11 +203,14 @@ export class MenuSubmenu extends Component<MenuSubmenuProps> {
156
203
  };
157
204
 
158
205
  render() {
159
- const { label, className, children } = this.props;
206
+ const Root: ElementType = this.componentProps.as ?? 'div';
207
+
208
+ const { as: _as, label, className, children, ...props } = this.componentProps;
160
209
  const { open } = this;
161
210
 
162
211
  return (
163
- <div
212
+ <Root
213
+ {...props}
164
214
  className={classNames('submenu', { show: open }, className)}
165
215
  onMouseEnter={this.handleMouseEnter}
166
216
  onMouseLeave={this.handleMouseLeave}
@@ -184,7 +234,7 @@ export class MenuSubmenu extends Component<MenuSubmenuProps> {
184
234
  >
185
235
  {children}
186
236
  </div>
187
- </div>
237
+ </Root>
188
238
  );
189
239
  }
190
240
  }
@@ -241,7 +291,17 @@ const MENU_BREAKPOINT_WIDTHS: Record<Breakpoint, number> = {
241
291
  };
242
292
 
243
293
  @observer
244
- export class Menu extends Component<MenuProps> {
294
+ export class Menu<T extends ElementType | undefined = 'div'> extends Component<
295
+ PolymorphicClassProps<T, MenuProps>
296
+ > {
297
+ static displayName = 'Menu';
298
+
299
+ declare readonly props: PolymorphicClassProps<T, MenuProps>;
300
+
301
+ protected get componentProps(): Readonly<MenuProps & AsProps> {
302
+ return this.props;
303
+ }
304
+
245
305
  @observable accessor open = false;
246
306
  @observable accessor responsiveDirection: MenuPlacement | undefined;
247
307
 
@@ -257,10 +317,9 @@ export class Menu extends Component<MenuProps> {
257
317
  }
258
318
 
259
319
  componentDidUpdate({ responsivePlacement }: MenuProps) {
260
- const { trigger, panel, position, props } = this;
261
-
262
- if (responsivePlacement !== props.responsivePlacement) this.observePlacement();
263
-
320
+ if (responsivePlacement !== this.componentProps.responsivePlacement)
321
+ this.observePlacement();
322
+ const { trigger, panel, position, componentProps: props } = this;
264
323
  if (!this.open) return position.dispose();
265
324
  if (!trigger.current || !panel.current) return;
266
325
 
@@ -293,7 +352,7 @@ export class Menu extends Component<MenuProps> {
293
352
  query.removeEventListener('change', this.handlePlacement);
294
353
 
295
354
  this.media = (Object.keys(MENU_BREAKPOINT_WIDTHS) as Breakpoint[]).flatMap(breakpoint => {
296
- const placement = this.props.responsivePlacement?.[breakpoint];
355
+ const placement = this.componentProps.responsivePlacement?.[breakpoint];
297
356
  if (!placement) return [];
298
357
 
299
358
  const query = window.matchMedia(`(min-width: ${MENU_BREAKPOINT_WIDTHS[breakpoint]}px)`);
@@ -313,11 +372,11 @@ export class Menu extends Component<MenuProps> {
313
372
  setOpen = (open: boolean) => {
314
373
  if (this.open === open) return;
315
374
  this.open = open;
316
- this.props.onOpenChange?.(open);
375
+ this.componentProps.onOpenChange?.(open);
317
376
  };
318
377
 
319
378
  handleOutside = ({ target }: PointerEvent) => {
320
- const { autoClose = true } = this.props;
379
+ const { autoClose = true } = this.componentProps;
321
380
  if (
322
381
  autoClose !== false &&
323
382
  autoClose !== 'inside' &&
@@ -336,7 +395,7 @@ export class Menu extends Component<MenuProps> {
336
395
  }
337
396
 
338
397
  handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
339
- this.props.onKeyDown?.(event);
398
+ this.componentProps.onKeyDown?.(event);
340
399
 
341
400
  const { key, defaultPrevented } = event;
342
401
  if (defaultPrevented) return;
@@ -375,7 +434,7 @@ export class Menu extends Component<MenuProps> {
375
434
  };
376
435
 
377
436
  handleBlur = (event: FocusEvent<HTMLDivElement>) => {
378
- const { onBlur, autoClose = true } = this.props;
437
+ const { onBlur, autoClose = true } = this.componentProps;
379
438
  onBlur?.(event);
380
439
 
381
440
  const { defaultPrevented, currentTarget, relatedTarget } = event;
@@ -391,7 +450,7 @@ export class Menu extends Component<MenuProps> {
391
450
  handleToggle = () => this.setOpen(!this.open);
392
451
 
393
452
  handleSelect = ({ target, defaultPrevented }: MouseEvent<HTMLDivElement>) => {
394
- const { autoClose = true } = this.props;
453
+ const { autoClose = true } = this.componentProps;
395
454
  if (
396
455
  autoClose &&
397
456
  !defaultPrevented &&
@@ -404,7 +463,10 @@ export class Menu extends Component<MenuProps> {
404
463
  };
405
464
 
406
465
  render() {
466
+ const Root: ElementType = this.componentProps.as ?? 'div';
467
+
407
468
  const {
469
+ as: _as,
408
470
  label,
409
471
  autoClose: _autoClose,
410
472
  align = 'end',
@@ -427,7 +489,7 @@ export class Menu extends Component<MenuProps> {
427
489
  className,
428
490
  id,
429
491
  ...props
430
- } = this.props;
492
+ } = this.componentProps;
431
493
  const { open } = this;
432
494
  const preferred = this.responsiveDirection ?? placement;
433
495
  const direction = this.position.element
@@ -438,7 +500,7 @@ export class Menu extends Component<MenuProps> {
438
500
  : ['top-100', align === 'end' ? 'end-0' : 'start-0'];
439
501
 
440
502
  return (
441
- <div
503
+ <Root
442
504
  {...props}
443
505
  ref={this.root}
444
506
  id={id}
@@ -485,7 +547,7 @@ export class Menu extends Component<MenuProps> {
485
547
  >
486
548
  {children}
487
549
  </div>
488
- </div>
550
+ </Root>
489
551
  );
490
552
  }
491
553
  }
@@ -6,13 +6,14 @@ import {
6
6
  ComponentPropsWithRef,
7
7
  createRef,
8
8
  DialogHTMLAttributes,
9
- FC,
9
+ ElementType,
10
10
  KeyboardEvent,
11
11
  MouseEvent,
12
12
  SyntheticEvent
13
13
  } from 'react';
14
14
 
15
15
  import { Breakpoint } from '../Grid';
16
+ import { AsProps, polymorphic, PolymorphicClassProps } from '../Polymorphic';
16
17
  import styles from './style.module.css';
17
18
 
18
19
  const transitionMilliseconds = (value: string) =>
@@ -32,24 +33,44 @@ export interface NativeDialogProps extends Omit<DialogHTMLAttributes<HTMLDialogE
32
33
  scroll?: boolean;
33
34
  }
34
35
 
35
- export const DialogHeader: FC<ComponentPropsWithRef<'div'>> = ({ className, ...props }) => (
36
- <div {...props} className={classNames('dialog-header', className)} />
36
+ export const DialogHeader = polymorphic<ComponentPropsWithRef<'div'>>(
37
+ ({ as: Root = 'div', className, ...props }) => (
38
+ <Root {...props} className={classNames('dialog-header', className)} />
39
+ )
37
40
  );
41
+ DialogHeader.displayName = 'DialogHeader';
38
42
 
39
- export const DialogTitle: FC<ComponentPropsWithRef<'h1'>> = ({ className, ...props }) => (
40
- <h1 {...props} className={classNames('dialog-title', className)} />
43
+ export const DialogTitle = polymorphic<ComponentPropsWithRef<'h1'>>(
44
+ ({ as: Root = 'h1', className, ...props }) => (
45
+ <Root {...props} className={classNames('dialog-title', className)} />
46
+ )
41
47
  );
48
+ DialogTitle.displayName = 'DialogTitle';
42
49
 
43
- export const DialogBody: FC<ComponentPropsWithRef<'div'>> = ({ className, ...props }) => (
44
- <div {...props} className={classNames('dialog-body', className)} />
50
+ export const DialogBody = polymorphic<ComponentPropsWithRef<'div'>>(
51
+ ({ as: Root = 'div', className, ...props }) => (
52
+ <Root {...props} className={classNames('dialog-body', className)} />
53
+ )
45
54
  );
55
+ DialogBody.displayName = 'DialogBody';
46
56
 
47
- export const DialogFooter: FC<ComponentPropsWithRef<'div'>> = ({ className, ...props }) => (
48
- <div {...props} className={classNames('dialog-footer', className)} />
57
+ export const DialogFooter = polymorphic<ComponentPropsWithRef<'div'>>(
58
+ ({ as: Root = 'div', className, ...props }) => (
59
+ <Root {...props} className={classNames('dialog-footer', className)} />
60
+ )
49
61
  );
62
+ DialogFooter.displayName = 'DialogFooter';
50
63
 
51
- export class NativeDialog extends Component<NativeDialogProps> {
52
- static scrollLocks = new Set<NativeDialog>();
64
+ export class NativeDialog<T extends ElementType | undefined = 'dialog'> extends Component<
65
+ PolymorphicClassProps<T, NativeDialogProps>
66
+ > {
67
+ declare readonly props: PolymorphicClassProps<T, NativeDialogProps>;
68
+
69
+ protected get componentProps(): Readonly<NativeDialogProps & AsProps> {
70
+ return this.props;
71
+ }
72
+
73
+ static scrollLocks = new Set<NativeDialog<ElementType>>();
53
74
  static bodyOverflow = '';
54
75
 
55
76
  element = createRef<HTMLDialogElement>();
@@ -62,25 +83,25 @@ export class NativeDialog extends Component<NativeDialogProps> {
62
83
  }
63
84
 
64
85
  componentDidMount() {
65
- if (this.props.open) this.show();
86
+ if (this.componentProps.open) this.show();
66
87
  }
67
88
 
68
89
  componentDidUpdate({ open, modal, scroll }: NativeDialogProps) {
69
- const modeChanged = this.props.modal !== modal;
70
- const openChanged = this.props.open !== open;
90
+ const modeChanged = this.componentProps.modal !== modal;
91
+ const openChanged = this.componentProps.open !== open;
71
92
  const wasOpen = this.open;
72
- if (!openChanged && !modeChanged && this.props.scroll === scroll) return;
93
+ if (!openChanged && !modeChanged && this.componentProps.scroll === scroll) return;
73
94
 
74
- if (this.open && this.props.modal !== modal) {
95
+ if (this.open && this.componentProps.modal !== modal) {
75
96
  this.switchingMode = true;
76
97
  this.close();
77
98
  }
78
- if (this.props.scroll) this.unlockScroll();
79
- else if (this.open && this.props.modal !== false) this.lockScroll();
99
+ if (this.componentProps.scroll) this.unlockScroll();
100
+ else if (this.open && this.componentProps.modal !== false) this.lockScroll();
80
101
 
81
102
  if (!openChanged && !modeChanged) return;
82
103
 
83
- if (this.props.open || (!openChanged && modeChanged && wasOpen)) this.show();
104
+ if (this.componentProps.open || (!openChanged && modeChanged && wasOpen)) this.show();
84
105
  else this.close();
85
106
  }
86
107
 
@@ -94,11 +115,9 @@ export class NativeDialog extends Component<NativeDialogProps> {
94
115
 
95
116
  if (!this.switchingMode && document.activeElement instanceof HTMLElement)
96
117
  this.returnFocus = document.activeElement;
97
-
98
- if (this.props.modal === false) element.show();
118
+ if (this.componentProps.modal === false) element.show();
99
119
  else element.showModal();
100
-
101
- if (this.props.modal !== false && !this.props.scroll) this.lockScroll();
120
+ if (this.componentProps.modal !== false && !this.componentProps.scroll) this.lockScroll();
102
121
  };
103
122
 
104
123
  lockScroll = () => {
@@ -148,7 +167,7 @@ export class NativeDialog extends Component<NativeDialogProps> {
148
167
  };
149
168
 
150
169
  handleClose = (event: SyntheticEvent<HTMLDialogElement>) => {
151
- if (!this.switchingMode) this.props.onClose?.(event);
170
+ if (!this.switchingMode) this.componentProps.onClose?.(event);
152
171
 
153
172
  if (!this.open) {
154
173
  this.unlockScroll();
@@ -162,7 +181,7 @@ export class NativeDialog extends Component<NativeDialogProps> {
162
181
  }
163
182
 
164
183
  handleCancel = (event: SyntheticEvent<HTMLDialogElement>) => {
165
- const { onCancel, keyboard = true } = this.props;
184
+ const { onCancel, keyboard = true } = this.componentProps;
166
185
  onCancel?.(event);
167
186
 
168
187
  const { defaultPrevented } = event;
@@ -172,7 +191,7 @@ export class NativeDialog extends Component<NativeDialogProps> {
172
191
  };
173
192
 
174
193
  handleKeyDown = (event: KeyboardEvent<HTMLDialogElement>) => {
175
- const { onKeyDown, keyboard = true } = this.props;
194
+ const { onKeyDown, keyboard = true } = this.componentProps;
176
195
  onKeyDown?.(event);
177
196
 
178
197
  const { key, defaultPrevented } = event;
@@ -183,7 +202,7 @@ export class NativeDialog extends Component<NativeDialogProps> {
183
202
  };
184
203
 
185
204
  handleClick = (event: MouseEvent<HTMLDialogElement>) => {
186
- const { onClick, backdrop = true } = this.props;
205
+ const { onClick, backdrop = true } = this.componentProps;
187
206
  onClick?.(event);
188
207
 
189
208
  const { defaultPrevented, target, currentTarget, clientX, clientY } = event;
@@ -194,7 +213,10 @@ export class NativeDialog extends Component<NativeDialogProps> {
194
213
  };
195
214
 
196
215
  render() {
216
+ const Root: ElementType = this.componentProps.as ?? 'dialog';
217
+
197
218
  const {
219
+ as: _as,
198
220
  open: _open,
199
221
  modal: _modal,
200
222
  keyboard: _keyboard,
@@ -212,10 +234,10 @@ export class NativeDialog extends Component<NativeDialogProps> {
212
234
  onKeyDown: _onKeyDown,
213
235
  onClose: _onClose,
214
236
  ...props
215
- } = this.props;
237
+ } = this.componentProps;
216
238
 
217
239
  return (
218
- <dialog
240
+ <Root
219
241
  {...props}
220
242
  ref={this.element}
221
243
  className={classNames(
package/source/Nav.tsx CHANGED
@@ -1,5 +1,7 @@
1
1
  import classNames from 'classnames';
2
- import { ComponentPropsWithRef, FC } from 'react';
2
+ import { ComponentPropsWithRef } from 'react';
3
+
4
+ import { polymorphic } from './Polymorphic';
3
5
 
4
6
  export interface NavProps extends ComponentPropsWithRef<'nav'> {
5
7
  variant?: 'tabs' | 'pills' | 'underline';
@@ -10,59 +12,57 @@ export interface NavProps extends ComponentPropsWithRef<'nav'> {
10
12
  navbar?: boolean;
11
13
  }
12
14
 
13
- export const Nav: FC<NavProps> = ({
14
- variant,
15
- vertical,
16
- fill,
17
- justified,
18
- cardHeader,
19
- navbar,
20
- className,
21
- ...props
22
- }) => (
23
- <nav
24
- {...props}
25
- className={classNames(
26
- 'nav',
27
- navbar && 'navbar-nav',
28
- variant && `nav-${variant}`,
29
- cardHeader && variant === 'tabs' && 'card-header-tabs',
30
- vertical && (variant === 'pills' ? 'nav-pills-vertical' : 'flex-column'),
31
- { 'nav-fill': fill, 'nav-justified': justified },
32
- className
33
- )}
34
- />
15
+ export const Nav = polymorphic<NavProps>(
16
+ ({
17
+ as: Root = 'nav',
18
+ variant,
19
+ vertical,
20
+ fill,
21
+ justified,
22
+ cardHeader,
23
+ navbar,
24
+ className,
25
+ ...props
26
+ }) => (
27
+ <Root
28
+ {...props}
29
+ className={classNames(
30
+ 'nav',
31
+ navbar && 'navbar-nav',
32
+ variant && `nav-${variant}`,
33
+ cardHeader && variant === 'tabs' && 'card-header-tabs',
34
+ vertical && (variant === 'pills' ? 'nav-pills-vertical' : 'flex-column'),
35
+ { 'nav-fill': fill, 'nav-justified': justified },
36
+ className
37
+ )}
38
+ />
39
+ )
35
40
  );
41
+ Nav.displayName = 'Nav';
36
42
 
37
- export const NavItem: FC<ComponentPropsWithRef<'li'>> = ({ className, ...props }) => (
38
- <li {...props} className={classNames('nav-item', className)} />
43
+ export const NavItem = polymorphic<ComponentPropsWithRef<'li'>>(
44
+ ({ as: Root = 'li', className, ...props }) => (
45
+ <Root {...props} className={classNames('nav-item', className)} />
46
+ )
39
47
  );
48
+ NavItem.displayName = 'NavItem';
40
49
 
41
50
  export interface NavLinkProps extends ComponentPropsWithRef<'a'> {
42
51
  disabled?: boolean;
43
52
  active?: boolean;
44
53
  }
45
54
 
46
- export const NavLink: FC<NavLinkProps> = ({
47
- disabled,
48
- active,
49
- className,
50
- href,
51
- onClick,
52
- tabIndex,
53
- ...props
54
- }) => (
55
- <a
56
- {...props}
57
- href={disabled ? undefined : href}
58
- tabIndex={disabled ? -1 : tabIndex}
59
- aria-disabled={disabled || undefined}
60
- aria-current={active ? 'page' : props['aria-current']}
61
- className={classNames('nav-link', { active, disabled, 'pe-none': disabled }, className)}
62
- onClick={disabled ? undefined : onClick}
63
- />
55
+ export const NavLink = polymorphic<NavLinkProps>(
56
+ ({ as: Root = 'a', disabled, active, className, href, onClick, tabIndex, ...props }) => (
57
+ <Root
58
+ {...props}
59
+ href={disabled ? undefined : href}
60
+ tabIndex={disabled ? -1 : tabIndex}
61
+ aria-disabled={disabled || undefined}
62
+ aria-current={active ? 'page' : props['aria-current']}
63
+ className={classNames('nav-link', { active, disabled, 'pe-none': disabled }, className)}
64
+ onClick={disabled ? undefined : onClick}
65
+ />
66
+ )
64
67
  );
65
-
66
- Nav.displayName = 'Nav';
67
- NavItem.displayName = 'NavItem';
68
68
  NavLink.displayName = 'NavLink';