@zayne-labs/toolkit-react 0.8.62 → 0.9.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.
@@ -1,5 +1,6 @@
1
+ import * as react from 'react';
1
2
  import { RefCallback } from 'react';
2
- import { AnyFunction, UnionDiscriminator, AnyObject, Prettify } from '@zayne-labs/toolkit-type-helpers';
3
+ import { EmptyObject, AnyFunction, UnionToIntersection as UnionToIntersection$1, UnionDiscriminator, AnyObject, Prettify } from '@zayne-labs/toolkit-type-helpers';
3
4
 
4
5
  type PossibleRef<TRef> = React.Ref<TRef> | undefined;
5
6
  /**
@@ -13,26 +14,152 @@ declare const setRef: <TRef>(ref: PossibleRef<TRef>, node: TRef) => ReturnType<R
13
14
  */
14
15
  declare const composeRefs: <TRef>(refs: Array<PossibleRef<TRef>>) => RefCallback<TRef>;
15
16
 
16
- type FunctionalComponent<TProps> = (props: TProps) => ReturnType<React.FunctionComponent<TProps>> | AnyFunction<React.ReactNode>;
17
- declare const isSlotElement: <TProps>(child: React.ReactNode, SlotWrapper: FunctionalComponent<TProps>) => boolean;
17
+ type FunctionalComponent<TProps = EmptyObject> = (props: TProps) => ReturnType<React.FunctionComponent<TProps>> | AnyFunction<React.ReactNode>;
18
+ /**
19
+ * @description Checks if a React node is a slot component using multiple matching strategies:
20
+ * 1. Matches by slot symbol property
21
+ * 2. Matches by direct component reference
22
+ * 3. Matches by stringified component
23
+ * 4. Matches by component name
24
+ */
25
+ declare const isSlot: <TProps>(child: React.ReactNode, SlotComponent: FunctionalComponent<TProps>) => boolean;
18
26
  type SlotOptions = {
19
27
  errorMessage?: string;
20
28
  throwOnMultipleSlotMatch?: boolean;
21
29
  };
22
30
  /**
23
- * @description Retrieves a single slot element from a collection of React children that matches the provided SlotWrapper component.
31
+ * @description Retrieves a single slot element from a collection of React children that matches the provided SlotComponent component.
24
32
  *
25
- * @throws {AssertionError} When throwOnMultipleSlotMatch is true and multiple slots are found
33
+ * @throws { AssertionError } when throwOnMultipleSlotMatch is true and multiple slots are found
34
+ */
35
+ declare const getSingleSlot: (children: React.ReactNode, SlotComponent: FunctionalComponent, options?: SlotOptions) => react.ReactNode;
36
+ declare const getMultipleSlots: <const TSlotComponents extends FunctionalComponent[]>(children: React.ReactNode, SlotComponents: TSlotComponents, options?: SlotOptions) => {
37
+ otherChildren: react.ReactNode[];
38
+ slots: { [Key in keyof TSlotComponents]: ReturnType<TSlotComponents[Key]>; };
39
+ };
40
+ /**
41
+ * @description Returns all children that are not slot elements (i.e., don't match any of the provided slot components)
26
42
  */
27
- declare const getSlotElement: <TProps = Record<string, unknown>>(children: React.ReactNode, SlotWrapper: FunctionalComponent<TProps>, options?: SlotOptions) => React.ReactElement<TProps> | undefined;
28
- declare const getOtherChildren: <TProps = Record<string, unknown>, TChildren extends React.ReactNode = React.ReactNode>(children: TChildren, SlotWrapperOrWrappers: Array<FunctionalComponent<TProps>> | FunctionalComponent<TProps>) => TChildren extends unknown[] ? TChildren : TChildren[];
43
+ declare const getOtherChildren: (children: React.ReactNode, SlotComponents: FunctionalComponent[]) => react.ReactNode[];
29
44
 
30
- type UnknownProps$1 = Record<never, never>;
31
- declare const mergeTwoProps: <TProps extends UnknownProps$1>(slotProps: TProps | undefined, childProps: TProps | undefined) => TProps;
45
+ /**
46
+ * Possible children types that can be passed to a slot
47
+ */
48
+ type PossibleSlotChildrenType = AnyFunction<React.ReactNode | React.ReactNode[]> | React.ReactNode | React.ReactNode[];
49
+ /**
50
+ * Maps slot names to their corresponding children types
51
+ */
52
+ type GetSlotMapResult<TSlotComponentProps extends GetSlotComponentProps> = UnionToIntersection$1<{
53
+ [TName in keyof TSlotComponentProps as TSlotComponentProps["name"]]: TSlotComponentProps["children"];
54
+ }>;
55
+ /**
56
+ * Symbol used to identify SlotComponent instances
57
+ */
58
+ declare const slotComponentSymbol: unique symbol;
59
+ /**
60
+ * @description Creates a map of named slots from React children. Returns an object mapping slot names to their children,
61
+ * with a default slot for unmatched children.
62
+ *
63
+ * @example
64
+ * ```tsx
65
+ * import { type GetSlotComponentProps, SlotComponent } from "@zayne-labs/toolkit-react/utils"
66
+ *
67
+ * type SlotProps = GetSlotComponentProps<"header" | "footer">;
68
+ *
69
+ * function Parent({ children }: { children: React.ReactNode }) {
70
+ * const slots = getSlotMap<SlotProps>(children);
71
+ *
72
+ * return (
73
+ * <div>
74
+ * <header>{slots.header}</header>
75
+ * <main>{slots.default}</main>
76
+ * <footer>{slots.footer}</footer>
77
+ * </div>
78
+ * );
79
+ * }
80
+ * ```
81
+ *
82
+ * Usage:
83
+ * ```tsx
84
+ * <Parent>
85
+ * <SlotComponent name="header">Header Content</SlotComponent>
86
+ * <div>Random stuff</div>
87
+ * <SlotComponent name="footer">Footer Content</SlotComponent>
88
+ * </Parent>
89
+ * ```
90
+ */
91
+ declare const getSlotMap: <TSlotComponentProps extends GetSlotComponentProps>(children: React.ReactNode) => GetSlotMapResult<TSlotComponentProps> & {
92
+ default: React.ReactNode[];
93
+ };
94
+ /**
95
+ * @description Produce props for the SlotComponent
96
+ */
97
+ type GetSlotComponentProps<TName extends string = string, TChildren extends PossibleSlotChildrenType = PossibleSlotChildrenType> = {
98
+ /** Content to render in the slot */
99
+ children: TChildren;
100
+ /** Name of the slot where content should be rendered */
101
+ name: TName;
102
+ };
103
+ /**
104
+ * @description Function used to create a slot component that defines named slots in a parent component. This component created doesn't render anything,
105
+ * it's used purely for slot definition.
106
+ *
107
+ * @example
108
+ * ```tsx
109
+ * import { type GetSlotComponentProps, createSlotComponent, SlotComponent } from "@zayne-labs/toolkit-react/utils"
110
+ *
111
+ * type SlotProps = GetSlotComponentProps<"header" | "footer">;
112
+ *
113
+ * function Parent({ children }: { children: React.ReactNode }) {
114
+ * const slots = getSlotMap<SlotProps>(children);
115
+ *
116
+ * return (
117
+ * <div>
118
+ * <header>{slots.header}</header>
119
+ * <main>{slots.default}</main>
120
+ * <footer>{slots.footer}</footer>
121
+ * </div>
122
+ * );
123
+ * }
124
+ *
125
+ * Parent.Slot = createSlotComponent<SlotProps>();
126
+ * // OR
127
+ * Parent.Slot = SlotComponent<SlotProps>
128
+ * ```
129
+ *
130
+ *
131
+ * @example
132
+ * Usage:
133
+ ```tsx
134
+ * function App() {
135
+ * return (
136
+ * <Parent>
137
+ * <Parent.Slot name="header">Header Content</Parent.Slot>
138
+ * <div>Default Content</div>
139
+ * <Parent.Slot name="footer">Footer Content</Parent.Slot>
140
+ * </Parent>
141
+ * );
142
+ * }
143
+ * ```
144
+ */
145
+ declare const createSlotComponent: <TBaseSlotComponentProps extends GetSlotComponentProps>() => {
146
+ <TSlotComponentProps extends TBaseSlotComponentProps>(props: TSlotComponentProps): React.ReactNode;
147
+ id: typeof slotComponentSymbol;
148
+ };
149
+ /**
150
+ * @description Slot component created by createSlotComponent
151
+ */
152
+ declare const SlotComponent: {
153
+ <TSlotComponentProps extends GetSlotComponentProps<string, PossibleSlotChildrenType>>(props: TSlotComponentProps): React.ReactNode;
154
+ id: typeof slotComponentSymbol;
155
+ };
32
156
 
33
157
  type UnionToIntersection<TUnion> = (TUnion extends unknown ? (param: TUnion) => void : "") extends (param: infer TParam) => void ? TParam : "";
158
+ type UnknownProps$1 = Record<never, never>;
159
+ declare const mergeProps: <TProps extends UnknownProps$1>(...parameters: Array<TProps | undefined>) => UnionToIntersection<TProps>;
160
+
34
161
  type UnknownProps = Record<never, never>;
35
- declare const mergeProps: <TProps extends UnknownProps>(...parameters: Array<TProps | undefined>) => UnionToIntersection<TProps>;
162
+ declare const mergeTwoProps: <TProps extends UnknownProps>(slotProps: TProps | undefined, childProps: TProps | undefined) => TProps;
36
163
 
37
164
  type ForwardedRefType<TComponent extends HTMLElement | React.ElementType> = TComponent extends React.ElementType ? React.ForwardedRef<React.Ref<TComponent>> : React.ForwardedRef<TComponent>;
38
165
  type InferProps<TComponent extends HTMLElement | React.ElementType> = TComponent extends React.ElementType ? React.ComponentPropsWithRef<TComponent> : React.HTMLAttributes<TComponent>;
@@ -61,4 +188,4 @@ type InferRestOfProps<TElement extends React.ElementType, TProps> = Omit<React.C
61
188
  type MergedPropsWithAs<TElement extends React.ElementType, TProps> = Prettify<Omit<AsProp<TElement>, keyof TProps> & TProps>;
62
189
  type PolymorphicProps<TElement extends React.ElementType, TProps extends AnyObject = NonNullable<unknown>> = MergedPropsWithAs<TElement, TProps> & InferRestOfProps<TElement, TProps>;
63
190
 
64
- export { type AsProp, type DiscriminatedRenderProps, type ForwardedRefType, type InferProps, type MyCustomCss, type PolymorphicProps, type StateSetter, composeRefs, getOtherChildren, getSlotElement, isSlotElement, mergeProps, mergeTwoProps, setRef };
191
+ export { type AsProp, type DiscriminatedRenderProps, type ForwardedRefType, type GetSlotComponentProps, type InferProps, type MyCustomCss, type PolymorphicProps, SlotComponent, type StateSetter, composeRefs, createSlotComponent, getMultipleSlots, getOtherChildren, getSingleSlot, getSlotMap, isSlot, mergeProps, mergeTwoProps, setRef, slotComponentSymbol };
@@ -1,4 +1,4 @@
1
- import { isFunction, AssertionError, isArray, isPlainObject } from '@zayne-labs/toolkit-type-helpers';
1
+ import { isFunction, isPlainObject, AssertionError } from '@zayne-labs/toolkit-type-helpers';
2
2
  import { toArray, mergeClassNames, mergeFunctions } from '@zayne-labs/toolkit-core';
3
3
  import { isValidElement } from 'react';
4
4
 
@@ -22,62 +22,82 @@ var composeRefs = (refs) => {
22
22
  };
23
23
  return refCallBack;
24
24
  };
25
- var isSlotElement = (child, SlotWrapper) => {
25
+ var isSlot = (child, SlotComponent2) => {
26
26
  if (!isValidElement(child)) {
27
27
  return false;
28
28
  }
29
- if (child.type.slot === SlotWrapper.slot) {
29
+ if (child.type.slot === SlotComponent2.slot) {
30
30
  return true;
31
31
  }
32
- if (child.type.name === SlotWrapper.name) {
32
+ if (child.type === SlotComponent2) {
33
33
  return true;
34
34
  }
35
- if (child.type === SlotWrapper) {
35
+ if (child.type.toString() === SlotComponent2.toString()) {
36
36
  return true;
37
37
  }
38
- return child.type.toString() === SlotWrapper.toString();
38
+ return child.type.name === SlotComponent2.name;
39
39
  };
40
- var getSlotElement = (children, SlotWrapper, options = {}) => {
40
+ var calculateSlotOccurrences = (childrenArray, SlotComponent2) => {
41
+ let count = 0;
42
+ for (const child of childrenArray) {
43
+ if (!isSlot(child, SlotComponent2)) continue;
44
+ count += 1;
45
+ }
46
+ return count;
47
+ };
48
+ var getSingleSlot = (children, SlotComponent2, options = {}) => {
41
49
  const {
42
50
  errorMessage = "Only one instance of the SlotComponent is allowed",
43
51
  throwOnMultipleSlotMatch = false
44
52
  } = options;
45
53
  const childrenArray = toArray(children);
46
- const Slot = childrenArray.filter((child) => isSlotElement(child, SlotWrapper));
47
- if (throwOnMultipleSlotMatch && Slot.length > 1) {
54
+ const shouldThrow = throwOnMultipleSlotMatch && calculateSlotOccurrences(childrenArray, SlotComponent2) > 1;
55
+ if (shouldThrow) {
48
56
  throw new AssertionError(errorMessage);
49
57
  }
50
- return Slot[0];
58
+ const slotElement = childrenArray.find((child) => isSlot(child, SlotComponent2));
59
+ return slotElement;
51
60
  };
52
- var isSlotElementMultiple = (child, SlotWrapperArray) => SlotWrapperArray.some((slotWrapper) => isSlotElement(child, slotWrapper));
53
- var getOtherChildren = (children, SlotWrapperOrWrappers) => {
61
+ var getMultipleSlots = (children, SlotComponents, options) => {
62
+ const slots = SlotComponents.map(
63
+ (SlotComponent2) => getSingleSlot(children, SlotComponent2, options)
64
+ );
65
+ const otherChildren = getOtherChildren(children, SlotComponents);
66
+ return { otherChildren, slots };
67
+ };
68
+ var getOtherChildren = (children, SlotComponents) => {
54
69
  const childrenArray = toArray(children);
55
- const otherChildren = isArray(SlotWrapperOrWrappers) ? childrenArray.filter((child) => !isSlotElementMultiple(child, SlotWrapperOrWrappers)) : childrenArray.filter((child) => !isSlotElement(child, SlotWrapperOrWrappers));
70
+ const otherChildren = childrenArray.filter(
71
+ (child) => !SlotComponents.some((SlotComponent2) => isSlot(child, SlotComponent2))
72
+ );
56
73
  return otherChildren;
57
74
  };
58
- var mergeTwoProps = (slotProps, childProps) => {
59
- if (!slotProps || !childProps) {
60
- return childProps ?? slotProps ?? {};
61
- }
62
- const overrideProps = { ...childProps };
63
- for (const propName of Object.keys(slotProps)) {
64
- const slotPropValue = slotProps[propName];
65
- const childPropValue = childProps[propName];
66
- if (propName === "style" && isPlainObject(slotPropValue) && isPlainObject(childPropValue)) {
67
- overrideProps[propName] = { ...slotPropValue, ...childPropValue };
68
- continue;
69
- }
70
- if (propName === "className" || propName === "class") {
71
- overrideProps[propName] = mergeClassNames(slotPropValue, childPropValue);
75
+ var slotComponentSymbol = Symbol("SlotComponent");
76
+ var getSlotMap = (children) => {
77
+ const childrenArray = toArray(children);
78
+ const slots = {
79
+ default: []
80
+ };
81
+ for (const child of childrenArray) {
82
+ const isSlotElementWithName = isValidElement(child) && child.type.id === slotComponentSymbol && child.props.name;
83
+ const isRegularElementWithSlotName = isValidElement(child) && child.props["data-slot-name"];
84
+ if (!isSlotElementWithName && !isRegularElementWithSlotName) {
85
+ slots.default.push(child);
72
86
  continue;
73
87
  }
74
- const isHandler = propName.startsWith("on");
75
- if (isHandler && isFunction(slotPropValue) && isFunction(childPropValue)) {
76
- overrideProps[propName] = mergeFunctions(childPropValue, slotPropValue);
77
- }
88
+ const slotName = isSlotElementWithName ? child.props.name : child.props["data-slot-name"];
89
+ slots[slotName] = child.props.children;
78
90
  }
79
- return { ...slotProps, ...overrideProps };
91
+ return slots;
80
92
  };
93
+ var createSlotComponent = () => {
94
+ function SlotComponent2(props) {
95
+ return null;
96
+ }
97
+ SlotComponent2.id = slotComponentSymbol;
98
+ return SlotComponent2;
99
+ };
100
+ var SlotComponent = createSlotComponent();
81
101
  var handleMergePropsIntoResult = (mergedResult, propsObject) => {
82
102
  for (const propName of Object.keys(mergedResult)) {
83
103
  const mergedResultValue = mergedResult[propName];
@@ -114,7 +134,30 @@ var mergeProps = (...parameters) => {
114
134
  }
115
135
  return mergedResult;
116
136
  };
137
+ var mergeTwoProps = (slotProps, childProps) => {
138
+ if (!slotProps || !childProps) {
139
+ return childProps ?? slotProps ?? {};
140
+ }
141
+ const overrideProps = { ...childProps };
142
+ for (const propName of Object.keys(slotProps)) {
143
+ const slotPropValue = slotProps[propName];
144
+ const childPropValue = childProps[propName];
145
+ if (propName === "style" && isPlainObject(slotPropValue) && isPlainObject(childPropValue)) {
146
+ overrideProps[propName] = { ...slotPropValue, ...childPropValue };
147
+ continue;
148
+ }
149
+ if (propName === "className" || propName === "class") {
150
+ overrideProps[propName] = mergeClassNames(slotPropValue, childPropValue);
151
+ continue;
152
+ }
153
+ const isHandler = propName.startsWith("on");
154
+ if (isHandler && isFunction(slotPropValue) && isFunction(childPropValue)) {
155
+ overrideProps[propName] = mergeFunctions(childPropValue, slotPropValue);
156
+ }
157
+ }
158
+ return { ...slotProps, ...overrideProps };
159
+ };
117
160
 
118
- export { composeRefs, getOtherChildren, getSlotElement, isSlotElement, mergeProps, mergeTwoProps, setRef };
161
+ export { SlotComponent, composeRefs, createSlotComponent, getMultipleSlots, getOtherChildren, getSingleSlot, getSlotMap, isSlot, mergeProps, mergeTwoProps, setRef, slotComponentSymbol };
119
162
  //# sourceMappingURL=index.js.map
120
163
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/utils/composeRefs.ts","../../../src/utils/getSlotElement.ts","../../../src/utils/mergeTwoProps.ts","../../../src/utils/mergeProps.ts"],"names":["isFunction","mergeClassNames","isPlainObject","mergeFunctions"],"mappings":";;;;;AAUa,IAAA,MAAA,GAAS,CAAO,GAAA,EAAwB,IAA8C,KAAA;AAClG,EAAA,IAAI,CAAC,GAAK,EAAA;AAEV,EAAI,IAAA,UAAA,CAAW,GAAG,CAAG,EAAA;AACpB,IAAA,OAAO,IAAI,IAAI,CAAA;AAAA;AAIhB,EAAA,GAAA,CAAI,OAAU,GAAA,IAAA;AACf;AAKa,IAAA,WAAA,GAAc,CAAO,IAAsD,KAAA;AACvF,EAAM,MAAA,WAAA,GAAiC,CAAC,IAAS,KAAA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A,UAAAA,CAAW,gBAAgB,CAAG,EAAA;AAC/E,MAAA,YAAA,CAAa,QAAQ,CAAA,GAAIG,cAAe,CAAA,gBAAA,EAAkB,iBAAiB,CAAA;AAC3E,MAAA;AAAA;AAGD,IAAA,YAAA,CAAa,QAAQ,CAAA,GAAI,gBAAqB,KAAA,MAAA,GAAY,gBAAmB,GAAA,iBAAA;AAAA;AAE/E,CAAA;AAEA,IAAM,uBAAA,GAA0B,CAC/B,YAAA,EACA,WACI,KAAA;AACJ,EAAA,KAAA,MAAW,QAAY,IAAA,MAAA,CAAO,IAAK,CAAA,WAAW,CAAG,EAAA;AAChD,IAAI,IAAA,YAAA,CAAa,QAAQ,CAAA,KAAM,MAAW,EAAA;AACzC,MAAa,YAAA,CAAA,QAAQ,CAAI,GAAA,WAAA,CAAY,QAAQ,CAAA;AAAA;AAC9C;AAEF,CAAA;AAKM,IAAA,UAAA,GAAa,IACf,UAC8B,KAAA;AACjC,EAAA,MAAM,eAAwC,EAAC;AAE/C,EAAA,KAAA,MAAW,eAAe,UAAY,EAAA;AACrC,IAAA,IAAI,CAAC,WAAa,EAAA;AAElB,IAAA,0BAAA,CAA2B,cAAc,WAAW,CAAA;AAGpD,IAAA,uBAAA,CAAwB,cAAc,WAAW,CAAA;AAAA;AAGlD,EAAO,OAAA,YAAA;AACR","file":"index.js","sourcesContent":["import { isFunction } from \"@zayne-labs/toolkit-type-helpers\";\nimport type { RefCallback } from \"react\";\n\ntype PossibleRef<TRef> = React.Ref<TRef> | undefined;\n\n/**\n * @description Set a given ref to a given value.\n *\n * This utility takes care of different types of refs: callback refs and RefObject(s)\n */\nexport const setRef = <TRef>(ref: PossibleRef<TRef>, node: TRef): ReturnType<RefCallback<TRef>> => {\n\tif (!ref) return;\n\n\tif (isFunction(ref)) {\n\t\treturn ref(node);\n\t}\n\n\t// eslint-disable-next-line no-param-reassign -- Mutation is needed here\n\tref.current = node;\n};\n\n/**\n * @description A utility to combine refs. Accepts callback refs and RefObject(s)\n */\nexport const composeRefs = <TRef>(refs: Array<PossibleRef<TRef>>): RefCallback<TRef> => {\n\tconst refCallBack: RefCallback<TRef> = (node) => {\n\t\tconst cleanupFnArray = refs.map((ref) => setRef(ref, node));\n\n\t\tconst cleanupFn = () => cleanupFnArray.forEach((cleanup) => cleanup?.());\n\n\t\t// == React 18 may not call the cleanup function so we need to call it manually on element unmount\n\t\tif (!node) {\n\t\t\tcleanupFn();\n\t\t\treturn;\n\t\t}\n\n\t\treturn cleanupFn;\n\t};\n\n\treturn refCallBack;\n};\n","import { toArray } from \"@zayne-labs/toolkit-core\";\nimport { type AnyFunction, AssertionError, isArray } from \"@zayne-labs/toolkit-type-helpers\";\nimport { isValidElement } from \"react\";\n\ntype Noop = () => void;\ntype WithSlot = { slot?: string };\n\ntype FunctionalComponent<TProps> = (\n\tprops: TProps\n\t// eslint-disable-next-line perfectionist/sort-union-types -- Lets keep the first one first\n) => ReturnType<React.FunctionComponent<TProps>> | AnyFunction<React.ReactNode>;\n\n// TODO - Add support for thing like <div slot=\"foo\"> OR <Slot name=\"foo\">\nexport const isSlotElement = <TProps>(\n\tchild: React.ReactNode,\n\tSlotWrapper: FunctionalComponent<TProps>\n) => {\n\tif (!isValidElement(child)) {\n\t\treturn false;\n\t}\n\n\tif ((child.type as WithSlot).slot === (SlotWrapper as WithSlot).slot) {\n\t\treturn true;\n\t}\n\n\tif ((child.type as Noop).name === (SlotWrapper as Noop).name) {\n\t\treturn true;\n\t}\n\n\tif (child.type === SlotWrapper) {\n\t\treturn true;\n\t}\n\n\treturn child.type.toString() === SlotWrapper.toString();\n};\n\ntype SlotOptions = {\n\terrorMessage?: string;\n\tthrowOnMultipleSlotMatch?: boolean;\n};\n\n/**\n * @description Retrieves a single slot element from a collection of React children that matches the provided SlotWrapper component.\n *\n * @throws {AssertionError} When throwOnMultipleSlotMatch is true and multiple slots are found\n */\nexport const getSlotElement = <TProps = Record<string, unknown>>(\n\tchildren: React.ReactNode,\n\tSlotWrapper: FunctionalComponent<TProps>,\n\toptions: SlotOptions = {}\n) => {\n\tconst {\n\t\terrorMessage = \"Only one instance of the SlotComponent is allowed\",\n\t\tthrowOnMultipleSlotMatch = false,\n\t} = options;\n\n\tconst childrenArray = toArray<React.ReactNode>(children);\n\n\tconst Slot = childrenArray.filter((child) => isSlotElement(child, SlotWrapper));\n\n\tif (throwOnMultipleSlotMatch && Slot.length > 1) {\n\t\tthrow new AssertionError(errorMessage);\n\t}\n\n\treturn Slot[0] as React.ReactElement<TProps> | undefined;\n};\n\nconst isSlotElementMultiple = <TProps>(\n\tchild: React.ReactNode,\n\tSlotWrapperArray: Array<FunctionalComponent<TProps>>\n) => SlotWrapperArray.some((slotWrapper) => isSlotElement(child, slotWrapper));\n\n// Check if the child is a Slot element by matching any in the SlotWrapperArray\nexport const getOtherChildren = <\n\tTProps = Record<string, unknown>,\n\tTChildren extends React.ReactNode = React.ReactNode,\n>(\n\tchildren: TChildren,\n\tSlotWrapperOrWrappers: Array<FunctionalComponent<TProps>> | FunctionalComponent<TProps>\n) => {\n\tconst childrenArray = toArray<TChildren>(children);\n\n\tconst otherChildren = isArray(SlotWrapperOrWrappers)\n\t\t? childrenArray.filter((child) => !isSlotElementMultiple(child, SlotWrapperOrWrappers))\n\t\t: childrenArray.filter((child) => !isSlotElement(child, SlotWrapperOrWrappers));\n\n\treturn otherChildren as TChildren extends unknown[] ? TChildren : TChildren[];\n};\n","import { mergeClassNames, mergeFunctions } from \"@zayne-labs/toolkit-core\";\nimport { isFunction, isPlainObject } from \"@zayne-labs/toolkit-type-helpers\";\n\ntype UnknownProps = Record<never, never>;\n\nconst mergeTwoProps = <TProps extends UnknownProps>(\n\tslotProps: TProps | undefined,\n\tchildProps: TProps | undefined\n): TProps => {\n\tif (!slotProps || !childProps) {\n\t\treturn childProps ?? slotProps ?? ({} as TProps);\n\t}\n\n\t// == all child props should override slotProps\n\tconst overrideProps = { ...childProps } as Record<string, unknown>;\n\n\tfor (const propName of Object.keys(slotProps)) {\n\t\tconst slotPropValue = (slotProps as Record<string, unknown>)[propName];\n\t\tconst childPropValue = (childProps as Record<string, unknown>)[propName];\n\n\t\t// == if it's `style`, we merge them\n\t\tif (propName === \"style\" && isPlainObject(slotPropValue) && isPlainObject(childPropValue)) {\n\t\t\toverrideProps[propName] = { ...slotPropValue, ...childPropValue };\n\t\t\tcontinue;\n\t\t}\n\n\t\t// == if it's `className` or `class`, we merge them\n\t\tif (propName === \"className\" || propName === \"class\") {\n\t\t\toverrideProps[propName] = mergeClassNames(slotPropValue as string, childPropValue as string);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst isHandler = propName.startsWith(\"on\");\n\n\t\t// == if the handler exists on both, we compose them\n\t\tif (isHandler && isFunction(slotPropValue) && isFunction(childPropValue)) {\n\t\t\toverrideProps[propName] = mergeFunctions(childPropValue, slotPropValue);\n\t\t}\n\t}\n\n\treturn { ...slotProps, ...overrideProps };\n};\n\nexport { mergeTwoProps };\n","import { mergeClassNames, mergeFunctions } from \"@zayne-labs/toolkit-core\";\nimport { isFunction, isPlainObject } from \"@zayne-labs/toolkit-type-helpers\";\n\n// const CSS_REGEX = /((?:--)?(?:\\w+-?)+)\\s*:\\s*([^;]*)/g;\n\n// const serialize = (style: string): Record<string, string> => {\n// \tconst res: Record<string, string> = {};\n// \tlet match: RegExpExecArray | null;\n// \twhile ((match = CSS_REGEX.exec(style))) {\n// \t\tres[match[1]!] = match[2]!;\n// \t}\n// \treturn res;\n// };\n\n// const css = (\n// \ta: Record<string, string> | string | undefined,\n// \tb: Record<string, string> | string | undefined\n// ): Record<string, string> | string => {\n// \tif (isString(a)) {\n// \t\tif (isString(b)) return `${a};${b}`;\n// \t\ta = serialize(a);\n// \t} else if (isString(b)) {\n// \t\tb = serialize(b);\n// \t}\n// \treturn Object.assign({}, a ?? {}, b ?? {});\n// };\n\ntype UnionToIntersection<TUnion> = (TUnion extends unknown ? (param: TUnion) => void : \"\") extends (\n\tparam: infer TParam\n) => void\n\t? TParam\n\t: \"\";\n\n/* eslint-disable no-param-reassign -- Mutation is fine here since it's an internally managed object */\nconst handleMergePropsIntoResult = (\n\tmergedResult: Record<string, unknown>,\n\tpropsObject: Record<string, unknown>\n) => {\n\tfor (const propName of Object.keys(mergedResult)) {\n\t\tconst mergedResultValue = mergedResult[propName];\n\t\tconst propsObjectValue = propsObject[propName];\n\n\t\tif (propName === \"className\" || propName === \"class\") {\n\t\t\tmergedResult[propName] = mergeClassNames(mergedResultValue as string, propsObjectValue as string);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (propName === \"style\" && isPlainObject(mergedResultValue) && isPlainObject(propsObjectValue)) {\n\t\t\t// mergedResult[propName] = css(mergedResultValue, propsObjectValue);\n\t\t\tmergedResult[propName] = { ...mergedResultValue, ...propsObjectValue };\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst isHandler = propName.startsWith(\"on\");\n\n\t\tif (isHandler && isFunction(mergedResultValue) && isFunction(propsObjectValue)) {\n\t\t\tmergedResult[propName] = mergeFunctions(propsObjectValue, mergedResultValue);\n\t\t\tcontinue;\n\t\t}\n\n\t\tmergedResult[propName] = propsObjectValue !== undefined ? propsObjectValue : mergedResultValue;\n\t}\n};\n\nconst addMissingPropsToResult = (\n\tmergedResult: Record<string, unknown>,\n\tpropsObject: Record<string, unknown>\n) => {\n\tfor (const propName of Object.keys(propsObject)) {\n\t\tif (mergedResult[propName] === undefined) {\n\t\t\tmergedResult[propName] = propsObject[propName];\n\t\t}\n\t}\n};\n/* eslint-enable no-param-reassign -- Mutation is fine here since it's an internally managed object */\n\ntype UnknownProps = Record<never, never>;\n\nconst mergeProps = <TProps extends UnknownProps>(\n\t...parameters: Array<TProps | undefined>\n): UnionToIntersection<TProps> => {\n\tconst mergedResult: Record<string, unknown> = {};\n\n\tfor (const propsObject of parameters) {\n\t\tif (!propsObject) continue;\n\n\t\thandleMergePropsIntoResult(mergedResult, propsObject);\n\n\t\t// == Add props from propsObject that are not in the mergedResult\n\t\taddMissingPropsToResult(mergedResult, propsObject);\n\t}\n\n\treturn mergedResult as never;\n};\n\nexport { mergeProps };\n"]}
1
+ {"version":3,"sources":["../../../src/utils/composeRefs.ts","../../../src/utils/getSlotElement.ts","../../../src/utils/getSlotMap.ts","../../../src/utils/mergeProps.ts","../../../src/utils/mergeTwoProps.ts"],"names":["SlotComponent","toArray","isValidElement","isFunction","isPlainObject","mergeClassNames","mergeFunctions"],"mappings":";;;;;AAUa,IAAA,MAAA,GAAS,CAAO,GAAA,EAAwB,IAA8C,KAAA;AAClG,EAAA,IAAI,CAAC,GAAK,EAAA;AAEV,EAAI,IAAA,UAAA,CAAW,GAAG,CAAG,EAAA;AACpB,IAAA,OAAO,IAAI,IAAI,CAAA;AAAA;AAIhB,EAAA,GAAA,CAAI,OAAU,GAAA,IAAA;AACf;AAKa,IAAA,WAAA,GAAc,CAAO,IAAsD,KAAA;AACvF,EAAM,MAAA,WAAA,GAAiC,CAAC,IAAS,KAAA;AAChD,IAAM,MAAA,cAAA,GAAiB,KAAK,GAAI,CAAA,CAAC,QAAQ,MAAO,CAAA,GAAA,EAAK,IAAI,CAAC,CAAA;AAE1D,IAAA,MAAM,YAAY,MAAM,cAAA,CAAe,QAAQ,CAAC,OAAA,KAAY,WAAW,CAAA;AAGvE,IAAA,IAAI,CAAC,IAAM,EAAA;AACV,MAAU,SAAA,EAAA;AACV,MAAA;AAAA;AAGD,IAAO,OAAA,SAAA;AAAA,GACR;AAEA,EAAO,OAAA,WAAA;AACR;ACvBa,IAAA,MAAA,GAAS,CAAS,KAAA,EAAwBA,cAA+C,KAAA;AACrG,EAAI,IAAA,CAAC,cAAe,CAAA,KAAK,CAAG,EAAA;AAC3B,IAAO,OAAA,KAAA;AAAA;AAKR,EAAA,IAAK,KAAM,CAAA,IAAA,CAAkB,IAAUA,KAAAA,cAAAA,CAA2B,IAAM,EAAA;AACvE,IAAO,OAAA,IAAA;AAAA;AAGR,EAAI,IAAA,KAAA,CAAM,SAASA,cAAe,EAAA;AACjC,IAAO,OAAA,IAAA;AA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gB,CAAA,aAAA,EAAyB,cAAwB,CAAA;AAC3F,MAAA;AAAA;AAGD,IAAM,MAAA,SAAA,GAAY,QAAS,CAAA,UAAA,CAAW,IAAI,CAAA;AAG1C,IAAA,IAAI,aAAaF,UAAW,CAAA,aAAa,CAAKA,IAAAA,UAAAA,CAAW,cAAc,CAAG,EAAA;AACzE,MAAA,aAAA,CAAc,QAAQ,CAAA,GAAIG,cAAe,CAAA,cAAA,EAAgB,aAAa,CAAA;AAAA;AACvE;AAGD,EAAA,OAAO,EAAE,GAAG,SAAW,EAAA,GAAG,aAAc,EAAA;AACzC","file":"index.js","sourcesContent":["import { isFunction } from \"@zayne-labs/toolkit-type-helpers\";\nimport type { RefCallback } from \"react\";\n\ntype PossibleRef<TRef> = React.Ref<TRef> | undefined;\n\n/**\n * @description Set a given ref to a given value.\n *\n * This utility takes care of different types of refs: callback refs and RefObject(s)\n */\nexport const setRef = <TRef>(ref: PossibleRef<TRef>, node: TRef): ReturnType<RefCallback<TRef>> => {\n\tif (!ref) return;\n\n\tif (isFunction(ref)) {\n\t\treturn ref(node);\n\t}\n\n\t// eslint-disable-next-line no-param-reassign -- Mutation is needed here\n\tref.current = node;\n};\n\n/**\n * @description A utility to combine refs. Accepts callback refs and RefObject(s)\n */\nexport const composeRefs = <TRef>(refs: Array<PossibleRef<TRef>>): RefCallback<TRef> => {\n\tconst refCallBack: RefCallback<TRef> = (node) => {\n\t\tconst cleanupFnArray = refs.map((ref) => setRef(ref, node));\n\n\t\tconst cleanupFn = () => cleanupFnArray.forEach((cleanup) => cleanup?.());\n\n\t\t// == React 18 may not call the cleanup function so we need to call it manually on element unmount\n\t\tif (!node) {\n\t\t\tcleanupFn();\n\t\t\treturn;\n\t\t}\n\n\t\treturn cleanupFn;\n\t};\n\n\treturn refCallBack;\n};\n","import { toArray } from \"@zayne-labs/toolkit-core\";\nimport { type AnyFunction, AssertionError, type EmptyObject } from \"@zayne-labs/toolkit-type-helpers\";\nimport { isValidElement } from \"react\";\n\ntype FunctionalComponent<TProps = EmptyObject> = (\n\tprops: TProps\n\t// eslint-disable-next-line perfectionist/sort-union-types -- Lets keep the first one first\n) => ReturnType<React.FunctionComponent<TProps>> | AnyFunction<React.ReactNode>;\n\n// TODO - Add support for thing like <div slot=\"foo\"> OR <Slot name=\"foo\">\n/**\n * @description Checks if a React node is a slot component using multiple matching strategies:\n * 1. Matches by slot symbol property\n * 2. Matches by direct component reference\n * 3. Matches by stringified component\n * 4. Matches by component name\n */\nexport const isSlot = <TProps>(child: React.ReactNode, SlotComponent: FunctionalComponent<TProps>) => {\n\tif (!isValidElement(child)) {\n\t\treturn false;\n\t}\n\n\ttype WithSlot = { slot?: string };\n\n\tif ((child.type as WithSlot).slot === (SlotComponent as WithSlot).slot) {\n\t\treturn true;\n\t}\n\n\tif (child.type === SlotComponent) {\n\t\treturn true;\n\t}\n\n\tif (child.type.toString() === SlotComponent.toString()) {\n\t\treturn true;\n\t}\n\n\treturn (child.type as FunctionalComponent<TProps>).name === SlotComponent.name;\n};\n\ntype SlotOptions = {\n\terrorMessage?: string;\n\tthrowOnMultipleSlotMatch?: boolean;\n};\n\n/**\n * @description Counts how many times a slot component appears in an array of children\n * @internal\n */\nconst calculateSlotOccurrences = (\n\tchildrenArray: React.ReactNode[],\n\tSlotComponent: FunctionalComponent\n) => {\n\tlet count = 0;\n\n\tfor (const child of childrenArray) {\n\t\tif (!isSlot(child, SlotComponent)) continue;\n\n\t\tcount += 1;\n\t}\n\n\treturn count;\n};\n\n/**\n * @description Retrieves a single slot element from a collection of React children that matches the provided SlotComponent component.\n *\n * @throws { AssertionError } when throwOnMultipleSlotMatch is true and multiple slots are found\n */\nexport const getSingleSlot = (\n\tchildren: React.ReactNode,\n\tSlotComponent: FunctionalComponent,\n\toptions: SlotOptions = {}\n) => {\n\tconst {\n\t\terrorMessage = \"Only one instance of the SlotComponent is allowed\",\n\t\tthrowOnMultipleSlotMatch = false,\n\t} = options;\n\n\tconst childrenArray = toArray<React.ReactNode>(children);\n\n\tconst shouldThrow =\n\t\tthrowOnMultipleSlotMatch && calculateSlotOccurrences(childrenArray, SlotComponent) > 1;\n\n\tif (shouldThrow) {\n\t\tthrow new AssertionError(errorMessage);\n\t}\n\n\tconst slotElement = childrenArray.find((child) => isSlot(child, SlotComponent));\n\n\treturn slotElement;\n};\n\n// NOTE - You can imitate const type parameter by extending readonly[] | []\n\nexport const getMultipleSlots = <const TSlotComponents extends FunctionalComponent[]>(\n\tchildren: React.ReactNode,\n\tSlotComponents: TSlotComponents,\n\toptions?: SlotOptions\n) => {\n\ttype SlotsType = { [Key in keyof TSlotComponents]: ReturnType<TSlotComponents[Key]> };\n\n\tconst slots = SlotComponents.map((SlotComponent) =>\n\t\tgetSingleSlot(children, SlotComponent, options)\n\t) as SlotsType;\n\n\tconst otherChildren = getOtherChildren(children, SlotComponents);\n\n\treturn { otherChildren, slots };\n};\n\n/**\n * @description Returns all children that are not slot elements (i.e., don't match any of the provided slot components)\n */\nexport const getOtherChildren = (children: React.ReactNode, SlotComponents: FunctionalComponent[]) => {\n\tconst childrenArray = toArray<React.ReactNode>(children);\n\n\tconst otherChildren = childrenArray.filter(\n\t\t(child) => !SlotComponents.some((SlotComponent) => isSlot(child, SlotComponent))\n\t);\n\n\treturn otherChildren;\n};\n","import { toArray } from \"@zayne-labs/toolkit-core\";\nimport type { AnyFunction, UnionToIntersection } from \"@zayne-labs/toolkit-type-helpers\";\nimport { isValidElement } from \"react\";\n\n/**\n * Possible children types that can be passed to a slot\n */\ntype PossibleSlotChildrenType =\n\t| AnyFunction<React.ReactNode | React.ReactNode[]>\n\t| React.ReactNode\n\t| React.ReactNode[];\n\n/**\n * Maps slot names to their corresponding children types\n */\ntype GetSlotMapResult<TSlotComponentProps extends GetSlotComponentProps> = UnionToIntersection<{\n\t[TName in keyof TSlotComponentProps as TSlotComponentProps[\"name\"]]: TSlotComponentProps[\"children\"];\n}>;\n\n/**\n * Symbol used to identify SlotComponent instances\n */\nexport const slotComponentSymbol = Symbol(\"SlotComponent\");\n\n/**\n * @description Creates a map of named slots from React children. Returns an object mapping slot names to their children,\n * with a default slot for unmatched children.\n *\n * @example\n * ```tsx\n * import { type GetSlotComponentProps, SlotComponent } from \"@zayne-labs/toolkit-react/utils\"\n *\n * type SlotProps = GetSlotComponentProps<\"header\" | \"footer\">;\n *\n * function Parent({ children }: { children: React.ReactNode }) {\n * const slots = getSlotMap<SlotProps>(children);\n *\n * return (\n * <div>\n * <header>{slots.header}</header>\n * <main>{slots.default}</main>\n * <footer>{slots.footer}</footer>\n * </div>\n * );\n * }\n * ```\n *\n * Usage:\n * ```tsx\n * <Parent>\n * <SlotComponent name=\"header\">Header Content</SlotComponent>\n * <div>Random stuff</div>\n * <SlotComponent name=\"footer\">Footer Content</SlotComponent>\n * </Parent>\n * ```\n */\nexport const getSlotMap = <TSlotComponentProps extends GetSlotComponentProps>(\n\tchildren: React.ReactNode\n) => {\n\tconst childrenArray = toArray<React.ReactNode>(children);\n\n\tconst slots: Record<string, PossibleSlotChildrenType> & { default: React.ReactNode[] } = {\n\t\tdefault: [],\n\t};\n\n\tfor (const child of childrenArray) {\n\t\ttype SlotElementProps = TSlotComponentProps & { \"data-slot-name\": never };\n\n\t\ttype RegularElementProps = Pick<TSlotComponentProps, \"children\"> & {\n\t\t\t\"data-slot-name\": string;\n\t\t\tname: never;\n\t\t};\n\n\t\tconst isSlotElementWithName =\n\t\t\tisValidElement<SlotElementProps>(child)\n\t\t\t&& (child.type as typeof SlotComponent).id === slotComponentSymbol\n\t\t\t&& child.props.name;\n\n\t\tconst isRegularElementWithSlotName =\n\t\t\tisValidElement<RegularElementProps>(child) && child.props[\"data-slot-name\"];\n\n\t\tif (!isSlotElementWithName && !isRegularElementWithSlotName) {\n\t\t\tslots.default.push(child);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst slotName = isSlotElementWithName ? child.props.name : child.props[\"data-slot-name\"];\n\n\t\tslots[slotName] = child.props.children;\n\t}\n\n\treturn slots as GetSlotMapResult<TSlotComponentProps> & { default: React.ReactNode[] };\n};\n\n/**\n * @description Produce props for the SlotComponent\n */\nexport type GetSlotComponentProps<\n\tTName extends string = string,\n\tTChildren extends PossibleSlotChildrenType = PossibleSlotChildrenType,\n> = {\n\t/** Content to render in the slot */\n\tchildren: TChildren;\n\t/** Name of the slot where content should be rendered */\n\tname: TName;\n};\n\n/**\n * @description Function used to create a slot component that defines named slots in a parent component. This component created doesn't render anything,\n * it's used purely for slot definition.\n *\n * @example\n * ```tsx\n * import { type GetSlotComponentProps, createSlotComponent, SlotComponent } from \"@zayne-labs/toolkit-react/utils\"\n *\n * type SlotProps = GetSlotComponentProps<\"header\" | \"footer\">;\n *\n * function Parent({ children }: { children: React.ReactNode }) {\n * const slots = getSlotMap<SlotProps>(children);\n *\n * return (\n * <div>\n * <header>{slots.header}</header>\n * <main>{slots.default}</main>\n * <footer>{slots.footer}</footer>\n * </div>\n * );\n * }\n *\n * Parent.Slot = createSlotComponent<SlotProps>();\n * // OR\n * Parent.Slot = SlotComponent<SlotProps>\n * ```\n *\n *\n * @example\n * Usage:\n\t```tsx\n * function App() {\n * return (\n * <Parent>\n * <Parent.Slot name=\"header\">Header Content</Parent.Slot>\n * <div>Default Content</div>\n * <Parent.Slot name=\"footer\">Footer Content</Parent.Slot>\n * </Parent>\n * );\n * }\n * ```\n */\n\nexport const createSlotComponent = <TBaseSlotComponentProps extends GetSlotComponentProps>() => {\n\tfunction SlotComponent<TSlotComponentProps extends TBaseSlotComponentProps>(\n\t\t// eslint-disable-next-line ts-eslint/no-unused-vars -- The props here are just for type definition really, as this component doesn't need to render anything\n\t\tprops: TSlotComponentProps\n\t) {\n\t\treturn null as React.ReactNode;\n\t}\n\n\tSlotComponent.id = slotComponentSymbol;\n\n\treturn SlotComponent;\n};\n\n/**\n * @description Slot component created by createSlotComponent\n */\nexport const SlotComponent = createSlotComponent();\n","import { mergeClassNames, mergeFunctions } from \"@zayne-labs/toolkit-core\";\nimport { isFunction, isPlainObject } from \"@zayne-labs/toolkit-type-helpers\";\n\n// const CSS_REGEX = /((?:--)?(?:\\w+-?)+)\\s*:\\s*([^;]*)/g;\n\n// const serialize = (style: string): Record<string, string> => {\n// \tconst res: Record<string, string> = {};\n// \tlet match: RegExpExecArray | null;\n// \twhile ((match = CSS_REGEX.exec(style))) {\n// \t\tres[match[1]!] = match[2]!;\n// \t}\n// \treturn res;\n// };\n\n// const css = (\n// \ta: Record<string, string> | string | undefined,\n// \tb: Record<string, string> | string | undefined\n// ): Record<string, string> | string => {\n// \tif (isString(a)) {\n// \t\tif (isString(b)) return `${a};${b}`;\n// \t\ta = serialize(a);\n// \t} else if (isString(b)) {\n// \t\tb = serialize(b);\n// \t}\n// \treturn Object.assign({}, a ?? {}, b ?? {});\n// };\n\ntype UnionToIntersection<TUnion> = (TUnion extends unknown ? (param: TUnion) => void : \"\") extends (\n\tparam: infer TParam\n) => void\n\t? TParam\n\t: \"\";\n\n/* eslint-disable no-param-reassign -- Mutation is fine here since it's an internally managed object */\nconst handleMergePropsIntoResult = (\n\tmergedResult: Record<string, unknown>,\n\tpropsObject: Record<string, unknown>\n) => {\n\tfor (const propName of Object.keys(mergedResult)) {\n\t\tconst mergedResultValue = mergedResult[propName];\n\t\tconst propsObjectValue = propsObject[propName];\n\n\t\tif (propName === \"className\" || propName === \"class\") {\n\t\t\tmergedResult[propName] = mergeClassNames(mergedResultValue as string, propsObjectValue as string);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (propName === \"style\" && isPlainObject(mergedResultValue) && isPlainObject(propsObjectValue)) {\n\t\t\t// mergedResult[propName] = css(mergedResultValue, propsObjectValue);\n\t\t\tmergedResult[propName] = { ...mergedResultValue, ...propsObjectValue };\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst isHandler = propName.startsWith(\"on\");\n\n\t\tif (isHandler && isFunction(mergedResultValue) && isFunction(propsObjectValue)) {\n\t\t\tmergedResult[propName] = mergeFunctions(propsObjectValue, mergedResultValue);\n\t\t\tcontinue;\n\t\t}\n\n\t\tmergedResult[propName] = propsObjectValue !== undefined ? propsObjectValue : mergedResultValue;\n\t}\n};\n\nconst addMissingPropsToResult = (\n\tmergedResult: Record<string, unknown>,\n\tpropsObject: Record<string, unknown>\n) => {\n\tfor (const propName of Object.keys(propsObject)) {\n\t\tif (mergedResult[propName] === undefined) {\n\t\t\tmergedResult[propName] = propsObject[propName];\n\t\t}\n\t}\n};\n/* eslint-enable no-param-reassign -- Mutation is fine here since it's an internally managed object */\n\ntype UnknownProps = Record<never, never>;\n\nconst mergeProps = <TProps extends UnknownProps>(\n\t...parameters: Array<TProps | undefined>\n): UnionToIntersection<TProps> => {\n\tconst mergedResult: Record<string, unknown> = {};\n\n\tfor (const propsObject of parameters) {\n\t\tif (!propsObject) continue;\n\n\t\thandleMergePropsIntoResult(mergedResult, propsObject);\n\n\t\t// == Add props from propsObject that are not in the mergedResult\n\t\taddMissingPropsToResult(mergedResult, propsObject);\n\t}\n\n\treturn mergedResult as never;\n};\n\nexport { mergeProps };\n","import { mergeClassNames, mergeFunctions } from \"@zayne-labs/toolkit-core\";\nimport { isFunction, isPlainObject } from \"@zayne-labs/toolkit-type-helpers\";\n\ntype UnknownProps = Record<never, never>;\n\nconst mergeTwoProps = <TProps extends UnknownProps>(\n\tslotProps: TProps | undefined,\n\tchildProps: TProps | undefined\n): TProps => {\n\tif (!slotProps || !childProps) {\n\t\treturn childProps ?? slotProps ?? ({} as TProps);\n\t}\n\n\t// == all child props should override slotProps\n\tconst overrideProps = { ...childProps } as Record<string, unknown>;\n\n\tfor (const propName of Object.keys(slotProps)) {\n\t\tconst slotPropValue = (slotProps as Record<string, unknown>)[propName];\n\t\tconst childPropValue = (childProps as Record<string, unknown>)[propName];\n\n\t\t// == if it's `style`, we merge them\n\t\tif (propName === \"style\" && isPlainObject(slotPropValue) && isPlainObject(childPropValue)) {\n\t\t\toverrideProps[propName] = { ...slotPropValue, ...childPropValue };\n\t\t\tcontinue;\n\t\t}\n\n\t\t// == if it's `className` or `class`, we merge them\n\t\tif (propName === \"className\" || propName === \"class\") {\n\t\t\toverrideProps[propName] = mergeClassNames(slotPropValue as string, childPropValue as string);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst isHandler = propName.startsWith(\"on\");\n\n\t\t// == if the handler exists on both, we compose them\n\t\tif (isHandler && isFunction(slotPropValue) && isFunction(childPropValue)) {\n\t\t\toverrideProps[propName] = mergeFunctions(childPropValue, slotPropValue);\n\t\t}\n\t}\n\n\treturn { ...slotProps, ...overrideProps };\n};\n\nexport { mergeTwoProps };\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zayne-labs/toolkit-react",
3
3
  "type": "module",
4
- "version": "0.8.62",
4
+ "version": "0.9.0",
5
5
  "description": "A collection of utility functions, types and composables used by my other projects. Nothing too fancy but can be useful.",
6
6
  "author": "Ryan Zayne",
7
7
  "license": "MIT",
@@ -50,8 +50,8 @@
50
50
  }
51
51
  },
52
52
  "dependencies": {
53
- "@zayne-labs/toolkit-core": "0.8.62",
54
- "@zayne-labs/toolkit-type-helpers": "0.8.62"
53
+ "@zayne-labs/toolkit-core": "0.9.0",
54
+ "@zayne-labs/toolkit-type-helpers": "0.9.0"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@arethetypeswrong/cli": "^0.17.4",
@@ -86,7 +86,7 @@
86
86
  },
87
87
  {
88
88
  "path": "./src/utils/index.ts",
89
- "limit": "1 kb"
89
+ "limit": "2 kb"
90
90
  },
91
91
  {
92
92
  "path": "./src/zustand/index.ts",