@zayne-labs/toolkit-react 0.9.38 → 0.9.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/utils/index.d.ts +2 -144
- package/dist/esm/utils/index.js +3 -117
- package/dist/esm/utils/index.js.map +1 -1
- package/package.json +5 -5
@@ -1,6 +1,5 @@
|
|
1
|
-
import * as react from 'react';
|
2
1
|
import { RefCallback } from 'react';
|
3
|
-
import { AnyFunction,
|
2
|
+
import { AnyFunction, UnionDiscriminator, AnyObject, Prettify } from '@zayne-labs/toolkit-type-helpers';
|
4
3
|
|
5
4
|
type PossibleRef<TRef extends HTMLElement> = React.Ref<TRef> | undefined;
|
6
5
|
/**
|
@@ -18,147 +17,6 @@ declare const useComposeRefs: <TRef extends HTMLElement>(...refs: Array<Possible
|
|
18
17
|
declare const composeTwoEventHandlers: (formerHandler: AnyFunction | undefined, latterHandler: AnyFunction | undefined) => (event: unknown) => unknown;
|
19
18
|
declare const composeEventHandlers: (...eventHandlerArray: Array<AnyFunction | undefined>) => (event: unknown) => unknown;
|
20
19
|
|
21
|
-
type FunctionalComponent<TProps extends UnknownObject = never> = React.FunctionComponent<TProps>;
|
22
|
-
/**
|
23
|
-
* @description Checks if a react child (within the children array) matches the provided SlotComponent using multiple matching strategies:
|
24
|
-
* 1. Matches by slot symbol property
|
25
|
-
* 2. Matches by component name
|
26
|
-
*/
|
27
|
-
declare const matchesSlotComponent: (child: React.ReactNode, SlotComponent: FunctionalComponent) => boolean;
|
28
|
-
/**
|
29
|
-
* @description Checks if a react child (within the children array) matches any of the provided SlotComponents.
|
30
|
-
*/
|
31
|
-
declare const matchesAnySlotComponent: (child: React.ReactNode, SlotComponents: FunctionalComponent[]) => boolean;
|
32
|
-
type SlotOptions = {
|
33
|
-
/**
|
34
|
-
* @description If false, the function will bail out early and return null as the slot.
|
35
|
-
* @default true
|
36
|
-
*/
|
37
|
-
condition?: boolean;
|
38
|
-
/**
|
39
|
-
* @description The error message to throw when multiple slots are found for a given slot component
|
40
|
-
*/
|
41
|
-
errorMessage?: string;
|
42
|
-
/**
|
43
|
-
* @description When true, an AssertionError will be thrown if multiple slots are found for a given slot component
|
44
|
-
*/
|
45
|
-
throwOnMultipleSlotMatch?: boolean;
|
46
|
-
};
|
47
|
-
/**
|
48
|
-
* @description Retrieves a single slot element from a collection of React children that matches the provided SlotComponent component.
|
49
|
-
*
|
50
|
-
* @throws { AssertionError } when throwOnMultipleSlotMatch is true and multiple slots are found
|
51
|
-
*/
|
52
|
-
declare const getSingleSlot: (children: React.ReactNode, SlotComponent: FunctionalComponent, options?: SlotOptions) => react.ReactNode;
|
53
|
-
type MultipleSlotsOptions = {
|
54
|
-
/**
|
55
|
-
* @description If false, the function will bail out early and return the regularChildren with the actual children and an empty slots array.
|
56
|
-
* @default true
|
57
|
-
*/
|
58
|
-
condition?: boolean;
|
59
|
-
/**
|
60
|
-
* @description The error message to throw when multiple slots are found for a given slot component
|
61
|
-
* If a string is provided, the same message will be used for all slot components
|
62
|
-
* If an array is provided, each string in the array will be used as the errorMessage for the corresponding slot component
|
63
|
-
*/
|
64
|
-
errorMessage?: string | string[];
|
65
|
-
/**
|
66
|
-
* @description When true, an AssertionError will be thrown if multiple slots are found for a given slot component
|
67
|
-
* If a boolean is provided, the same value will be used for all slot components
|
68
|
-
* If an array is provided, each boolean in the array will be used as the throwOnMultipleSlotMatch value for the corresponding slot component
|
69
|
-
*/
|
70
|
-
throwOnMultipleSlotMatch?: boolean | boolean[];
|
71
|
-
};
|
72
|
-
type GetMultipleSlotsResult<TSlotComponents extends FunctionalComponent[]> = {
|
73
|
-
regularChildren: React.ReactNode[];
|
74
|
-
slots: {
|
75
|
-
[Key in keyof TSlotComponents]: ReturnType<TSlotComponents[Key]>;
|
76
|
-
};
|
77
|
-
};
|
78
|
-
/**
|
79
|
-
* @description The same as getSingleSlot, but for multiple slot components
|
80
|
-
*/
|
81
|
-
declare const getMultipleSlots: <const TSlotComponents extends FunctionalComponent[]>(children: React.ReactNode, SlotComponents: TSlotComponents, options?: MultipleSlotsOptions) => Prettify<GetMultipleSlotsResult<TSlotComponents>>;
|
82
|
-
/**
|
83
|
-
* @description Returns all children that are not slot elements (i.e., don't match any of the provided slot components)
|
84
|
-
*/
|
85
|
-
declare const getRegularChildren: (children: React.ReactNode, SlotComponentOrComponents: FunctionalComponent | FunctionalComponent[], options?: Pick<SlotOptions, "condition">) => react.ReactNode[];
|
86
|
-
|
87
|
-
/**
|
88
|
-
* Maps slot names to their corresponding children types
|
89
|
-
*/
|
90
|
-
type GetSlotMapResult<TSlotComponentProps extends GetSlotComponentProps> = UnionToIntersection$1<{
|
91
|
-
[TName in keyof TSlotComponentProps as TSlotComponentProps["name"]]: TSlotComponentProps["children"];
|
92
|
-
}> & {
|
93
|
-
default: React.ReactNode[];
|
94
|
-
};
|
95
|
-
/**
|
96
|
-
* Symbol used to identify SlotComponent instances
|
97
|
-
*/
|
98
|
-
declare const slotComponentSymbol: unique symbol;
|
99
|
-
type GetSlotMapOptions = {
|
100
|
-
/**
|
101
|
-
* If false, the function will bail out early and return only the default slot with the actual children.
|
102
|
-
* @default true
|
103
|
-
*/
|
104
|
-
condition?: boolean;
|
105
|
-
};
|
106
|
-
/**
|
107
|
-
* @description Creates a map of named slots from React children. Returns an object mapping slot names to their children,
|
108
|
-
* with a default slot for unmatched children.
|
109
|
-
*
|
110
|
-
* @example
|
111
|
-
* ```tsx
|
112
|
-
* import { type GetSlotComponentProps, SlotComponent } from "@zayne-labs/toolkit-react/utils"
|
113
|
-
*
|
114
|
-
* type SlotProps = GetSlotComponentProps<"header" | "footer">;
|
115
|
-
*
|
116
|
-
* function Parent({ children }: { children: React.ReactNode }) {
|
117
|
-
* const slots = getSlotMap<SlotProps>(children);
|
118
|
-
*
|
119
|
-
* return (
|
120
|
-
* <div>
|
121
|
-
* <header>{slots.header}</header>
|
122
|
-
* <main>{slots.default}</main>
|
123
|
-
* <footer>{slots.footer}</footer>
|
124
|
-
* </div>
|
125
|
-
* );
|
126
|
-
* }
|
127
|
-
* ```
|
128
|
-
*
|
129
|
-
* Usage:
|
130
|
-
* ```tsx
|
131
|
-
* <Parent>
|
132
|
-
* <SlotComponent name="header">Header Content</SlotComponent>
|
133
|
-
* <div>Random stuff</div>
|
134
|
-
* <SlotComponent name="footer">Footer Content</SlotComponent>
|
135
|
-
* </Parent>
|
136
|
-
* ```
|
137
|
-
*/
|
138
|
-
declare const getSlotMap: <TSlotComponentProps extends GetSlotComponentProps>(children: React.ReactNode, options?: GetSlotMapOptions) => Prettify<GetSlotMapResult<TSlotComponentProps>>;
|
139
|
-
/**
|
140
|
-
* @description Produce props for the SlotComponent
|
141
|
-
*/
|
142
|
-
type GetSlotComponentProps<TName extends string = string, TChildren extends React.ReactNode = React.ReactNode> = {
|
143
|
-
/** Content to render in the slot */
|
144
|
-
children: TChildren;
|
145
|
-
/** Name of the slot where content should be rendered */
|
146
|
-
name: TName;
|
147
|
-
};
|
148
|
-
/**
|
149
|
-
* @description Slot component created by createSlotComponent
|
150
|
-
*/
|
151
|
-
declare const createSlotComponent: <TSlotComponentProps extends GetSlotComponentProps>() => {
|
152
|
-
(props: TSlotComponentProps): react.ReactNode;
|
153
|
-
slotSymbol: typeof slotComponentSymbol;
|
154
|
-
};
|
155
|
-
type SlotWithNameAndSymbol<TSlotComponentProps extends Pick<GetSlotComponentProps, "name"> = Pick<GetSlotComponentProps, "name">, TActualProps extends UnknownObject = UnknownObject> = {
|
156
|
-
(props: Pick<GetSlotComponentProps, "children"> & TActualProps): React.ReactNode;
|
157
|
-
readonly slotName?: TSlotComponentProps["name"];
|
158
|
-
readonly slotSymbol?: symbol;
|
159
|
-
};
|
160
|
-
declare const withSlotNameAndSymbol: <TSlotComponentProps extends Pick<GetSlotComponentProps, "name">, TActualProps extends UnknownObject = UnknownObject>(name: TSlotComponentProps["name"], SlotComponent?: SlotWithNameAndSymbol<TSlotComponentProps, TActualProps>) => SlotWithNameAndSymbol<TSlotComponentProps, TActualProps>;
|
161
|
-
|
162
20
|
type UnionToIntersection<TUnion> = (TUnion extends unknown ? (param: TUnion) => void : "") extends (param: infer TParam) => void ? TParam : "";
|
163
21
|
/**
|
164
22
|
* Merges multiple sets of React props.
|
@@ -205,4 +63,4 @@ type InferRemainingProps<TElement extends React.ElementType, TProps> = Omit<Infe
|
|
205
63
|
type MergedGivenPropsWithAs<TElement extends React.ElementType, TProps> = Prettify<Omit<AsProp<TElement>, keyof TProps> & TProps>;
|
206
64
|
type PolymorphicProps<TElement extends React.ElementType, TProps extends AnyObject = NonNullable<unknown>> = MergedGivenPropsWithAs<TElement, TProps> & InferRemainingProps<TElement, TProps>;
|
207
65
|
|
208
|
-
export { type AsProp, type CssWithCustomProperties, type DiscriminatedRenderProps, type ForwardedRefType, type
|
66
|
+
export { type AsProp, type CssWithCustomProperties, type DiscriminatedRenderProps, type ForwardedRefType, type InferProps, type PolymorphicProps, type StateSetter, composeEventHandlers, composeRefs, composeTwoEventHandlers, mergeProps, mergeTwoProps, setRef, useComposeRefs };
|
package/dist/esm/utils/index.js
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
import { isFunction,
|
2
|
-
import { useCallback
|
3
|
-
import { toArray } from '@zayne-labs/toolkit-core';
|
1
|
+
import { isFunction, isObject } from '@zayne-labs/toolkit-type-helpers';
|
2
|
+
import { useCallback } from 'react';
|
4
3
|
|
5
4
|
// src/utils/composeRefs.ts
|
6
5
|
var setRef = (ref, node) => {
|
@@ -55,119 +54,6 @@ var composeEventHandlers = (...eventHandlerArray) => {
|
|
55
54
|
};
|
56
55
|
return mergedEventHandler;
|
57
56
|
};
|
58
|
-
var isWithSlotSymbol = (component) => {
|
59
|
-
return "slotSymbol" in component && Boolean(component.slotSymbol);
|
60
|
-
};
|
61
|
-
var isWithSlotReference = (component) => {
|
62
|
-
return "slotReference" in component && Boolean(component.slotReference);
|
63
|
-
};
|
64
|
-
var matchesSlotComponent = (child, SlotComponent) => {
|
65
|
-
if (!isValidElement(child) || !isFunction(child.type)) {
|
66
|
-
return false;
|
67
|
-
}
|
68
|
-
const resolvedChildType = isWithSlotReference(child.type) ? child.type.slotReference : child.type;
|
69
|
-
const hasMatchingSlotSymbol = isWithSlotSymbol(resolvedChildType) && isWithSlotSymbol(SlotComponent) && resolvedChildType.slotSymbol === SlotComponent.slotSymbol;
|
70
|
-
if (hasMatchingSlotSymbol) {
|
71
|
-
return true;
|
72
|
-
}
|
73
|
-
if (child.type.name === SlotComponent.name) {
|
74
|
-
return true;
|
75
|
-
}
|
76
|
-
return false;
|
77
|
-
};
|
78
|
-
var matchesAnySlotComponent = (child, SlotComponents) => {
|
79
|
-
const matchesSlot = SlotComponents.some((SlotComponent) => matchesSlotComponent(child, SlotComponent));
|
80
|
-
return matchesSlot;
|
81
|
-
};
|
82
|
-
var calculateSlotOccurrences = (childrenArray, SlotComponent) => {
|
83
|
-
let count = 0;
|
84
|
-
for (const child of childrenArray) {
|
85
|
-
if (!matchesSlotComponent(child, SlotComponent)) continue;
|
86
|
-
count += 1;
|
87
|
-
}
|
88
|
-
return count;
|
89
|
-
};
|
90
|
-
var getSingleSlot = (children, SlotComponent, options = {}) => {
|
91
|
-
const {
|
92
|
-
condition = true,
|
93
|
-
errorMessage = "Only one instance of the SlotComponent is allowed",
|
94
|
-
throwOnMultipleSlotMatch = false
|
95
|
-
} = options;
|
96
|
-
if (!condition) {
|
97
|
-
return null;
|
98
|
-
}
|
99
|
-
const actualChildren = isValidElement(children) && children.type === Fragment ? children.props.children : children;
|
100
|
-
const childrenArray = toArray(actualChildren);
|
101
|
-
const shouldThrow = throwOnMultipleSlotMatch && calculateSlotOccurrences(childrenArray, SlotComponent) > 1;
|
102
|
-
if (shouldThrow) {
|
103
|
-
throw new AssertionError(errorMessage);
|
104
|
-
}
|
105
|
-
const slotElement = childrenArray.find((child) => matchesSlotComponent(child, SlotComponent));
|
106
|
-
return slotElement;
|
107
|
-
};
|
108
|
-
var getMultipleSlots = (children, SlotComponents, options) => {
|
109
|
-
const { condition = true, errorMessage, throwOnMultipleSlotMatch } = options ?? {};
|
110
|
-
if (!condition) {
|
111
|
-
return { regularChildren: children, slots: [] };
|
112
|
-
}
|
113
|
-
const slots = SlotComponents.map(
|
114
|
-
(SlotComponent, index) => getSingleSlot(children, SlotComponent, {
|
115
|
-
errorMessage: isArray(errorMessage) ? errorMessage[index] : errorMessage,
|
116
|
-
throwOnMultipleSlotMatch: isArray(throwOnMultipleSlotMatch) ? throwOnMultipleSlotMatch[index] : throwOnMultipleSlotMatch
|
117
|
-
})
|
118
|
-
);
|
119
|
-
const regularChildren = getRegularChildren(children, SlotComponents);
|
120
|
-
return { regularChildren, slots };
|
121
|
-
};
|
122
|
-
var getRegularChildren = (children, SlotComponentOrComponents, options) => {
|
123
|
-
const { condition = true } = options ?? {};
|
124
|
-
if (!condition) {
|
125
|
-
return [];
|
126
|
-
}
|
127
|
-
const actualChildren = isValidElement(children) && children.type === Fragment ? children.props.children : children;
|
128
|
-
const childrenArray = toArray(actualChildren);
|
129
|
-
const regularChildren = childrenArray.filter(
|
130
|
-
(child) => !matchesAnySlotComponent(child, toArray(SlotComponentOrComponents))
|
131
|
-
);
|
132
|
-
return regularChildren;
|
133
|
-
};
|
134
|
-
var slotComponentSymbol = Symbol("slot-component");
|
135
|
-
var getSlotMap = (children, options) => {
|
136
|
-
const { condition = true } = options ?? {};
|
137
|
-
if (!condition) {
|
138
|
-
return { default: children };
|
139
|
-
}
|
140
|
-
const actualChildren = isValidElement(children) && children.type === Fragment ? children.props.children : children;
|
141
|
-
const childrenArray = toArray(actualChildren);
|
142
|
-
const slots = {
|
143
|
-
default: []
|
144
|
-
};
|
145
|
-
for (const child of childrenArray) {
|
146
|
-
if (!isValidElement(child) || !isFunction(child.type)) {
|
147
|
-
slots.default.push(child);
|
148
|
-
continue;
|
149
|
-
}
|
150
|
-
const resolvedChildType = Object.hasOwn(child.type, "slotReference") ? child.type.slotReference : child.type;
|
151
|
-
const isSlotElement = resolvedChildType.slotSymbol === slotComponentSymbol && Boolean(resolvedChildType.slotName ?? child.props.name);
|
152
|
-
if (!isSlotElement) {
|
153
|
-
slots.default.push(child);
|
154
|
-
continue;
|
155
|
-
}
|
156
|
-
const slotName = resolvedChildType.slotName ?? child.props.name;
|
157
|
-
slots[slotName] = child;
|
158
|
-
}
|
159
|
-
return slots;
|
160
|
-
};
|
161
|
-
var createSlotComponent = () => {
|
162
|
-
const SlotComponent = (props) => props.children;
|
163
|
-
SlotComponent.slotSymbol = slotComponentSymbol;
|
164
|
-
return SlotComponent;
|
165
|
-
};
|
166
|
-
var withSlotNameAndSymbol = (name, SlotComponent = (props) => props.children) => {
|
167
|
-
SlotComponent.slotSymbol = slotComponentSymbol;
|
168
|
-
SlotComponent.slotName = name;
|
169
|
-
return SlotComponent;
|
170
|
-
};
|
171
57
|
var isEventHandler = (key, value) => {
|
172
58
|
const thirdCharCode = key.codePointAt(2);
|
173
59
|
if (!isFunction(value) || thirdCharCode === void 0) {
|
@@ -232,6 +118,6 @@ var mergeProps = (...propsObjectArray) => {
|
|
232
118
|
return accumulatedProps;
|
233
119
|
};
|
234
120
|
|
235
|
-
export { composeEventHandlers, composeRefs, composeTwoEventHandlers,
|
121
|
+
export { composeEventHandlers, composeRefs, composeTwoEventHandlers, mergeProps, mergeTwoProps, setRef, useComposeRefs };
|
236
122
|
//# sourceMappingURL=index.js.map
|
237
123
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/utils/composeRefs.ts","../../../src/utils/composeEventHandlers.ts","../../../src/utils/getSlot.ts","../../../src/utils/getSlotMap.ts","../../../src/utils/mergeTwoProps.ts","../../../src/utils/mergeProps.ts"],"names":["result","isFunction","ReactFragment","isValidElement","toArray"],"mappings":";;;;;AAUa,IAAA,MAAA,GAAS,CACrB,GAAA,EACA,IACmC,KAAA;AACnC,EAAA,IAAI,CAAC,GAAK,EAAA;AAEV,EAAI,IAAA,UAAA,CAAW,GAAG,CAAG,EAAA;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G1B,EAAA,IAAI,mBAA4C,EAAC;AAEjD,EAAA,KAAA,MAAW,eAAe,gBAAkB,EAAA;AAC3C,IAAA,IAAI,CAAC,WAAa,EAAA;AAElB,IAAmB,gBAAA,GAAA,aAAA,CAAc,kBAAkB,WAAW,CAAA;AAAA;AAG/D,EAAO,OAAA,gBAAA;AACR","file":"index.js","sourcesContent":["import { isFunction } from \"@zayne-labs/toolkit-type-helpers\";\nimport { type RefCallback, useCallback } from \"react\";\n\ntype PossibleRef<TRef extends HTMLElement> = 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 extends HTMLElement>(\n\tref: PossibleRef<TRef>,\n\tnode: TRef | null\n): 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\t...refs: 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\treturn cleanupFn;\n\t};\n\n\treturn mergedRefCallBack;\n};\n\nexport const useComposeRefs = <TRef extends HTMLElement>(...refs: Array<PossibleRef<TRef>>) => {\n\t// eslint-disable-next-line react-hooks/exhaustive-deps -- Allow\n\tconst mergedRef = useCallback(() => composeRefs(...refs), refs);\n\n\treturn mergedRef;\n};\n","import { type AnyFunction, isObject } from \"@zayne-labs/toolkit-type-helpers\";\n\nconst isSyntheticEvent = (event: unknown): event is React.SyntheticEvent => {\n\treturn isObject(event) && Object.hasOwn(event, \"nativeEvent\");\n};\n\nexport const composeTwoEventHandlers = (\n\tformerHandler: AnyFunction | undefined,\n\tlatterHandler: AnyFunction | undefined\n) => {\n\tconst mergedEventHandler = (event: unknown) => {\n\t\tif (isSyntheticEvent(event)) {\n\t\t\tconst result = latterHandler?.(event) as unknown;\n\n\t\t\tif (!event.defaultPrevented) {\n\t\t\t\tformerHandler?.(event);\n\t\t\t}\n\n\t\t\treturn result;\n\t\t}\n\n\t\tconst result = latterHandler?.(event) as unknown;\n\t\tformerHandler?.(event);\n\t\treturn result;\n\t};\n\n\treturn mergedEventHandler;\n};\n\nexport const composeEventHandlers = (...eventHandlerArray: Array<AnyFunction | undefined>) => {\n\tconst mergedEventHandler = (event: unknown) => {\n\t\tif (eventHandlerArray.length === 0) return;\n\n\t\tif (eventHandlerArray.length === 1) {\n\t\t\treturn eventHandlerArray[0]?.(event) as unknown;\n\t\t}\n\n\t\tlet accumulatedHandlers: AnyFunction | undefined;\n\n\t\tfor (const eventHandler of eventHandlerArray) {\n\t\t\tif (!eventHandler) continue;\n\n\t\t\taccumulatedHandlers = composeTwoEventHandlers(accumulatedHandlers, eventHandler);\n\t\t}\n\n\t\treturn accumulatedHandlers?.(event) as unknown;\n\t};\n\n\treturn mergedEventHandler;\n};\n","import { toArray } from \"@zayne-labs/toolkit-core\";\nimport {\n\ttype AnyFunction,\n\tAssertionError,\n\ttype Prettify,\n\ttype UnknownObject,\n\tisArray,\n\tisFunction,\n} 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\nconst isWithSlotSymbol = <TFunction extends AnyFunction>(\n\tcomponent: TFunction\n): component is Record<\"slotSymbol\", unknown> & TFunction => {\n\treturn \"slotSymbol\" in component && Boolean(component.slotSymbol);\n};\n\nconst isWithSlotReference = <TFunction extends AnyFunction>(\n\tcomponent: TFunction\n): component is Record<\"slotReference\", unknown> & TFunction => {\n\treturn \"slotReference\" in component && Boolean(component.slotReference);\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) || !isFunction(child.type)) {\n\t\treturn false;\n\t}\n\n\tconst resolvedChildType = isWithSlotReference(child.type)\n\t\t? (child.type.slotReference as FunctionalComponent)\n\t\t: child.type;\n\n\tconst hasMatchingSlotSymbol =\n\t\tisWithSlotSymbol(resolvedChildType)\n\t\t&& isWithSlotSymbol(SlotComponent)\n\t\t&& resolvedChildType.slotSymbol === SlotComponent.slotSymbol;\n\n\tif (hasMatchingSlotSymbol) {\n\t\treturn true;\n\t}\n\n\tif (child.type.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 If false, the function will bail out early and return null as the slot.\n\t * @default true\n\t */\n\tcondition?: boolean;\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 given 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\tcondition = true,\n\t\terrorMessage = \"Only one instance of the SlotComponent is allowed\",\n\t\tthrowOnMultipleSlotMatch = false,\n\t} = options;\n\n\tif (!condition) {\n\t\treturn null;\n\t}\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 If false, the function will bail out early and return the regularChildren with the actual children and an empty slots array.\n\t * @default true\n\t */\n\tcondition?: boolean;\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\ntype GetMultipleSlotsResult<TSlotComponents extends FunctionalComponent[]> = {\n\tregularChildren: React.ReactNode[];\n\tslots: { [Key in keyof TSlotComponents]: ReturnType<TSlotComponents[Key]> };\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): Prettify<GetMultipleSlotsResult<TSlotComponents>> => {\n\tconst { condition = true, errorMessage, throwOnMultipleSlotMatch } = options ?? {};\n\n\tif (!condition) {\n\t\treturn { regularChildren: children as never, slots: [] } as GetMultipleSlotsResult<TSlotComponents>;\n\t}\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);\n\n\tconst regularChildren = getRegularChildren(children, SlotComponents);\n\n\treturn { regularChildren, slots } as GetMultipleSlotsResult<TSlotComponents>;\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 = (\n\tchildren: React.ReactNode,\n\tSlotComponentOrComponents: FunctionalComponent | FunctionalComponent[],\n\toptions?: Pick<SlotOptions, \"condition\">\n) => {\n\tconst { condition = true } = options ?? {};\n\n\tif (!condition) {\n\t\treturn [];\n\t}\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;\n};\n","import { toArray } from \"@zayne-labs/toolkit-core\";\nimport {\n\ttype AnyFunction,\n\ttype Prettify,\n\ttype UnionToIntersection,\n\ttype UnknownObject,\n\tisFunction,\n} 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}> & { default: React.ReactNode[] };\n\n/**\n * Symbol used to identify SlotComponent instances\n */\nexport const slotComponentSymbol = Symbol(\"slot-component\");\n\ntype GetSlotMapOptions = {\n\t/**\n\t * If false, the function will bail out early and return only the default slot with the actual children.\n\t * @default true\n\t */\n\tcondition?: boolean;\n};\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\toptions?: GetSlotMapOptions\n): Prettify<GetSlotMapResult<TSlotComponentProps>> => {\n\tconst { condition = true } = options ?? {};\n\n\tif (!condition) {\n\t\treturn { default: children } as GetSlotMapResult<TSlotComponentProps>;\n\t}\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[] } = {\n\t\tdefault: [],\n\t};\n\n\tfor (const child of childrenArray) {\n\t\tif (!isValidElement<TSlotComponentProps>(child) || !isFunction(child.type)) {\n\t\t\tslots.default.push(child);\n\t\t\tcontinue;\n\t\t}\n\n\t\ttype ElementWithSlotReference = AnyFunction & { slotReference: SlotWithNameAndSymbol };\n\n\t\tconst resolvedChildType = Object.hasOwn(child.type, \"slotReference\")\n\t\t\t? (child.type as ElementWithSlotReference).slotReference\n\t\t\t: (child.type as SlotWithNameAndSymbol);\n\n\t\tconst isSlotElement =\n\t\t\tresolvedChildType.slotSymbol === slotComponentSymbol\n\t\t\t&& Boolean(resolvedChildType.slotName ?? child.props.name);\n\n\t\tif (!isSlotElement) {\n\t\t\tslots.default.push(child);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst slotName = resolvedChildType.slotName ?? child.props.name;\n\n\t\tslots[slotName] = child;\n\t}\n\n\treturn slots as GetSlotMapResult<TSlotComponentProps>;\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 SlotWithNameAndSymbol<\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\treadonly slotName?: TSlotComponentProps[\"name\"];\n\treadonly slotSymbol?: symbol;\n};\n\nexport const withSlotNameAndSymbol = <\n\tTSlotComponentProps extends Pick<GetSlotComponentProps, \"name\">,\n\tTActualProps extends UnknownObject = UnknownObject,\n>(\n\tname: TSlotComponentProps[\"name\"],\n\tSlotComponent: SlotWithNameAndSymbol<TSlotComponentProps, TActualProps> = (props) => props.children\n) => {\n\t/* eslint-disable no-param-reassign -- This is necessary */\n\t// @ts-expect-error -- This is necessary for the time being, to prevent type errors and accidental overrides on consumer side\n\tSlotComponent.slotSymbol = slotComponentSymbol;\n\t// @ts-expect-error -- This is necessary for the time being, to prevent type errors and accidental overrides on consumer side\n\tSlotComponent.slotName = name;\n\t/* eslint-enable no-param-reassign -- This is necessary */\n\n\treturn SlotComponent;\n};\n","import { type AnyFunction, isFunction } from \"@zayne-labs/toolkit-type-helpers\";\nimport { composeTwoEventHandlers } from \"./composeEventHandlers\";\n\n// == This approach is more efficient than using a regex.\nconst isEventHandler = (key: string, value: unknown): value is AnyFunction => {\n\tconst thirdCharCode = key.codePointAt(2);\n\n\tif (!isFunction(value) || thirdCharCode === undefined) {\n\t\treturn false;\n\t}\n\n\tconst isHandler = key.startsWith(\"on\") && thirdCharCode >= 65 /* A */ && thirdCharCode <= 90; /* Z */\n\n\treturn isHandler;\n};\n\nconst mergeTwoClassNames = (formerClassName: string | undefined, latterClassName: string | undefined) => {\n\tif (!latterClassName || !formerClassName) {\n\t\t// eslint-disable-next-line ts-eslint/prefer-nullish-coalescing -- Logical OR is fit for this case\n\t\treturn latterClassName || formerClassName;\n\t}\n\n\t// eslint-disable-next-line prefer-template -- String concatenation is more performant than template literals in this case\n\treturn formerClassName + \" \" + latterClassName;\n};\n\nexport const mergeTwoProps = <TProps extends Record<never, never>>(\n\tformerProps: TProps | undefined,\n\tlatterProps: TProps | undefined\n): TProps => {\n\t// == If no props are provided, return an empty object\n\tif (!latterProps || !formerProps) {\n\t\treturn latterProps ?? formerProps ?? ({} as TProps);\n\t}\n\n\tconst propsAccumulator = { ...formerProps } as Record<string, unknown>;\n\n\tfor (const latterPropName of Object.keys(latterProps)) {\n\t\tconst formerPropValue = (formerProps as Record<string, unknown>)[latterPropName];\n\t\tconst latterPropValue = (latterProps as Record<string, unknown>)[latterPropName];\n\n\t\t// == If the prop is `className` or `class`, we merge them\n\t\tif (latterPropName === \"className\" || latterPropName === \"class\") {\n\t\t\tpropsAccumulator[latterPropName] = mergeTwoClassNames(\n\t\t\t\tformerPropValue as string,\n\t\t\t\tlatterPropValue as string\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\n\t\t// == If the prop is `style`, we merge them\n\t\tif (latterPropName === \"style\") {\n\t\t\tpropsAccumulator[latterPropName] = {\n\t\t\t\t...(formerPropValue as object),\n\t\t\t\t...(latterPropValue as object),\n\t\t\t};\n\t\t\tcontinue;\n\t\t}\n\n\t\t// == If the handler exists on both, we compose them\n\t\tif (isEventHandler(latterPropName, latterPropValue)) {\n\t\t\tpropsAccumulator[latterPropName] = composeTwoEventHandlers(\n\t\t\t\tformerPropValue as AnyFunction | undefined,\n\t\t\t\tlatterPropValue\n\t\t\t);\n\n\t\t\tcontinue;\n\t\t}\n\n\t\t// == latterProps override by default\n\t\tpropsAccumulator[latterPropName] = latterPropValue;\n\t}\n\n\treturn propsAccumulator as TProps;\n};\n","import { mergeTwoProps } from \"./mergeTwoProps\";\n\ntype UnionToIntersection<TUnion> = (TUnion extends unknown ? (param: TUnion) => void : \"\") extends (\n\tparam: infer TParam\n) => void\n\t? TParam\n\t: \"\";\n\n/**\n * Merges multiple sets of React props.\n *\n * - It follows the Object.assign pattern where the rightmost object's fields overwrite\n * the conflicting ones from others. This doesn't apply to event handlers, `className` and `style` props.\n * - Event handlers are merged such that they are called in sequence (the rightmost one being called first),\n * and allows the user to prevent the previous event handlers from being executed by calling the `preventDefault` method.\n * - It also merges the `className` and `style` props, whereby the classes are concatenated\n * and the rightmost styles overwrite the previous ones.\n *\n * @important **`ref` is not merged.**\n * @param props props to merge.\n * @returns the merged props.\n */\n\nconst mergeProps = <TProps extends Record<never, never>>(\n\t...propsObjectArray: Array<TProps | undefined>\n): UnionToIntersection<TProps> => {\n\tif (propsObjectArray.length === 0) {\n\t\treturn {} as never;\n\t}\n\n\tif (propsObjectArray.length === 1) {\n\t\treturn propsObjectArray[0] as never;\n\t}\n\n\tlet accumulatedProps: Record<string, unknown> = {};\n\n\tfor (const propsObject of propsObjectArray) {\n\t\tif (!propsObject) continue;\n\n\t\taccumulatedProps = mergeTwoProps(accumulatedProps, propsObject);\n\t}\n\n\treturn accumulatedProps as never;\n};\n\nexport { mergeProps };\n"]}
|
1
|
+
{"version":3,"sources":["../../../src/utils/composeRefs.ts","../../../src/utils/composeEventHandlers.ts","../../../src/utils/mergeTwoProps.ts","../../../src/utils/mergeProps.ts"],"names":["result","isFunction"],"mappings":";;;;AAUa,IAAA,MAAA,GAAS,CACrB,GAAA,EACA,IACmC,KAAA;AACnC,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,IACvB,IACoB,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,CAAA;AAEvE,IAAO,OAAA,SAAA;AAAA,GACR;AAEA,EAAO,OAAA,iBAAA;AACR;AAEa,IAAA,cAAA,GAAiB,IAA8B,IAAmC,KAAA;AAE9F,EAAA,MAAM,YAAY,WAAY,CAAA,MAAM,YAAY,GAAG,IAAI,GAAG,IAAI,CAAA;AAE9D,EAAO,OAAA,SAAA;AACR;AC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AAA,OACL;AACA,MAAA;AAAA;AAID,IAAI,IAAA,cAAA,CAAe,cAAgB,EAAA,eAAe,CAAG,EAAA;AACpD,MAAA,gBAAA,CAAiB,cAAc,CAAI,GAAA,uBAAA;AAAA,QAClC,eAAA;AAAA,QACA;AAAA,OACD;AAEA,MAAA;AAAA;AAID,IAAA,gBAAA,CAAiB,cAAc,CAAI,GAAA,eAAA;AAAA;AAGpC,EAAO,OAAA,gBAAA;AACR;;;ACnDM,IAAA,UAAA,GAAa,IACf,gBAC8B,KAAA;AACjC,EAAI,IAAA,gBAAA,CAAiB,WAAW,CAAG,EAAA;AAClC,IAAA,OAAO,EAAC;AAAA;AAGT,EAAI,IAAA,gBAAA,CAAiB,WAAW,CAAG,EAAA;AAClC,IAAA,OAAO,iBAAiB,CAAC,CAAA;AAAA;AAG1B,EAAA,IAAI,mBAA4C,EAAC;AAEjD,EAAA,KAAA,MAAW,eAAe,gBAAkB,EAAA;AAC3C,IAAA,IAAI,CAAC,WAAa,EAAA;AAElB,IAAmB,gBAAA,GAAA,aAAA,CAAc,kBAAkB,WAAW,CAAA;AAAA;AAG/D,EAAO,OAAA,gBAAA;AACR","file":"index.js","sourcesContent":["import { isFunction } from \"@zayne-labs/toolkit-type-helpers\";\nimport { type RefCallback, useCallback } from \"react\";\n\ntype PossibleRef<TRef extends HTMLElement> = 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 extends HTMLElement>(\n\tref: PossibleRef<TRef>,\n\tnode: TRef | null\n): 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\t...refs: 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\treturn cleanupFn;\n\t};\n\n\treturn mergedRefCallBack;\n};\n\nexport const useComposeRefs = <TRef extends HTMLElement>(...refs: Array<PossibleRef<TRef>>) => {\n\t// eslint-disable-next-line react-hooks/exhaustive-deps -- Allow\n\tconst mergedRef = useCallback(() => composeRefs(...refs), refs);\n\n\treturn mergedRef;\n};\n","import { type AnyFunction, isObject } from \"@zayne-labs/toolkit-type-helpers\";\n\nconst isSyntheticEvent = (event: unknown): event is React.SyntheticEvent => {\n\treturn isObject(event) && Object.hasOwn(event, \"nativeEvent\");\n};\n\nexport const composeTwoEventHandlers = (\n\tformerHandler: AnyFunction | undefined,\n\tlatterHandler: AnyFunction | undefined\n) => {\n\tconst mergedEventHandler = (event: unknown) => {\n\t\tif (isSyntheticEvent(event)) {\n\t\t\tconst result = latterHandler?.(event) as unknown;\n\n\t\t\tif (!event.defaultPrevented) {\n\t\t\t\tformerHandler?.(event);\n\t\t\t}\n\n\t\t\treturn result;\n\t\t}\n\n\t\tconst result = latterHandler?.(event) as unknown;\n\t\tformerHandler?.(event);\n\t\treturn result;\n\t};\n\n\treturn mergedEventHandler;\n};\n\nexport const composeEventHandlers = (...eventHandlerArray: Array<AnyFunction | undefined>) => {\n\tconst mergedEventHandler = (event: unknown) => {\n\t\tif (eventHandlerArray.length === 0) return;\n\n\t\tif (eventHandlerArray.length === 1) {\n\t\t\treturn eventHandlerArray[0]?.(event) as unknown;\n\t\t}\n\n\t\tlet accumulatedHandlers: AnyFunction | undefined;\n\n\t\tfor (const eventHandler of eventHandlerArray) {\n\t\t\tif (!eventHandler) continue;\n\n\t\t\taccumulatedHandlers = composeTwoEventHandlers(accumulatedHandlers, eventHandler);\n\t\t}\n\n\t\treturn accumulatedHandlers?.(event) as unknown;\n\t};\n\n\treturn mergedEventHandler;\n};\n","import { type AnyFunction, isFunction } from \"@zayne-labs/toolkit-type-helpers\";\nimport { composeTwoEventHandlers } from \"./composeEventHandlers\";\n\n// == This approach is more efficient than using a regex.\nconst isEventHandler = (key: string, value: unknown): value is AnyFunction => {\n\tconst thirdCharCode = key.codePointAt(2);\n\n\tif (!isFunction(value) || thirdCharCode === undefined) {\n\t\treturn false;\n\t}\n\n\tconst isHandler = key.startsWith(\"on\") && thirdCharCode >= 65 /* A */ && thirdCharCode <= 90; /* Z */\n\n\treturn isHandler;\n};\n\nconst mergeTwoClassNames = (formerClassName: string | undefined, latterClassName: string | undefined) => {\n\tif (!latterClassName || !formerClassName) {\n\t\t// eslint-disable-next-line ts-eslint/prefer-nullish-coalescing -- Logical OR is fit for this case\n\t\treturn latterClassName || formerClassName;\n\t}\n\n\t// eslint-disable-next-line prefer-template -- String concatenation is more performant than template literals in this case\n\treturn formerClassName + \" \" + latterClassName;\n};\n\nexport const mergeTwoProps = <TProps extends Record<never, never>>(\n\tformerProps: TProps | undefined,\n\tlatterProps: TProps | undefined\n): TProps => {\n\t// == If no props are provided, return an empty object\n\tif (!latterProps || !formerProps) {\n\t\treturn latterProps ?? formerProps ?? ({} as TProps);\n\t}\n\n\tconst propsAccumulator = { ...formerProps } as Record<string, unknown>;\n\n\tfor (const latterPropName of Object.keys(latterProps)) {\n\t\tconst formerPropValue = (formerProps as Record<string, unknown>)[latterPropName];\n\t\tconst latterPropValue = (latterProps as Record<string, unknown>)[latterPropName];\n\n\t\t// == If the prop is `className` or `class`, we merge them\n\t\tif (latterPropName === \"className\" || latterPropName === \"class\") {\n\t\t\tpropsAccumulator[latterPropName] = mergeTwoClassNames(\n\t\t\t\tformerPropValue as string,\n\t\t\t\tlatterPropValue as string\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\n\t\t// == If the prop is `style`, we merge them\n\t\tif (latterPropName === \"style\") {\n\t\t\tpropsAccumulator[latterPropName] = {\n\t\t\t\t...(formerPropValue as object),\n\t\t\t\t...(latterPropValue as object),\n\t\t\t};\n\t\t\tcontinue;\n\t\t}\n\n\t\t// == If the handler exists on both, we compose them\n\t\tif (isEventHandler(latterPropName, latterPropValue)) {\n\t\t\tpropsAccumulator[latterPropName] = composeTwoEventHandlers(\n\t\t\t\tformerPropValue as AnyFunction | undefined,\n\t\t\t\tlatterPropValue\n\t\t\t);\n\n\t\t\tcontinue;\n\t\t}\n\n\t\t// == latterProps override by default\n\t\tpropsAccumulator[latterPropName] = latterPropValue;\n\t}\n\n\treturn propsAccumulator as TProps;\n};\n","import { mergeTwoProps } from \"./mergeTwoProps\";\n\ntype UnionToIntersection<TUnion> = (TUnion extends unknown ? (param: TUnion) => void : \"\") extends (\n\tparam: infer TParam\n) => void\n\t? TParam\n\t: \"\";\n\n/**\n * Merges multiple sets of React props.\n *\n * - It follows the Object.assign pattern where the rightmost object's fields overwrite\n * the conflicting ones from others. This doesn't apply to event handlers, `className` and `style` props.\n * - Event handlers are merged such that they are called in sequence (the rightmost one being called first),\n * and allows the user to prevent the previous event handlers from being executed by calling the `preventDefault` method.\n * - It also merges the `className` and `style` props, whereby the classes are concatenated\n * and the rightmost styles overwrite the previous ones.\n *\n * @important **`ref` is not merged.**\n * @param props props to merge.\n * @returns the merged props.\n */\n\nconst mergeProps = <TProps extends Record<never, never>>(\n\t...propsObjectArray: Array<TProps | undefined>\n): UnionToIntersection<TProps> => {\n\tif (propsObjectArray.length === 0) {\n\t\treturn {} as never;\n\t}\n\n\tif (propsObjectArray.length === 1) {\n\t\treturn propsObjectArray[0] as never;\n\t}\n\n\tlet accumulatedProps: Record<string, unknown> = {};\n\n\tfor (const propsObject of propsObjectArray) {\n\t\tif (!propsObject) continue;\n\n\t\taccumulatedProps = mergeTwoProps(accumulatedProps, propsObject);\n\t}\n\n\treturn accumulatedProps as never;\n};\n\nexport { mergeProps };\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.
|
4
|
+
"version": "0.9.40",
|
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",
|
@@ -34,8 +34,8 @@
|
|
34
34
|
"@types/react": ">=18.0.0",
|
35
35
|
"react": ">=18.0.0",
|
36
36
|
"zustand": ">=5.0.0",
|
37
|
-
"@zayne-labs/toolkit-core": "^0.9.
|
38
|
-
"@zayne-labs/toolkit-type-helpers": "^0.9.
|
37
|
+
"@zayne-labs/toolkit-core": "^0.9.40",
|
38
|
+
"@zayne-labs/toolkit-type-helpers": "^0.9.40"
|
39
39
|
},
|
40
40
|
"peerDependenciesMeta": {
|
41
41
|
"@types/react": {
|
@@ -66,8 +66,8 @@
|
|
66
66
|
"tsup": "^8.4.0",
|
67
67
|
"typescript": "5.8.3",
|
68
68
|
"zustand": "^5.0.3",
|
69
|
-
"@zayne-labs/toolkit-core": "0.9.
|
70
|
-
"@zayne-labs/toolkit-type-helpers": "0.9.
|
69
|
+
"@zayne-labs/toolkit-core": "0.9.40",
|
70
|
+
"@zayne-labs/toolkit-type-helpers": "0.9.40"
|
71
71
|
},
|
72
72
|
"publishConfig": {
|
73
73
|
"access": "public",
|