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
@@ -9,6 +9,7 @@ import {
9
9
  ComponentPropsWithRef,
10
10
  createRef,
11
11
  CSSProperties,
12
+ ElementType,
12
13
  isValidElement,
13
14
  KeyboardEvent,
14
15
  MouseEvent,
@@ -19,6 +20,7 @@ import {
19
20
  import { Icon } from '../Extension/Icon';
20
21
  import { Menu, MenuProps } from '../Menu';
21
22
  import { Nav, NavProps } from '../Nav';
23
+ import { AsProps, PolymorphicClassProps } from '../Polymorphic';
22
24
  import { NavOverflowModel } from './model';
23
25
 
24
26
  interface OverflowChildProps {
@@ -47,7 +49,15 @@ export interface NavOverflowProps
47
49
  }
48
50
 
49
51
  @observer
50
- export class NavOverflow extends Component<NavOverflowProps> {
52
+ export class NavOverflow<T extends ElementType | undefined = 'div'> extends Component<
53
+ PolymorphicClassProps<T, NavOverflowProps>
54
+ > {
55
+ declare readonly props: PolymorphicClassProps<T, NavOverflowProps>;
56
+
57
+ protected get componentProps(): Readonly<NavOverflowProps & AsProps> {
58
+ return this.props;
59
+ }
60
+
51
61
  static displayName = 'NavOverflow';
52
62
 
53
63
  root = createRef<HTMLDivElement>();
@@ -109,7 +119,10 @@ export class NavOverflow extends Component<NavOverflowProps> {
109
119
  const visible = items.map((_, index) => index);
110
120
  const minimum = Math.max(
111
121
  0,
112
- Math.min(this.props.minimumVisibleItems ?? this.props.threshold ?? 0, items.length)
122
+ Math.min(
123
+ this.componentProps.minimumVisibleItems ?? this.componentProps.threshold ?? 0,
124
+ items.length
125
+ )
113
126
  );
114
127
  let visibleWidth = allWidth;
115
128
 
@@ -134,11 +147,11 @@ export class NavOverflow extends Component<NavOverflowProps> {
134
147
  current.some((index, position) => index !== overflowIndices[position]);
135
148
 
136
149
  if (!this.store.initialized || changed) this.store.setOverflowIndices(overflowIndices);
137
- if (changed) this.props.onOverflowChange?.(overflowIndices.length, visible.length);
150
+ if (changed) this.componentProps.onOverflowChange?.(overflowIndices.length, visible.length);
138
151
  };
139
152
 
140
153
  private getCollapseBelow = () => {
141
- const { collapseBelow = 0 } = this.props;
154
+ const { collapseBelow = 0 } = this.componentProps;
142
155
  if (typeof collapseBelow === 'number') return collapseBelow;
143
156
 
144
157
  const value = getComputedStyle(this.root.current as HTMLElement).getPropertyValue(
@@ -148,7 +161,7 @@ export class NavOverflow extends Component<NavOverflowProps> {
148
161
  };
149
162
 
150
163
  handleClick = (event: MouseEvent<HTMLDivElement>) => {
151
- this.props.onClick?.(event);
164
+ this.componentProps.onClick?.(event);
152
165
 
153
166
  const target = event.target instanceof Element ? event.target : null;
154
167
  if (event.defaultPrevented) return;
@@ -159,7 +172,7 @@ export class NavOverflow extends Component<NavOverflowProps> {
159
172
  };
160
173
 
161
174
  handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
162
- this.props.onKeyDown?.(event);
175
+ this.componentProps.onKeyDown?.(event);
163
176
  if (event.defaultPrevented) return;
164
177
 
165
178
  if (event.key === 'Escape' && this.store.open) {
@@ -207,7 +220,10 @@ export class NavOverflow extends Component<NavOverflowProps> {
207
220
  };
208
221
 
209
222
  render() {
223
+ const Root: ElementType = this.componentProps.as ?? 'div';
224
+
210
225
  const {
226
+ as: _as,
211
227
  children,
212
228
  className,
213
229
  collapseBelow: _collapseBelow,
@@ -226,10 +242,10 @@ export class NavOverflow extends Component<NavOverflowProps> {
226
242
  'aria-label': ariaLabel,
227
243
  variant,
228
244
  ...props
229
- } = this.props;
245
+ } = this.componentProps;
230
246
  const items = Children.toArray(children);
231
247
  const hasOverflow = this.store.overflowIndices.length > 0;
232
- const menuId = `${this.props.id}-menu`;
248
+ const menuId = `${this.componentProps.id}-menu`;
233
249
  const [vertical, horizontal] = menuPlacement.split('-');
234
250
  const placementClasses =
235
251
  vertical === 'top'
@@ -243,7 +259,7 @@ export class NavOverflow extends Component<NavOverflowProps> {
243
259
  const text = moreText === false ? null : moreText;
244
260
 
245
261
  return (
246
- <div
262
+ <Root
247
263
  {...props}
248
264
  ref={this.root}
249
265
  className={classNames(
@@ -301,7 +317,7 @@ export class NavOverflow extends Component<NavOverflowProps> {
301
317
  </div>
302
318
  </div>
303
319
  </Nav>
304
- </div>
320
+ </Root>
305
321
  );
306
322
  }
307
323
  }
package/source/Navbar.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 { Button } from './Button';
5
+ import { polymorphic } from './Polymorphic';
5
6
 
6
7
  export interface NavbarProps extends ComponentPropsWithRef<'nav'> {
7
8
  theme?: 'light' | 'dark';
@@ -9,53 +10,58 @@ export interface NavbarProps extends ComponentPropsWithRef<'nav'> {
9
10
  translucent?: boolean;
10
11
  }
11
12
 
12
- export const Navbar: FC<NavbarProps> = ({ expand, theme, translucent, className, ...props }) => (
13
- <nav
14
- {...props}
15
- className={classNames(
16
- 'navbar',
17
- translucent && 'navbar-translucent',
18
- expand && (expand === true ? 'navbar-expand' : `${expand}:navbar-expand`),
19
- className
20
- )}
21
- data-bs-theme={theme}
22
- />
13
+ export const Navbar = polymorphic<NavbarProps>(
14
+ ({ as: Root = 'nav', expand, theme, translucent, className, ...props }) => (
15
+ <Root
16
+ {...props}
17
+ className={classNames(
18
+ 'navbar',
19
+ translucent && 'navbar-translucent',
20
+ expand && (expand === true ? 'navbar-expand' : `${expand}:navbar-expand`),
21
+ className
22
+ )}
23
+ data-bs-theme={theme}
24
+ />
25
+ )
23
26
  );
27
+ Navbar.displayName = 'Navbar';
24
28
 
25
29
  export type NavbarBrandProps =
26
30
  (ComponentPropsWithRef<'a'> & { as?: 'a' }) | (ComponentPropsWithRef<'span'> & { as: 'span' });
27
31
 
28
- export const NavbarBrand: FC<NavbarBrandProps> = props => {
29
- if (props.as === 'span') {
30
- const { as: _as, className, ...spanProps } = props;
31
-
32
- return <span {...spanProps} className={classNames('navbar-brand', className)} />;
33
- }
34
- const { as: _as, className, ...anchorProps } = props;
35
-
36
- return <a {...anchorProps} className={classNames('navbar-brand', className)} />;
37
- };
32
+ export const NavbarBrand = polymorphic<ComponentPropsWithRef<'a'>>(
33
+ ({ as: Root = 'a', className, ...props }) => (
34
+ <Root {...props} className={classNames('navbar-brand', className)} />
35
+ )
36
+ );
37
+ NavbarBrand.displayName = 'NavbarBrand';
38
38
 
39
39
  export interface NavbarNavProps extends ComponentPropsWithRef<'ul'> {
40
40
  variant?: 'tabs' | 'pills' | 'underline';
41
41
  nav?: boolean;
42
42
  }
43
43
 
44
- export const NavbarNav: FC<NavbarNavProps> = ({ variant, nav, className, ...props }) => (
45
- <ul
46
- {...props}
47
- className={classNames(
48
- 'navbar-nav',
49
- (nav || variant) && 'nav',
50
- variant && `nav-${variant}`,
51
- className
52
- )}
53
- />
44
+ export const NavbarNav = polymorphic<NavbarNavProps>(
45
+ ({ as: Root = 'ul', variant, nav, className, ...props }) => (
46
+ <Root
47
+ {...props}
48
+ className={classNames(
49
+ 'navbar-nav',
50
+ (nav || variant) && 'nav',
51
+ variant && `nav-${variant}`,
52
+ className
53
+ )}
54
+ />
55
+ )
54
56
  );
57
+ NavbarNav.displayName = 'NavbarNav';
55
58
 
56
- export const NavbarText: FC<ComponentPropsWithRef<'span'>> = ({ className, ...props }) => (
57
- <span {...props} className={classNames('navbar-text', className)} />
59
+ export const NavbarText = polymorphic<ComponentPropsWithRef<'span'>>(
60
+ ({ as: Root = 'span', className, ...props }) => (
61
+ <Root {...props} className={classNames('navbar-text', className)} />
62
+ )
58
63
  );
64
+ NavbarText.displayName = 'NavbarText';
59
65
 
60
66
  export interface NavbarTogglerProps extends Omit<
61
67
  ComponentPropsWithRef<'button'>,
@@ -64,26 +70,25 @@ export interface NavbarTogglerProps extends Omit<
64
70
  expanded?: boolean;
65
71
  }
66
72
 
67
- export const NavbarToggler: FC<NavbarTogglerProps> = ({
68
- expanded,
69
- className,
70
- 'aria-label': ariaLabel = 'Toggle navigation',
71
- ...props
72
- }) => (
73
- <Button
74
- {...props}
75
- type="button"
76
- className={classNames('navbar-toggler', className)}
77
- aria-label={ariaLabel}
78
- aria-expanded={expanded}
79
- icon
80
- >
81
- <span className="navbar-toggler-icon" aria-hidden="true" />
82
- </Button>
73
+ export const NavbarToggler = polymorphic<NavbarTogglerProps>(
74
+ ({
75
+ as: RootAs,
76
+ expanded,
77
+ className,
78
+ 'aria-label': ariaLabel = 'Toggle navigation',
79
+ ...props
80
+ }) => (
81
+ <Button
82
+ as={RootAs}
83
+ {...props}
84
+ type="button"
85
+ className={classNames('navbar-toggler', className)}
86
+ aria-label={ariaLabel}
87
+ aria-expanded={expanded}
88
+ icon
89
+ >
90
+ <span className="navbar-toggler-icon" aria-hidden="true" />
91
+ </Button>
92
+ )
83
93
  );
84
-
85
- Navbar.displayName = 'Navbar';
86
- NavbarBrand.displayName = 'NavbarBrand';
87
- NavbarNav.displayName = 'NavbarNav';
88
- NavbarText.displayName = 'NavbarText';
89
94
  NavbarToggler.displayName = 'NavbarToggler';
@@ -1,92 +1,86 @@
1
1
  import classNames from 'classnames';
2
- import { cloneElement, ComponentPropsWithRef, FC, isValidElement } from 'react';
2
+ import { cloneElement, ComponentPropsWithRef, isValidElement } from 'react';
3
3
 
4
4
  import { ComponentTheme } from './Button';
5
+ import { polymorphic } from './Polymorphic';
5
6
 
6
7
  export interface PaginationProps extends ComponentPropsWithRef<'ul'> {
7
8
  theme?: ComponentTheme;
8
9
  size?: 'sm' | 'lg';
9
10
  }
10
11
 
11
- export const Pagination: FC<PaginationProps> = ({ theme, size, className, ...props }) => (
12
- <ul
13
- {...props}
14
- className={classNames(
15
- 'pagination',
16
- theme && `theme-${theme}`,
17
- size && `pagination-${size}`,
18
- className
19
- )}
20
- />
12
+ export const Pagination = polymorphic<PaginationProps>(
13
+ ({ as: Root = 'ul', theme, size, className, ...props }) => (
14
+ <Root
15
+ {...props}
16
+ className={classNames(
17
+ 'pagination',
18
+ theme && `theme-${theme}`,
19
+ size && `pagination-${size}`,
20
+ className
21
+ )}
22
+ />
23
+ )
21
24
  );
25
+ Pagination.displayName = 'Pagination';
22
26
 
23
27
  type PageState = Partial<Record<'active' | 'disabled', boolean>>;
24
28
 
25
29
  export type PageItemProps = ComponentPropsWithRef<'li'> & PageState;
26
30
 
27
- export const PageItem: FC<PageItemProps> = ({
28
- active,
29
- disabled,
30
- className,
31
- children,
32
- ...props
33
- }) => {
34
- const child =
35
- isValidElement<PageLinkProps>(children) && children.type === PageLink
36
- ? cloneElement(children, {
37
- active: children.props.active ?? active,
38
- disabled: children.props.disabled ?? disabled
39
- })
40
- : children;
31
+ export const PageItem = polymorphic<PageItemProps>(
32
+ ({ as: Root = 'li', active, disabled, className, children, ...props }) => {
33
+ const child =
34
+ isValidElement<PageLinkProps>(children) && children.type === PageLink
35
+ ? cloneElement(children, {
36
+ active: children.props.active ?? active,
37
+ disabled: children.props.disabled ?? disabled
38
+ })
39
+ : children;
41
40
 
42
- return (
43
- <li {...props} className={classNames('page-item', { active, disabled }, className)}>
44
- {child}
45
- </li>
46
- );
47
- };
41
+ return (
42
+ <Root {...props} className={classNames('page-item', { active, disabled }, className)}>
43
+ {child}
44
+ </Root>
45
+ );
46
+ }
47
+ );
48
+ PageItem.displayName = 'PageItem';
48
49
 
49
50
  export type PageLinkProps = ComponentPropsWithRef<'a'> & PageState;
50
51
 
51
- export const PageLink: FC<PageLinkProps> = ({
52
- active,
53
- disabled,
54
- className,
55
- href,
56
- tabIndex,
57
- onClick,
58
- 'aria-current': current,
59
- ...props
60
- }) => {
61
- const linkProps = {
62
- ...props,
63
- className: classNames('page-link', className),
64
- tabIndex: disabled ? -1 : tabIndex,
65
- 'aria-disabled': disabled || undefined,
66
- 'aria-current': current ?? (active ? 'page' : undefined)
67
- };
68
-
69
- return href !== undefined ? (
70
- <a
71
- {...linkProps}
72
- href={disabled ? undefined : href}
73
- onClick={disabled ? undefined : onClick}
74
- />
75
- ) : (
76
- <span
77
- {...props}
78
- className={classNames('page-link', className)}
79
- aria-disabled={disabled || undefined}
80
- aria-current={current ?? (active ? 'page' : undefined)}
81
- />
82
- );
83
- };
52
+ export const PageLink = polymorphic<PageLinkProps>(
53
+ ({
54
+ as: RootAs,
55
+ active,
56
+ disabled,
57
+ className,
58
+ href,
59
+ tabIndex,
60
+ onClick,
61
+ 'aria-current': current,
62
+ ...props
63
+ }) => {
64
+ const Root = RootAs ?? (href === undefined ? 'span' : 'a');
84
65
 
85
- export const PageText: FC<ComponentPropsWithRef<'span'>> = ({ className, ...props }) => (
86
- <span {...props} className={classNames('page-link', className)} />
66
+ return (
67
+ <Root
68
+ {...props}
69
+ className={classNames('page-link', className)}
70
+ href={disabled ? undefined : href}
71
+ tabIndex={disabled ? -1 : tabIndex}
72
+ aria-disabled={disabled || undefined}
73
+ aria-current={current ?? (active ? 'page' : undefined)}
74
+ onClick={disabled ? undefined : onClick}
75
+ />
76
+ );
77
+ }
87
78
  );
88
-
89
- Pagination.displayName = 'Pagination';
90
- PageItem.displayName = 'PageItem';
91
79
  PageLink.displayName = 'PageLink';
80
+
81
+ export const PageText = polymorphic<ComponentPropsWithRef<'span'>>(
82
+ ({ as: Root = 'span', className, ...props }) => (
83
+ <Root {...props} className={classNames('page-link', className)} />
84
+ )
85
+ );
92
86
  PageText.displayName = 'PageText';
@@ -1,7 +1,8 @@
1
1
  import classNames from 'classnames';
2
- import { ComponentPropsWithRef, CSSProperties, FC } from 'react';
2
+ import { ComponentPropsWithRef, CSSProperties } from 'react';
3
3
 
4
4
  import { Button, ButtonProps, ComponentTheme } from './Button';
5
+ import { polymorphic } from './Polymorphic';
5
6
 
6
7
  export interface PlaceholderProps extends ComponentPropsWithRef<'span'> {
7
8
  size?: 'xs' | 'sm' | 'lg';
@@ -10,57 +11,60 @@ export interface PlaceholderProps extends ComponentPropsWithRef<'span'> {
10
11
  width?: CSSProperties['width'];
11
12
  }
12
13
 
13
- export const Placeholder: FC<PlaceholderProps> = ({
14
- size,
15
- animation,
16
- theme,
17
- width,
18
- className,
19
- style,
20
- 'aria-hidden': hidden = true,
21
- ...props
22
- }) => (
23
- <span
24
- {...props}
25
- className={classNames(
26
- 'placeholder',
27
- size && `placeholder-${size}`,
28
- animation && `placeholder-${animation}`,
29
- theme && `bg-${theme}`,
30
- className
31
- )}
32
- style={{ width, ...style }}
33
- aria-hidden={hidden}
34
- />
14
+ export const Placeholder = polymorphic<PlaceholderProps>(
15
+ ({
16
+ as: Root = 'span',
17
+ size,
18
+ animation,
19
+ theme,
20
+ width,
21
+ className,
22
+ style,
23
+ 'aria-hidden': hidden = true,
24
+ ...props
25
+ }) => (
26
+ <Root
27
+ {...props}
28
+ className={classNames(
29
+ 'placeholder',
30
+ size && `placeholder-${size}`,
31
+ animation && `placeholder-${animation}`,
32
+ theme && `bg-${theme}`,
33
+ className
34
+ )}
35
+ style={{ width, ...style }}
36
+ aria-hidden={hidden}
37
+ />
38
+ )
35
39
  );
40
+ Placeholder.displayName = 'Placeholder';
36
41
 
37
42
  export interface PlaceholderAnimationProps extends ComponentPropsWithRef<'div'> {
38
43
  animation?: PlaceholderProps['animation'];
39
44
  }
40
45
 
41
- export const PlaceholderAnimation: FC<PlaceholderAnimationProps> = ({
42
- animation = 'wave',
43
- className,
44
- ...props
45
- }) => (
46
- <div
47
- {...props}
48
- className={classNames(`placeholder-${animation}`, className)}
49
- aria-hidden="true"
50
- />
46
+ export const PlaceholderAnimation = polymorphic<PlaceholderAnimationProps>(
47
+ ({ as: Root = 'div', animation = 'wave', className, ...props }) => (
48
+ <Root
49
+ {...props}
50
+ className={classNames(`placeholder-${animation}`, className)}
51
+ aria-hidden="true"
52
+ />
53
+ )
51
54
  );
55
+ PlaceholderAnimation.displayName = 'PlaceholderAnimation';
52
56
 
53
57
  export type PlaceholderButtonProps = ButtonProps;
54
58
 
55
- export const PlaceholderButton: FC<PlaceholderButtonProps> = ({ className, ...props }) => (
56
- <Button
57
- {...props}
58
- className={classNames('placeholder', className)}
59
- aria-hidden="true"
60
- disabled
61
- />
59
+ export const PlaceholderButton = polymorphic<PlaceholderButtonProps>(
60
+ ({ as: RootAs, className, ...props }) => (
61
+ <Button
62
+ as={RootAs}
63
+ {...props}
64
+ className={classNames('placeholder', className)}
65
+ aria-hidden="true"
66
+ disabled
67
+ />
68
+ )
62
69
  );
63
-
64
- Placeholder.displayName = 'Placeholder';
65
- PlaceholderAnimation.displayName = 'PlaceholderAnimation';
66
70
  PlaceholderButton.displayName = 'PlaceholderButton';
@@ -0,0 +1,57 @@
1
+ import { ComponentPropsWithoutRef, ComponentPropsWithRef, ElementType, FC, ReactNode } from 'react';
2
+
3
+ export interface AsProps<T extends ElementType = ElementType> {
4
+ as?: T;
5
+ }
6
+
7
+ type RequiredKeys<T> = {
8
+ [K in keyof T]-?: {} extends Pick<T, K> ? never : K;
9
+ }[keyof T];
10
+
11
+ type TargetProps<T, P extends object> = T extends unknown
12
+ ? Omit<T, keyof P | 'as'> & Pick<T, Extract<RequiredKeys<T>, keyof P>>
13
+ : never;
14
+
15
+ type ReadonlyTargetProps<T, P extends object> = T extends unknown
16
+ ? Readonly<TargetProps<T, P>>
17
+ : never;
18
+
19
+ type PolymorphicRef<T extends ElementType> = T extends unknown
20
+ ? 'ref' extends keyof ComponentPropsWithRef<T>
21
+ ? ComponentPropsWithRef<T>['ref']
22
+ : never
23
+ : never;
24
+
25
+ export type PolymorphicProps<T extends ElementType, P extends object = {}> = P extends unknown
26
+ ? Omit<P, 'as' | 'ref'> &
27
+ TargetProps<ComponentPropsWithRef<T>, P> & {
28
+ ref?: NoInfer<PolymorphicRef<T>>;
29
+ } & AsProps<T>
30
+ : never;
31
+
32
+ export type PolymorphicClassProps<
33
+ T extends ElementType | undefined,
34
+ P extends object = {}
35
+ > = P extends unknown
36
+ ? Readonly<Omit<P, 'as' | 'ref'> & { as?: T }> &
37
+ NoInfer<
38
+ [T] extends [undefined]
39
+ ? {}
40
+ : ReadonlyTargetProps<ComponentPropsWithoutRef<Exclude<T, undefined>>, P>
41
+ >
42
+ : never;
43
+
44
+ export interface PolymorphicComponent<P extends object> {
45
+ <T extends ElementType | undefined>(
46
+ props: { as: T } & ([T] extends [undefined]
47
+ ? P
48
+ : NoInfer<PolymorphicProps<Exclude<T, undefined>, P>>)
49
+ ): ReactNode;
50
+ (props: P & { as?: never }): ReactNode;
51
+ displayName?: string;
52
+ }
53
+
54
+ // Preserve the component's identity and lifecycle; only its public call signature changes.
55
+ export const polymorphic = <P extends object>(
56
+ component: FC<P & AsProps>
57
+ ): PolymorphicComponent<P> => component as PolymorphicComponent<P>;
@@ -1,41 +1,44 @@
1
1
  import classNames from 'classnames';
2
- import { FC, ReactNode } from 'react';
2
+ import { ReactNode } from 'react';
3
3
 
4
4
  import { OVERLAY_ARROW_POSITIONS, OVERLAY_DIRECTIONS, OverlayTriggerProps } from './Overlay';
5
+ import { polymorphic } from './Polymorphic';
5
6
 
6
7
  export interface PopoverProps extends Omit<OverlayTriggerProps, 'title'> {
7
8
  title?: ReactNode;
8
9
  }
9
10
 
10
- export const Popover: FC<PopoverProps> = ({
11
- title,
12
- placement = 'top',
13
- show = true,
14
- role = 'tooltip',
15
- className,
16
- children,
17
- arrowStyle,
18
- ...props
19
- }) => (
20
- <div
21
- {...props}
22
- className={classNames(
23
- 'popover',
24
- { show },
25
- `bs-popover-${OVERLAY_DIRECTIONS[placement]}`,
26
- className
27
- )}
28
- role={role}
29
- hidden={!show}
30
- data-bs-placement={placement}
31
- >
32
- <div
33
- style={arrowStyle}
34
- className={classNames('popover-arrow', OVERLAY_ARROW_POSITIONS[placement])}
35
- />
36
- {title != null && <h3 className="popover-header">{title}</h3>}
37
- <div className="popover-body">{children}</div>
38
- </div>
11
+ export const Popover = polymorphic<PopoverProps>(
12
+ ({
13
+ as: Root = 'div',
14
+ title,
15
+ placement = 'top',
16
+ show = true,
17
+ role = 'tooltip',
18
+ className,
19
+ children,
20
+ arrowStyle,
21
+ ...props
22
+ }) => (
23
+ <Root
24
+ {...props}
25
+ className={classNames(
26
+ 'popover',
27
+ { show },
28
+ `bs-popover-${OVERLAY_DIRECTIONS[placement]}`,
29
+ className
30
+ )}
31
+ role={role}
32
+ hidden={!show}
33
+ data-bs-placement={placement}
34
+ >
35
+ <div
36
+ style={arrowStyle}
37
+ className={classNames('popover-arrow', OVERLAY_ARROW_POSITIONS[placement])}
38
+ />
39
+ {title != null && <h3 className="popover-header">{title}</h3>}
40
+ <div className="popover-body">{children}</div>
41
+ </Root>
42
+ )
39
43
  );
40
-
41
44
  Popover.displayName = 'Popover';