idea-react 3.0.0-beta.0 → 3.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/AGENTS.md +19 -2
  2. package/ReadMe.md +95 -54
  3. package/dist/DatePickerPlugin-CpGuQRsi.js +1 -0
  4. package/dist/DatePickerPlugin-DB3nnslN.mjs +1 -0
  5. package/dist/index.d.mts +1232 -474
  6. package/dist/index.d.ts +1232 -474
  7. package/dist/index.js +16 -16
  8. package/dist/index.mjs +15 -15
  9. package/dist/rolldown-runtime-5iZMYShA.js +1 -0
  10. package/dist/style.css +66 -66
  11. package/package.json +1 -1
  12. package/source/Accordion.tsx +161 -0
  13. package/source/Alert.tsx +153 -0
  14. package/source/Avatar.tsx +57 -46
  15. package/source/Badge.tsx +17 -15
  16. package/source/Breadcrumb.tsx +50 -0
  17. package/source/Button.tsx +69 -40
  18. package/source/ButtonGroup.tsx +23 -18
  19. package/source/Card.tsx +86 -17
  20. package/source/Carousel/index.tsx +392 -0
  21. package/source/Carousel/model.ts +113 -0
  22. package/source/Carousel/scroll.ts +79 -0
  23. package/source/CloseButton.tsx +20 -15
  24. package/source/Collapse.tsx +233 -0
  25. package/source/Drawer.tsx +158 -38
  26. package/source/{ClickBoundary.tsx → Extension/ClickBoundary.tsx} +24 -5
  27. package/source/Extension/CodeBlock.tsx +64 -0
  28. package/source/{Countdown.tsx → Extension/Countdown.tsx} +21 -9
  29. package/source/{Dialog.tsx → Extension/Dialog.tsx} +10 -6
  30. package/source/Extension/Editor.tsx +78 -0
  31. package/source/Extension/EditorHTML.tsx +20 -0
  32. package/source/{HorizontalMarquee → Extension/HorizontalMarquee}/index.module.less +1 -3
  33. package/source/Extension/HorizontalMarquee/index.tsx +85 -0
  34. package/source/Extension/Icon.tsx +19 -0
  35. package/source/Extension/Loading.tsx +30 -0
  36. package/source/{MonthCalendar.tsx → Extension/MonthCalendar.tsx} +29 -13
  37. package/source/{Nameplate.tsx → Extension/Nameplate.tsx} +1 -1
  38. package/source/Extension/OverlayBox.tsx +293 -0
  39. package/source/{PageNav.tsx → Extension/PageNav.tsx} +43 -16
  40. package/source/{ScrollNav → Extension/ScrollNav}/Scroller.tsx +21 -5
  41. package/source/Extension/ScrollNav/index.tsx +47 -0
  42. package/source/Extension/Select.tsx +128 -0
  43. package/source/{ShareBox.tsx → Extension/ShareBox.tsx} +1 -1
  44. package/source/Extension/SpinnerButton.tsx +30 -0
  45. package/source/Extension/TableSpinner.tsx +22 -0
  46. package/source/Extension/TextTruncate/index.tsx +29 -0
  47. package/source/Extension/Time.tsx +17 -0
  48. package/source/Extension/TimeDistance.tsx +45 -0
  49. package/source/Extension/Timeline/index.tsx +63 -0
  50. package/source/Extension/TypeEcho/Content.tsx +123 -0
  51. package/source/Extension/TypeEcho/index.tsx +22 -0
  52. package/source/Extension/UserRank/Address.tsx +22 -0
  53. package/source/Extension/UserRank/index.tsx +106 -0
  54. package/source/Extension/VerticalMarquee/index.tsx +23 -0
  55. package/source/Extension/ZodiacBar.tsx +59 -0
  56. package/source/Extension/index.ts +27 -0
  57. package/source/Form/Adorn.tsx +45 -26
  58. package/source/Form/Checkbox.tsx +20 -11
  59. package/source/Form/ChipInput.tsx +129 -0
  60. package/source/Form/Chips.tsx +57 -37
  61. package/source/Form/Combobox.tsx +362 -78
  62. package/source/Form/Control.tsx +72 -42
  63. package/source/Form/DatePicker.tsx +245 -45
  64. package/source/Form/DatePickerPlugin.ts +37 -0
  65. package/source/Form/Feedback.tsx +21 -16
  66. package/source/Form/FloatingLabel.tsx +21 -7
  67. package/source/Form/InputGroup.tsx +162 -11
  68. package/source/Form/OTP.tsx +156 -45
  69. package/source/Form/PasswordStrength.tsx +43 -17
  70. package/source/Form/Plugin.tsx +53 -0
  71. package/source/Form/Primitives.tsx +70 -20
  72. package/source/Form/Radio.tsx +35 -40
  73. package/source/Form/Range.tsx +125 -56
  74. package/source/Form/Switch.tsx +53 -27
  75. package/source/Form/index.ts +1 -0
  76. package/source/Grid.tsx +68 -56
  77. package/source/Image.tsx +10 -6
  78. package/source/ListGroup.tsx +59 -58
  79. package/source/Menu.tsx +317 -46
  80. package/source/NativeDialog/index.tsx +148 -22
  81. package/source/Nav.tsx +49 -33
  82. package/source/NavOverflow/index.tsx +323 -0
  83. package/source/NavOverflow/model.ts +21 -0
  84. package/source/Navbar.tsx +65 -45
  85. package/source/Overlay.ts +163 -1
  86. package/source/Pagination.tsx +86 -0
  87. package/source/Placeholder.tsx +70 -0
  88. package/source/Polymorphic.ts +57 -0
  89. package/source/Popover.tsx +34 -26
  90. package/source/Progress/index.tsx +81 -0
  91. package/source/Progress/style.module.css +15 -0
  92. package/source/ScrollSpy/index.tsx +380 -0
  93. package/source/ScrollSpy/model.ts +17 -0
  94. package/source/Spinner.tsx +31 -17
  95. package/source/Stack.tsx +25 -0
  96. package/source/Stepper.tsx +91 -0
  97. package/source/Tab.tsx +261 -0
  98. package/source/Table.tsx +53 -42
  99. package/source/Toast.tsx +93 -46
  100. package/source/ToggleButton.tsx +47 -30
  101. package/source/Toggler.tsx +117 -0
  102. package/source/Tooltip.tsx +31 -24
  103. package/source/index.ts +17 -27
  104. package/source/CodeBlock.tsx +0 -40
  105. package/source/Editor.tsx +0 -43
  106. package/source/EditorHTML.tsx +0 -16
  107. package/source/HorizontalMarquee/index.tsx +0 -61
  108. package/source/Icon.tsx +0 -24
  109. package/source/Loading.tsx +0 -22
  110. package/source/OverlayBox.tsx +0 -208
  111. package/source/ScrollNav/index.tsx +0 -22
  112. package/source/Select.tsx +0 -95
  113. package/source/SpinnerButton.tsx +0 -35
  114. package/source/TableSpinner.tsx +0 -26
  115. package/source/TextTruncate/index.tsx +0 -25
  116. package/source/Time.tsx +0 -15
  117. package/source/TimeDistance.tsx +0 -41
  118. package/source/Timeline/index.tsx +0 -60
  119. package/source/TypeEcho/Content.tsx +0 -58
  120. package/source/TypeEcho/index.tsx +0 -14
  121. package/source/UserRank/Address.tsx +0 -20
  122. package/source/UserRank/index.tsx +0 -111
  123. package/source/VerticalMarquee/index.tsx +0 -25
  124. package/source/ZodiacBar.tsx +0 -51
  125. /package/source/{LiveTSX.tsx → Extension/LiveTSX.tsx} +0 -0
  126. /package/source/{ScrollNav → Extension/ScrollNav}/index.module.less +0 -0
  127. /package/source/{TextTruncate → Extension/TextTruncate}/Content.tsx +0 -0
  128. /package/source/{Timeline → Extension/Timeline}/index.module.less +0 -0
  129. /package/source/{UserRank → Extension/UserRank}/index.module.less +0 -0
  130. /package/source/{VerticalMarquee → Extension/VerticalMarquee}/index.module.less +0 -0
@@ -0,0 +1,323 @@
1
+ 'use client';
2
+
3
+ import classNames from 'classnames';
4
+ import { observer } from 'mobx-react';
5
+ import {
6
+ Children,
7
+ cloneElement,
8
+ Component,
9
+ ComponentPropsWithRef,
10
+ createRef,
11
+ CSSProperties,
12
+ ElementType,
13
+ isValidElement,
14
+ KeyboardEvent,
15
+ MouseEvent,
16
+ ReactElement,
17
+ ReactNode
18
+ } from 'react';
19
+
20
+ import { Icon } from '../Extension/Icon';
21
+ import { Menu, MenuProps } from '../Menu';
22
+ import { Nav, NavProps } from '../Nav';
23
+ import { AsProps, PolymorphicClassProps } from '../Polymorphic';
24
+ import { NavOverflowModel } from './model';
25
+
26
+ interface OverflowChildProps {
27
+ 'aria-hidden'?: boolean;
28
+ className?: string;
29
+ role?: string;
30
+ style?: CSSProperties;
31
+ tabIndex?: number;
32
+ }
33
+
34
+ export interface NavOverflowProps
35
+ extends
36
+ Omit<ComponentPropsWithRef<'div'>, 'onChange' | 'children'>,
37
+ Pick<NavProps, 'variant' | 'fill' | 'justified'> {
38
+ children: ReactNode;
39
+ collapseBelow?: number | string;
40
+ id: string;
41
+ iconPlacement?: 'start' | 'end';
42
+ menuPlacement?: string;
43
+ menuStrategy?: 'absolute' | 'fixed';
44
+ minimumVisibleItems?: number;
45
+ moreIcon?: ReactNode;
46
+ moreText?: string | false;
47
+ onOverflowChange?: (overflowCount: number, visibleCount: number) => unknown;
48
+ threshold?: number;
49
+ }
50
+
51
+ @observer
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
+
61
+ static displayName = 'NavOverflow';
62
+
63
+ root = createRef<HTMLDivElement>();
64
+
65
+ nav = createRef<HTMLElement>();
66
+
67
+ toggleButton = createRef<HTMLButtonElement>();
68
+
69
+ store = new NavOverflowModel();
70
+
71
+ private resizeObserver?: ResizeObserver;
72
+
73
+ componentDidMount() {
74
+ this.updateOverflow();
75
+ window.addEventListener('resize', this.updateOverflow);
76
+ document.addEventListener('pointerdown', this.handleOutside);
77
+
78
+ if (typeof ResizeObserver !== 'undefined') {
79
+ this.resizeObserver = new ResizeObserver(this.updateOverflow);
80
+
81
+ if (this.root.current) this.resizeObserver.observe(this.root.current);
82
+ if (this.nav.current) this.resizeObserver.observe(this.nav.current);
83
+
84
+ for (const item of this.nav.current?.children ?? []) this.resizeObserver.observe(item);
85
+ }
86
+ }
87
+
88
+ componentDidUpdate() {
89
+ this.updateOverflow();
90
+ }
91
+
92
+ componentWillUnmount() {
93
+ this.resizeObserver?.disconnect();
94
+ window.removeEventListener('resize', this.updateOverflow);
95
+ document.removeEventListener('pointerdown', this.handleOutside);
96
+ }
97
+
98
+ updateOverflow = () => {
99
+ const root = this.root.current;
100
+ const nav = this.nav.current;
101
+ if (!root || !nav) return;
102
+
103
+ const items = [...nav.children].filter(
104
+ item => item !== this.toggleButton.current?.parentElement
105
+ ) as HTMLElement[];
106
+ const widths = items.map(item => item.getBoundingClientRect().width || item.offsetWidth);
107
+ const gap = Number.parseFloat(getComputedStyle(nav).columnGap) || 0;
108
+ const allWidth =
109
+ widths.reduce((total, width) => total + width, 0) + gap * Math.max(0, items.length - 1);
110
+ const toggleWidth =
111
+ this.toggleButton.current?.getBoundingClientRect().width ||
112
+ this.toggleButton.current?.offsetWidth ||
113
+ 0;
114
+ const containerWidth = root.clientWidth;
115
+ const collapseBelow = this.getCollapseBelow();
116
+ const shouldCollapseAll =
117
+ collapseBelow > 0 && containerWidth > 0 && containerWidth < collapseBelow;
118
+ const keep = items.map(item => item.classList.contains('nav-overflow-keep'));
119
+ const visible = items.map((_, index) => index);
120
+ const minimum = Math.max(
121
+ 0,
122
+ Math.min(
123
+ this.componentProps.minimumVisibleItems ?? this.componentProps.threshold ?? 0,
124
+ items.length
125
+ )
126
+ );
127
+ let visibleWidth = allWidth;
128
+
129
+ if (shouldCollapseAll) {
130
+ for (let index = visible.length - 1; index >= 0; index--)
131
+ if (!keep[index]) visible.splice(index, 1);
132
+ } else if (containerWidth > 0 && allWidth > containerWidth)
133
+ while (visibleWidth + toggleWidth > containerWidth && visible.length > minimum) {
134
+ const index = visible.findLast(candidate => !keep[candidate]);
135
+ if (index === undefined) break;
136
+
137
+ visibleWidth -= widths[index] + (visible.length > 1 ? gap : 0);
138
+ visible.splice(visible.indexOf(index), 1);
139
+ }
140
+ const visibleSet = new Set(visible);
141
+ const overflowIndices = items
142
+ .map((_, index) => index)
143
+ .filter(index => !visibleSet.has(index));
144
+ const current = this.store.overflowIndices;
145
+ const changed =
146
+ current.length !== overflowIndices.length ||
147
+ current.some((index, position) => index !== overflowIndices[position]);
148
+
149
+ if (!this.store.initialized || changed) this.store.setOverflowIndices(overflowIndices);
150
+ if (changed) this.componentProps.onOverflowChange?.(overflowIndices.length, visible.length);
151
+ };
152
+
153
+ private getCollapseBelow = () => {
154
+ const { collapseBelow = 0 } = this.componentProps;
155
+ if (typeof collapseBelow === 'number') return collapseBelow;
156
+
157
+ const value = getComputedStyle(this.root.current as HTMLElement).getPropertyValue(
158
+ `--bs-breakpoint-${collapseBelow}`
159
+ );
160
+ return Number.parseFloat(value) || 0;
161
+ };
162
+
163
+ handleClick = (event: MouseEvent<HTMLDivElement>) => {
164
+ this.componentProps.onClick?.(event);
165
+
166
+ const target = event.target instanceof Element ? event.target : null;
167
+ if (event.defaultPrevented) return;
168
+
169
+ if (target?.closest('[data-overflow-toggle]')) this.store.toggle();
170
+ else if (target?.closest('.nav-overflow-menu') && !target.closest('[aria-haspopup="true"]'))
171
+ this.store.setOpen(false);
172
+ };
173
+
174
+ handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
175
+ this.componentProps.onKeyDown?.(event);
176
+ if (event.defaultPrevented) return;
177
+
178
+ if (event.key === 'Escape' && this.store.open) {
179
+ this.store.setOpen(false);
180
+ this.toggleButton.current?.focus();
181
+ } else if (event.key === 'ArrowDown' && event.target === this.toggleButton.current) {
182
+ event.preventDefault();
183
+ this.store.setOpen(true);
184
+
185
+ queueMicrotask(() =>
186
+ this.root.current
187
+ ?.querySelector<HTMLElement>(
188
+ '.nav-overflow-menu a[href]:not([aria-disabled="true"]), .nav-overflow-menu button:not(:disabled)'
189
+ )
190
+ ?.focus()
191
+ );
192
+ }
193
+ };
194
+
195
+ handleOutside = (event: PointerEvent) => {
196
+ if (event.target instanceof Node && !this.root.current?.contains(event.target))
197
+ this.store.setOpen(false);
198
+ };
199
+
200
+ renderChild = (child: ReactNode, index: number, overflow = false) => {
201
+ if (!isValidElement<OverflowChildProps>(child)) return <span key={index}>{child}</span>;
202
+
203
+ if (overflow) {
204
+ if (child.type === Menu)
205
+ return cloneElement(child as ReactElement<MenuProps>, {
206
+ className: classNames(child.props.className, 'nav-overflow-submenu')
207
+ });
208
+ return cloneElement(child as ReactElement<OverflowChildProps>, {
209
+ className: classNames(child.props.className, 'menu-item'),
210
+ role: 'menuitem'
211
+ });
212
+ }
213
+ const hidden = this.store.overflowIndices.includes(index);
214
+
215
+ return cloneElement(child as ReactElement<OverflowChildProps>, {
216
+ className: classNames(child.props.className, hidden && 'position-absolute invisible'),
217
+ 'aria-hidden': hidden || undefined,
218
+ tabIndex: hidden ? -1 : child.props.tabIndex
219
+ });
220
+ };
221
+
222
+ render() {
223
+ const Root: ElementType = this.componentProps.as ?? 'div';
224
+
225
+ const {
226
+ as: _as,
227
+ children,
228
+ className,
229
+ collapseBelow: _collapseBelow,
230
+ fill,
231
+ iconPlacement = 'start',
232
+ justified,
233
+ menuPlacement = 'bottom-end',
234
+ menuStrategy = 'absolute',
235
+ minimumVisibleItems: _minimumVisibleItems,
236
+ moreIcon,
237
+ moreText = 'More',
238
+ onClick: _onClick,
239
+ onKeyDown: _onKeyDown,
240
+ onOverflowChange: _onOverflowChange,
241
+ threshold: _threshold,
242
+ 'aria-label': ariaLabel,
243
+ variant,
244
+ ...props
245
+ } = this.componentProps;
246
+ const items = Children.toArray(children);
247
+ const hasOverflow = this.store.overflowIndices.length > 0;
248
+ const menuId = `${this.componentProps.id}-menu`;
249
+ const [vertical, horizontal] = menuPlacement.split('-');
250
+ const placementClasses =
251
+ vertical === 'top'
252
+ ? ['bottom-100', horizontal === 'end' ? 'end-0' : 'start-0']
253
+ : vertical === 'bottom'
254
+ ? ['top-100', horizontal === 'end' ? 'end-0' : 'start-0']
255
+ : vertical === 'left'
256
+ ? ['end-100', horizontal === 'end' ? 'bottom-0' : 'top-0']
257
+ : ['start-100', horizontal === 'end' ? 'bottom-0' : 'top-0'];
258
+ const icon = moreIcon ?? <Icon name="three-dots" aria-hidden="true" />;
259
+ const text = moreText === false ? null : moreText;
260
+
261
+ return (
262
+ <Root
263
+ {...props}
264
+ ref={this.root}
265
+ className={classNames(
266
+ 'nav-overflow d-flex align-items-start position-relative',
267
+ this.store.initialized && 'nav-overflow-initialized',
268
+ className
269
+ )}
270
+ onClick={this.handleClick}
271
+ onKeyDown={this.handleKeyDown}
272
+ >
273
+ <Nav
274
+ ref={this.nav}
275
+ variant={variant}
276
+ fill={fill}
277
+ justified={justified}
278
+ aria-label={ariaLabel}
279
+ className="flex-nowrap flex-grow-1"
280
+ >
281
+ {items.map((child, index) => this.renderChild(child, index))}
282
+ <div
283
+ className={classNames(
284
+ 'nav-item nav-overflow-item flex-shrink-0',
285
+ !hasOverflow && 'position-absolute invisible'
286
+ )}
287
+ data-menu-placement={menuPlacement}
288
+ >
289
+ <button
290
+ ref={this.toggleButton}
291
+ type="button"
292
+ className="nav-link"
293
+ aria-haspopup="menu"
294
+ aria-expanded={this.store.open}
295
+ aria-controls={menuId}
296
+ aria-label={text ? undefined : 'More'}
297
+ data-overflow-toggle
298
+ >
299
+ {iconPlacement === 'start' && icon}
300
+ {text}
301
+ {iconPlacement === 'end' && icon}
302
+ </button>
303
+ <div
304
+ id={menuId}
305
+ className={classNames(
306
+ 'menu nav-overflow-menu',
307
+ placementClasses,
308
+ menuStrategy === 'fixed' && 'position-fixed',
309
+ { show: this.store.open && hasOverflow }
310
+ )}
311
+ role="menu"
312
+ hidden={!this.store.open || !hasOverflow}
313
+ >
314
+ {this.store.overflowIndices.map(index =>
315
+ this.renderChild(items[index], index, true)
316
+ )}
317
+ </div>
318
+ </div>
319
+ </Nav>
320
+ </Root>
321
+ );
322
+ }
323
+ }
@@ -0,0 +1,21 @@
1
+ import { action, observable } from 'mobx';
2
+
3
+ export class NavOverflowModel {
4
+ @observable accessor initialized = false;
5
+
6
+ @observable accessor open = false;
7
+
8
+ @observable accessor overflowIndices: number[] = [];
9
+
10
+ @action
11
+ setOpen = (open: boolean) => (this.open = open);
12
+
13
+ @action
14
+ toggle = () => (this.open = !this.open);
15
+
16
+ @action
17
+ setOverflowIndices = (indices: number[]) => {
18
+ this.initialized = true;
19
+ this.overflowIndices = indices;
20
+ };
21
+ }
package/source/Navbar.tsx CHANGED
@@ -1,46 +1,67 @@
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';
8
9
  expand?: boolean | 'sm' | 'md' | 'lg' | 'xl' | '2xl';
10
+ translucent?: boolean;
9
11
  }
10
12
 
11
- export const Navbar: FC<NavbarProps> = ({ expand, theme, className, ...props }) => (
12
- <nav
13
- {...props}
14
- className={classNames(
15
- 'navbar',
16
- expand && (expand === true ? 'navbar-expand' : `${expand}:navbar-expand`),
17
- className
18
- )}
19
- data-bs-theme={theme}
20
- />
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
+ )
21
26
  );
27
+ Navbar.displayName = 'Navbar';
22
28
 
23
29
  export type NavbarBrandProps =
24
30
  (ComponentPropsWithRef<'a'> & { as?: 'a' }) | (ComponentPropsWithRef<'span'> & { as: 'span' });
25
31
 
26
- export const NavbarBrand: FC<NavbarBrandProps> = props => {
27
- if (props.as === 'span') {
28
- const { as: _as, className, ...spanProps } = props;
29
-
30
- return <span {...spanProps} className={classNames('navbar-brand', className)} />;
31
- }
32
- const { as: _as, className, ...anchorProps } = props;
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';
33
38
 
34
- return <a {...anchorProps} className={classNames('navbar-brand', className)} />;
35
- };
39
+ export interface NavbarNavProps extends ComponentPropsWithRef<'ul'> {
40
+ variant?: 'tabs' | 'pills' | 'underline';
41
+ nav?: boolean;
42
+ }
36
43
 
37
- export const NavbarNav: FC<ComponentPropsWithRef<'ul'>> = ({ className, ...props }) => (
38
- <ul {...props} className={classNames('navbar-nav', className)} />
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
+ )
39
56
  );
57
+ NavbarNav.displayName = 'NavbarNav';
40
58
 
41
- export const NavbarText: FC<ComponentPropsWithRef<'span'>> = ({ className, ...props }) => (
42
- <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
+ )
43
63
  );
64
+ NavbarText.displayName = 'NavbarText';
44
65
 
45
66
  export interface NavbarTogglerProps extends Omit<
46
67
  ComponentPropsWithRef<'button'>,
@@ -49,26 +70,25 @@ export interface NavbarTogglerProps extends Omit<
49
70
  expanded?: boolean;
50
71
  }
51
72
 
52
- export const NavbarToggler: FC<NavbarTogglerProps> = ({
53
- expanded,
54
- className,
55
- 'aria-label': ariaLabel = 'Toggle navigation',
56
- ...props
57
- }) => (
58
- <Button
59
- {...props}
60
- type="button"
61
- className={classNames('navbar-toggler', className)}
62
- aria-label={ariaLabel}
63
- aria-expanded={expanded}
64
- icon
65
- >
66
- <span className="navbar-toggler-icon" aria-hidden="true" />
67
- </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
+ )
68
93
  );
69
-
70
- Navbar.displayName = 'Navbar';
71
- NavbarBrand.displayName = 'NavbarBrand';
72
- NavbarNav.displayName = 'NavbarNav';
73
- NavbarText.displayName = 'NavbarText';
74
94
  NavbarToggler.displayName = 'NavbarToggler';
package/source/Overlay.ts CHANGED
@@ -1,10 +1,172 @@
1
- import { ComponentPropsWithRef } from 'react';
1
+ import { action, observable } from 'mobx';
2
+ import { ComponentPropsWithRef, CSSProperties } from 'react';
3
+
4
+ import { Breakpoint } from './Grid';
2
5
 
3
6
  export type OverlayPlacement = 'top' | 'right' | 'bottom' | 'left';
7
+ export type FloatingPlacement = OverlayPlacement | `${OverlayPlacement}-${'start' | 'end'}`;
8
+ export type OverlayOffset = number | readonly [number, number];
9
+ export type OverlayContainer = string | HTMLElement | (() => HTMLElement);
10
+
11
+ export interface FloatingOptions {
12
+ placement?: FloatingPlacement;
13
+ responsivePlacement?: Partial<Record<Breakpoint, FloatingPlacement>>;
14
+ offset?: OverlayOffset;
15
+ padding?: number;
16
+ flip?: boolean;
17
+ }
18
+
19
+ const BREAKPOINTS: Record<Breakpoint, number> = {
20
+ sm: 576,
21
+ md: 768,
22
+ lg: 1024,
23
+ xl: 1280,
24
+ '2xl': 1536
25
+ };
26
+ const OPPOSITE: Record<OverlayPlacement, OverlayPlacement> = {
27
+ top: 'bottom',
28
+ bottom: 'top',
29
+ left: 'right',
30
+ right: 'left'
31
+ };
32
+
33
+ /** Owns geometry and subscriptions shared by menus, tooltips and popovers. */
34
+ export class FloatingPosition {
35
+ @observable accessor placement: FloatingPlacement = 'top';
36
+ @observable accessor style: CSSProperties = {};
37
+ @observable accessor arrowStyle: CSSProperties = {};
38
+
39
+ reference?: HTMLElement;
40
+ element?: HTMLElement;
41
+ resize?: ResizeObserver;
42
+ options: FloatingOptions = {};
43
+
44
+ attach(reference: HTMLElement, element: HTMLElement, options: FloatingOptions) {
45
+ this.dispose();
46
+ this.reference = reference;
47
+ this.element = element;
48
+ this.options = options;
49
+
50
+ window.addEventListener('resize', this.update);
51
+ document.addEventListener('scroll', this.update, true);
52
+
53
+ this.resize = new window.ResizeObserver(this.update);
54
+ this.resize.observe(reference);
55
+ this.resize.observe(element);
56
+ this.update();
57
+ }
58
+
59
+ dispose = () => {
60
+ window.removeEventListener('resize', this.update);
61
+ document.removeEventListener('scroll', this.update, true);
62
+ this.resize?.disconnect();
63
+ this.resize = undefined;
64
+ this.reference = undefined;
65
+ this.element = undefined;
66
+ };
67
+
68
+ @action
69
+ update = () => {
70
+ const { reference, element, options } = this;
71
+ if (!reference || !element) return;
72
+
73
+ const { responsivePlacement, offset = 0, padding = 8, flip = true } = options;
74
+ const responsive = (Object.keys(BREAKPOINTS) as Breakpoint[])
75
+ .reverse()
76
+ .find(key => window.innerWidth >= BREAKPOINTS[key] && responsivePlacement?.[key]);
77
+ const preferred =
78
+ (responsive && responsivePlacement?.[responsive]) || options.placement || 'top';
79
+ const [side, alignment] = preferred.split('-') as [
80
+ OverlayPlacement,
81
+ 'start' | 'end' | undefined
82
+ ];
83
+ const anchor = reference.getBoundingClientRect();
84
+ const box = element.getBoundingClientRect();
85
+ const [skid, distance] = typeof offset === 'number' ? [0, offset] : offset;
86
+ const available: Record<OverlayPlacement, number> = {
87
+ top: anchor.top - padding,
88
+ bottom: window.innerHeight - anchor.bottom - padding,
89
+ left: anchor.left - padding,
90
+ right: window.innerWidth - anchor.right - padding
91
+ };
92
+ const required = (side === 'top' || side === 'bottom' ? box.height : box.width) + distance;
93
+ const actual =
94
+ flip && available[side] < required && available[OPPOSITE[side]] > available[side]
95
+ ? OPPOSITE[side]
96
+ : side;
97
+ let left = anchor.left + (anchor.width - box.width) / 2;
98
+ let top = anchor.top + (anchor.height - box.height) / 2;
99
+
100
+ if (actual === 'top' || actual === 'bottom') {
101
+ top = actual === 'top' ? anchor.top - box.height - distance : anchor.bottom + distance;
102
+ if (alignment) left = alignment === 'start' ? anchor.left : anchor.right - box.width;
103
+ left += skid;
104
+ } else {
105
+ left = actual === 'left' ? anchor.left - box.width - distance : anchor.right + distance;
106
+ if (alignment) top = alignment === 'start' ? anchor.top : anchor.bottom - box.height;
107
+ top += skid;
108
+ }
109
+ left = Math.max(padding, Math.min(left, window.innerWidth - box.width - padding));
110
+ top = Math.max(padding, Math.min(top, window.innerHeight - box.height - padding));
111
+
112
+ const arrowStyle: CSSProperties =
113
+ actual === 'top' || actual === 'bottom'
114
+ ? {
115
+ left: Math.max(
116
+ 8,
117
+ Math.min(box.width - 8, anchor.left + anchor.width / 2 - left)
118
+ )
119
+ }
120
+ : {
121
+ top: Math.max(
122
+ 8,
123
+ Math.min(box.height - 8, anchor.top + anchor.height / 2 - top)
124
+ )
125
+ };
126
+ if (this.arrowStyle.left !== arrowStyle.left || this.arrowStyle.top !== arrowStyle.top)
127
+ this.arrowStyle = arrowStyle;
128
+
129
+ const parent = element.offsetParent;
130
+ const positionedParent =
131
+ parent instanceof HTMLElement &&
132
+ !(parent === document.body && getComputedStyle(parent).position === 'static')
133
+ ? parent
134
+ : undefined;
135
+ const parentBox = positionedParent?.getBoundingClientRect();
136
+ left += positionedParent
137
+ ? positionedParent.scrollLeft - parentBox!.left - positionedParent.clientLeft
138
+ : window.scrollX;
139
+ top += positionedParent
140
+ ? positionedParent.scrollTop - parentBox!.top - positionedParent.clientTop
141
+ : window.scrollY;
142
+ const placement: FloatingPlacement = alignment ? `${actual}-${alignment}` : actual;
143
+ if (this.placement !== placement) this.placement = placement;
144
+
145
+ const maxWidth = Math.max(0, window.innerWidth - padding * 2);
146
+ const maxHeight = Math.max(0, window.innerHeight - padding * 2);
147
+ if (
148
+ this.style.left !== left ||
149
+ this.style.top !== top ||
150
+ this.style.maxWidth !== maxWidth ||
151
+ this.style.maxHeight !== maxHeight
152
+ )
153
+ this.style = {
154
+ position: 'absolute',
155
+ left,
156
+ top,
157
+ right: 'auto',
158
+ bottom: 'auto',
159
+ margin: 0,
160
+ maxWidth,
161
+ maxHeight
162
+ };
163
+ };
164
+ }
4
165
 
5
166
  export interface OverlayTriggerProps extends ComponentPropsWithRef<'div'> {
6
167
  placement?: OverlayPlacement;
7
168
  show?: boolean;
169
+ arrowStyle?: CSSProperties;
8
170
  }
9
171
 
10
172
  export const OVERLAY_DIRECTIONS: Record<OverlayPlacement, string> = {