@grandbazar/ui-baza 1.1.5 → 1.2.1

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/dist/index.cjs CHANGED
@@ -20,6 +20,27 @@ function classNames(...classes) {
20
20
  return classes.filter(Boolean).join(" ");
21
21
  }
22
22
 
23
+ function composeEventHandlers(childHandler, parentHandler) {
24
+ return (event) => {
25
+ childHandler?.(event);
26
+ if (!event.defaultPrevented) {
27
+ parentHandler?.(event);
28
+ }
29
+ };
30
+ }
31
+
32
+ function composeRefs(...refs) {
33
+ return (node) => {
34
+ for (const ref of refs) {
35
+ if (typeof ref === "function") {
36
+ ref(node);
37
+ } else if (ref != null) {
38
+ ref.current = node;
39
+ }
40
+ }
41
+ };
42
+ }
43
+
23
44
  function createRange(start, length) {
24
45
  return Array.from({ length }, (_, index) => start + index);
25
46
  }
@@ -604,26 +625,44 @@ const styles$r = {
604
625
  defaultSeparator: defaultSeparator
605
626
  };
606
627
 
628
+ function isEventHandler(key) {
629
+ return /^on[A-Z]/.test(key);
630
+ }
631
+
607
632
  function Slot({
608
633
  children,
634
+ ref,
609
635
  ...props
610
636
  }) {
611
637
  if (React.isValidElement(children)) {
612
638
  const childProps = children.props;
613
- const parentProps = props;
614
- const mergedProps = {
615
- ...childProps,
616
- ...parentProps,
617
- style: {
618
- ...childProps.style,
619
- ...parentProps.style
620
- },
621
- className: classNames(childProps.className, parentProps.className)
622
- };
639
+ const mergedProps = { ...childProps };
640
+ for (const key of Object.keys(props)) {
641
+ const parentValue = props[key];
642
+ const childValue = childProps[key];
643
+ if (key === "style") {
644
+ mergedProps.style = {
645
+ ...childValue,
646
+ ...parentValue
647
+ };
648
+ } else if (key === "className") {
649
+ mergedProps.className = classNames(childValue, parentValue);
650
+ } else if (isEventHandler(key) && typeof childValue === "function") {
651
+ mergedProps[key] = composeEventHandlers(
652
+ childValue,
653
+ parentValue
654
+ );
655
+ } else {
656
+ mergedProps[key] = parentValue;
657
+ }
658
+ }
659
+ if (ref) {
660
+ mergedProps.ref = composeRefs(childProps.ref, ref);
661
+ }
623
662
  return React.cloneElement(children, mergedProps);
624
663
  }
625
664
  if (React.Children.count(children) > 1) {
626
- React.Children.only(null);
665
+ throw new Error("Slot expects a single child element");
627
666
  }
628
667
  return null;
629
668
  }
@@ -726,11 +765,19 @@ function Button({
726
765
  color = "accent",
727
766
  size = "lg",
728
767
  mode = "primary",
729
- as = "button",
768
+ asChild,
730
769
  ...props
731
770
  }) {
732
- const commonClassNames = classNames(styles$p.button, styles$p[color], styles$p[size], styles$p[mode], className);
733
- return as === "button" ? /* @__PURE__ */ jsxRuntime.jsx("button", { className: commonClassNames, type: "button", ...props, children }) : /* @__PURE__ */ jsxRuntime.jsx("a", { className: commonClassNames, ...props, children });
771
+ const Component = asChild ? Slot : "button";
772
+ return /* @__PURE__ */ jsxRuntime.jsx(
773
+ Component,
774
+ {
775
+ className: classNames(styles$p.button, styles$p[color], styles$p[size], styles$p[mode], className),
776
+ ...!asChild && { type: "button" },
777
+ ...props,
778
+ children
779
+ }
780
+ );
734
781
  }
735
782
  Button.Function = ButtonFunction;
736
783
 
package/dist/index.d.ts CHANGED
@@ -70,18 +70,16 @@ export declare namespace Breadcrumbs {
70
70
  }
71
71
 
72
72
  /**
73
- * Button renders a clickable button or anchor element with various styles.
74
- * Supports polymorphic rendering as either a button or anchor based on the 'as' prop.
73
+ * Button renders a clickable button element with various styles.
74
+ * Supports the asChild pattern for polymorphic rendering via Slot.
75
75
  *
76
76
  * @param color - Color variant of the button (accent, neutral)
77
77
  * @param size - Size of the button (lg, md, sm)
78
78
  * @param mode - Visual mode of the button (primary, secondary, tertiary, line)
79
- * @param as - Element type to render (button, a)
79
+ * @param asChild - When true, merges props onto the child element instead of rendering a button
80
80
  * @param children - Content to display inside the button
81
81
  */
82
- export declare function Button(props: TButtonAsButtonProps): React.JSX.Element;
83
-
84
- export declare function Button(props: TButtonAsAnchorProps): React.JSX.Element;
82
+ export declare function Button({ children, className, color, size, mode, asChild, ...props }: TButtonPropsWithAsChild): JSX.Element;
85
83
 
86
84
  export declare namespace Button {
87
85
  var Function: typeof ButtonFunction;
@@ -476,7 +474,7 @@ declare type TAsChildProps<TDefaultElementProps> = ({
476
474
  asChild?: false;
477
475
  } & TDefaultElementProps) | {
478
476
  asChild: true;
479
- children: default_2.ReactNode;
477
+ children: React.ReactNode;
480
478
  };
481
479
 
482
480
  declare type TBadgeProps = React.HTMLAttributes<HTMLDivElement> & {
@@ -507,20 +505,11 @@ declare type TBreadcrumbsProps = {
507
505
  separator?: default_2.ReactNode;
508
506
  };
509
507
 
510
- export declare type TButtonAsAnchorProps = TButtonCommonProps & React.AnchorHTMLAttributes<HTMLAnchorElement> & {
511
- as: 'a';
512
- ref?: React.Ref<HTMLAnchorElement> | null;
513
- };
514
-
515
- export declare type TButtonAsButtonProps = TButtonCommonProps & React.ButtonHTMLAttributes<HTMLButtonElement> & {
516
- as?: 'button';
517
- ref?: React.Ref<HTMLButtonElement> | null;
518
- };
519
-
520
508
  declare type TButtonCommonProps = {
521
509
  color?: TArrayElement<typeof BUTTON_COLORS>;
522
510
  size?: TArrayElement<typeof BUTTON_SIZES>;
523
511
  mode?: TArrayElement<typeof BUTTON_MODES>;
512
+ className?: string;
524
513
  };
525
514
 
526
515
  declare type TButtonFunctionProps = TAsChildProps<React.ButtonHTMLAttributes<HTMLButtonElement>> & {
@@ -530,7 +519,9 @@ declare type TButtonFunctionProps = TAsChildProps<React.ButtonHTMLAttributes<HTM
530
519
  disabled?: boolean;
531
520
  };
532
521
 
533
- export declare type TButtonProps = TButtonAsButtonProps | TButtonAsAnchorProps;
522
+ export declare type TButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement> & TButtonCommonProps;
523
+
524
+ declare type TButtonPropsWithAsChild = TAsChildProps<React.ButtonHTMLAttributes<HTMLButtonElement>> & TButtonCommonProps;
534
525
 
535
526
  export declare type TCalendarDay = {
536
527
  date: number;
package/dist/index.js CHANGED
@@ -16,6 +16,27 @@ function classNames(...classes) {
16
16
  return classes.filter(Boolean).join(" ");
17
17
  }
18
18
 
19
+ function composeEventHandlers(childHandler, parentHandler) {
20
+ return (event) => {
21
+ childHandler?.(event);
22
+ if (!event.defaultPrevented) {
23
+ parentHandler?.(event);
24
+ }
25
+ };
26
+ }
27
+
28
+ function composeRefs(...refs) {
29
+ return (node) => {
30
+ for (const ref of refs) {
31
+ if (typeof ref === "function") {
32
+ ref(node);
33
+ } else if (ref != null) {
34
+ ref.current = node;
35
+ }
36
+ }
37
+ };
38
+ }
39
+
19
40
  function createRange(start, length) {
20
41
  return Array.from({ length }, (_, index) => start + index);
21
42
  }
@@ -600,26 +621,44 @@ const styles$r = {
600
621
  defaultSeparator: defaultSeparator
601
622
  };
602
623
 
624
+ function isEventHandler(key) {
625
+ return /^on[A-Z]/.test(key);
626
+ }
627
+
603
628
  function Slot({
604
629
  children,
630
+ ref,
605
631
  ...props
606
632
  }) {
607
633
  if (isValidElement(children)) {
608
634
  const childProps = children.props;
609
- const parentProps = props;
610
- const mergedProps = {
611
- ...childProps,
612
- ...parentProps,
613
- style: {
614
- ...childProps.style,
615
- ...parentProps.style
616
- },
617
- className: classNames(childProps.className, parentProps.className)
618
- };
635
+ const mergedProps = { ...childProps };
636
+ for (const key of Object.keys(props)) {
637
+ const parentValue = props[key];
638
+ const childValue = childProps[key];
639
+ if (key === "style") {
640
+ mergedProps.style = {
641
+ ...childValue,
642
+ ...parentValue
643
+ };
644
+ } else if (key === "className") {
645
+ mergedProps.className = classNames(childValue, parentValue);
646
+ } else if (isEventHandler(key) && typeof childValue === "function") {
647
+ mergedProps[key] = composeEventHandlers(
648
+ childValue,
649
+ parentValue
650
+ );
651
+ } else {
652
+ mergedProps[key] = parentValue;
653
+ }
654
+ }
655
+ if (ref) {
656
+ mergedProps.ref = composeRefs(childProps.ref, ref);
657
+ }
619
658
  return cloneElement(children, mergedProps);
620
659
  }
621
660
  if (React.Children.count(children) > 1) {
622
- React.Children.only(null);
661
+ throw new Error("Slot expects a single child element");
623
662
  }
624
663
  return null;
625
664
  }
@@ -722,11 +761,19 @@ function Button({
722
761
  color = "accent",
723
762
  size = "lg",
724
763
  mode = "primary",
725
- as = "button",
764
+ asChild,
726
765
  ...props
727
766
  }) {
728
- const commonClassNames = classNames(styles$p.button, styles$p[color], styles$p[size], styles$p[mode], className);
729
- return as === "button" ? /* @__PURE__ */ jsx("button", { className: commonClassNames, type: "button", ...props, children }) : /* @__PURE__ */ jsx("a", { className: commonClassNames, ...props, children });
767
+ const Component = asChild ? Slot : "button";
768
+ return /* @__PURE__ */ jsx(
769
+ Component,
770
+ {
771
+ className: classNames(styles$p.button, styles$p[color], styles$p[size], styles$p[mode], className),
772
+ ...!asChild && { type: "button" },
773
+ ...props,
774
+ children
775
+ }
776
+ );
730
777
  }
731
778
  Button.Function = ButtonFunction;
732
779
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grandbazar/ui-baza",
3
- "version": "1.1.5",
3
+ "version": "1.2.1",
4
4
  "description": "A lightweight and modular UI kit for building modern, accessible web interfaces. Designed for flexibility, scalability, and developer happiness.",
5
5
  "repository": {
6
6
  "type": "git",