@mhome/ui 0.1.3 → 0.1.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mhome/ui",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "description": "mHome UI Component Library",
6
6
  "main": "dist/index.cjs.js",
@@ -2,7 +2,7 @@ import * as React from "react";
2
2
  import { createPortal } from "react-dom";
3
3
  import { cn, spacingToPx, useIsDarkMode } from "../lib/utils";
4
4
  import { getBackground } from "../common/adaptive-theme-provider";
5
- import { useRecoilValue, backgroundTypeState } from "../global-state";
5
+ import { backgroundTypeState, useRecoilValue } from "../global-state";
6
6
 
7
7
  const Dialog = ({
8
8
  open,
@@ -330,14 +330,23 @@ const DialogContent = React.forwardRef(
330
330
  );
331
331
  DialogContent.displayName = "DialogContent";
332
332
 
333
+ // Context to track if DialogTitle is inside DialogHeader
334
+ const DialogHeaderContext = React.createContext(false);
335
+
333
336
  const DialogHeader = ({ className, ...props }) => (
334
- <div
335
- className={cn(
336
- "flex flex-col space-y-1.5 text-center sm:text-left px-6 pt-6",
337
- className
338
- )}
339
- {...props}
340
- />
337
+ <DialogHeaderContext.Provider value={true}>
338
+ <div
339
+ className={cn(
340
+ "flex flex-col space-y-1.5 px-6 pt-6",
341
+ className
342
+ )}
343
+ style={{
344
+ textAlign: "left", // Ensure left alignment for all children
345
+ ...props.style,
346
+ }}
347
+ {...props}
348
+ />
349
+ </DialogHeaderContext.Provider>
341
350
  );
342
351
  DialogHeader.displayName = "DialogHeader";
343
352
 
@@ -364,6 +373,9 @@ const DialogTitle = React.forwardRef(
364
373
  bgColor.startsWith("radial-gradient") ||
365
374
  bgColor.startsWith("conic-gradient"));
366
375
 
376
+ // Check if DialogTitle is inside DialogHeader
377
+ const isInsideDialogHeader = React.useContext(DialogHeaderContext);
378
+
367
379
  // Convert sx prop to style if provided, handling MUI spacing
368
380
  const sxStyles = React.useMemo(() => {
369
381
  if (!sx) return {};
@@ -455,14 +467,30 @@ const DialogTitle = React.forwardRef(
455
467
  ...restStyle
456
468
  } = style || {};
457
469
 
458
- // Check if padding is explicitly set (if padding is set, individual paddings should be undefined)
459
- // If no padding is set at all, use defaults
470
+ // Check if padding is explicitly set in style, sxStyles, or className
471
+ // Check for padding-related Tailwind classes in className
472
+ const hasPaddingInClassName = className && (
473
+ /\b(p|px|py|pt|pb|pl|pr)-\d+/.test(className) ||
474
+ /\bpadding/.test(className)
475
+ );
476
+
460
477
  const hasAnyPadding =
461
478
  padding !== undefined ||
462
479
  paddingTop !== undefined ||
463
480
  paddingBottom !== undefined ||
464
481
  paddingLeft !== undefined ||
465
- paddingRight !== undefined;
482
+ paddingRight !== undefined ||
483
+ sxStyles.padding !== undefined ||
484
+ sxStyles.paddingTop !== undefined ||
485
+ sxStyles.paddingBottom !== undefined ||
486
+ sxStyles.paddingLeft !== undefined ||
487
+ sxStyles.paddingRight !== undefined ||
488
+ hasPaddingInClassName;
489
+
490
+ // DialogTitle should have default padding when used standalone (not inside DialogHeader)
491
+ // When inside DialogHeader, DialogHeader provides px-6 pt-6, so DialogTitle doesn't need padding
492
+ // When used standalone, DialogTitle should have px-6 pt-6 to match DialogHeader's padding
493
+ const defaultPaddingClass = !isInsideDialogHeader && !hasAnyPadding ? "px-6 pt-6" : "";
466
494
 
467
495
  return (
468
496
  <h2
@@ -471,9 +499,8 @@ const DialogTitle = React.forwardRef(
471
499
  "rounded-t-[inherit]",
472
500
  "text-lg font-semibold leading-none tracking-tight",
473
501
  "text-foreground",
474
- // DialogTitle is typically used inside DialogHeader which has padding
475
- // Only add padding if explicitly needed (when used standalone)
476
- // For normal use, padding is handled by DialogHeader
502
+ "text-left", // Always left align
503
+ defaultPaddingClass,
477
504
  className
478
505
  )}
479
506
  style={{
@@ -485,12 +512,13 @@ const DialogTitle = React.forwardRef(
485
512
  ? { background: backgroundColor }
486
513
  : { backgroundColor }
487
514
  : {}),
488
- // Allow padding override via style prop
515
+ // Allow padding override via style prop (for standalone use)
489
516
  padding: padding,
490
517
  paddingLeft: paddingLeft,
491
518
  paddingRight: paddingRight,
492
519
  paddingTop: paddingTop,
493
520
  paddingBottom: paddingBottom,
521
+ textAlign: "left", // Ensure left alignment
494
522
  ...sxStyles,
495
523
  ...restStyle,
496
524
  }}
@@ -17,6 +17,7 @@ const Drawer = React.forwardRef(
17
17
  variant = "temporary",
18
18
  ModalProps,
19
19
  SlideProps,
20
+ BackdropProps,
20
21
  ...props
21
22
  },
22
23
  ref
@@ -173,9 +174,13 @@ const Drawer = React.forwardRef(
173
174
  opacity: open ? 1 : 0,
174
175
  transition: `opacity ${transitionDuration}ms cubic-bezier(0.4, 0, 0.2, 1)`,
175
176
  pointerEvents: open ? "auto" : "none",
177
+ ...BackdropProps?.style,
176
178
  }}
177
179
  onClick={handleBackdropClick}
178
180
  aria-hidden="true"
181
+ {...Object.fromEntries(
182
+ Object.entries(BackdropProps || {}).filter(([key]) => key !== "style")
183
+ )}
179
184
  />
180
185
  )}
181
186
 
@@ -200,7 +205,7 @@ const Drawer = React.forwardRef(
200
205
  }}
201
206
  role="presentation"
202
207
  {...Object.fromEntries(
203
- Object.entries(props).filter(([key]) => key !== "style")
208
+ Object.entries(props).filter(([key]) => key !== "style" && key !== "BackdropProps")
204
209
  )}
205
210
  >
206
211
  {/* Paper (drawer content) */}
@@ -24,6 +24,56 @@ const Menu = React.forwardRef(
24
24
  const positionType = disablePortal ? positionProp : "fixed";
25
25
  const menuRef = React.useRef(null);
26
26
  const [position, setPosition] = React.useState({ top: 0, left: 0 });
27
+ const previousPositionRef = React.useRef({ top: 0, left: 0 });
28
+
29
+ // Extract borderRadius to a stable value to prevent infinite loops
30
+ // Use a ref to track the last borderRadius value to avoid dependency issues
31
+ // MUST be called before any early returns (React Hooks rule)
32
+ const borderRadiusRef = React.useRef(PaperProps?.style?.borderRadius || "8px");
33
+ if (PaperProps?.style?.borderRadius !== undefined) {
34
+ borderRadiusRef.current = PaperProps.style.borderRadius;
35
+ }
36
+
37
+ // Process children to add borderRadius info to MenuItems
38
+ // We'll do this in the render phase instead of useMemo to avoid issues
39
+ const processChildren = (childrenToProcess) => {
40
+ if (!childrenToProcess) return childrenToProcess;
41
+
42
+ const borderRadius = borderRadiusRef.current;
43
+
44
+ // Get all MenuItem children first
45
+ const childrenArray = React.Children.toArray(childrenToProcess).filter(
46
+ (c) => {
47
+ if (!React.isValidElement(c)) return false;
48
+ const type = c.type;
49
+ return type?.displayName === "MenuItem" ||
50
+ (typeof type === 'function' && type.name === 'MenuItem');
51
+ }
52
+ );
53
+
54
+ return React.Children.map(childrenToProcess, (child) => {
55
+ if (!React.isValidElement(child)) return child;
56
+
57
+ // Check if child is a MenuItem
58
+ const type = child.type;
59
+ const isMenuItem = type?.displayName === "MenuItem" ||
60
+ (typeof type === 'function' && type.name === 'MenuItem');
61
+
62
+ if (!isMenuItem) return child;
63
+
64
+ const childIndex = childrenArray.findIndex((c) => c === child);
65
+ const isFirst = childIndex === 0;
66
+ const isLast = childIndex === childrenArray.length - 1;
67
+
68
+ // Clone child and pass borderRadius info
69
+ return React.cloneElement(child, {
70
+ ...child.props,
71
+ _isFirst: isFirst,
72
+ _isLast: isLast,
73
+ _menuBorderRadius: borderRadius,
74
+ });
75
+ });
76
+ };
27
77
 
28
78
  React.useImperativeHandle(ref, () => menuRef.current);
29
79
 
@@ -255,7 +305,7 @@ const Menu = React.forwardRef(
255
305
  }
256
306
  };
257
307
  }
258
- }, [open, anchorEl, anchorOrigin, positionType, PaperProps?.style?.minWidth]);
308
+ }, [open, anchorEl, anchorOrigin.vertical, anchorOrigin.horizontal, positionType, PaperProps?.style?.minWidth]);
259
309
 
260
310
  // Recalculate position after menu is rendered (to get actual dimensions)
261
311
  React.useLayoutEffect(() => {
@@ -338,7 +388,15 @@ const Menu = React.forwardRef(
338
388
  top = Math.max(padding, viewportHeight - menuHeight - padding);
339
389
  }
340
390
 
341
- setPosition({ top, left });
391
+ // Only update position if it actually changed to prevent infinite loops
392
+ const newPosition = { top, left };
393
+ if (
394
+ Math.abs(previousPositionRef.current.top - newPosition.top) > 0.5 ||
395
+ Math.abs(previousPositionRef.current.left - newPosition.left) > 0.5
396
+ ) {
397
+ previousPositionRef.current = newPosition;
398
+ setPosition(newPosition);
399
+ }
342
400
  } catch (e) {
343
401
  // Silently fail if calculation error
344
402
  }
@@ -354,7 +412,7 @@ const Menu = React.forwardRef(
354
412
  cancelAnimationFrame(rafId);
355
413
  }
356
414
  };
357
- }, [open, anchorEl, anchorOrigin, positionType]); // Re-run when menu opens or anchor changes
415
+ }, [open, anchorEl, anchorOrigin.vertical, anchorOrigin.horizontal, positionType]); // Re-run when menu opens or anchor changes
358
416
 
359
417
  // Handle backdrop click
360
418
  React.useEffect(() => {
@@ -420,9 +478,11 @@ const Menu = React.forwardRef(
420
478
  borderRadius: PaperProps?.style?.borderRadius || "8px",
421
479
  minWidth: PaperProps?.style?.minWidth || 180,
422
480
  maxWidth: "calc(100vw - 32px)",
423
- padding: "4px 0",
481
+ padding: "4px 0", // Vertical padding only, no horizontal padding so MenuItem hover extends to border
424
482
  height: "auto",
425
- overflow: "visible",
483
+ overflow: "hidden", // Ensure children don't overflow rounded corners, but allow MenuItem hover to extend to edges
484
+ // Ensure MenuItem can extend to edges
485
+ boxSizing: "border-box",
426
486
  ...PaperProps?.style,
427
487
  ...props.style,
428
488
  }}
@@ -430,7 +490,7 @@ const Menu = React.forwardRef(
430
490
  Object.entries(props || {}).filter(([key]) => key !== "style")
431
491
  )}
432
492
  >
433
- {children}
493
+ {processChildren(children)}
434
494
  </div>
435
495
  );
436
496
 
@@ -450,7 +510,7 @@ Menu.displayName = "Menu";
450
510
 
451
511
  const MenuItem = React.forwardRef(
452
512
  (
453
- { className, onClick, disabled = false, sx, style, children, ...props },
513
+ { className, onClick, disabled = false, sx, style, children, _isFirst, _isLast, _menuBorderRadius, ...props },
454
514
  ref
455
515
  ) => {
456
516
  const [hovered, setHovered] = React.useState(false);
@@ -475,13 +535,13 @@ const MenuItem = React.forwardRef(
475
535
  paddingRight: `${menuItemPadding.x}px`,
476
536
  paddingTop: `${menuItemPadding.y}px`,
477
537
  paddingBottom: `${menuItemPadding.y}px`,
538
+ margin: 0, // Ensure no margin that could cause overflow
478
539
  cursor: disabled ? "not-allowed" : "pointer",
479
540
  fontSize: menuItemFontSize,
480
541
  color: disabled
481
542
  ? "#666666"
482
543
  : "#000000", // Use fixed color for testing
483
- backgroundColor:
484
- hovered && !disabled ? "hsl(var(--accent))" : "transparent",
544
+ backgroundColor: hovered && !disabled ? "hsl(var(--accent))" : "transparent",
485
545
  opacity: disabled
486
546
  ? 0.5
487
547
  : sxStyles.opacity !== undefined
@@ -493,7 +553,16 @@ const MenuItem = React.forwardRef(
493
553
  textAlign: "left",
494
554
  fontWeight: "normal",
495
555
  width: "100%",
556
+ maxWidth: "100%", // Ensure it doesn't exceed parent width
496
557
  boxSizing: "border-box",
558
+ position: "relative",
559
+ // Ensure MenuItem doesn't overflow Menu boundaries
560
+ overflow: "hidden",
561
+ // Apply border radius to match Menu's borderRadius
562
+ borderTopLeftRadius: _isFirst ? _menuBorderRadius : 0,
563
+ borderTopRightRadius: _isFirst ? _menuBorderRadius : 0,
564
+ borderBottomLeftRadius: _isLast ? _menuBorderRadius : 0,
565
+ borderBottomRightRadius: _isLast ? _menuBorderRadius : 0,
497
566
  ...sxStyles,
498
567
  ...style,
499
568
  };
@@ -533,7 +602,11 @@ const MenuItem = React.forwardRef(
533
602
  "focus:outline-none focus:bg-accent focus:text-accent-foreground",
534
603
  className
535
604
  )}
536
- style={mergedStyle}
605
+ style={{
606
+ ...mergedStyle,
607
+ // Use pseudo-element approach: extend hover background to edges
608
+ position: "relative",
609
+ }}
537
610
  onClick={handleClick}
538
611
  onMouseDown={handleMouseDown}
539
612
  onKeyDown={handleKeyDown}
@@ -46,7 +46,7 @@ const typographyVariants = cva("", {
46
46
 
47
47
  const Typography = React.forwardRef(
48
48
  (
49
- { className, variant = "body1", component, style, color, ...props },
49
+ { className, variant = "body1", component, style, color, noWrap, ...props },
50
50
  ref
51
51
  ) => {
52
52
  // Map MUI color props to shadcn colors
@@ -66,7 +66,10 @@ const Typography = React.forwardRef(
66
66
  typographyVariants({ variant, color: mappedColor }),
67
67
  className
68
68
  )}
69
- style={style}
69
+ style={{
70
+ ...(noWrap && { whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }),
71
+ ...style,
72
+ }}
70
73
  {...props}
71
74
  />
72
75
  );