@zayne-labs/toolkit-react 0.9.11 → 0.9.14

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.
@@ -70,10 +70,6 @@ declare const getMultipleSlots: <const TSlotComponents extends FunctionalCompone
70
70
  */
71
71
  declare const getRegularChildren: <TChildren extends React.ReactNode>(children: TChildren, SlotComponentOrComponents: FunctionalComponent | FunctionalComponent[]) => TChildren extends unknown[] ? TChildren : TChildren[];
72
72
 
73
- /**
74
- * Possible children types that can be passed to a slot
75
- */
76
- type PossibleSlotChildrenType = React.ReactNode | React.ReactNode[];
77
73
  /**
78
74
  * Maps slot names to their corresponding children types
79
75
  */
@@ -122,65 +118,25 @@ declare const getSlotMap: <TSlotComponentProps extends GetSlotComponentProps>(ch
122
118
  /**
123
119
  * @description Produce props for the SlotComponent
124
120
  */
125
- type GetSlotComponentProps<TName extends string = string, TChildren extends PossibleSlotChildrenType = PossibleSlotChildrenType> = {
121
+ type GetSlotComponentProps<TName extends string = string, TChildren extends React.ReactNode = React.ReactNode> = {
126
122
  /** Content to render in the slot */
127
123
  children: TChildren;
128
124
  /** Name of the slot where content should be rendered */
129
125
  name: TName;
130
126
  };
131
- /**
132
- * @description Function used to create a slot component that defines named slots in a parent component. This component created doesn't render anything,
133
- * it's used purely for slot definition.
134
- *
135
- * @example
136
- * ```tsx
137
- * import { type GetSlotComponentProps, createSlotComponent, SlotComponent } from "@zayne-labs/toolkit-react/utils"
138
- *
139
- * type SlotProps = GetSlotComponentProps<"header" | "footer">;
140
- *
141
- * function Parent({ children }: { children: React.ReactNode }) {
142
- * const slots = getSlotMap<SlotProps>(children);
143
- *
144
- * return (
145
- * <div>
146
- * <header>{slots.header}</header>
147
- * <main>{slots.default}</main>
148
- * <footer>{slots.footer}</footer>
149
- * </div>
150
- * );
151
- * }
152
- *
153
- * Parent.Slot = createSlotComponent<SlotProps>();
154
- * // OR
155
- * Parent.Slot = SlotComponent<SlotProps>
156
- * ```
157
- *
158
- *
159
- * @example
160
- * Usage:
161
- ```tsx
162
- * function App() {
163
- * return (
164
- * <Parent>
165
- * <Parent.Slot name="header">Header Content</Parent.Slot>
166
- * <div>Default Content</div>
167
- * <Parent.Slot name="footer">Footer Content</Parent.Slot>
168
- * </Parent>
169
- * );
170
- * }
171
- * ```
172
- */
173
- declare const createSlotComponent: <TBaseSlotComponentProps extends GetSlotComponentProps>() => {
174
- <TSlotComponentProps extends TBaseSlotComponentProps>(props: TSlotComponentProps): React.ReactNode;
175
- slotSymbol: typeof slotComponentSymbol;
176
- };
177
127
  /**
178
128
  * @description Slot component created by createSlotComponent
179
129
  */
180
- declare const SlotComponent: {
181
- <TSlotComponentProps extends GetSlotComponentProps<string, PossibleSlotChildrenType>>(props: TSlotComponentProps): React.ReactNode;
130
+ declare const createSlotComponent: <TSlotComponentProps extends GetSlotComponentProps>() => {
131
+ (props: TSlotComponentProps): react.ReactNode;
182
132
  slotSymbol: typeof slotComponentSymbol;
183
133
  };
134
+ type WithSlotSymbolAndName<TSlotComponentProps extends Pick<GetSlotComponentProps, "name"> = Pick<GetSlotComponentProps, "name">, TActualProps extends UnknownObject = UnknownObject> = {
135
+ (props: Pick<GetSlotComponentProps, "children"> & TActualProps): React.ReactNode;
136
+ slotName?: TSlotComponentProps["name"];
137
+ slotSymbol?: typeof slotComponentSymbol;
138
+ };
139
+ declare const withSlotSymbolAndName: <TSlotComponentProps extends Pick<GetSlotComponentProps, "name">, TActualProps extends UnknownObject = UnknownObject>(name: TSlotComponentProps["name"], SlotComponent: WithSlotSymbolAndName<TSlotComponentProps, TActualProps>) => WithSlotSymbolAndName<TSlotComponentProps, TActualProps>;
184
140
 
185
141
  type UnionToIntersection<TUnion> = (TUnion extends unknown ? (param: TUnion) => void : "") extends (param: infer TParam) => void ? TParam : "";
186
142
  type UnknownProps$1 = Record<never, never>;
@@ -216,4 +172,4 @@ type InferRestOfProps<TElement extends React.ElementType, TProps> = Omit<React.C
216
172
  type MergedPropsWithAs<TElement extends React.ElementType, TProps> = Prettify<Omit<AsProp<TElement>, keyof TProps> & TProps>;
217
173
  type PolymorphicProps<TElement extends React.ElementType, TProps extends AnyObject = NonNullable<unknown>> = MergedPropsWithAs<TElement, TProps> & InferRestOfProps<TElement, TProps>;
218
174
 
219
- export { type AsProp, type DiscriminatedRenderProps, type ForwardedRefType, type FunctionalComponent, type GetSlotComponentProps, type InferProps, type MyCustomCss, type PolymorphicProps, SlotComponent, type StateSetter, composeEventHandlers, composeRefs, createSlotComponent, getMultipleSlots, getRegularChildren, getSingleSlot, getSlotMap, matchesAnySlotComponent, matchesSlotComponent, mergeProps, mergeTwoProps, setRef, slotComponentSymbol };
175
+ export { type AsProp, type DiscriminatedRenderProps, type ForwardedRefType, type FunctionalComponent, type GetSlotComponentProps, type InferProps, type MyCustomCss, type PolymorphicProps, type StateSetter, composeEventHandlers, composeRefs, createSlotComponent, getMultipleSlots, getRegularChildren, getSingleSlot, getSlotMap, matchesAnySlotComponent, matchesSlotComponent, mergeProps, mergeTwoProps, setRef, slotComponentSymbol, withSlotSymbolAndName };
@@ -1,6 +1,6 @@
1
1
  import { isFunction, isArray, isPlainObject, AssertionError } from '@zayne-labs/toolkit-type-helpers';
2
2
  import { toArray, mergeClassNames, mergeFunctions } from '@zayne-labs/toolkit-core';
3
- import { isValidElement } from 'react';
3
+ import { isValidElement, Fragment } from 'react';
4
4
 
5
5
  // src/utils/composeRefs.ts
6
6
  var setRef = (ref, node) => {
@@ -28,47 +28,48 @@ var composeEventHandlers = (eventHandlers) => {
28
28
  const mergedEventHandler = (event) => eventHandlers.forEach((handler) => handler?.(event));
29
29
  return mergedEventHandler;
30
30
  };
31
- var matchesSlotComponent = (child, SlotComponent2) => {
31
+ var matchesSlotComponent = (child, SlotComponent) => {
32
32
  if (!isValidElement(child)) {
33
33
  return false;
34
34
  }
35
- if (child.type?.slotSymbol && SlotComponent2?.slotSymbol && Object.is(child.type?.slotSymbol, SlotComponent2?.slotSymbol)) {
35
+ if (child.type?.slotSymbol && SlotComponent?.slotSymbol && Object.is(child.type?.slotSymbol, SlotComponent?.slotSymbol)) {
36
36
  return true;
37
37
  }
38
- if (child.type.name === SlotComponent2.name) {
38
+ if (child.type.name === SlotComponent.name) {
39
39
  return true;
40
40
  }
41
41
  return false;
42
42
  };
43
43
  var matchesAnySlotComponent = (child, SlotComponents) => {
44
- const matchesSlot = SlotComponents.some((SlotComponent2) => matchesSlotComponent(child, SlotComponent2));
44
+ const matchesSlot = SlotComponents.some((SlotComponent) => matchesSlotComponent(child, SlotComponent));
45
45
  return matchesSlot;
46
46
  };
47
- var calculateSlotOccurrences = (childrenArray, SlotComponent2) => {
47
+ var calculateSlotOccurrences = (childrenArray, SlotComponent) => {
48
48
  let count = 0;
49
49
  for (const child of childrenArray) {
50
- if (!matchesSlotComponent(child, SlotComponent2)) continue;
50
+ if (!matchesSlotComponent(child, SlotComponent)) continue;
51
51
  count += 1;
52
52
  }
53
53
  return count;
54
54
  };
55
- var getSingleSlot = (children, SlotComponent2, options = {}) => {
55
+ var getSingleSlot = (children, SlotComponent, options = {}) => {
56
56
  const {
57
57
  errorMessage = "Only one instance of the SlotComponent is allowed",
58
58
  throwOnMultipleSlotMatch = false
59
59
  } = options;
60
- const childrenArray = toArray(children);
61
- const shouldThrow = throwOnMultipleSlotMatch && calculateSlotOccurrences(childrenArray, SlotComponent2) > 1;
60
+ const actualChildren = isValidElement(children) && children.type === Fragment ? children.props.children : children;
61
+ const childrenArray = toArray(actualChildren);
62
+ const shouldThrow = throwOnMultipleSlotMatch && calculateSlotOccurrences(childrenArray, SlotComponent) > 1;
62
63
  if (shouldThrow) {
63
64
  throw new AssertionError(errorMessage);
64
65
  }
65
- const slotElement = childrenArray.find((child) => matchesSlotComponent(child, SlotComponent2));
66
+ const slotElement = childrenArray.find((child) => matchesSlotComponent(child, SlotComponent));
66
67
  return slotElement;
67
68
  };
68
69
  var getMultipleSlots = (children, SlotComponents, options) => {
69
70
  const { errorMessage, throwOnMultipleSlotMatch } = options ?? {};
70
71
  const slots = SlotComponents.map(
71
- (SlotComponent2, index) => getSingleSlot(children, SlotComponent2, {
72
+ (SlotComponent, index) => getSingleSlot(children, SlotComponent, {
72
73
  errorMessage: isArray(errorMessage) ? errorMessage[index] : errorMessage,
73
74
  throwOnMultipleSlotMatch: isArray(throwOnMultipleSlotMatch) ? throwOnMultipleSlotMatch[index] : throwOnMultipleSlotMatch
74
75
  })
@@ -77,7 +78,8 @@ var getMultipleSlots = (children, SlotComponents, options) => {
77
78
  return { regularChildren, slots };
78
79
  };
79
80
  var getRegularChildren = (children, SlotComponentOrComponents) => {
80
- const childrenArray = toArray(children);
81
+ const actualChildren = isValidElement(children) && children.type === Fragment ? children.props.children : children;
82
+ const childrenArray = toArray(actualChildren);
81
83
  const regularChildren = childrenArray.filter(
82
84
  (child) => !matchesAnySlotComponent(child, toArray(SlotComponentOrComponents))
83
85
  );
@@ -85,30 +87,31 @@ var getRegularChildren = (children, SlotComponentOrComponents) => {
85
87
  };
86
88
  var slotComponentSymbol = Symbol("slot-component");
87
89
  var getSlotMap = (children) => {
88
- const childrenArray = toArray(children);
89
- const slots = {
90
- default: []
91
- };
90
+ const actualChildren = isValidElement(children) && children.type === Fragment ? children.props.children : children;
91
+ const childrenArray = toArray(actualChildren);
92
+ const slots = { default: [] };
92
93
  for (const child of childrenArray) {
93
- const isSlotElementWithName = isValidElement(child) && child.type.slotSymbol === slotComponentSymbol && child.props.name;
94
- const isRegularElementWithSlotName = isValidElement(child) && child.props["data-slot-name"];
94
+ const isSlotElementWithName = isValidElement(child) && child.type.slotSymbol === slotComponentSymbol && Boolean(child.type.slotName ?? child.props.name);
95
+ const isRegularElementWithSlotName = isValidElement(child) && Boolean(child.props["data-slot-name"]);
95
96
  if (!isSlotElementWithName && !isRegularElementWithSlotName) {
96
97
  slots.default.push(child);
97
98
  continue;
98
99
  }
99
- const slotName = isSlotElementWithName ? child.props.name : child.props["data-slot-name"];
100
+ const slotName = isSlotElementWithName ? child.type.slotName ?? child.props.name : child.props["data-slot-name"];
100
101
  slots[slotName] = child.props.children;
101
102
  }
102
103
  return slots;
103
104
  };
104
105
  var createSlotComponent = () => {
105
- function SlotComponent2(props) {
106
- return props;
107
- }
108
- SlotComponent2.slotSymbol = slotComponentSymbol;
109
- return SlotComponent2;
106
+ const SlotComponent = (props) => props.children;
107
+ SlotComponent.slotSymbol = slotComponentSymbol;
108
+ return SlotComponent;
109
+ };
110
+ var withSlotSymbolAndName = (name, SlotComponent) => {
111
+ SlotComponent.slotSymbol = slotComponentSymbol;
112
+ SlotComponent.slotName = name;
113
+ return SlotComponent;
110
114
  };
111
- var SlotComponent = createSlotComponent();
112
115
  var handleMergePropsIntoResult = (mergedResult, propsObject) => {
113
116
  for (const propName of Object.keys(mergedResult)) {
114
117
  const mergedResultValue = mergedResult[propName];
@@ -169,6 +172,6 @@ var mergeTwoProps = (slotProps, childProps) => {
169
172
  return { ...slotProps, ...overrideProps };
170
173
  };
171
174
 
172
- export { SlotComponent, composeEventHandlers, composeRefs, createSlotComponent, getMultipleSlots, getRegularChildren, getSingleSlot, getSlotMap, matchesAnySlotComponent, matchesSlotComponent, mergeProps, mergeTwoProps, setRef, slotComponentSymbol };
175
+ export { composeEventHandlers, composeRefs, createSlotComponent, getMultipleSlots, getRegularChildren, getSingleSlot, getSlotMap, matchesAnySlotComponent, matchesSlotComponent, mergeProps, mergeTwoProps, setRef, slotComponentSymbol, withSlotSymbolAndName };
173
176
  //# sourceMappingURL=index.js.map
174
177
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/utils/composeRefs.ts","../../../src/utils/composeEventHandlers.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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AWC,IAAAA,aAAAA,CAAc,aAAa,CAAKA,IAAAA,aAAAA,CAAc,cAAc,CAAG,EAAA;AAC1F,MAAA,aAAA,CAAc,QAAQ,CAAI,GAAA,EAAE,GAAG,aAAA,EAAe,GAAG,cAAe,EAAA;AAChE,MAAA;AAAA;AAID,IAAI,IAAA,QAAA,KAAa,WAAe,IAAA,QAAA,KAAa,OAAS,EAAA;AACrD,MAAA,aAAA,CAAc,QAAQ,CAAA,GAAIC,eAAgB,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 extends HTMLElement>(\n\trefs: Array<PossibleRef<TRef>>\n): RefCallback<TRef> => {\n\tconst mergedRefCallBack: 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 mergedRefCallBack;\n};\n","type PossibleEventHandler<TSyntheticEvent extends React.SyntheticEvent> =\n\t| React.EventHandler<TSyntheticEvent>\n\t| undefined;\n\nexport const composeEventHandlers = <TSyntheticEvent extends React.SyntheticEvent>(\n\teventHandlers: Array<PossibleEventHandler<TSyntheticEvent>>\n): React.EventHandler<TSyntheticEvent> => {\n\t// prettier-ignore\n\tconst mergedEventHandler: React.EventHandler<TSyntheticEvent> = (event) => eventHandlers.forEach((handler) => handler?.(event));\n\n\treturn mergedEventHandler;\n};\n","import { toArray } from \"@zayne-labs/toolkit-core\";\nimport { AssertionError, type UnknownObject, isArray } from \"@zayne-labs/toolkit-type-helpers\";\nimport { isValidElement } from \"react\";\n\nexport type FunctionalComponent<TProps extends UnknownObject = never> = React.FunctionComponent<TProps>;\n\n/**\n * @description Checks if a react child (within the children array) matches the provided SlotComponent using multiple matching strategies:\n * 1. Matches by slot symbol property\n * 2. Matches by component name\n */\n\nexport const matchesSlotComponent = (child: React.ReactNode, SlotComponent: FunctionalComponent) => {\n\tif (!isValidElement(child)) {\n\t\treturn false;\n\t}\n\n\ttype WithSlot = { readonly slotSymbol?: unique symbol } | undefined;\n\n\tif (\n\t\t(child.type as WithSlot)?.slotSymbol\n\t\t&& (SlotComponent as WithSlot)?.slotSymbol\n\t\t&& Object.is((child.type as WithSlot)?.slotSymbol, (SlotComponent as WithSlot)?.slotSymbol)\n\t) {\n\t\treturn true;\n\t}\n\n\tif ((child.type as FunctionalComponent).name === SlotComponent.name) {\n\t\treturn true;\n\t}\n\n\treturn false;\n};\n\n/**\n * @description Checks if a react child (within the children array) matches any of the provided SlotComponents.\n */\nexport const matchesAnySlotComponent = (child: React.ReactNode, SlotComponents: FunctionalComponent[]) => {\n\tconst matchesSlot = SlotComponents.some((SlotComponent) => matchesSlotComponent(child, SlotComponent));\n\n\treturn matchesSlot;\n};\n\ntype SlotOptions = {\n\t/**\n\t * @description The error message to throw when multiple slots are found for a given slot component\n\t */\n\terrorMessage?: string;\n\t/**\n\t * @description When true, an AssertionError will be thrown if multiple slots are found for a given slot component\n\t */\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 (!matchesSlotComponent(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) => matchesSlotComponent(child, SlotComponent));\n\n\treturn slotElement;\n};\n\n// NOTE - You can imitate const type parameter by extending readonly[] | []\n\ntype MultipleSlotsOptions = {\n\t/**\n\t * @description The error message to throw when multiple slots are found for a given slot component\n\t * If a string is provided, the same message will be used for all slot components\n\t * If an array is provided, each string in the array will be used as the errorMessage for the corresponding slot component\n\t */\n\terrorMessage?: string | string[];\n\t/**\n\t * @description When true, an AssertionError will be thrown if multiple slots are found for a given slot component\n\t * If a boolean is provided, the same value will be used for all slot components\n\t * If an array is provided, each boolean in the array will be used as the throwOnMultipleSlotMatch value for the corresponding slot component\n\t */\n\tthrowOnMultipleSlotMatch?: boolean | boolean[];\n};\n\n/**\n * @description The same as getSingleSlot, but for multiple slot components\n */\nexport const getMultipleSlots = <const TSlotComponents extends FunctionalComponent[]>(\n\tchildren: React.ReactNode,\n\tSlotComponents: TSlotComponents,\n\toptions?: MultipleSlotsOptions\n) => {\n\ttype SlotsType = { [Key in keyof TSlotComponents]: ReturnType<TSlotComponents[Key]> };\n\n\tconst { errorMessage, throwOnMultipleSlotMatch } = options ?? {};\n\n\tconst slots = SlotComponents.map((SlotComponent, index) =>\n\t\tgetSingleSlot(children, SlotComponent, {\n\t\t\terrorMessage: isArray(errorMessage) ? errorMessage[index] : errorMessage,\n\t\t\tthrowOnMultipleSlotMatch: isArray(throwOnMultipleSlotMatch)\n\t\t\t\t? throwOnMultipleSlotMatch[index]\n\t\t\t\t: throwOnMultipleSlotMatch,\n\t\t})\n\t) as SlotsType;\n\n\tconst regularChildren = getRegularChildren(children, SlotComponents);\n\n\treturn { regularChildren, 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 getRegularChildren = <TChildren extends React.ReactNode>(\n\tchildren: TChildren,\n\tSlotComponentOrComponents: FunctionalComponent | FunctionalComponent[]\n) => {\n\tconst childrenArray = toArray<React.ReactNode>(children);\n\n\tconst regularChildren = childrenArray.filter(\n\t\t(child) => !matchesAnySlotComponent(child, toArray(SlotComponentOrComponents))\n\t);\n\n\treturn regularChildren as TChildren extends unknown[] ? TChildren : TChildren[];\n};\n","import { toArray } from \"@zayne-labs/toolkit-core\";\nimport type { 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 = React.ReactNode | 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(\"slot-component\");\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).slotSymbol === 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\tprops: TSlotComponentProps\n\t): React.ReactNode {\n\t\treturn props as unknown as React.ReactNode;\n\t}\n\n\tSlotComponent.slotSymbol = 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"]}
1
+ {"version":3,"sources":["../../../src/utils/composeRefs.ts","../../../src/utils/composeEventHandlers.ts","../../../src/utils/getSlotElement.ts","../../../src/utils/getSlotMap.ts","../../../src/utils/mergeProps.ts","../../../src/utils/mergeTwoProps.ts"],"names":["ReactFragment","isValidElement","toArray","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,CAC1B,IACuB,KAAA;AACvB,EAAM,MAAA,iBAAA,GAAuC,CAAC,IAAS,KAAA;AACtD,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,C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,MAAW,QAAY,IAAA,MAAA,CAAO,IAAK,CAAA,SAAS,CAAG,EAAA;AAC9C,IAAM,MAAA,aAAA,GAAiB,UAAsC,QAAQ,CAAA;AACrE,IAAM,MAAA,cAAA,GAAkB,WAAuC,QAAQ,CAAA;AAGvE,IAAA,IAAI,aAAa,OAAWC,IAAAA,aAAAA,CAAc,aAAa,CAAKA,IAAAA,aAAAA,CAAc,cAAc,CAAG,EAAA;AAC1F,MAAA,aAAA,CAAc,QAAQ,CAAI,GAAA,EAAE,GAAG,aAAA,EAAe,GAAG,cAAe,EAAA;AAChE,MAAA;AAAA;AAID,IAAI,IAAA,QAAA,KAAa,WAAe,IAAA,QAAA,KAAa,OAAS,EAAA;AACrD,MAAA,aAAA,CAAc,QAAQ,CAAA,GAAIC,eAAgB,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 extends HTMLElement>(\n\trefs: Array<PossibleRef<TRef>>\n): RefCallback<TRef> => {\n\tconst mergedRefCallBack: 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 mergedRefCallBack;\n};\n","type PossibleEventHandler<TSyntheticEvent extends React.SyntheticEvent> =\n\t| React.EventHandler<TSyntheticEvent>\n\t| undefined;\n\nexport const composeEventHandlers = <TSyntheticEvent extends React.SyntheticEvent>(\n\teventHandlers: Array<PossibleEventHandler<TSyntheticEvent>>\n): React.EventHandler<TSyntheticEvent> => {\n\t// prettier-ignore\n\tconst mergedEventHandler: React.EventHandler<TSyntheticEvent> = (event) => eventHandlers.forEach((handler) => handler?.(event));\n\n\treturn mergedEventHandler;\n};\n","import { toArray } from \"@zayne-labs/toolkit-core\";\nimport { AssertionError, type UnknownObject, isArray } from \"@zayne-labs/toolkit-type-helpers\";\nimport { Fragment as ReactFragment, isValidElement } from \"react\";\nimport type { InferProps } from \"./types\";\n\nexport type FunctionalComponent<TProps extends UnknownObject = never> = React.FunctionComponent<TProps>;\n\n/**\n * @description Checks if a react child (within the children array) matches the provided SlotComponent using multiple matching strategies:\n * 1. Matches by slot symbol property\n * 2. Matches by component name\n */\n\nexport const matchesSlotComponent = (child: React.ReactNode, SlotComponent: FunctionalComponent) => {\n\tif (!isValidElement(child)) {\n\t\treturn false;\n\t}\n\n\ttype WithSlot = { readonly slotSymbol?: unique symbol } | undefined;\n\n\tif (\n\t\t(child.type as WithSlot)?.slotSymbol\n\t\t&& (SlotComponent as WithSlot)?.slotSymbol\n\t\t&& Object.is((child.type as WithSlot)?.slotSymbol, (SlotComponent as WithSlot)?.slotSymbol)\n\t) {\n\t\treturn true;\n\t}\n\n\tif ((child.type as FunctionalComponent).name === SlotComponent.name) {\n\t\treturn true;\n\t}\n\n\treturn false;\n};\n\n/**\n * @description Checks if a react child (within the children array) matches any of the provided SlotComponents.\n */\nexport const matchesAnySlotComponent = (child: React.ReactNode, SlotComponents: FunctionalComponent[]) => {\n\tconst matchesSlot = SlotComponents.some((SlotComponent) => matchesSlotComponent(child, SlotComponent));\n\n\treturn matchesSlot;\n};\n\ntype SlotOptions = {\n\t/**\n\t * @description The error message to throw when multiple slots are found for a given slot component\n\t */\n\terrorMessage?: string;\n\t/**\n\t * @description When true, an AssertionError will be thrown if multiple slots are found for a given slot component\n\t */\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 (!matchesSlotComponent(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 actualChildren =\n\t\tisValidElement<InferProps<typeof ReactFragment>>(children) && children.type === ReactFragment\n\t\t\t? children.props.children\n\t\t\t: children;\n\n\tconst childrenArray = toArray<React.ReactNode>(actualChildren);\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) => matchesSlotComponent(child, SlotComponent));\n\n\treturn slotElement;\n};\n\n// NOTE - You can imitate const type parameter by extending readonly[] | []\n\ntype MultipleSlotsOptions = {\n\t/**\n\t * @description The error message to throw when multiple slots are found for a given slot component\n\t * If a string is provided, the same message will be used for all slot components\n\t * If an array is provided, each string in the array will be used as the errorMessage for the corresponding slot component\n\t */\n\terrorMessage?: string | string[];\n\t/**\n\t * @description When true, an AssertionError will be thrown if multiple slots are found for a given slot component\n\t * If a boolean is provided, the same value will be used for all slot components\n\t * If an array is provided, each boolean in the array will be used as the throwOnMultipleSlotMatch value for the corresponding slot component\n\t */\n\tthrowOnMultipleSlotMatch?: boolean | boolean[];\n};\n\n/**\n * @description The same as getSingleSlot, but for multiple slot components\n */\nexport const getMultipleSlots = <const TSlotComponents extends FunctionalComponent[]>(\n\tchildren: React.ReactNode,\n\tSlotComponents: TSlotComponents,\n\toptions?: MultipleSlotsOptions\n) => {\n\ttype SlotsType = { [Key in keyof TSlotComponents]: ReturnType<TSlotComponents[Key]> };\n\n\tconst { errorMessage, throwOnMultipleSlotMatch } = options ?? {};\n\n\tconst slots = SlotComponents.map((SlotComponent, index) =>\n\t\tgetSingleSlot(children, SlotComponent, {\n\t\t\terrorMessage: isArray(errorMessage) ? errorMessage[index] : errorMessage,\n\t\t\tthrowOnMultipleSlotMatch: isArray(throwOnMultipleSlotMatch)\n\t\t\t\t? throwOnMultipleSlotMatch[index]\n\t\t\t\t: throwOnMultipleSlotMatch,\n\t\t})\n\t) as SlotsType;\n\n\tconst regularChildren = getRegularChildren(children, SlotComponents);\n\n\treturn { regularChildren, 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 getRegularChildren = <TChildren extends React.ReactNode>(\n\tchildren: TChildren,\n\tSlotComponentOrComponents: FunctionalComponent | FunctionalComponent[]\n) => {\n\tconst actualChildren =\n\t\tisValidElement<InferProps<typeof ReactFragment>>(children) && children.type === ReactFragment\n\t\t\t? children.props.children\n\t\t\t: children;\n\n\tconst childrenArray = toArray<React.ReactNode>(actualChildren);\n\n\tconst regularChildren = childrenArray.filter(\n\t\t(child) => !matchesAnySlotComponent(child, toArray(SlotComponentOrComponents))\n\t);\n\n\treturn regularChildren as TChildren extends unknown[] ? TChildren : TChildren[];\n};\n","import { toArray } from \"@zayne-labs/toolkit-core\";\nimport type { UnionToIntersection, UnknownObject } from \"@zayne-labs/toolkit-type-helpers\";\nimport { Fragment as ReactFragment, isValidElement } from \"react\";\nimport type { InferProps } from \"./types\";\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(\"slot-component\");\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 actualChildren =\n\t\tisValidElement<InferProps<typeof ReactFragment>>(children) && children.type === ReactFragment\n\t\t\t? children.props.children\n\t\t\t: children;\n\n\tconst childrenArray = toArray<React.ReactNode>(actualChildren);\n\n\tconst slots: Record<string, React.ReactNode> & { default: React.ReactNode[] } = { default: [] };\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 SlotComponentType).slotSymbol === slotComponentSymbol\n\t\t\t&& Boolean((child.type as WithSlotSymbolAndName).slotName ?? child.props.name);\n\n\t\tconst isRegularElementWithSlotName =\n\t\t\tisValidElement<RegularElementProps>(child) && Boolean(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\n\t\t\t? ((child.type as WithSlotSymbolAndName).slotName ?? child.props.name)\n\t\t\t: 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 React.ReactNode = React.ReactNode,\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 Slot component created by createSlotComponent\n */\n\nexport const createSlotComponent = <TSlotComponentProps extends GetSlotComponentProps>() => {\n\tconst SlotComponent = (props: TSlotComponentProps) => props.children;\n\n\tSlotComponent.slotSymbol = slotComponentSymbol;\n\n\treturn SlotComponent;\n};\n\ntype SlotComponentType = ReturnType<typeof createSlotComponent>;\n\ntype WithSlotSymbolAndName<\n\tTSlotComponentProps extends Pick<GetSlotComponentProps, \"name\"> = Pick<GetSlotComponentProps, \"name\">,\n\tTActualProps extends UnknownObject = UnknownObject,\n> = {\n\t(props: Pick<GetSlotComponentProps, \"children\"> & TActualProps): React.ReactNode;\n\tslotName?: TSlotComponentProps[\"name\"];\n\tslotSymbol?: typeof slotComponentSymbol;\n};\n\nexport const withSlotSymbolAndName = <\n\tTSlotComponentProps extends Pick<GetSlotComponentProps, \"name\">,\n\tTActualProps extends UnknownObject = UnknownObject,\n>(\n\tname: TSlotComponentProps[\"name\"],\n\tSlotComponent: WithSlotSymbolAndName<TSlotComponentProps, TActualProps>\n) => {\n\t/* eslint-disable no-param-reassign -- This is necessary */\n\tSlotComponent.slotSymbol = slotComponentSymbol;\n\tSlotComponent.slotName = name;\n\t/* eslint-enable no-param-reassign -- This is necessary */\n\n\treturn SlotComponent;\n};\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.9.11",
4
+ "version": "0.9.14",
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.9.11",
54
- "@zayne-labs/toolkit-type-helpers": "0.9.11"
53
+ "@zayne-labs/toolkit-core": "0.9.14",
54
+ "@zayne-labs/toolkit-type-helpers": "0.9.14"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@arethetypeswrong/cli": "^0.17.4",