idea-react 2.3.5 → 3.0.0-alpha.0

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.
@@ -0,0 +1,184 @@
1
+ 'use client';
2
+
3
+ import classNames from 'classnames';
4
+ import { action, observable } from 'mobx';
5
+ import { observer } from 'mobx-react';
6
+ import {
7
+ Component,
8
+ createRef,
9
+ FC,
10
+ FocusEvent,
11
+ HTMLAttributes,
12
+ KeyboardEvent,
13
+ MouseEvent,
14
+ ReactNode
15
+ } from 'react';
16
+
17
+ import { Button, ButtonProps } from './Button';
18
+
19
+ export const MenuItem: FC<ButtonProps> = ({ className, ...props }) => (
20
+ <Button {...props} className={classNames('menu-item', className)} />
21
+ );
22
+
23
+ export interface MenuProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange'> {
24
+ label: ReactNode;
25
+ autoClose?: boolean | 'inside' | 'outside';
26
+ align?: 'start' | 'end';
27
+ menuClassName?: string;
28
+ toggleProps?: Omit<
29
+ Extract<ButtonProps, { href?: never }>,
30
+ 'children' | 'onClick' | 'onKeyDown'
31
+ >;
32
+ onOpenChange?: (open: boolean) => void;
33
+ }
34
+
35
+ @observer
36
+ export class Menu extends Component<MenuProps> {
37
+ @observable accessor open = false;
38
+ root = createRef<HTMLDivElement>();
39
+ trigger = createRef<HTMLButtonElement>();
40
+ panel = createRef<HTMLDivElement>();
41
+
42
+ componentDidMount() {
43
+ document.addEventListener('pointerdown', this.handleOutside);
44
+ }
45
+
46
+ componentWillUnmount() {
47
+ document.removeEventListener('pointerdown', this.handleOutside);
48
+ }
49
+
50
+ @action
51
+ setOpen = (open: boolean) => {
52
+ if (this.open === open) return;
53
+ this.open = open;
54
+ this.props.onOpenChange?.(open);
55
+ };
56
+
57
+ handleOutside = ({ target }: PointerEvent) => {
58
+ const { autoClose = true } = this.props;
59
+ if (
60
+ autoClose !== false &&
61
+ autoClose !== 'inside' &&
62
+ target instanceof Node &&
63
+ !this.root.current?.contains(target)
64
+ )
65
+ this.setOpen(false);
66
+ };
67
+
68
+ items() {
69
+ return [
70
+ ...(this.panel.current?.querySelectorAll<HTMLElement>(
71
+ 'a[href]:not([aria-disabled="true"]), button:not(:disabled):not([aria-disabled="true"])'
72
+ ) ?? [])
73
+ ].filter(item => !item.closest('[hidden]'));
74
+ }
75
+
76
+ handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
77
+ this.props.onKeyDown?.(event);
78
+
79
+ const { key, defaultPrevented } = event;
80
+ if (defaultPrevented) return;
81
+
82
+ if (key === 'Escape' && this.open) {
83
+ event.preventDefault();
84
+ this.setOpen(false);
85
+ return this.trigger.current?.focus();
86
+ }
87
+ if (key === 'Tab') return this.setOpen(false);
88
+ if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(key)) return;
89
+
90
+ event.preventDefault();
91
+ this.setOpen(true);
92
+
93
+ queueMicrotask(() => {
94
+ const items = this.items();
95
+ const index = items.findIndex(item => item === document.activeElement);
96
+
97
+ let next = key === 'Home' ? 0 : items.length - 1;
98
+
99
+ if (key === 'ArrowDown') next = (index + 1) % items.length;
100
+ if (key === 'ArrowUp') next = index <= 0 ? items.length - 1 : index - 1;
101
+ items[next]?.focus();
102
+ });
103
+ };
104
+
105
+ handleBlur = (event: FocusEvent<HTMLDivElement>) => {
106
+ const { onBlur, autoClose = true } = this.props;
107
+ onBlur?.(event);
108
+
109
+ const { defaultPrevented, currentTarget, relatedTarget } = event;
110
+ if (
111
+ !defaultPrevented &&
112
+ autoClose !== false &&
113
+ autoClose !== 'inside' &&
114
+ !currentTarget.contains(relatedTarget)
115
+ )
116
+ this.setOpen(false);
117
+ };
118
+
119
+ handleToggle = () => this.setOpen(!this.open);
120
+
121
+ handleSelect = ({ target }: MouseEvent<HTMLDivElement>) => {
122
+ const { autoClose = true } = this.props;
123
+ if (
124
+ autoClose &&
125
+ autoClose !== 'outside' &&
126
+ target instanceof Element &&
127
+ target.closest('a[href], button:not(:disabled)')
128
+ )
129
+ this.setOpen(false);
130
+ };
131
+
132
+ render() {
133
+ const {
134
+ label,
135
+ autoClose: _autoClose,
136
+ align = 'end',
137
+ menuClassName,
138
+ toggleProps,
139
+ onOpenChange: _onOpenChange,
140
+ onKeyDown: _onKeyDown,
141
+ onBlur: _onBlur,
142
+ children,
143
+ className,
144
+ id,
145
+ ...props
146
+ } = this.props;
147
+ const { open } = this;
148
+
149
+ return (
150
+ <div
151
+ {...props}
152
+ ref={this.root}
153
+ id={id}
154
+ className={classNames('position-relative', className)}
155
+ onKeyDown={this.handleKeyDown}
156
+ onBlur={this.handleBlur}
157
+ >
158
+ <Button
159
+ {...toggleProps}
160
+ ref={this.trigger}
161
+ aria-expanded={open}
162
+ aria-controls={id ? `${id}-menu` : undefined}
163
+ onClick={this.handleToggle}
164
+ >
165
+ {label}
166
+ </Button>
167
+ <div
168
+ ref={this.panel}
169
+ id={id ? `${id}-menu` : undefined}
170
+ className={classNames(
171
+ 'menu top-100',
172
+ align === 'end' ? 'end-0' : 'start-0',
173
+ { show: open },
174
+ menuClassName
175
+ )}
176
+ hidden={!open}
177
+ onClick={this.handleSelect}
178
+ >
179
+ {children}
180
+ </div>
181
+ </div>
182
+ );
183
+ }
184
+ }
@@ -0,0 +1,103 @@
1
+ 'use client';
2
+
3
+ import classNames from 'classnames';
4
+ import {
5
+ Component,
6
+ createRef,
7
+ DialogHTMLAttributes,
8
+ KeyboardEvent,
9
+ MouseEvent,
10
+ SyntheticEvent
11
+ } from 'react';
12
+
13
+ import styles from './style.module.css';
14
+
15
+ export interface NativeDialogProps extends Omit<DialogHTMLAttributes<HTMLDialogElement>, 'open'> {
16
+ modal?: boolean;
17
+ keyboard?: boolean;
18
+ backdrop?: boolean | 'static';
19
+ }
20
+
21
+ export class NativeDialog extends Component<NativeDialogProps> {
22
+ element = createRef<HTMLDialogElement>();
23
+ returnFocus?: HTMLElement;
24
+
25
+ get open() {
26
+ return this.element.current?.open ?? false;
27
+ }
28
+
29
+ show = () => {
30
+ if (this.open) return;
31
+ const element = this.element.current;
32
+ if (!element) return;
33
+ if (document.activeElement instanceof HTMLElement)
34
+ this.returnFocus = document.activeElement;
35
+ if (this.props.modal === false) element.show();
36
+ else element.showModal();
37
+ };
38
+
39
+ close = () => {
40
+ if (!this.open) return;
41
+ this.element.current?.close();
42
+ this.returnFocus?.focus();
43
+ };
44
+
45
+ componentWillUnmount() {
46
+ this.close();
47
+ }
48
+
49
+ handleCancel = (event: SyntheticEvent<HTMLDialogElement>) => {
50
+ const { onCancel, keyboard = true } = this.props;
51
+ onCancel?.(event);
52
+ const { defaultPrevented } = event;
53
+ event.preventDefault();
54
+ if (keyboard && !defaultPrevented) this.close();
55
+ };
56
+
57
+ handleKeyDown = (event: KeyboardEvent<HTMLDialogElement>) => {
58
+ const { onKeyDown, keyboard = true } = this.props;
59
+ onKeyDown?.(event);
60
+ const { key, defaultPrevented } = event;
61
+ if (key !== 'Escape' || !keyboard || defaultPrevented) return;
62
+ event.preventDefault();
63
+ this.close();
64
+ };
65
+
66
+ handleClick = (event: MouseEvent<HTMLDialogElement>) => {
67
+ const { onClick, backdrop = true } = this.props;
68
+ onClick?.(event);
69
+ const { defaultPrevented, target, currentTarget, clientX, clientY } = event;
70
+ if (backdrop !== true || defaultPrevented || target !== currentTarget) return;
71
+ const { left, right, top, bottom } = currentTarget.getBoundingClientRect();
72
+ if (clientX < left || clientX > right || clientY < top || clientY > bottom) this.close();
73
+ };
74
+
75
+ render() {
76
+ const {
77
+ modal: _modal,
78
+ keyboard: _keyboard,
79
+ backdrop = true,
80
+ className,
81
+ onCancel: _onCancel,
82
+ onClick: _onClick,
83
+ onKeyDown: _onKeyDown,
84
+ ...props
85
+ } = this.props;
86
+
87
+ return (
88
+ <dialog
89
+ {...props}
90
+ ref={this.element}
91
+ className={classNames(
92
+ 'dialog',
93
+ backdrop === false && styles.hiddenBackdrop,
94
+ className
95
+ )}
96
+ data-backdrop={backdrop === false ? 'false' : undefined}
97
+ onCancel={this.handleCancel}
98
+ onKeyDown={this.handleKeyDown}
99
+ onClick={this.handleClick}
100
+ />
101
+ );
102
+ }
103
+ }
@@ -0,0 +1,4 @@
1
+ .hiddenBackdrop {
2
+ --bs-dialog-backdrop-bg: transparent;
3
+ --bs-dialog-backdrop-blur: 0;
4
+ }
@@ -0,0 +1,17 @@
1
+ import classNames from 'classnames';
2
+ import { ComponentPropsWithRef, FC } from 'react';
3
+
4
+ export interface NavbarProps extends ComponentPropsWithRef<'nav'> {
5
+ expand?: boolean | 'sm' | 'md' | 'lg' | 'xl' | '2xl';
6
+ }
7
+
8
+ export const Navbar: FC<NavbarProps> = ({ expand, className, ...props }) => (
9
+ <nav
10
+ {...props}
11
+ className={classNames(
12
+ 'navbar',
13
+ expand && (expand === true ? 'navbar-expand' : `${expand}:navbar-expand`),
14
+ className
15
+ )}
16
+ />
17
+ );
package/source/index.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from './animate';
2
2
  export * from './Avatar';
3
+ export * from './Badge';
4
+ export * from './Button';
3
5
  export * from './ClickBoundary';
4
6
  export * from './CodeBlock';
5
7
  export * from './color';
@@ -11,8 +13,11 @@ export * from './HorizontalMarquee';
11
13
  export * from './Icon';
12
14
  export * from './LiveTSX';
13
15
  export * from './Loading';
16
+ export * from './Menu';
14
17
  export * from './MonthCalendar';
15
18
  export * from './Nameplate';
19
+ export * from './NativeDialog';
20
+ export * from './Navbar';
16
21
  export * from './OverlayBox';
17
22
  export * from './PageNav';
18
23
  export * from './ScrollNav';
package/source/types.d.ts CHANGED
@@ -6,3 +6,7 @@ declare module '*.png' {
6
6
  const path: string;
7
7
  export default path;
8
8
  }
9
+ declare module '*.module.css' {
10
+ const classes: Readonly<Record<string, string>>;
11
+ export default classes;
12
+ }
package/tsconfig.json CHANGED
@@ -8,14 +8,18 @@
8
8
  "useDefineForClassFields": true,
9
9
  "jsx": "react-jsx",
10
10
  "skipLibCheck": true,
11
- "lib": ["ES2024", "DOM"]
11
+ "lib": ["ES2024", "DOM", "DOM.Iterable"]
12
12
  },
13
13
  "include": ["*.ts", "source/**/*"],
14
14
  "typedocOptions": {
15
15
  "name": "Idea React",
16
+ "includeVersion": true,
16
17
  "excludeExternals": true,
17
18
  "excludePrivate": true,
19
+ "plugin": ["typedoc-plugin-mdn-links"],
18
20
  "readme": "./ReadMe.md",
19
- "plugin": ["typedoc-plugin-mdn-links"]
21
+ "navigationLinks": {
22
+ "GitHub": "https://github.com/idea2app/Idea-React"
23
+ }
20
24
  }
21
25
  }