notionsoft-ui 1.0.33 → 1.0.35

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 (35) hide show
  1. package/.storybook/main.ts +19 -13
  2. package/.storybook/preview.css +0 -16
  3. package/package.json +7 -2
  4. package/src/notion-ui/animated-item/animated-item.tsx +1 -1
  5. package/src/notion-ui/animated-item/index.ts +1 -1
  6. package/src/notion-ui/button/Button.stories.tsx +31 -8
  7. package/src/notion-ui/button/button.tsx +10 -2
  8. package/src/notion-ui/button-spinner/ButtonSpinner.stories.tsx +42 -34
  9. package/src/notion-ui/button-spinner/button-spinner.tsx +4 -5
  10. package/src/notion-ui/cache-svg/CachedSvg.stories.tsx +74 -0
  11. package/src/notion-ui/cache-svg/cached-svg.tsx +150 -0
  12. package/src/notion-ui/cache-svg/index.ts +3 -0
  13. package/src/notion-ui/cache-svg/utils.ts +7 -0
  14. package/src/notion-ui/cached-image/cached-image.stories.tsx +109 -0
  15. package/src/notion-ui/cached-image/cached-image.tsx +213 -0
  16. package/src/notion-ui/cached-image/index.ts +3 -0
  17. package/src/notion-ui/cached-image/utils.ts +7 -0
  18. package/src/notion-ui/date-picker/DatePicker.stories.tsx +0 -2
  19. package/src/notion-ui/date-picker/date-picker.tsx +5 -5
  20. package/src/notion-ui/input/Input.stories.tsx +1 -1
  21. package/src/notion-ui/input/input.tsx +5 -4
  22. package/src/notion-ui/multi-date-picker/multi-date-picker.tsx +5 -5
  23. package/src/notion-ui/page-size-select/index.ts +3 -0
  24. package/src/notion-ui/page-size-select/page-size-select.stories.tsx +117 -0
  25. package/src/notion-ui/page-size-select/page-size-select.tsx +283 -0
  26. package/src/notion-ui/password-input/password-input.tsx +3 -3
  27. package/src/notion-ui/phone-input/phone-input.tsx +38 -8
  28. package/src/notion-ui/shimmer/shimmer.tsx +9 -3
  29. package/src/notion-ui/shining-text/shining-text.tsx +2 -6
  30. package/src/notion-ui/sidebar/index.ts +3 -0
  31. package/src/notion-ui/sidebar/sidebar-item.tsx +198 -0
  32. package/src/notion-ui/sidebar/sidebar.stories.tsx +181 -0
  33. package/src/notion-ui/sidebar/sidebar.tsx +284 -0
  34. package/src/notion-ui/textarea/Textarea.stories.tsx +1 -1
  35. package/src/notion-ui/textarea/textarea.tsx +3 -3
@@ -0,0 +1,283 @@
1
+ import { cn } from "../../utils/cn";
2
+ import { Check, ChevronDown } from "lucide-react";
3
+ import React, { useEffect, useLayoutEffect, useRef, useState } from "react";
4
+ import { createPortal } from "react-dom";
5
+
6
+ interface Option {
7
+ value: string;
8
+ label: string;
9
+ }
10
+
11
+ interface SelectProps {
12
+ placeholder: string;
13
+ className?: string;
14
+ paginationKey: string;
15
+ emptyPlaceholder: string;
16
+ rangePlaceholder: string;
17
+ options: Option[];
18
+ onChange?: (value: string) => void;
19
+
20
+ save?: (key: string, data: any) => Promise<void> | void;
21
+ load?: (key: string) => Promise<any> | any;
22
+ }
23
+
24
+ const KEYS = {
25
+ input: 0,
26
+ default: 1,
27
+ };
28
+
29
+ // ---------------- Default Storage ----------------
30
+ const defaultSave = (key: string, data: any, STORAGE_KEY: string) => {
31
+ try {
32
+ localStorage.setItem(STORAGE_KEY + key, JSON.stringify(data));
33
+ } catch {}
34
+ };
35
+
36
+ const defaultLoad = (key: string, STORAGE_KEY: string) => {
37
+ try {
38
+ const raw = localStorage.getItem(STORAGE_KEY + key);
39
+ return raw ? JSON.parse(raw) : null;
40
+ } catch {
41
+ return null;
42
+ }
43
+ };
44
+
45
+ const PageSizeSelect: React.FC<SelectProps> = ({
46
+ placeholder,
47
+ emptyPlaceholder,
48
+ rangePlaceholder,
49
+ options,
50
+ onChange,
51
+ className,
52
+ paginationKey,
53
+ save,
54
+ load,
55
+ }) => {
56
+ const [mounted, setMounted] = useState(false);
57
+ const [dropDirection, setDropDirection] = useState<"up" | "down">("down");
58
+ const [position, setPosition] = useState({
59
+ top: 0,
60
+ left: 0,
61
+ width: 0,
62
+ });
63
+
64
+ const [selectData, setSelectData] = useState({
65
+ isOpen: false,
66
+ showIcon: false,
67
+ select: { key: "", value: "", option: -1 },
68
+ });
69
+
70
+ const selectRef = useRef<HTMLDivElement>(null);
71
+ const dropdownRef = useRef<HTMLDivElement>(null);
72
+ const inputRef = useRef<HTMLInputElement>(null);
73
+
74
+ const saveFn = save
75
+ ? save
76
+ : (key: string, data: any) => defaultSave(key, data, paginationKey);
77
+
78
+ const loadFn = load ? load : (key: string) => defaultLoad(key, paginationKey);
79
+
80
+ // ---------------- Mount ----------------
81
+ useEffect(() => {
82
+ setMounted(true);
83
+ }, []);
84
+
85
+ // ---------------- Load Cache ----------------
86
+ useEffect(() => {
87
+ const loadCache = async () => {
88
+ const cached = await loadFn(paginationKey);
89
+ if (cached) {
90
+ setSelectData((p) => ({ ...p, select: cached }));
91
+ onChange?.(cached.value);
92
+ } else {
93
+ const item = { key: paginationKey, value: "10", option: KEYS.default };
94
+ setSelectData((p) => ({ ...p, select: item }));
95
+ saveFn(paginationKey, item);
96
+ onChange?.("10");
97
+ }
98
+ };
99
+ loadCache();
100
+ }, [paginationKey]);
101
+
102
+ // ---------------- Positioning ----------------
103
+ const updatePosition = () => {
104
+ const trigger = selectRef.current;
105
+ const dropdown = dropdownRef.current;
106
+ if (!trigger || !dropdown) return;
107
+
108
+ const rect = trigger.getBoundingClientRect();
109
+ const viewportHeight = window.innerHeight;
110
+ const viewportWidth = window.innerWidth;
111
+ const gap = 6;
112
+
113
+ const dropdownHeight = Math.min(dropdown.offsetHeight || 0, 260);
114
+ const dropdownWidth = dropdown.offsetWidth || rect.width;
115
+
116
+ const spaceBelow = viewportHeight - rect.bottom;
117
+ const spaceAbove = rect.top;
118
+
119
+ const spaceRight = viewportWidth - rect.left;
120
+ const spaceLeft = rect.right;
121
+
122
+ /* ---------- Vertical (Up / Down) ---------- */
123
+ let top: number;
124
+ if (spaceBelow < dropdownHeight && spaceAbove > spaceBelow) {
125
+ setDropDirection("up");
126
+ top = rect.top + window.scrollY - dropdownHeight - gap;
127
+ } else {
128
+ setDropDirection("down");
129
+ top = rect.bottom + window.scrollY + gap;
130
+ }
131
+
132
+ /* ---------- Horizontal (Left / Right) ---------- */
133
+ let left = rect.left + window.scrollX;
134
+
135
+ // If dropdown overflows right viewport → shift left
136
+ if (spaceRight < dropdownWidth && spaceLeft >= dropdownWidth) {
137
+ left = rect.right + window.scrollX - dropdownWidth;
138
+ }
139
+
140
+ // Clamp to viewport (safety)
141
+ left = Math.max(8, Math.min(left, viewportWidth - dropdownWidth - 8));
142
+
143
+ setPosition({
144
+ top,
145
+ left,
146
+ width: rect.width,
147
+ });
148
+ };
149
+
150
+ useLayoutEffect(() => {
151
+ if (selectData.isOpen) updatePosition();
152
+ }, [selectData.isOpen, options.length]);
153
+
154
+ useEffect(() => {
155
+ if (!selectData.isOpen) return;
156
+ window.addEventListener("resize", updatePosition);
157
+ window.addEventListener("scroll", updatePosition, true);
158
+ return () => {
159
+ window.removeEventListener("resize", updatePosition);
160
+ window.removeEventListener("scroll", updatePosition, true);
161
+ };
162
+ }, [selectData.isOpen]);
163
+
164
+ // ---------------- Outside Click ----------------
165
+ useEffect(() => {
166
+ const handler = (e: MouseEvent) => {
167
+ if (
168
+ !selectRef.current?.contains(e.target as Node) &&
169
+ !dropdownRef.current?.contains(e.target as Node)
170
+ ) {
171
+ setSelectData((p) => ({ ...p, isOpen: false, showIcon: false }));
172
+ }
173
+ };
174
+ document.addEventListener("mousedown", handler);
175
+ return () => document.removeEventListener("mousedown", handler);
176
+ }, []);
177
+
178
+ // ---------------- Select ----------------
179
+ const handleSelect = async (value: string) => {
180
+ const item = { key: paginationKey, value, option: KEYS.default };
181
+ onChange?.(value);
182
+ setSelectData((p) => ({ ...p, isOpen: false, select: item }));
183
+ await saveFn(paginationKey, item);
184
+ };
185
+
186
+ // ---------------- Render ----------------
187
+ return (
188
+ <div ref={selectRef} className={cn("w-full", className)}>
189
+ <button
190
+ onClick={() => setSelectData((p) => ({ ...p, isOpen: !p.isOpen }))}
191
+ className="w-full px-3 py-2 border rounded-md flex items-center justify-between bg-card"
192
+ >
193
+ {selectData.select.value || placeholder}
194
+ <ChevronDown
195
+ className={cn(
196
+ "size-3 transition-transform",
197
+ selectData.isOpen && "rotate-180"
198
+ )}
199
+ />
200
+ </button>
201
+
202
+ {mounted &&
203
+ selectData.isOpen &&
204
+ createPortal(
205
+ <div
206
+ ref={dropdownRef}
207
+ className={cn(
208
+ "absolute min-w-fit z-50 bg-card border border-primary/15 shadow-lg",
209
+ dropDirection === "down" ? "rounded-b-md" : "rounded-t-md"
210
+ )}
211
+ style={{
212
+ top: position.top,
213
+ left: position.left,
214
+ width: position.width,
215
+ }}
216
+ >
217
+ {/* Input */}
218
+ <div className="relative">
219
+ <input
220
+ ref={inputRef}
221
+ type="number"
222
+ placeholder={rangePlaceholder}
223
+ onFocus={() => setSelectData((p) => ({ ...p, showIcon: true }))}
224
+ defaultValue={
225
+ selectData.select.option === KEYS.input
226
+ ? selectData.select.value
227
+ : ""
228
+ }
229
+ className={`bg-card dark:bg-card-secondary text-tertiary rtl:text-[17px] w-full [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none text-center text-sm px-4 py-2 border-b border-primary/15 rounded-t-md focus:outline-none`}
230
+ />
231
+ <Check
232
+ className={cn(
233
+ "size-4 absolute top-2.5 right-2 cursor-pointer",
234
+ !selectData.showIcon && "hidden"
235
+ )}
236
+ onClick={async () => {
237
+ const value = inputRef.current?.value || "10";
238
+ const option = value ? KEYS.input : KEYS.default;
239
+ const item = { key: paginationKey, value, option };
240
+ onChange?.(value);
241
+ await saveFn(paginationKey, item);
242
+ setSelectData((p) => ({
243
+ ...p,
244
+ isOpen: false,
245
+ showIcon: false,
246
+ select: item,
247
+ }));
248
+ }}
249
+ />
250
+ </div>
251
+
252
+ {/* Options */}
253
+ <ul className="max-h-60 overflow-auto">
254
+ {options.length === 0 ? (
255
+ <li className="px-4 py-2 text-center text-sm">
256
+ {emptyPlaceholder}
257
+ </li>
258
+ ) : (
259
+ options.map((o) => (
260
+ <li
261
+ key={o.value}
262
+ onClick={() => handleSelect(o.value)}
263
+ className={cn(
264
+ "px-4 py-2 cursor-pointer flex justify-between hover:bg-primary/10",
265
+ selectData.select.value === o.value && "bg-primary/10"
266
+ )}
267
+ >
268
+ {o.label}
269
+ {selectData.select.value === o.value && (
270
+ <Check className="size-3" />
271
+ )}
272
+ </li>
273
+ ))
274
+ )}
275
+ </ul>
276
+ </div>,
277
+ document.body
278
+ )}
279
+ </div>
280
+ );
281
+ };
282
+
283
+ export default PageSizeSelect;
@@ -93,7 +93,7 @@ const PasswordInput = React.forwardRef<HTMLInputElement, PasswordInputProps>(
93
93
  {/* Password strength text */}
94
94
  <p
95
95
  id="password-strength"
96
- className="mb-2 text-start rtl:text-xl-rtl ltr:text-xl-ltr font-medium text-foreground"
96
+ className="mb-2 text-start rtl:text-lg ltr:text-sm font-medium text-foreground"
97
97
  >
98
98
  {`${getStrengthText(strengthScore)}. ${text.must_contain}`}
99
99
  </p>
@@ -108,9 +108,9 @@ const PasswordInput = React.forwardRef<HTMLInputElement, PasswordInputProps>(
108
108
  <X size={16} className="text-muted-foreground/80" />
109
109
  )}
110
110
  <span
111
- className={`ltr:text-xs rtl:text-lg-rtl ${
111
+ className={`ltr:text-xs rtl:text-[17px] ${
112
112
  req.met
113
- ? "text-emerald-600 ltr:text-xl-ltr"
113
+ ? "text-emerald-600 ltr:text-sm"
114
114
  : "text-muted-foreground"
115
115
  }`}
116
116
  >
@@ -78,6 +78,9 @@ interface PhoneInputProps extends React.InputHTMLAttributes<HTMLInputElement> {
78
78
  rootDivClassName?: string;
79
79
  iconClassName?: string;
80
80
  };
81
+ text: {
82
+ searchInputPlaceholder: string;
83
+ };
81
84
  measurement?: PhoneInputSize;
82
85
  ROW_HEIGHT?: number;
83
86
  VISIBLE_ROWS?: number;
@@ -97,11 +100,14 @@ const PhoneInput: React.FC<PhoneInputProps> = ({
97
100
  ROW_HEIGHT = 32,
98
101
  VISIBLE_ROWS = 10,
99
102
  BUFFER = 5,
103
+ text,
100
104
  ...rest
101
105
  }) => {
102
106
  const { rootDivClassName, iconClassName = "size-4" } = classNames || {};
103
107
  const [open, setOpen] = useState(false);
104
108
  const [highlightedIndex, setHighlightedIndex] = useState<number>(0);
109
+ const { searchInputPlaceholder } = text;
110
+
105
111
  const initialCountry = (() => {
106
112
  if (typeof value === "string" && value.startsWith("+")) {
107
113
  const matched = defaultCountries.find((c) =>
@@ -119,6 +125,20 @@ const PhoneInput: React.FC<PhoneInputProps> = ({
119
125
  const dropdownRef = useRef<HTMLDivElement>(null);
120
126
  const inputRef = useRef<HTMLInputElement>(null);
121
127
  const [position, setPosition] = useState({ top: 0, left: 0, width: 0 });
128
+ const [search, setSearch] = useState("");
129
+ const filteredCountries = useMemo(() => {
130
+ if (!search.trim()) return defaultCountries;
131
+ const s = search.toLowerCase();
132
+ return defaultCountries.filter(
133
+ (c) =>
134
+ c.name.toLowerCase().includes(s) ||
135
+ c.iso2.toLowerCase().includes(s) ||
136
+ ("+" + c.dialCode).includes(s)
137
+ );
138
+ }, [search]);
139
+ useEffect(() => {
140
+ setHighlightedIndex(0);
141
+ }, [search]);
122
142
 
123
143
  const [dropDirection, setDropDirection] = useState<"down" | "up">("down");
124
144
 
@@ -163,14 +183,14 @@ const PhoneInput: React.FC<PhoneInputProps> = ({
163
183
 
164
184
  if (e.key === "ArrowDown") {
165
185
  setHighlightedIndex((prev) =>
166
- Math.min(prev + 1, defaultCountries.length - 1)
186
+ Math.min(prev + 1, filteredCountries.length - 1)
167
187
  );
168
188
  e.preventDefault();
169
189
  } else if (e.key === "ArrowUp") {
170
190
  setHighlightedIndex((prev) => Math.max(prev - 1, 0));
171
191
  e.preventDefault();
172
192
  } else if (e.key === "Enter") {
173
- chooseCountry(defaultCountries[highlightedIndex]);
193
+ chooseCountry(filteredCountries[highlightedIndex]);
174
194
  e.preventDefault();
175
195
  } else if (e.key === "Escape") {
176
196
  setOpen(false);
@@ -247,7 +267,7 @@ const PhoneInput: React.FC<PhoneInputProps> = ({
247
267
  }
248
268
  };
249
269
 
250
- useLayoutEffect(() => updateDropdownPosition(), [open]);
270
+ useLayoutEffect(() => updateDropdownPosition(), [open, search]);
251
271
  useEffect(() => {
252
272
  if (!open) return;
253
273
  window.addEventListener("resize", updateDropdownPosition);
@@ -357,6 +377,7 @@ const PhoneInput: React.FC<PhoneInputProps> = ({
357
377
  +{country.dialCode}
358
378
  </span>
359
379
  </button>
380
+
360
381
  <input
361
382
  ref={inputRef}
362
383
  type="tel"
@@ -377,16 +398,14 @@ const PhoneInput: React.FC<PhoneInputProps> = ({
377
398
  "focus-visible:border-tertiary/60",
378
399
  "[&::-webkit-outer-spin-button]:appearance-none",
379
400
  "[&::-webkit-inner-spin-button]:appearance-none",
380
- "[-moz-appearance:textfield] ",
401
+ "[-moz-appearance:textfield] rtl:text-right",
381
402
  hasError && "border-red-400",
382
403
  className
383
404
  )}
384
405
  {...rest}
385
406
  disabled={readOnly}
386
- dir="ltr"
387
407
  />
388
408
  </div>
389
-
390
409
  {open &&
391
410
  createPortal(
392
411
  <div
@@ -403,10 +422,21 @@ const PhoneInput: React.FC<PhoneInputProps> = ({
403
422
  }}
404
423
  role="listbox"
405
424
  >
425
+ {/* 🔍 Search bar */}
426
+ <div className="p-2 border-b bg-card sticky top-0 z-10">
427
+ <input
428
+ type="text"
429
+ autoFocus
430
+ className="w-full px-2 py-1 text-sm border rounded-sm bg-input/30 focus:outline-none"
431
+ placeholder={searchInputPlaceholder}
432
+ value={search}
433
+ onChange={(e) => setSearch(e.target.value)}
434
+ />
435
+ </div>
406
436
  <VirtualList
407
437
  ROW_HEIGHT={ROW_HEIGHT}
408
438
  BUFFER={BUFFER}
409
- items={defaultCountries}
439
+ items={filteredCountries}
410
440
  height={ROW_HEIGHT * VISIBLE_ROWS}
411
441
  renderRow={(c, i) => (
412
442
  <div
@@ -446,7 +476,7 @@ const PhoneInput: React.FC<PhoneInputProps> = ({
446
476
  }}
447
477
  intersectionArgs={{ once: true, rootMargin: "-5% 0%" }}
448
478
  >
449
- <h1 className="text-red-400 text-start capitalize rtl:text-sm rtl:font-medium ltr:text-sm-ltr">
479
+ <h1 className="text-red-400 text-start capitalize rtl:text-sm rtl:font-medium ltr:text-[11px]">
450
480
  {errorMessage}
451
481
  </h1>
452
482
  </AnimatedItem>
@@ -1,8 +1,14 @@
1
1
  import { cn } from "../../utils/cn";
2
2
 
3
- export interface ShimmerProps extends React.HTMLAttributes<HTMLDivElement> {}
3
+ export interface ShimmerProps extends React.HTMLAttributes<HTMLDivElement> {
4
+ stop?: boolean;
5
+ }
4
6
 
5
- export default function Shimmer({ className, children }: ShimmerProps) {
7
+ export default function Shimmer({
8
+ stop = false,
9
+ className,
10
+ children,
11
+ }: ShimmerProps) {
6
12
  return (
7
13
  <div
8
14
  className={cn("relative w-full overflow-hidden *:rounded-sm", className)}
@@ -30,7 +36,7 @@ export default function Shimmer({ className, children }: ShimmerProps) {
30
36
  var(--from-shimmer) 25%
31
37
  )`,
32
38
  backgroundSize: "1200px 100%",
33
- animation: "shimmer 2.2s linear infinite",
39
+ animation: !stop ? "shimmer 2.2s linear infinite" : "",
34
40
  }}
35
41
  />
36
42
 
@@ -7,11 +7,7 @@ interface ShiningTextProps extends React.HTMLAttributes<HTMLSpanElement> {
7
7
  text: string;
8
8
  }
9
9
 
10
- export default function ShiningText({
11
- text,
12
- className,
13
- ...props
14
- }: ShiningTextProps) {
10
+ export function ShiningText({ text, className, ...props }: ShiningTextProps) {
15
11
  // Animate strictly left → right
16
12
  const styles = useSpring({
17
13
  from: { backgroundPosition: "-100% 0%" }, // start offscreen left
@@ -27,7 +23,7 @@ export default function ShiningText({
27
23
  ...styles,
28
24
  }}
29
25
  className={cn(
30
- "bg-gradient-to-r from-gray-300 via-white to-gray-300", // left→right gradient
26
+ "bg-gradient-to-r text-md font-medium from-black via-gray-100 to-black", // left→right gradient
31
27
  "bg-clip-text text-transparent",
32
28
  className
33
29
  )}
@@ -0,0 +1,3 @@
1
+ import Sidebar from "./sidebar";
2
+
3
+ export default Sidebar;
@@ -0,0 +1,198 @@
1
+ import { useMemo, useCallback, memo, useEffect, useState } from "react";
2
+ import { ChevronRight } from "lucide-react";
3
+ import { useLocation } from "react-router";
4
+ import CachedSvg, {
5
+ CachedSvgProps,
6
+ } from "@/components/notion-ui/cache-svg/cached-svg";
7
+ import AnimatedItem from "@/components/notion-ui/animated-item";
8
+ import { cn } from "@/utils/cn";
9
+
10
+ export interface SubPermission {
11
+ id: number;
12
+ name: string;
13
+ is_category: boolean;
14
+ }
15
+
16
+ export type Permission = {
17
+ id: number;
18
+ visible: boolean;
19
+ permission: string;
20
+ icon: string;
21
+ sub: Map<number, SubPermission>;
22
+ };
23
+
24
+ export interface SidebarItemProps {
25
+ path: string;
26
+ isActive: boolean;
27
+ permission: Permission;
28
+ icon: CachedSvgProps;
29
+ navigateTo: (path: string) => void;
30
+ translate?: (key: string) => string;
31
+ classNames?: {};
32
+ }
33
+
34
+ export const SidebarItem = memo(function SidebarItem({
35
+ isActive,
36
+ navigateTo,
37
+ permission,
38
+ path,
39
+ icon,
40
+ translate,
41
+ }: SidebarItemProps) {
42
+ const location = useLocation();
43
+ const [showDropdown, setShowDropdown] = useState(false);
44
+
45
+ // Calculate categories and selectedSubId
46
+ const { categories, selectedSubId } = useMemo(() => {
47
+ const subs = Array.from(permission.sub.values()).filter(
48
+ (sub) => sub.is_category
49
+ );
50
+ const selectedId = Number(location.pathname.split("/").pop());
51
+ return { categories: subs, selectedSubId: selectedId };
52
+ }, [permission.sub, location.pathname]);
53
+
54
+ // Auto-open dropdown if current URL matches any category
55
+ useEffect(() => {
56
+ const matched = categories.find((sub) =>
57
+ location.pathname.includes(`${path}/${sub.id}`)
58
+ );
59
+ setShowDropdown(matched ? true : false);
60
+ }, [location.pathname, categories, path]);
61
+ useEffect(() => {
62
+ const handleCloseDropdowns = (e: Event) => {
63
+ const customEvent = e as CustomEvent;
64
+ if (customEvent.detail?.forceClose) {
65
+ setShowDropdown(false);
66
+ }
67
+ };
68
+
69
+ // Listen for close events
70
+ window.addEventListener("sidebar-close-dropdowns", handleCloseDropdowns);
71
+
72
+ return () => {
73
+ window.removeEventListener(
74
+ "sidebar-close-dropdowns",
75
+ handleCloseDropdowns
76
+ );
77
+ };
78
+ }, []);
79
+ const handleClick = useCallback(
80
+ (e: React.MouseEvent) => {
81
+ if (categories.length === 0) {
82
+ navigateTo(path);
83
+ } else {
84
+ setShowDropdown((prev) => !prev);
85
+ // Dispatch custom event to parent (Sidebar)
86
+ const expandEvent = new CustomEvent("sidebar-item-expand", {
87
+ bubbles: true, // This makes the event bubble up through DOM
88
+ composed: true, // This allows it to cross shadow DOM boundaries if any
89
+ detail: {
90
+ hasChildren: true,
91
+ },
92
+ });
93
+
94
+ // Dispatch from the clicked element
95
+ e.currentTarget.dispatchEvent(expandEvent);
96
+ }
97
+ },
98
+ [categories.length, navigateTo, path]
99
+ );
100
+
101
+ const handleCategoryClick = useCallback(
102
+ (cat: SubPermission) => {
103
+ navigateTo(`${path}/${cat.id}`);
104
+ },
105
+ [navigateTo, path]
106
+ );
107
+
108
+ const spring = useMemo(
109
+ () => ({
110
+ springProps: {
111
+ from: { opacity: 0, transform: "translateY(-8px)" },
112
+ config: { mass: 1, tension: 210, friction: 20 },
113
+ to: { opacity: 1, transform: "translateY(0px)" },
114
+ },
115
+ intersectionArgs: { rootMargin: "-10% 0%", once: true },
116
+ }),
117
+ []
118
+ );
119
+
120
+ const dropdownContent = useMemo(() => {
121
+ if (!showDropdown || categories.length === 0) return null;
122
+ return (
123
+ <div className="relative ltr:ml-5 rtl:mr-5 mt-1 mb-4 space-y-1 ltr:pl-2 rtl:pr-2 before:absolute before:top-3 before:bottom-0 rtl:before:right-1 ltr:before:left-1 before:w-px rounded-full before:bg-primary/30">
124
+ {categories.map((cat, index) => {
125
+ const selected = selectedSubId === cat.id;
126
+ return (
127
+ <AnimatedItem
128
+ key={cat.id}
129
+ springProps={{
130
+ ...spring.springProps,
131
+ delay: index * 100,
132
+ to: {
133
+ ...spring.springProps.to,
134
+ delay: index * 100,
135
+ },
136
+ }}
137
+ intersectionArgs={spring.intersectionArgs}
138
+ >
139
+ <div className="relative flex items-center before:absolute ltr:before:left-1 rtl:before:right-1 before:top-1/2 before:w-3 before:h-px before:bg-primary/40">
140
+ <button
141
+ onClick={handleCategoryClick.bind(null, cat)}
142
+ className={`cursor-pointer text-primary/80 ltr:ml-5 rtl:mr-5 rtl:text-sm rtl:font-bold ltr:text-xs flex items-center gap-x-2 py-1 px-2 w-[85%] rounded-sm transition-colors ${
143
+ selected
144
+ ? "font-semibold bg-tertiary/10 text-primary"
145
+ : "hover:opacity-75"
146
+ }`}
147
+ >
148
+ {translate ? translate(cat.name) : cat.name}
149
+ </button>
150
+ </div>
151
+ </AnimatedItem>
152
+ );
153
+ })}
154
+ </div>
155
+ );
156
+ }, [
157
+ showDropdown,
158
+ categories,
159
+ selectedSubId,
160
+ spring,
161
+ handleCategoryClick,
162
+ translate,
163
+ ]);
164
+ return (
165
+ <>
166
+ <div
167
+ onClick={handleClick}
168
+ className={cn(
169
+ `grid grid-cols-[1fr_auto] ltr:py-2 rtl:p-1 ltr:pl-2.5 rtl:pr-2 ltr:mx-1 rtl:mx-1.5 text-primary items-center rtl:text-lg ltr:text-xs cursor-pointer rounded-md ${
170
+ isActive
171
+ ? `bg-tertiary/90 text-card font-semibold`
172
+ : "hover:opacity-75"
173
+ }`
174
+ )}
175
+ key={permission.permission}
176
+ >
177
+ <div className="flex items-center gap-x-4 w-full">
178
+ <CachedSvg {...icon} className={cn("rtl", icon.className)} />
179
+ <h1 className="truncate">
180
+ {translate
181
+ ? translate(permission.permission)
182
+ : permission.permission}
183
+ </h1>
184
+ </div>
185
+
186
+ {categories.length > 0 && (
187
+ <ChevronRight
188
+ className={`size-3.5 min-h-3.5 min-w-3.5 text-primary ltr:mr-2 transition-transform duration-300 ease-in-out ${
189
+ showDropdown ? "rotate-90" : "rtl:rotate-180"
190
+ }`}
191
+ />
192
+ )}
193
+ </div>
194
+
195
+ {dropdownContent}
196
+ </>
197
+ );
198
+ }); // Pass custom comparison function