@inceptionbg/iui 2.0.22 → 2.0.24

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 (237) hide show
  1. package/dist/NoAccessPage-CJisyKrk.js +2 -0
  2. package/dist/{NoAccessPage-BmizYfw0.js.map → NoAccessPage-CJisyKrk.js.map} +1 -1
  3. package/dist/NotFoundPage-BeRit0kr.js +2 -0
  4. package/dist/{NotFoundPage-Cv544vAr.js.map → NotFoundPage-BeRit0kr.js.map} +1 -1
  5. package/dist/icons/index.d.ts +3 -1
  6. package/dist/icons/index.js +1 -1
  7. package/dist/index.d.ts +452 -408
  8. package/dist/index.js +1 -1
  9. package/dist/index.js.map +1 -1
  10. package/dist/iui.css +1 -1
  11. package/idea/GridTable/GridTable.tsx +1 -1
  12. package/idea/Menu/Menu.tsx +5 -3
  13. package/idea/Menu/MenuItem.tsx +3 -2
  14. package/idea/Menu/hooks/useMenuControl.ts +1 -1
  15. package/idea/Menu.tsx +3 -2
  16. package/idea/NoAccessInfo.tsx +1 -1
  17. package/idea/Table/Components/Edit/ItemActionsMenu.tsx +2 -1
  18. package/idea/Table/Components/Edit/ItemEditOptionsButtons.tsx +1 -1
  19. package/idea/Table/Components/Edit/TableEditRow.tsx +3 -2
  20. package/idea/Table/Components/FilterItem.tsx +2 -2
  21. package/idea/Table/Components/Header/TableHeader.tsx +3 -2
  22. package/idea/Table/Components/Header/TableHeaderRow.tsx +2 -2
  23. package/idea/Table/Components/Print/CustomTablePrint.tsx +3 -2
  24. package/idea/Table/Components/SetSortList.tsx +2 -2
  25. package/idea/Table/Components/SetTableFilter.tsx +3 -2
  26. package/idea/Table/Components/TableFooter.tsx +6 -4
  27. package/idea/Table/Components/TableOptions.tsx +3 -2
  28. package/idea/Table/Table.tsx +3 -2
  29. package/idea/Table/hooks/useTableKeyboard.ts +2 -2
  30. package/package.json +1 -1
  31. package/src/assets/icons/duotone/faBell.ts +17 -17
  32. package/src/assets/icons/duotone/faCircleUser.ts +17 -17
  33. package/src/assets/icons/index.ts +25 -24
  34. package/src/assets/icons/light/faArrowDownShortWide.ts +15 -15
  35. package/src/assets/icons/light/faArrowDownWideShort.ts +15 -15
  36. package/src/assets/icons/light/faArrowLeft.ts +5 -5
  37. package/src/assets/icons/light/faArrowRight.ts +5 -5
  38. package/src/assets/icons/light/faArrowUpArrowDown.ts +15 -15
  39. package/src/assets/icons/light/faArrowUpRightFromSquare.ts +15 -15
  40. package/src/assets/icons/light/faArrowsToLine.ts +15 -15
  41. package/src/assets/icons/light/faArrowsUpDown.ts +15 -15
  42. package/src/assets/icons/light/faBell.ts +15 -15
  43. package/src/assets/icons/light/faBookmark.ts +15 -15
  44. package/src/assets/icons/light/faBookmarkSlash.ts +15 -15
  45. package/src/assets/icons/light/faCalendarPlus.ts +15 -15
  46. package/src/assets/icons/light/faCheck.ts +15 -15
  47. package/src/assets/icons/light/faChevronDown.ts +15 -15
  48. package/src/assets/icons/light/faChevronRight.ts +15 -15
  49. package/src/assets/icons/light/faCircleInfo.ts +15 -15
  50. package/src/assets/icons/light/faCircleXmark.ts +15 -15
  51. package/src/assets/icons/light/faClipboardCheck.ts +15 -15
  52. package/src/assets/icons/light/faClockRotateLeft.ts +15 -15
  53. package/src/assets/icons/light/faEllipsisVertical.ts +15 -15
  54. package/src/assets/icons/light/faEnvelope.ts +15 -15
  55. package/src/assets/icons/light/faEye.ts +15 -15
  56. package/src/assets/icons/light/faEyeSlash.ts +15 -15
  57. package/src/assets/icons/light/faFilter.ts +15 -15
  58. package/src/assets/icons/light/faGear.ts +15 -15
  59. package/src/assets/icons/light/faHouse.ts +15 -15
  60. package/src/assets/icons/light/faIdBadge.ts +15 -15
  61. package/src/assets/icons/light/faLineColumns.ts +15 -15
  62. package/src/assets/icons/light/faLink.ts +15 -15
  63. package/src/assets/icons/light/faMagnifyingGlass.ts +15 -15
  64. package/src/assets/icons/light/faPen.ts +15 -15
  65. package/src/assets/icons/light/faPrint.ts +15 -15
  66. package/src/assets/icons/light/faQuestion.ts +15 -15
  67. package/src/assets/icons/light/faRotateRight.ts +15 -15
  68. package/src/assets/icons/light/faTrashCan.ts +15 -15
  69. package/src/assets/icons/light/faTriangleExclamation.ts +15 -15
  70. package/src/assets/icons/light/faXmark.ts +15 -15
  71. package/src/assets/icons/regular/faArrowLeft.ts +15 -15
  72. package/src/assets/icons/regular/faArrowRightArrowLeft.ts +15 -15
  73. package/src/assets/icons/regular/faCircleCheck.ts +15 -15
  74. package/src/assets/icons/regular/faCircleExclamation.ts +15 -15
  75. package/src/assets/icons/regular/faCircleInfo.ts +15 -15
  76. package/src/assets/icons/regular/faFileArrowDown.ts +15 -15
  77. package/src/assets/icons/regular/faFilterCircleXmark.ts +15 -15
  78. package/src/assets/icons/regular/faTriangleExclamation.ts +15 -15
  79. package/src/assets/icons/solid/faAngleLeft.ts +15 -15
  80. package/src/assets/icons/solid/faAngleRight.ts +15 -15
  81. package/src/assets/icons/solid/faArrowDownWideShort.ts +15 -15
  82. package/src/assets/icons/solid/faCaretDown.ts +15 -15
  83. package/src/assets/icons/solid/faCheck.ts +15 -15
  84. package/src/assets/icons/solid/faEllipsisVertical.ts +15 -15
  85. package/src/assets/icons/solid/faEnvelopeDot.ts +15 -15
  86. package/src/assets/icons/solid/faFilter.ts +15 -15
  87. package/src/assets/icons/solid/faFloppyDisk.ts +15 -15
  88. package/src/assets/icons/solid/faGripDotsVertical.ts +15 -15
  89. package/src/assets/icons/solid/faListUl.ts +15 -15
  90. package/src/assets/icons/solid/faMinus.ts +15 -15
  91. package/src/assets/icons/solid/faPlus.ts +15 -15
  92. package/src/assets/icons/solid/faPrint.ts +15 -15
  93. package/src/assets/icons/solid/faRotateRight.ts +15 -15
  94. package/src/assets/icons/solid/faXmark.ts +15 -15
  95. package/src/components/Accordions/Accordions.tsx +75 -74
  96. package/src/components/Alert/Alert.tsx +31 -31
  97. package/src/components/Badge/DotBadge.tsx +16 -16
  98. package/src/components/Badge/NotificationBadge.tsx +29 -29
  99. package/src/components/Badge/PillBadge.tsx +13 -13
  100. package/src/components/Button/Button.tsx +56 -56
  101. package/src/components/Button/IconButton.tsx +51 -51
  102. package/src/components/Button/SplitButton.tsx +91 -91
  103. package/src/components/Dashboard/Dashboard.tsx +9 -9
  104. package/src/components/Dashboard/DashboardWidget.tsx +44 -44
  105. package/src/components/Dashboard/FastLinksWidget/FastLinksWidget.tsx +37 -37
  106. package/src/components/Dialog/Dialog.tsx +166 -162
  107. package/src/components/Dialog/components/DialogFooter.tsx +100 -98
  108. package/src/components/Header/Components/EnvBadge.tsx +17 -17
  109. package/src/components/Header/Components/ModuleSelect.tsx +68 -68
  110. package/src/components/Header/Components/Notifications.tsx +202 -208
  111. package/src/components/Header/Components/UserMenu.tsx +52 -52
  112. package/src/components/Header/Header.tsx +37 -36
  113. package/src/components/Helper/Collapse.tsx +53 -52
  114. package/src/components/Inputs/Checkbox.tsx +53 -53
  115. package/src/components/Inputs/CurrencyInput.tsx +124 -123
  116. package/src/components/Inputs/DateInput/DateInput.tsx +184 -183
  117. package/src/components/Inputs/DateInput/components/DatePartInput.tsx +159 -158
  118. package/src/components/Inputs/InputWrapper.tsx +95 -95
  119. package/src/components/Inputs/NumberInput.tsx +86 -86
  120. package/src/components/Inputs/PasswordInput.tsx +40 -39
  121. package/src/components/Inputs/PhoneInput/PhoneInput.tsx +108 -0
  122. package/src/components/Inputs/PhoneInput/utils/countryCode.ts +20 -0
  123. package/src/components/Inputs/Radio.tsx +59 -59
  124. package/src/components/Inputs/RadioGroup.tsx +48 -48
  125. package/src/components/Inputs/RadioLarge.tsx +39 -39
  126. package/src/components/Inputs/SearchInput.tsx +46 -45
  127. package/src/components/Inputs/Selects/Select.tsx +17 -17
  128. package/src/components/Inputs/Selects/components/CustomSelectComponents.tsx +20 -19
  129. package/src/components/Inputs/Selects/components/SelectWrapper.tsx +118 -118
  130. package/src/components/Inputs/Selects/utils/selectStyles.ts +101 -101
  131. package/src/components/Inputs/TextAreaInput.tsx +68 -67
  132. package/src/components/Inputs/TextInput.tsx +100 -99
  133. package/src/components/Inputs/TimeInput/TimeInput.tsx +81 -81
  134. package/src/components/List/List.tsx +21 -20
  135. package/src/components/List/ListItem.tsx +70 -69
  136. package/src/components/Loader/Loader.tsx +59 -53
  137. package/src/components/Loader/ProgressBar.tsx +41 -41
  138. package/src/components/Menu/Menu.tsx +81 -80
  139. package/src/components/Menu/MenuItem.tsx +46 -46
  140. package/src/components/Menu/NewMenu.tsx +63 -63
  141. package/src/components/Menu/hooks/useMenuPosition.tsx +116 -115
  142. package/src/components/Pullover/Pullover.tsx +197 -196
  143. package/src/components/Router/Router.tsx +52 -51
  144. package/src/components/Sidebar/AddButton.tsx +22 -22
  145. package/src/components/Sidebar/Sidebar.tsx +85 -83
  146. package/src/components/Sidebar/SidebarItem.tsx +84 -84
  147. package/src/components/Sidebar/types/ISidebar.ts +28 -28
  148. package/src/components/Table/Table.tsx +259 -258
  149. package/src/components/Table/components/columns/ColumnsList.tsx +60 -59
  150. package/src/components/Table/components/columns/TableColumnsEdit.tsx +114 -112
  151. package/src/components/Table/components/edit/TableEditRow.tsx +78 -77
  152. package/src/components/Table/components/filters/FilterItem.tsx +15 -15
  153. package/src/components/Table/components/filters/TableFilters.tsx +126 -125
  154. package/src/components/Table/components/footer/TableFooter.tsx +126 -124
  155. package/src/components/Table/components/header/TableHeader.tsx +40 -40
  156. package/src/components/Table/components/header/TableHeaderRow.tsx +57 -57
  157. package/src/components/Table/components/items/TableItemActions.tsx +78 -78
  158. package/src/components/Table/components/print/TablePrint.tsx +200 -199
  159. package/src/components/Table/components/select/TableSelect.tsx +50 -49
  160. package/src/components/Table/components/sort/TableSort.tsx +73 -72
  161. package/src/components/Table/components/templates/CreateTemplateDialog.tsx +58 -48
  162. package/src/components/Table/components/templates/TableTemplates.tsx +65 -112
  163. package/src/components/Table/components/templates/TemplatesPullover.tsx +88 -0
  164. package/src/components/Table/contexts/TableContext.tsx +116 -123
  165. package/src/components/Table/hooks/localHooks/useLocalTableColumns.tsx +70 -70
  166. package/src/components/Table/hooks/localHooks/useLocalTableData.tsx +84 -84
  167. package/src/components/Table/hooks/localHooks/useLocalTableKeyboard.ts +176 -177
  168. package/src/components/Table/hooks/localHooks/useLocalTablePagination.ts +12 -12
  169. package/src/components/Table/hooks/useTableColumns.ts +34 -34
  170. package/src/components/Table/hooks/useTableEdit.tsx +140 -140
  171. package/src/components/Table/hooks/useTableFilterFields.tsx +150 -150
  172. package/src/components/Table/hooks/useTablePagination.ts +19 -19
  173. package/src/components/Table/hooks/useTablePrint.ts +83 -83
  174. package/src/components/Table/hooks/useTableSearch.ts +31 -30
  175. package/src/components/Table/hooks/useTableSelect.ts +19 -19
  176. package/src/components/Table/hooks/useTableSort.ts +8 -8
  177. package/src/components/Tabs/Tabs.tsx +68 -67
  178. package/src/components/Tooltip/Tooltip.tsx +191 -200
  179. package/src/components/Tree/Tree.tsx +22 -22
  180. package/src/components/Tree/TreeItem.tsx +57 -56
  181. package/src/components/Wrappers/AppLayout.tsx +17 -17
  182. package/src/components/Wrappers/ConditionalWrapper.tsx +10 -10
  183. package/src/components/Wrappers/FormWrapper.tsx +84 -83
  184. package/src/components/Wrappers/PageLayout.tsx +156 -155
  185. package/src/hooks/useBackgroundClose.ts +19 -18
  186. package/src/hooks/useGetFocusableElements.ts +43 -42
  187. package/src/hooks/useIsMenuOpen.ts +11 -11
  188. package/src/hooks/useLocalPopoverControl.ts +32 -32
  189. package/src/hooks/useOnEsc.ts +14 -14
  190. package/src/hooks/usePopupControl.ts +22 -22
  191. package/src/hooks/useZendesk.ts +21 -21
  192. package/src/index.ts +379 -382
  193. package/src/pages/NoAccessPage.tsx +27 -27
  194. package/src/pages/NotFoundPage.tsx +26 -26
  195. package/src/styles/common/helpers/_size.scss +0 -3
  196. package/src/styles/components/_accordions.scss +2 -1
  197. package/src/styles/components/_buttonSplit.scss +4 -29
  198. package/src/styles/components/_dialog.scss +2 -2
  199. package/src/styles/components/_input.scss +9 -0
  200. package/src/styles/components/_page.scss +2 -3
  201. package/src/types/IBasic.ts +32 -31
  202. package/src/types/ICountryCode.ts +5 -0
  203. package/src/types/IError.ts +5 -5
  204. package/src/types/IHeader.ts +40 -40
  205. package/src/types/IInfo.ts +1 -1
  206. package/src/types/IKeyboard.ts +33 -33
  207. package/src/types/IMenu.ts +19 -19
  208. package/src/types/INotifications.ts +15 -15
  209. package/src/types/IPopup.ts +17 -17
  210. package/src/types/IRouter.ts +6 -6
  211. package/src/types/ISelect.ts +54 -54
  212. package/src/types/ITab.ts +1 -1
  213. package/src/types/ITable.ts +290 -279
  214. package/src/types/ITree.ts +6 -6
  215. package/src/utils/InputPatternValidation.ts +12 -12
  216. package/src/utils/dateUtils.ts +32 -32
  217. package/src/utils/fileUtils.ts +177 -177
  218. package/src/utils/i18n/i18nIUICyrilic.ts +119 -116
  219. package/src/utils/i18n/i18nIUILatin.ts +120 -117
  220. package/src/utils/i18n/i18nIUIMe.ts +118 -115
  221. package/src/utils/icons.ts +13 -13
  222. package/src/utils/localStorageHelper.ts +24 -24
  223. package/src/utils/logoUtils.ts +7 -7
  224. package/src/utils/numberUtils.ts +21 -21
  225. package/src/utils/objectUtils.ts +114 -114
  226. package/src/utils/popupUtils.ts +82 -82
  227. package/src/utils/rootDir.ts +1 -1
  228. package/src/utils/stringUtils.ts +18 -18
  229. package/src/utils/tableUtils.ts +130 -130
  230. package/src/utils/toasts.ts +6 -6
  231. package/src/utils/urlUtils.ts +4 -4
  232. package/tsconfig.json +1 -0
  233. package/dist/NoAccessPage-BmizYfw0.js +0 -2
  234. package/dist/NotFoundPage-Cv544vAr.js +0 -2
  235. package/src/components/Dialog/hooks/useDialogKeyboard.ts +0 -42
  236. package/src/components/Inputs/Select2/Select.tsx +0 -258
  237. package/src/components/Inputs/Select2/select.scss +0 -42
@@ -1,63 +1,63 @@
1
- // import '@oddbird/popover-polyfill';
2
- // import polyfill from 'https://unpkg.com/@oddbird/css-anchor-positioning/fn';
3
- import '../../styles/components/_menu-v2.scss';
4
-
5
- import clsx from 'clsx';
6
- import { FC, ReactNode, RefObject, useEffect, useRef } from 'react';
7
- import { MenuItem } from './MenuItem';
8
- import { IMenuItem, IMenuPlacement } from '../../types/IMenu';
9
-
10
- interface Props {
11
- id: string;
12
- renderButton: (ref: RefObject<any>) => ReactNode;
13
- items: IMenuItem[];
14
- placement?: IMenuPlacement;
15
- size?: 's' | 'm' | 'l';
16
- className?: string;
17
- }
18
-
19
- export const MenuV2: FC<Props> = ({
20
- id,
21
- renderButton,
22
- items,
23
- placement = 'bottom-left',
24
- size,
25
- className,
26
- }) => {
27
- const buttonRef = useRef<HTMLButtonElement>(null);
28
- const popoverRef = useRef<HTMLDivElement>(null);
29
-
30
- useEffect(() => {
31
- // polyfill();
32
- buttonRef.current?.setAttribute('popovertarget', id);
33
- buttonRef.current?.style.setProperty('anchor-name', `--${id}`);
34
- popoverRef.current?.style.setProperty('position-anchor', `--${id}`);
35
- }, [id]);
36
-
37
- return (
38
- <>
39
- {renderButton(buttonRef)}
40
- <div
41
- ref={popoverRef}
42
- role="menu"
43
- // @ts-ignore
44
- popover="auto"
45
- className={clsx('iui-menu-v2', size, placement)}
46
- id={id}
47
- >
48
- <div className={className}>
49
- {items.map(item => (
50
- <MenuItem
51
- {...item}
52
- key={item.label}
53
- onClick={e => {
54
- item.onClick?.(e);
55
- !item.keepOpen && popoverRef.current?.hidePopover();
56
- }}
57
- />
58
- ))}
59
- </div>
60
- </div>
61
- </>
62
- );
63
- };
1
+ // import '@oddbird/popover-polyfill';
2
+ // import polyfill from 'https://unpkg.com/@oddbird/css-anchor-positioning/fn';
3
+ import '../../styles/components/_menu-v2.scss';
4
+ import type { FC, ReactNode, RefObject } from 'react';
5
+ import type { IMenuItem, IMenuPlacement } from '../../types/IMenu';
6
+ import { useEffect, useRef } from 'react';
7
+ import clsx from 'clsx';
8
+ import { MenuItem } from './MenuItem';
9
+
10
+ interface Props {
11
+ id: string;
12
+ renderButton: (ref: RefObject<any>) => ReactNode;
13
+ items: IMenuItem[];
14
+ placement?: IMenuPlacement;
15
+ size?: 's' | 'm' | 'l';
16
+ className?: string;
17
+ }
18
+
19
+ export const MenuV2: FC<Props> = ({
20
+ id,
21
+ renderButton,
22
+ items,
23
+ placement = 'bottom-left',
24
+ size,
25
+ className,
26
+ }) => {
27
+ const buttonRef = useRef<HTMLButtonElement>(null);
28
+ const popoverRef = useRef<HTMLDivElement>(null);
29
+
30
+ useEffect(() => {
31
+ // polyfill();
32
+ buttonRef.current?.setAttribute('popovertarget', id);
33
+ buttonRef.current?.style.setProperty('anchor-name', `--${id}`);
34
+ popoverRef.current?.style.setProperty('position-anchor', `--${id}`);
35
+ }, [id]);
36
+
37
+ return (
38
+ <>
39
+ {renderButton(buttonRef)}
40
+ <div
41
+ ref={popoverRef}
42
+ role="menu"
43
+ // @ts-ignore
44
+ popover="auto"
45
+ className={clsx('iui-menu-v2', size, placement)}
46
+ id={id}
47
+ >
48
+ <div className={className}>
49
+ {items.map(item => (
50
+ <MenuItem
51
+ {...item}
52
+ key={item.label}
53
+ onClick={e => {
54
+ item.onClick?.(e);
55
+ !item.keepOpen && popoverRef.current?.hidePopover();
56
+ }}
57
+ />
58
+ ))}
59
+ </div>
60
+ </div>
61
+ </>
62
+ );
63
+ };
@@ -1,115 +1,116 @@
1
- import { RefObject, useCallback, useLayoutEffect, useState } from 'react';
2
- import { IMenuPlacement } from '../../../types/IMenu';
3
- import { IAnyObject } from '../../../types/IBasic';
4
-
5
- interface Offsets {
6
- offsetTop: number;
7
- offsetLeft: number;
8
- }
9
-
10
- interface Props {
11
- isOpen: boolean;
12
- placement: IMenuPlacement;
13
- containerRef: RefObject<Element | null>;
14
- menuRef: RefObject<Element | null>;
15
- withMinWidth?: boolean;
16
- }
17
-
18
- export const useMenuPosition = ({
19
- isOpen,
20
- placement,
21
- containerRef,
22
- menuRef,
23
- withMinWidth,
24
- }: Props) => {
25
- const [offsets, setOffsets] = useState<Offsets>({
26
- offsetLeft: 0,
27
- offsetTop: 0,
28
- });
29
- const [maxHeight, setMaxHeight] = useState<number>();
30
- const [minWidth, setMinWidth] = useState(200);
31
-
32
- const recalculatePosition = useCallback(() => {
33
- if (containerRef.current && menuRef.current) {
34
- const containerRect = containerRef.current.getBoundingClientRect();
35
- const menuRect = menuRef.current.getBoundingClientRect();
36
- let [placementY, placementX] = placement.split('-');
37
-
38
- setMinWidth(containerRect.width);
39
-
40
- let offsetTop = containerRect.bottom + 5;
41
-
42
- let offsetLeft =
43
- containerRect.left +
44
- (placementX === 'right' ? containerRect.width - menuRect.width : 0);
45
- // + document.documentElement.scrollLeft;
46
-
47
- // Check for horizontal overflow and adjust placement
48
- if (placementX === 'right' && offsetLeft < 0) {
49
- placementX = 'left';
50
- offsetLeft = containerRect.left;
51
- } else if (
52
- placementX === 'left' &&
53
- offsetLeft + menuRect.width > window.innerWidth
54
- ) {
55
- placementX = 'right';
56
- offsetLeft = containerRect.left + containerRect.width - menuRect.width;
57
- }
58
-
59
- if (placementY === 'bottom') {
60
- setMaxHeight(window.innerHeight - containerRect.bottom - 20);
61
- }
62
-
63
- if (placementY === 'top') {
64
- setMaxHeight(
65
- window.innerHeight - containerRect.top + 40
66
- // - 2 * document.documentElement.scrollTop
67
- );
68
- }
69
-
70
- // Check for vertical overflow and adjust placement
71
- if (placementY === 'bottom' && offsetTop + menuRect.height > window.innerHeight) {
72
- placementY = 'top';
73
- offsetTop = containerRect.top - menuRect.height - 5;
74
- setMaxHeight(containerRect.top - 20);
75
- } else if (placementY === 'top' && offsetTop < 0) {
76
- placementY = 'bottom';
77
- offsetTop = containerRect.bottom + 5;
78
- setMaxHeight(window.innerHeight - containerRect.bottom - 20);
79
- }
80
-
81
- // check for space on the bottom
82
- if (
83
- (placementY === 'auto' && offsetTop + menuRect.height > window.innerHeight) ||
84
- (placement.split('-')[0] === 'top' && placementY !== 'bottom')
85
- ) {
86
- offsetTop = containerRect.top - menuRect.height - 5; //+ document.documentElement.scrollTop
87
- }
88
-
89
- setOffsets({ offsetTop, offsetLeft });
90
- }
91
- }, [menuRef, containerRef, placement]);
92
-
93
- useLayoutEffect(() => {
94
- isOpen
95
- ? document.addEventListener('resize', recalculatePosition)
96
- : document.removeEventListener('resize', recalculatePosition);
97
-
98
- recalculatePosition();
99
- return () => {
100
- window.removeEventListener('resize', recalculatePosition);
101
- };
102
- }, [isOpen, recalculatePosition]);
103
-
104
- const data: IAnyObject = {
105
- top: offsets.offsetTop + 'px',
106
- left: offsets.offsetLeft + 'px',
107
- maxHeight,
108
- };
109
-
110
- if (withMinWidth) {
111
- data.minWidth = minWidth;
112
- }
113
-
114
- return data;
115
- };
1
+ import type { RefObject } from 'react';
2
+ import type { IAnyObject } from '../../../types/IBasic';
3
+ import type { IMenuPlacement } from '../../../types/IMenu';
4
+ import { useCallback, useLayoutEffect, useState } from 'react';
5
+
6
+ interface Offsets {
7
+ offsetTop: number;
8
+ offsetLeft: number;
9
+ }
10
+
11
+ interface Props {
12
+ isOpen: boolean;
13
+ placement: IMenuPlacement;
14
+ containerRef: RefObject<Element | null>;
15
+ menuRef: RefObject<Element | null>;
16
+ withMinWidth?: boolean;
17
+ }
18
+
19
+ export const useMenuPosition = ({
20
+ isOpen,
21
+ placement,
22
+ containerRef,
23
+ menuRef,
24
+ withMinWidth,
25
+ }: Props) => {
26
+ const [offsets, setOffsets] = useState<Offsets>({
27
+ offsetLeft: 0,
28
+ offsetTop: 0,
29
+ });
30
+ const [maxHeight, setMaxHeight] = useState<number>();
31
+ const [minWidth, setMinWidth] = useState(200);
32
+
33
+ const recalculatePosition = useCallback(() => {
34
+ if (containerRef.current && menuRef.current) {
35
+ const containerRect = containerRef.current.getBoundingClientRect();
36
+ const menuRect = menuRef.current.getBoundingClientRect();
37
+ let [placementY, placementX] = placement.split('-');
38
+
39
+ setMinWidth(containerRect.width);
40
+
41
+ let offsetTop = containerRect.bottom + 5;
42
+
43
+ let offsetLeft =
44
+ containerRect.left +
45
+ (placementX === 'right' ? containerRect.width - menuRect.width : 0);
46
+ // + document.documentElement.scrollLeft;
47
+
48
+ // Check for horizontal overflow and adjust placement
49
+ if (placementX === 'right' && offsetLeft < 0) {
50
+ placementX = 'left';
51
+ offsetLeft = containerRect.left;
52
+ } else if (
53
+ placementX === 'left' &&
54
+ offsetLeft + menuRect.width > window.innerWidth
55
+ ) {
56
+ placementX = 'right';
57
+ offsetLeft = containerRect.left + containerRect.width - menuRect.width;
58
+ }
59
+
60
+ if (placementY === 'bottom') {
61
+ setMaxHeight(window.innerHeight - containerRect.bottom - 20);
62
+ }
63
+
64
+ if (placementY === 'top') {
65
+ setMaxHeight(
66
+ window.innerHeight - containerRect.top + 40
67
+ // - 2 * document.documentElement.scrollTop
68
+ );
69
+ }
70
+
71
+ // Check for vertical overflow and adjust placement
72
+ if (placementY === 'bottom' && offsetTop + menuRect.height > window.innerHeight) {
73
+ placementY = 'top';
74
+ offsetTop = containerRect.top - menuRect.height - 5;
75
+ setMaxHeight(containerRect.top - 20);
76
+ } else if (placementY === 'top' && offsetTop < 0) {
77
+ placementY = 'bottom';
78
+ offsetTop = containerRect.bottom + 5;
79
+ setMaxHeight(window.innerHeight - containerRect.bottom - 20);
80
+ }
81
+
82
+ // check for space on the bottom
83
+ if (
84
+ (placementY === 'auto' && offsetTop + menuRect.height > window.innerHeight) ||
85
+ (placement.split('-')[0] === 'top' && placementY !== 'bottom')
86
+ ) {
87
+ offsetTop = containerRect.top - menuRect.height - 5; //+ document.documentElement.scrollTop
88
+ }
89
+
90
+ setOffsets({ offsetTop, offsetLeft });
91
+ }
92
+ }, [menuRef, containerRef, placement]);
93
+
94
+ useLayoutEffect(() => {
95
+ isOpen
96
+ ? document.addEventListener('resize', recalculatePosition)
97
+ : document.removeEventListener('resize', recalculatePosition);
98
+
99
+ recalculatePosition();
100
+ return () => {
101
+ window.removeEventListener('resize', recalculatePosition);
102
+ };
103
+ }, [isOpen, recalculatePosition]);
104
+
105
+ const data: IAnyObject = {
106
+ top: offsets.offsetTop + 'px',
107
+ left: offsets.offsetLeft + 'px',
108
+ maxHeight,
109
+ };
110
+
111
+ if (withMinWidth) {
112
+ data.minWidth = minWidth;
113
+ }
114
+
115
+ return data;
116
+ };