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
package/dist/style.css CHANGED
@@ -2,9 +2,6 @@
2
2
  --bs-dialog-backdrop-bg: transparent;
3
3
  --bs-dialog-backdrop-blur: 0;
4
4
  }
5
- .eJn_MW_code span[style] {
6
- color: light-dark(var(--shiki-light), var(--shiki-dark));
7
- }
8
5
  @keyframes ONAURG_scrollItem {
9
6
  0%, 10% {
10
7
  transform: translateX(0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "idea-react",
3
- "version": "3.0.0-beta.1",
3
+ "version": "3.0.0-beta.3",
4
4
  "license": "LGPL-3.0-or-later",
5
5
  "author": "shiy2008@gmail.com",
6
6
  "description": "A React advanced components library based on TypeScript & Bootstrap, built by idea2app remote developers team.",
@@ -31,9 +31,9 @@
31
31
  "@editorjs/editorjs": "^2.31.7",
32
32
  "@editorjs/paragraph": "^2.11.7",
33
33
  "@react-editor-js/core": "^2.1.0",
34
- "@shikijs/langs": "^4.5.0",
35
- "@shikijs/themes": "^4.5.0",
36
- "@swc/helpers": "^0.5.23",
34
+ "@shikijs/langs": "^4.6.0",
35
+ "@shikijs/themes": "^4.6.0",
36
+ "@swc/helpers": "^0.5.24",
37
37
  "bootstrap": "6.0.0-alpha.1",
38
38
  "classnames": "^2.5.1",
39
39
  "editorjs-html": "^4.0.5",
@@ -43,7 +43,7 @@
43
43
  "mobx-react-helper": "^0.5.4",
44
44
  "react-editor-js": "^2.1.0",
45
45
  "react-live": "^5.0.1",
46
- "shiki": "^4.5.0",
46
+ "shiki": "^4.6.0",
47
47
  "vanilla-calendar-pro": "3.4.0",
48
48
  "web-utility": "^4.8.1"
49
49
  },
@@ -55,7 +55,7 @@
55
55
  "react-element-to-jsx-string": ">=15"
56
56
  },
57
57
  "devDependencies": {
58
- "@cspell/eslint-plugin": "^10.4.0",
58
+ "@cspell/eslint-plugin": "^10.4.1",
59
59
  "@eslint/js": "^10.0.1",
60
60
  "@parcel/config-default": "~2.16.4",
61
61
  "@parcel/packager-ts": "~2.16.4",
@@ -63,10 +63,10 @@
63
63
  "@parcel/transformer-typescript-tsc": "~2.16.4",
64
64
  "@parcel/transformer-typescript-types": "~2.16.4",
65
65
  "@softonus/prettier-plugin-duplicate-remover": "^1.1.2",
66
- "@swc/core": "^1.16.20",
66
+ "@swc/core": "^1.16.22",
67
67
  "@testing-library/dom": "^10.4.2",
68
68
  "@testing-library/react": "^16.3.3",
69
- "@tsdown/css": "^0.23.4",
69
+ "@tsdown/css": "^0.23.7",
70
70
  "@types/lodash": "^4.17.27",
71
71
  "@types/node": "^26.6.6",
72
72
  "@types/react": "^19.3.0",
@@ -76,14 +76,14 @@
76
76
  "eslint-config-prettier": "^10.1.8",
77
77
  "eslint-plugin-react": "^7.37.5",
78
78
  "eslint-plugin-simple-import-sort": "^14.0.0",
79
- "globals": "^17.13.0",
79
+ "globals": "^17.14.0",
80
80
  "happy-dom": "^21.0.4",
81
81
  "husky": "^9.1.7",
82
82
  "jiti": "^2.7.0",
83
83
  "koapache": "^3.0.0",
84
84
  "less": "^4.9.1",
85
85
  "lint-staged": "^17.6.0",
86
- "mobx": "^7.1.3",
86
+ "mobx": "^7.1.5",
87
87
  "mobx-react": "^10.0.3",
88
88
  "parcel": "~2.16.4",
89
89
  "postcss": "^8.5.32",
@@ -94,11 +94,11 @@
94
94
  "react": "^19.3.1",
95
95
  "react-dom": "^19.3.1",
96
96
  "react-element-to-jsx-string": "^18.0.0",
97
- "rimraf": "^6.1.3",
98
- "tsdown": "^0.23.4",
97
+ "rimraf": "^6.1.4",
98
+ "tsdown": "^0.23.7",
99
99
  "typedoc": "^0.28.20",
100
100
  "typedoc-plugin-mdn-links": "^5.1.1",
101
- "typescript": "~5.9.3",
101
+ "typescript": "~6.0.3",
102
102
  "typescript-eslint": "^8.72.1",
103
103
  "unplugin-swc": "^2.0.0",
104
104
  "unrun": "^0.3.1"
@@ -3,10 +3,17 @@
3
3
  import classNames from 'classnames';
4
4
  import { action, observable } from 'mobx';
5
5
  import { observer } from 'mobx-react';
6
- import { Component, ComponentPropsWithoutRef, ComponentPropsWithRef, FC, MouseEvent } from 'react';
6
+ import {
7
+ Component,
8
+ ComponentPropsWithoutRef,
9
+ ComponentPropsWithRef,
10
+ ElementType,
11
+ MouseEvent
12
+ } from 'react';
7
13
 
8
14
  import { Button, ButtonProps, ComponentTheme } from './Button';
9
15
  import { Icon } from './Extension/Icon';
16
+ import { AsProps, polymorphic, PolymorphicClassProps } from './Polymorphic';
10
17
 
11
18
  export interface AccordionProps extends ComponentPropsWithRef<'div'> {
12
19
  theme?: ComponentTheme;
@@ -15,46 +22,48 @@ export interface AccordionProps extends ComponentPropsWithRef<'div'> {
15
22
  gap?: boolean;
16
23
  }
17
24
 
18
- export const Accordion: FC<AccordionProps> = ({ theme, size, flush, gap, className, ...props }) => (
19
- <div
20
- {...props}
21
- className={classNames(
22
- 'accordion',
23
- theme && `theme-${theme}`,
24
- size && `accordion-${size}`,
25
- { 'accordion-flush': flush, 'accordion-gap': gap },
26
- className
27
- )}
28
- />
25
+ export const Accordion = polymorphic<AccordionProps>(
26
+ ({ as: Root = 'div', theme, size, flush, gap, className, ...props }) => (
27
+ <Root
28
+ {...props}
29
+ className={classNames(
30
+ 'accordion',
31
+ theme && `theme-${theme}`,
32
+ size && `accordion-${size}`,
33
+ { 'accordion-flush': flush, 'accordion-gap': gap },
34
+ className
35
+ )}
36
+ />
37
+ )
29
38
  );
39
+ Accordion.displayName = 'Accordion';
30
40
 
31
- export const AccordionItem: FC<ComponentPropsWithRef<'details'>> = ({ className, ...props }) => (
32
- <details {...props} className={classNames('accordion-item', className)} />
41
+ export const AccordionItem = polymorphic<ComponentPropsWithRef<'details'>>(
42
+ ({ as: Root = 'details', className, ...props }) => (
43
+ <Root {...props} className={classNames('accordion-item', className)} />
44
+ )
33
45
  );
46
+ AccordionItem.displayName = 'AccordionItem';
34
47
 
35
48
  export interface AccordionHeaderProps extends ComponentPropsWithRef<'summary'> {
36
49
  icon?: string | false;
37
50
  }
38
51
 
39
- export const AccordionHeader: FC<AccordionHeaderProps> = ({
40
- icon = 'chevron-down',
41
- className,
42
- children,
43
- ...props
44
- }) => (
45
- <summary {...props} className={classNames('accordion-header', className)}>
46
- {children}
47
- {icon && <Icon name={icon} className="accordion-icon" aria-hidden="true" />}
48
- </summary>
52
+ export const AccordionHeader = polymorphic<AccordionHeaderProps>(
53
+ ({ as: Root = 'summary', icon = 'chevron-down', className, children, ...props }) => (
54
+ <Root {...props} className={classNames('accordion-header', className)}>
55
+ {children}
56
+ {icon && <Icon name={icon} className="accordion-icon" aria-hidden="true" />}
57
+ </Root>
58
+ )
49
59
  );
60
+ AccordionHeader.displayName = 'AccordionHeader';
50
61
 
51
- export const AccordionBody: FC<ComponentPropsWithRef<'div'>> = ({ className, ...props }) => (
52
- <div {...props} className={classNames('accordion-body', className)} />
62
+ export const AccordionBody = polymorphic<ComponentPropsWithRef<'div'>>(
63
+ ({ as: Root = 'div', className, ...props }) => (
64
+ <Root {...props} className={classNames('accordion-body', className)} />
65
+ )
53
66
  );
54
-
55
- Accordion.displayName = 'Accordion';
56
- AccordionItem.displayName = 'AccordionItem';
57
- AccordionHeader.displayName = 'AccordionHeader';
58
67
  AccordionBody.displayName = 'AccordionBody';
59
68
 
60
69
  export interface AccordionToggleProps
@@ -65,7 +74,15 @@ export interface AccordionToggleProps
65
74
  }
66
75
 
67
76
  @observer
68
- export class AccordionToggle extends Component<AccordionToggleProps> {
77
+ export class AccordionToggle<T extends ElementType | undefined = 'button'> extends Component<
78
+ PolymorphicClassProps<T, AccordionToggleProps>
79
+ > {
80
+ declare readonly props: PolymorphicClassProps<T, AccordionToggleProps>;
81
+
82
+ protected get componentProps(): Readonly<AccordionToggleProps & AsProps> {
83
+ return this.props;
84
+ }
85
+
69
86
  static displayName = 'AccordionToggle';
70
87
 
71
88
  @observable accessor expanded = false;
@@ -75,7 +92,7 @@ export class AccordionToggle extends Component<AccordionToggleProps> {
75
92
  get items() {
76
93
  return [
77
94
  ...(document
78
- .querySelector(this.props.target)
95
+ .querySelector(this.componentProps.target)
79
96
  ?.querySelectorAll<HTMLDetailsElement>(':scope > .accordion-item') ?? [])
80
97
  ];
81
98
  }
@@ -86,7 +103,7 @@ export class AccordionToggle extends Component<AccordionToggleProps> {
86
103
  }
87
104
 
88
105
  componentDidUpdate(previous: AccordionToggleProps) {
89
- if (previous.target !== this.props.target) {
106
+ if (previous.target !== this.componentProps.target) {
90
107
  this.restoreNames();
91
108
  this.refresh();
92
109
  }
@@ -109,7 +126,7 @@ export class AccordionToggle extends Component<AccordionToggleProps> {
109
126
  });
110
127
 
111
128
  handleClick = (event: MouseEvent<HTMLButtonElement>) => {
112
- this.props.onClick?.(event);
129
+ this.componentProps.onClick?.(event);
113
130
 
114
131
  if (event.defaultPrevented) return;
115
132
 
@@ -130,8 +147,15 @@ export class AccordionToggle extends Component<AccordionToggleProps> {
130
147
  };
131
148
 
132
149
  render() {
133
- const { target: _target, ...props } = this.props;
134
-
135
- return <Button {...props} aria-expanded={this.expanded} onClick={this.handleClick} />;
150
+ const { target: _target, ...props } = this.componentProps;
151
+
152
+ return (
153
+ <Button
154
+ as={this.componentProps.as}
155
+ {...props}
156
+ aria-expanded={this.expanded}
157
+ onClick={this.handleClick}
158
+ />
159
+ );
136
160
  }
137
161
  }
package/source/Alert.tsx CHANGED
@@ -8,13 +8,14 @@ import {
8
8
  ComponentPropsWithoutRef,
9
9
  ComponentPropsWithRef,
10
10
  createRef,
11
- FC,
11
+ ElementType,
12
12
  MouseEvent,
13
13
  TransitionEvent
14
14
  } from 'react';
15
15
 
16
16
  import { ComponentTheme } from './Button';
17
17
  import { CloseButton } from './CloseButton';
18
+ import { AsProps, polymorphic, PolymorphicClassProps } from './Polymorphic';
18
19
 
19
20
  export interface AlertProps extends ComponentPropsWithoutRef<'div'> {
20
21
  theme?: ComponentTheme;
@@ -26,7 +27,15 @@ export interface AlertProps extends ComponentPropsWithoutRef<'div'> {
26
27
  }
27
28
 
28
29
  @observer
29
- export class Alert extends Component<AlertProps> {
30
+ export class Alert<T extends ElementType | undefined = 'div'> extends Component<
31
+ PolymorphicClassProps<T, AlertProps>
32
+ > {
33
+ declare readonly props: PolymorphicClassProps<T, AlertProps>;
34
+
35
+ protected get componentProps(): Readonly<AlertProps & AsProps> {
36
+ return this.props;
37
+ }
38
+
30
39
  static displayName = 'Alert';
31
40
 
32
41
  @observable accessor hiding = false;
@@ -39,7 +48,7 @@ export class Alert extends Component<AlertProps> {
39
48
  componentDidUpdate() {
40
49
  if (this.closed && !this.notified) {
41
50
  this.notified = true;
42
- this.props.onClosed?.();
51
+ this.componentProps.onClosed?.();
43
52
  }
44
53
  }
45
54
 
@@ -57,9 +66,9 @@ export class Alert extends Component<AlertProps> {
57
66
  close = action(() => {
58
67
  if (this.hiding || this.closed) return;
59
68
 
60
- this.props.onClose?.();
69
+ this.componentProps.onClose?.();
61
70
 
62
- if (this.props.instant) return this.finishClose();
71
+ if (this.componentProps.instant) return this.finishClose();
63
72
 
64
73
  this.hiding = true;
65
74
 
@@ -81,13 +90,16 @@ export class Alert extends Component<AlertProps> {
81
90
  };
82
91
 
83
92
  handleTransitionEnd = (event: TransitionEvent<HTMLDivElement>) => {
84
- this.props.onTransitionEnd?.(event);
93
+ this.componentProps.onTransitionEnd?.(event);
85
94
 
86
95
  if (event.target === event.currentTarget && this.hiding) this.finishClose();
87
96
  };
88
97
 
89
98
  render() {
99
+ const Root: ElementType = this.componentProps.as ?? 'div';
100
+
90
101
  const {
102
+ as: _as,
91
103
  theme,
92
104
  dismissible,
93
105
  instant,
@@ -97,10 +109,10 @@ export class Alert extends Component<AlertProps> {
97
109
  className,
98
110
  children,
99
111
  ...props
100
- } = this.props;
112
+ } = this.componentProps;
101
113
 
102
114
  return this.closed ? null : (
103
- <div
115
+ <Root
104
116
  {...props}
105
117
  ref={this.root}
106
118
  role={props.role ?? 'alert'}
@@ -121,18 +133,21 @@ export class Alert extends Component<AlertProps> {
121
133
  onClick={this.handleClose}
122
134
  />
123
135
  )}
124
- </div>
136
+ </Root>
125
137
  );
126
138
  }
127
139
  }
128
140
 
129
- export const AlertLink: FC<ComponentPropsWithRef<'a'>> = ({ className, ...props }) => (
130
- <a {...props} className={classNames('alert-link', className)} />
141
+ export const AlertLink = polymorphic<ComponentPropsWithRef<'a'>>(
142
+ ({ as: Root = 'a', className, ...props }) => (
143
+ <Root {...props} className={classNames('alert-link', className)} />
144
+ )
131
145
  );
146
+ AlertLink.displayName = 'AlertLink';
132
147
 
133
- export const AlertHeading: FC<ComponentPropsWithRef<'h4'>> = ({ className, ...props }) => (
134
- <h4 {...props} className={classNames('alert-heading', className)} />
148
+ export const AlertHeading = polymorphic<ComponentPropsWithRef<'h4'>>(
149
+ ({ as: Root = 'h4', className, ...props }) => (
150
+ <Root {...props} className={classNames('alert-heading', className)} />
151
+ )
135
152
  );
136
-
137
- AlertLink.displayName = 'AlertLink';
138
153
  AlertHeading.displayName = 'AlertHeading';
package/source/Avatar.tsx CHANGED
@@ -1,7 +1,8 @@
1
1
  import classNames from 'classnames';
2
- import { ComponentPropsWithRef, CSSProperties, FC, HTMLAttributes } from 'react';
2
+ import { ComponentPropsWithRef, CSSProperties, HTMLAttributes } from 'react';
3
3
 
4
4
  import type { ComponentTheme } from './Button';
5
+ import { polymorphic } from './Polymorphic';
5
6
 
6
7
  export type AvatarSize = 'xs' | 'sm' | 'lg' | 'xl';
7
8
  export type AvatarStatus = 'online' | 'offline' | 'busy' | 'away';
@@ -20,65 +21,69 @@ export interface AvatarProps extends HTMLAttributes<HTMLSpanElement> {
20
21
  statusLabel?: string;
21
22
  }
22
23
 
23
- export const Avatar: FC<AvatarProps> = ({
24
- className,
25
- style,
26
- size,
27
- theme,
28
- subtle,
29
- src,
30
- alt,
31
- status,
32
- statusLabel = status,
33
- children,
34
- ...props
35
- }) => (
36
- <span
37
- className={classNames(
38
- 'avatar',
39
- typeof size === 'string' && `avatar-${size}`,
40
- theme && `theme-${theme}`,
41
- subtle && 'avatar-subtle',
42
- className
43
- )}
44
- style={
45
- typeof size === 'number'
46
- ? ({ '--bs-avatar-size': `${size}rem`, ...style } as CSSProperties)
47
- : style
48
- }
49
- {...props}
50
- >
51
- {src ? <img className="avatar-img" src={src} alt={alt} /> : children}
24
+ export const Avatar = polymorphic<AvatarProps>(
25
+ ({
26
+ as: Root = 'span',
27
+ className,
28
+ style,
29
+ size,
30
+ theme,
31
+ subtle,
32
+ src,
33
+ alt,
34
+ status,
35
+ statusLabel = status,
36
+ children,
37
+ ...props
38
+ }) => (
39
+ <Root
40
+ className={classNames(
41
+ 'avatar',
42
+ typeof size === 'string' && `avatar-${size}`,
43
+ theme && `theme-${theme}`,
44
+ subtle && 'avatar-subtle',
45
+ className
46
+ )}
47
+ style={
48
+ typeof size === 'number'
49
+ ? ({ '--bs-avatar-size': `${size}rem`, ...style } as CSSProperties)
50
+ : style
51
+ }
52
+ {...props}
53
+ >
54
+ {src ? <img className="avatar-img" src={src} alt={alt} /> : children}
52
55
 
53
- {status && (
54
- <span
55
- className={`avatar-status status-${status}`}
56
- role="img"
57
- aria-label={statusLabel}
58
- />
59
- )}
60
- </span>
56
+ {status && (
57
+ <span
58
+ className={`avatar-status status-${status}`}
59
+ role="img"
60
+ aria-label={statusLabel}
61
+ />
62
+ )}
63
+ </Root>
64
+ )
61
65
  );
62
-
63
66
  Avatar.displayName = 'Avatar';
64
67
 
65
68
  export interface AvatarStackProps extends HTMLAttributes<HTMLDivElement> {
66
69
  size?: AvatarSize;
67
70
  }
68
71
 
69
- export const AvatarImage: FC<ComponentPropsWithRef<'img'>> = ({ className, ...props }) => (
70
- <img {...props} className={classNames('avatar-img', className)} />
72
+ export const AvatarImage = polymorphic<ComponentPropsWithRef<'img'>>(
73
+ ({ as: Root = 'img', className, ...props }) => (
74
+ <Root {...props} className={classNames('avatar-img', className)} />
75
+ )
71
76
  );
72
-
73
77
  AvatarImage.displayName = 'AvatarImage';
74
78
 
75
- export const AvatarStack: FC<AvatarStackProps> = ({ className, size, children, ...props }) => (
76
- <div
77
- className={classNames('avatar-stack', size && `avatar-stack-${size}`, className)}
78
- {...props}
79
- >
80
- {children}
81
- </div>
79
+ export const AvatarStack = polymorphic<AvatarStackProps>(
80
+ ({ as: Root = 'div', className, size, children, ...props }) => (
81
+ <Root
82
+ className={classNames('avatar-stack', size && `avatar-stack-${size}`, className)}
83
+ {...props}
84
+ >
85
+ {children}
86
+ </Root>
87
+ )
82
88
  );
83
-
84
89
  AvatarStack.displayName = 'AvatarStack';
package/source/Badge.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 { ComponentTheme } from './Button';
5
+ import { polymorphic } from './Polymorphic';
5
6
 
6
7
  interface BadgeStyleProps {
7
8
  variant?: 'solid' | 'subtle' | 'outline';
@@ -12,25 +13,24 @@ interface BadgeStyleProps {
12
13
  type BadgeLinkAttributes = ComponentPropsWithRef<'a'> & { href: string };
13
14
  type BadgeElementAttributes =
14
15
  (ComponentPropsWithRef<'span'> & { href?: never }) | BadgeLinkAttributes;
16
+
15
17
  export type BadgeProps = BadgeStyleProps & BadgeElementAttributes;
16
18
 
17
19
  const isBadgeLink = (props: BadgeElementAttributes): props is BadgeLinkAttributes =>
18
20
  typeof props.href === 'string';
19
21
 
20
- export const Badge: FC<BadgeProps> = ({ variant = 'solid', theme, pill, className, ...props }) => {
21
- const classes = classNames(
22
- 'badge',
23
- variant !== 'solid' && `badge-${variant}`,
24
- theme && `theme-${theme}`,
25
- { 'rounded-pill': pill },
26
- className
27
- );
28
-
29
- return isBadgeLink(props) ? (
30
- <a {...props} className={classes} />
31
- ) : (
32
- <span {...props} className={classes} />
33
- );
34
- };
22
+ export const Badge = polymorphic<BadgeProps>(
23
+ ({ as: RootAs, variant = 'solid', theme, pill, className, ...props }) => {
24
+ const Root = RootAs ?? (isBadgeLink(props) ? 'a' : 'span');
25
+ const classes = classNames(
26
+ 'badge',
27
+ variant !== 'solid' && `badge-${variant}`,
28
+ theme && `theme-${theme}`,
29
+ { 'rounded-pill': pill },
30
+ className
31
+ );
35
32
 
33
+ return <Root {...props} className={classes} />;
34
+ }
35
+ );
36
36
  Badge.displayName = 'Badge';
@@ -1,36 +1,50 @@
1
1
  import classNames from 'classnames';
2
- import { ComponentPropsWithRef, FC } from 'react';
2
+ import { ComponentPropsWithRef } from 'react';
3
3
 
4
- export const Breadcrumb: FC<ComponentPropsWithRef<'ol'>> = ({ className, ...props }) => (
5
- <ol {...props} className={classNames('breadcrumb', className)} />
4
+ import { polymorphic } from './Polymorphic';
5
+
6
+ export const Breadcrumb = polymorphic<ComponentPropsWithRef<'ol'>>(
7
+ ({ as: Root = 'ol', className, ...props }) => (
8
+ <Root {...props} className={classNames('breadcrumb', className)} />
9
+ )
6
10
  );
11
+ Breadcrumb.displayName = 'Breadcrumb';
7
12
 
8
- export const BreadcrumbItem: FC<ComponentPropsWithRef<'li'>> = props => (
9
- <li {...props} className={classNames('breadcrumb-item', props.className)} />
13
+ export const BreadcrumbItem = polymorphic<ComponentPropsWithRef<'li'>>(
14
+ ({ as: Root = 'li', ...props }) => (
15
+ <Root {...props} className={classNames('breadcrumb-item', props.className)} />
16
+ )
10
17
  );
18
+ BreadcrumbItem.displayName = 'BreadcrumbItem';
11
19
 
12
20
  export interface BreadcrumbLinkProps extends ComponentPropsWithRef<'a'> {
13
21
  active?: boolean;
14
22
  }
15
23
 
16
- export const BreadcrumbLink: FC<BreadcrumbLinkProps> = ({
17
- active,
18
- className,
19
- 'aria-current': current = active ? 'page' : undefined,
20
- ...props
21
- }) => (
22
- <a
23
- {...props}
24
- className={classNames('breadcrumb-link', { active }, className)}
25
- aria-current={current}
26
- />
24
+ export const BreadcrumbLink = polymorphic<BreadcrumbLinkProps>(
25
+ ({
26
+ as: Root = 'a',
27
+ active,
28
+ className,
29
+ 'aria-current': current = active ? 'page' : undefined,
30
+ ...props
31
+ }) => (
32
+ <Root
33
+ {...props}
34
+ className={classNames('breadcrumb-link', { active }, className)}
35
+ aria-current={current}
36
+ />
37
+ )
27
38
  );
39
+ BreadcrumbLink.displayName = 'BreadcrumbLink';
28
40
 
29
- export const BreadcrumbDivider: FC<ComponentPropsWithRef<'li'>> = ({ className, ...props }) => (
30
- <li {...props} className={classNames('breadcrumb-divider', className)} aria-hidden="true" />
41
+ export const BreadcrumbDivider = polymorphic<ComponentPropsWithRef<'li'>>(
42
+ ({ as: Root = 'li', className, ...props }) => (
43
+ <Root
44
+ {...props}
45
+ className={classNames('breadcrumb-divider', className)}
46
+ aria-hidden="true"
47
+ />
48
+ )
31
49
  );
32
-
33
- Breadcrumb.displayName = 'Breadcrumb';
34
- BreadcrumbItem.displayName = 'BreadcrumbItem';
35
- BreadcrumbLink.displayName = 'BreadcrumbLink';
36
50
  BreadcrumbDivider.displayName = 'BreadcrumbDivider';