@fluentui/react-utilities 0.0.0-nightly9edaf68d4420211116.1 → 0.0.0-nightlya0aef7969220220228.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +169 -6
- package/CHANGELOG.md +46 -6
- package/dist/react-utilities.d.ts +238 -104
- package/lib/compose/getSlots.d.ts +11 -10
- package/lib/compose/getSlots.js +7 -6
- package/lib/compose/getSlots.js.map +1 -1
- package/lib/compose/index.d.ts +0 -1
- package/lib/compose/index.js +0 -1
- package/lib/compose/index.js.map +1 -1
- package/lib/compose/resolveShorthand.d.ts +10 -6
- package/lib/compose/resolveShorthand.js +5 -5
- package/lib/compose/resolveShorthand.js.map +1 -1
- package/lib/compose/types.d.ts +86 -35
- package/lib/compose/types.js.map +1 -1
- package/lib/hooks/index.d.ts +1 -1
- package/lib/hooks/index.js +1 -1
- package/lib/hooks/index.js.map +1 -1
- package/lib/hooks/useBoolean.js.map +1 -1
- package/lib/hooks/useConst.js.map +1 -1
- package/lib/hooks/useControllableState.d.ts +16 -6
- package/lib/hooks/useControllableState.js +13 -8
- package/lib/hooks/useControllableState.js.map +1 -1
- package/lib/hooks/useEventCallback.js.map +1 -1
- package/lib/hooks/useFirstMount.js.map +1 -1
- package/lib/hooks/useForceUpdate.js.map +1 -1
- package/lib/hooks/useId.js.map +1 -1
- package/lib/hooks/useIsomorphicLayoutEffect.js.map +1 -1
- package/lib/hooks/useMergedEventCallbacks.d.ts +18 -0
- package/lib/hooks/useMergedEventCallbacks.js +26 -0
- package/lib/hooks/useMergedEventCallbacks.js.map +1 -0
- package/lib/hooks/useMergedRefs.js.map +1 -1
- package/lib/hooks/useMount.js.map +1 -1
- package/lib/hooks/useOnClickOutside.js.map +1 -1
- package/lib/hooks/useOnScrollOutside.js.map +1 -1
- package/lib/hooks/usePrevious.js.map +1 -1
- package/lib/hooks/useTimeout.js.map +1 -1
- package/lib/hooks/useUnmount.js.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib/ssr/SSRContext.js.map +1 -1
- package/lib/ssr/canUseDOM.js.map +1 -1
- package/lib/ssr/index.js.map +1 -1
- package/lib/utils/applyTriggerPropsToChildren.d.ts +1 -1
- package/lib/utils/applyTriggerPropsToChildren.js +23 -3
- package/lib/utils/applyTriggerPropsToChildren.js.map +1 -1
- package/lib/utils/clamp.js.map +1 -1
- package/lib/utils/getNativeElementProps.d.ts +22 -0
- package/lib/utils/getNativeElementProps.js +24 -1
- package/lib/utils/getNativeElementProps.js.map +1 -1
- package/lib/utils/getRTLSafeKey.js.map +1 -1
- package/lib/utils/getReactCallbackName.d.ts +25 -0
- package/lib/utils/getReactCallbackName.js +26 -0
- package/lib/utils/getReactCallbackName.js.map +1 -0
- package/lib/utils/getTriggerChild.d.ts +22 -0
- package/lib/utils/getTriggerChild.js +26 -0
- package/lib/utils/getTriggerChild.js.map +1 -0
- package/lib/utils/index.d.ts +2 -1
- package/lib/utils/index.js +2 -1
- package/lib/utils/index.js.map +1 -1
- package/lib/utils/isFluentTrigger.d.ts +22 -0
- package/lib/utils/isFluentTrigger.js +8 -0
- package/lib/utils/isFluentTrigger.js.map +1 -0
- package/lib/utils/omit.js.map +1 -1
- package/lib/utils/properties.d.ts +6 -0
- package/lib/utils/properties.js +48 -20
- package/lib/utils/properties.js.map +1 -1
- package/lib/utils/shouldPreventDefaultOnKeyDown.js.map +1 -1
- package/lib-commonjs/compose/getSlots.d.ts +11 -10
- package/lib-commonjs/compose/getSlots.js +7 -7
- package/lib-commonjs/compose/getSlots.js.map +1 -1
- package/lib-commonjs/compose/index.d.ts +0 -1
- package/lib-commonjs/compose/index.js +0 -2
- package/lib-commonjs/compose/index.js.map +1 -1
- package/lib-commonjs/compose/resolveShorthand.d.ts +10 -6
- package/lib-commonjs/compose/resolveShorthand.js +5 -5
- package/lib-commonjs/compose/resolveShorthand.js.map +1 -1
- package/lib-commonjs/compose/types.d.ts +86 -35
- package/lib-commonjs/compose/types.js.map +1 -1
- package/lib-commonjs/hooks/index.d.ts +1 -1
- package/lib-commonjs/hooks/index.js +2 -2
- package/lib-commonjs/hooks/index.js.map +1 -1
- package/lib-commonjs/hooks/useBoolean.js.map +1 -1
- package/lib-commonjs/hooks/useConst.js.map +1 -1
- package/lib-commonjs/hooks/useControllableState.d.ts +16 -6
- package/lib-commonjs/hooks/useControllableState.js +13 -8
- package/lib-commonjs/hooks/useControllableState.js.map +1 -1
- package/lib-commonjs/hooks/useEventCallback.js.map +1 -1
- package/lib-commonjs/hooks/useFirstMount.js.map +1 -1
- package/lib-commonjs/hooks/useForceUpdate.js.map +1 -1
- package/lib-commonjs/hooks/useId.js.map +1 -1
- package/lib-commonjs/hooks/useIsomorphicLayoutEffect.js.map +1 -1
- package/lib-commonjs/hooks/useMergedEventCallbacks.d.ts +18 -0
- package/lib-commonjs/hooks/useMergedEventCallbacks.js +36 -0
- package/lib-commonjs/hooks/useMergedEventCallbacks.js.map +1 -0
- package/lib-commonjs/hooks/useMergedRefs.js.map +1 -1
- package/lib-commonjs/hooks/useMount.js.map +1 -1
- package/lib-commonjs/hooks/useOnClickOutside.js.map +1 -1
- package/lib-commonjs/hooks/useOnScrollOutside.js.map +1 -1
- package/lib-commonjs/hooks/usePrevious.js.map +1 -1
- package/lib-commonjs/hooks/useTimeout.js.map +1 -1
- package/lib-commonjs/hooks/useUnmount.js.map +1 -1
- package/lib-commonjs/index.js.map +1 -1
- package/lib-commonjs/ssr/SSRContext.js.map +1 -1
- package/lib-commonjs/ssr/canUseDOM.js.map +1 -1
- package/lib-commonjs/ssr/index.js.map +1 -1
- package/lib-commonjs/utils/applyTriggerPropsToChildren.d.ts +1 -1
- package/lib-commonjs/utils/applyTriggerPropsToChildren.js +24 -4
- package/lib-commonjs/utils/applyTriggerPropsToChildren.js.map +1 -1
- package/lib-commonjs/utils/clamp.js.map +1 -1
- package/lib-commonjs/utils/getNativeElementProps.d.ts +22 -0
- package/lib-commonjs/utils/getNativeElementProps.js +26 -1
- package/lib-commonjs/utils/getNativeElementProps.js.map +1 -1
- package/lib-commonjs/utils/getRTLSafeKey.js.map +1 -1
- package/lib-commonjs/utils/getReactCallbackName.d.ts +25 -0
- package/lib-commonjs/utils/getReactCallbackName.js +35 -0
- package/lib-commonjs/utils/getReactCallbackName.js.map +1 -0
- package/lib-commonjs/utils/getTriggerChild.d.ts +22 -0
- package/lib-commonjs/utils/getTriggerChild.js +37 -0
- package/lib-commonjs/utils/getTriggerChild.js.map +1 -0
- package/lib-commonjs/utils/index.d.ts +2 -1
- package/lib-commonjs/utils/index.js +4 -2
- package/lib-commonjs/utils/index.js.map +1 -1
- package/lib-commonjs/utils/isFluentTrigger.d.ts +22 -0
- package/lib-commonjs/utils/isFluentTrigger.js +17 -0
- package/lib-commonjs/utils/isFluentTrigger.js.map +1 -0
- package/lib-commonjs/utils/omit.js.map +1 -1
- package/lib-commonjs/utils/properties.d.ts +6 -0
- package/lib-commonjs/utils/properties.js +49 -21
- package/lib-commonjs/utils/properties.js.map +1 -1
- package/lib-commonjs/utils/shouldPreventDefaultOnKeyDown.js.map +1 -1
- package/package.json +6 -10
- package/lib/compose/nullRender.d.ts +0 -4
- package/lib/compose/nullRender.js +0 -5
- package/lib/compose/nullRender.js.map +0 -1
- package/lib/hooks/useControllableValue.d.ts +0 -20
- package/lib/hooks/useControllableValue.js +0 -57
- package/lib/hooks/useControllableValue.js.map +0 -1
- package/lib/utils/onlyChild.d.ts +0 -5
- package/lib/utils/onlyChild.js +0 -13
- package/lib/utils/onlyChild.js.map +0 -1
- package/lib-commonjs/compose/nullRender.d.ts +0 -4
- package/lib-commonjs/compose/nullRender.js +0 -14
- package/lib-commonjs/compose/nullRender.js.map +0 -1
- package/lib-commonjs/hooks/useControllableValue.d.ts +0 -20
- package/lib-commonjs/hooks/useControllableValue.js +0 -68
- package/lib-commonjs/hooks/useControllableValue.js.map +0 -1
- package/lib-commonjs/utils/onlyChild.d.ts +0 -5
- package/lib-commonjs/utils/onlyChild.js +0 -23
- package/lib-commonjs/utils/onlyChild.js.map +0 -1
@@ -11,7 +11,7 @@ export declare const anchorProperties: Record<string, number>;
|
|
11
11
|
/**
|
12
12
|
* Apply the trigger props to the children, either by calling the render function, or cloning with the new props.
|
13
13
|
*/
|
14
|
-
export declare const applyTriggerPropsToChildren: <TTriggerProps>(children: React_2.ReactElement<any, string |
|
14
|
+
export declare const applyTriggerPropsToChildren: <TTriggerProps>(children: React_2.ReactElement<any, string | React_2.JSXElementConstructor<any>> | ((props: TTriggerProps) => React_2.ReactElement | null) | null | undefined, triggerProps: TTriggerProps) => React_2.ReactElement | null;
|
15
15
|
|
16
16
|
/**
|
17
17
|
* Helper type for inferring the type of the as prop from a Props type.
|
@@ -58,8 +58,6 @@ export declare const buttonProperties: Record<string, number>;
|
|
58
58
|
*/
|
59
59
|
export declare function canUseDOM(): boolean;
|
60
60
|
|
61
|
-
export declare type ChangeCallback<TElement extends HTMLElement, TValue, TEvent extends React_2.SyntheticEvent<TElement> | undefined> = (ev: TEvent, newValue: TValue | undefined) => void;
|
62
|
-
|
63
61
|
/**
|
64
62
|
* Clamps `value` to a number between the min and max.
|
65
63
|
*
|
@@ -73,26 +71,22 @@ export declare const colGroupProperties: Record<string, number>;
|
|
73
71
|
|
74
72
|
export declare const colProperties: Record<string, number>;
|
75
73
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
export declare type
|
81
|
-
components?: {
|
82
|
-
[Key in keyof Shorthands]-?: React_2.ComponentType<NonNullable<Shorthands[Key]> extends ObjectShorthandProps<infer P> ? P : NonNullable<Shorthands[Key]>> | (NonNullable<Shorthands[Key]> extends AsIntrinsicElement<infer As> ? As : keyof JSX.IntrinsicElements);
|
83
|
-
};
|
84
|
-
} & Shorthands;
|
74
|
+
/**
|
75
|
+
* Defines the Props type for a component given its slots and the definition of which one is the primary slot,
|
76
|
+
* defaulting to root if one is not provided.
|
77
|
+
*/
|
78
|
+
export declare type ComponentProps<Slots extends SlotPropsRecord, Primary extends keyof Slots = 'root'> = Omit<Slots, Primary & 'root'> & PropsWithoutRef<ExtractSlotProps<Slots[Primary]>>;
|
85
79
|
|
86
80
|
/**
|
87
|
-
*
|
88
|
-
*
|
89
|
-
* This should ONLY be used in type templates as in `extends DefaultObjectShorthandProps`;
|
90
|
-
* it shouldn't be used as the type of a slot.
|
81
|
+
* Defines the State object of a component given its slots.
|
91
82
|
*/
|
92
|
-
export declare type
|
93
|
-
|
94
|
-
|
95
|
-
}
|
83
|
+
export declare type ComponentState<Slots extends SlotPropsRecord> = {
|
84
|
+
components: {
|
85
|
+
[Key in keyof Slots]-?: React_2.ComponentType<ExtractSlotProps<Slots[Key]>> | (ExtractSlotProps<Slots[Key]> extends AsIntrinsicElement<infer As> ? As : keyof JSX.IntrinsicElements);
|
86
|
+
};
|
87
|
+
} & {
|
88
|
+
[Key in keyof Slots]: ReplaceNullWithUndefined<Exclude<Slots[Key], SlotShorthandValue | (Key extends 'root' ? null : never)>>;
|
89
|
+
};
|
96
90
|
|
97
91
|
/**
|
98
92
|
* Default context value to use in case there is no SSRProvider. This is fine for client-only apps.
|
@@ -102,16 +96,47 @@ export declare type DefaultObjectShorthandProps = ObjectShorthandProps<{
|
|
102
96
|
export declare const defaultSSRContextValue: SSRContextValue;
|
103
97
|
|
104
98
|
/**
|
105
|
-
*
|
99
|
+
* An array of DIV tag properties and events.
|
100
|
+
*
|
101
|
+
* @public
|
106
102
|
*/
|
107
|
-
declare
|
103
|
+
export declare const divProperties: Record<string, number>;
|
108
104
|
|
109
105
|
/**
|
110
|
-
*
|
106
|
+
* HTML element types that are not allowed to have children.
|
107
|
+
*
|
108
|
+
* Reference: https://developer.mozilla.org/en-US/docs/Glossary/Empty_element
|
109
|
+
*/
|
110
|
+
declare type EmptyIntrisicElements = 'area' | 'base' | 'br' | 'col' | 'embed' | 'hr' | 'img' | 'input' | 'link' | 'meta' | 'param' | 'source' | 'track' | 'wbr';
|
111
|
+
|
112
|
+
/**
|
113
|
+
* Removes SlotShorthandValue and null from the slot type, extracting just the slot's Props object.
|
114
|
+
*/
|
115
|
+
export declare type ExtractSlotProps<S> = Exclude<S, SlotShorthandValue | null | undefined>;
|
116
|
+
|
117
|
+
/**
|
118
|
+
* An array of FIELDSET tag properties and events.
|
111
119
|
*
|
112
120
|
* @public
|
113
121
|
*/
|
114
|
-
export declare const
|
122
|
+
export declare const fieldsetProperties: Record<string, number>;
|
123
|
+
|
124
|
+
/**
|
125
|
+
* Allows a component to be tagged as a FluentUI trigger component.
|
126
|
+
*
|
127
|
+
* Triggers are special-case components: they attach event listeners and other props on their child,
|
128
|
+
* and use them to trigger another component to show. Examples include `MenuTrigger` and `Tooltip`.
|
129
|
+
*
|
130
|
+
* A component can be tagged as a trigger as follows:
|
131
|
+
* ```ts
|
132
|
+
* const MyComponent: React.FC<MyComponentProps> & FluentTriggerComponent = ...;
|
133
|
+
*
|
134
|
+
* MyComponent.isFluentTriggerComponent = true; // MUST also set this to true
|
135
|
+
* ```
|
136
|
+
*/
|
137
|
+
export declare type FluentTriggerComponent = {
|
138
|
+
isFluentTriggerComponent?: boolean;
|
139
|
+
};
|
115
140
|
|
116
141
|
/**
|
117
142
|
* An array of FORM tag properties and events.
|
@@ -150,6 +175,29 @@ export declare function getNativeElementProps<TAttributes extends React_2.HTMLAt
|
|
150
175
|
*/
|
151
176
|
export declare function getNativeProps<T extends Record<string, any>>(props: Record<string, any>, allowedPropNames: string[] | Record<string, number>, excludedPropNames?: string[]): T;
|
152
177
|
|
178
|
+
/**
|
179
|
+
* Splits the native props into ones that go to the `root` slot, and ones that go to the primary slot.
|
180
|
+
*
|
181
|
+
* This function is only for use with components that have a primary slot other than `root`.
|
182
|
+
* Most components should use {@link getNativeElementProps} for their root slot if it is the primary slot.
|
183
|
+
*
|
184
|
+
* @returns An object containing the native props for the `root` and primary slots.
|
185
|
+
*/
|
186
|
+
export declare const getPartitionedNativeProps: <Props extends Pick<React_2.HTMLAttributes<HTMLElement>, "style" | "className">, ExcludedPropKeys extends Extract<keyof Props, string> = never>({ primarySlotTagName, props, excludedPropNames, }: {
|
187
|
+
/** The primary slot's element type (e.g. 'div') */
|
188
|
+
primarySlotTagName: keyof JSX.IntrinsicElements;
|
189
|
+
/** The component's props object */
|
190
|
+
props: Props;
|
191
|
+
/** List of native props to exclude from the returned value */
|
192
|
+
excludedPropNames?: ExcludedPropKeys[] | undefined;
|
193
|
+
}) => {
|
194
|
+
root: {
|
195
|
+
style: React_2.CSSProperties | undefined;
|
196
|
+
className: string | undefined;
|
197
|
+
};
|
198
|
+
primary: Omit<Props, ExcludedPropKeys>;
|
199
|
+
};
|
200
|
+
|
153
201
|
/**
|
154
202
|
* Finds and swaps a provided key for it's right to left format.
|
155
203
|
*/
|
@@ -164,16 +212,39 @@ export declare const getRTLSafeKey: (key: string, dir: 'ltr' | 'rtl') => string;
|
|
164
212
|
* Slots will render as null if they are rendered as primitives with undefined children.
|
165
213
|
*
|
166
214
|
* The slotProps will always omit the `as` prop within them, and for slots that are string
|
167
|
-
* primitives, the props will be filtered according
|
168
|
-
* slot is rendered `as: 'a'`, the props will be filtered for acceptable
|
215
|
+
* primitives, the props will be filtered according to the slot type by the type system.
|
216
|
+
* For example, if the slot is rendered `as: 'a'`, the props will be filtered for acceptable
|
217
|
+
* anchor props. Note that this is only enforced at build time by Typescript -- there is no
|
218
|
+
* runtime code filtering props in this function.
|
169
219
|
*
|
170
220
|
* @param state - State including slot definitions
|
171
|
-
* @param slotNames - Name of which props are slots
|
172
221
|
* @returns An object containing the `slots` map and `slotProps` map.
|
173
222
|
*/
|
174
|
-
export declare function getSlots<R extends
|
223
|
+
export declare function getSlots<R extends SlotPropsRecord>(state: ComponentState<R>): {
|
175
224
|
slots: Slots<R>;
|
176
|
-
slotProps:
|
225
|
+
slotProps: ObjectSlotProps<R>;
|
226
|
+
};
|
227
|
+
|
228
|
+
/**
|
229
|
+
* Gets the trigger element of a FluentTriggerComponent (such as Tooltip or MenuTrigger).
|
230
|
+
*
|
231
|
+
* In the case where the immediate child is itself a FluentTriggerComponent and/or React Fragment,
|
232
|
+
* it returns the first descendant that is _not_ a FluentTriggerComponent or Fragment.
|
233
|
+
* This allows multiple triggers to be stacked, and still apply their props to the actual trigger element.
|
234
|
+
*
|
235
|
+
* For example, the following returns `<div id="child" />`:
|
236
|
+
* ```jsx
|
237
|
+
* getTriggerChild(
|
238
|
+
* <Tooltip>
|
239
|
+
* <MenuTrigger>
|
240
|
+
* <div id="child" />
|
241
|
+
* </MenuTrigger>
|
242
|
+
* </Tooltip>
|
243
|
+
* );
|
244
|
+
* ```
|
245
|
+
*/
|
246
|
+
export declare const getTriggerChild: (children: React_2.ReactNode) => React_2.ReactElement & {
|
247
|
+
ref?: React_2.Ref<unknown>;
|
177
248
|
};
|
178
249
|
|
179
250
|
/**
|
@@ -210,25 +281,19 @@ export declare const imgProperties: Record<string, number>;
|
|
210
281
|
export declare const inputProperties: Record<string, number>;
|
211
282
|
|
212
283
|
/**
|
213
|
-
*
|
214
|
-
*
|
215
|
-
*
|
216
|
-
* The first param is the slot's default type if no `as` prop is specified.
|
217
|
-
* The second param is an optional union of alternative types that can be specified for the `as` prop.
|
218
|
-
*
|
219
|
-
* ```
|
220
|
-
* IntrinsicShorthandProps<'div'> // Slot is always div
|
221
|
-
* IntrinsicShorthandProps<'button', 'a'> // Defaults to button, but allows as="a" with anchor-specific props
|
222
|
-
* IntrinsicShorthandProps<'label', 'span' | 'div'>; // Defaults to label, but allows as="span" or as="div"
|
223
|
-
* ```
|
284
|
+
* Helper type for {@link Slot}. Modifies `JSX.IntrinsicElements[Type]`:
|
285
|
+
* * Removes legacy string ref.
|
286
|
+
* * Disallows children for empty tags like 'img'.
|
224
287
|
*/
|
225
|
-
|
226
|
-
|
227
|
-
}
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
}
|
288
|
+
declare type IntrisicElementProps<Type extends keyof JSX.IntrinsicElements> = React_2.PropsWithRef<JSX.IntrinsicElements[Type]> & (Type extends EmptyIntrisicElements ? {
|
289
|
+
children?: never;
|
290
|
+
} : {});
|
291
|
+
|
292
|
+
/**
|
293
|
+
* Checks if a given element is a FluentUI trigger (e.g. `MenuTrigger` or `Tooltip`).
|
294
|
+
* See the {@link FluentTriggerComponent} type for more info.
|
295
|
+
*/
|
296
|
+
export declare const isFluentTrigger: (element: React_2.ReactElement) => boolean | undefined;
|
232
297
|
|
233
298
|
/**
|
234
299
|
* Evaluates to true if the given type contains exactly one string, or false if it is a union of strings.
|
@@ -256,24 +321,10 @@ export declare const labelProperties: Record<string, number>;
|
|
256
321
|
*/
|
257
322
|
export declare const liProperties: Record<string, number>;
|
258
323
|
|
259
|
-
|
260
|
-
|
261
|
-
*/
|
262
|
-
export declare const nullRender: () => null;
|
263
|
-
|
264
|
-
/**
|
265
|
-
* Defines the slot props for a slot that supports a Component type.
|
266
|
-
*
|
267
|
-
* For intrinsic elements like 'div', use {@link IntrinsicShorthandProps} instead.
|
268
|
-
*/
|
269
|
-
export declare type ObjectShorthandProps<Props extends {
|
270
|
-
children?: React_2.ReactNode;
|
271
|
-
} = {}> = Props & {
|
272
|
-
children?: Props['children'] | ShorthandRenderFunction<Props>;
|
324
|
+
declare type ObjectSlotProps<S extends SlotPropsRecord> = {
|
325
|
+
[K in keyof S]-?: ExtractSlotProps<S[K]> extends AsIntrinsicElement<infer As> ? UnionToIntersection<JSX.IntrinsicElements[As]> : ExtractSlotProps<S[K]> extends React_2.ComponentType<infer P> ? P : never;
|
273
326
|
};
|
274
327
|
|
275
|
-
export declare type ObjectShorthandPropsRecord = Record<string, DefaultObjectShorthandProps | undefined>;
|
276
|
-
|
277
328
|
/**
|
278
329
|
* This is part of a hack to infer the element type from a native element *props* type.
|
279
330
|
* The only place the original element is found in a native props type (at least that's workable
|
@@ -310,16 +361,11 @@ export declare const olProperties: Record<string, number>;
|
|
310
361
|
*/
|
311
362
|
export declare function omit<TObj extends Record<string, any>, Exclusions extends (keyof TObj)[]>(obj: TObj, exclusions: Exclusions): Omit<TObj, Exclusions[number]>;
|
312
363
|
|
313
|
-
/**
|
314
|
-
* Similar to React.Children.only, but drills into fragments rather than treating them as a single child
|
315
|
-
*/
|
316
|
-
export declare const onlyChild: <P>(child: string | number | boolean | {} | React_2.ReactNodeArray | React_2.ReactPortal | React_2.ReactElement<P, string | ((props: any) => React_2.ReactElement<any, string | any | (new (props: any) => React_2.Component<any, any, any>)> | null) | (new (props: any) => React_2.Component<any, any, any>)> | null | undefined) => React_2.ReactElement<P, string | ((props: any) => React_2.ReactElement<any, string | any | (new (props: any) => React_2.Component<any, any, any>)> | null) | (new (props: any) => React_2.Component<any, any, any>)>;
|
317
|
-
|
318
364
|
export declare const optionProperties: Record<string, number>;
|
319
365
|
|
320
366
|
/**
|
321
367
|
* Removes the 'ref' prop from the given Props type, leaving unions intact (such as the discriminated union created by
|
322
|
-
*
|
368
|
+
* IntrinsicSlotProps). This allows IntrinsicSlotProps to be used with React.forwardRef.
|
323
369
|
*
|
324
370
|
* The conditional "extends unknown" (always true) exploits a quirk in the way TypeScript handles conditional
|
325
371
|
* types, to prevent unions from being expanded.
|
@@ -332,6 +378,11 @@ export declare type PropsWithoutRef<P> = 'ref' extends keyof P ? (P extends unkn
|
|
332
378
|
*/
|
333
379
|
export declare type RefObjectFunction<T> = React_2.RefObject<T> & ((value: T) => void);
|
334
380
|
|
381
|
+
/**
|
382
|
+
* If type T includes `null`, remove it and add `undefined` instead.
|
383
|
+
*/
|
384
|
+
export declare type ReplaceNullWithUndefined<T> = T extends null ? Exclude<T, null> | undefined : T;
|
385
|
+
|
335
386
|
/**
|
336
387
|
* Resets generated IDs, should be used only in tests.
|
337
388
|
*
|
@@ -340,14 +391,19 @@ export declare type RefObjectFunction<T> = React_2.RefObject<T> & ((value: T) =>
|
|
340
391
|
export declare function resetIdsForTests(): void;
|
341
392
|
|
342
393
|
/**
|
343
|
-
* Resolves
|
394
|
+
* Resolves shorthands into slot props, to ensure normalization of the signature
|
344
395
|
* being passed down to getSlots method
|
345
|
-
* @param value - the base
|
346
|
-
* @param options - options to resolve
|
396
|
+
* @param value - the base shorthand props
|
397
|
+
* @param options - options to resolve shorthand props
|
347
398
|
*/
|
348
|
-
export declare
|
399
|
+
export declare const resolveShorthand: ResolveShorthandFunction;
|
400
|
+
|
401
|
+
export declare type ResolveShorthandFunction<Props extends UnknownSlotProps = UnknownSlotProps> = {
|
402
|
+
<P extends Props | null>(value: P | SlotShorthandValue | undefined, options?: ResolveShorthandOptions<P, true>): ReplaceNullWithUndefined<P>;
|
403
|
+
<P extends Props | null>(value: P | SlotShorthandValue | undefined, options?: ResolveShorthandOptions<P, boolean>): ReplaceNullWithUndefined<P> | undefined;
|
404
|
+
};
|
349
405
|
|
350
|
-
export declare type ResolveShorthandOptions<Props
|
406
|
+
export declare type ResolveShorthandOptions<Props, Required extends boolean = false> = {
|
351
407
|
required?: Required;
|
352
408
|
defaultProps?: Props;
|
353
409
|
};
|
@@ -359,10 +415,6 @@ export declare type ResolveShorthandOptions<Props extends Record<string, any>, R
|
|
359
415
|
*/
|
360
416
|
export declare const selectProperties: Record<string, number>;
|
361
417
|
|
362
|
-
export declare type ShorthandProps<Props extends DefaultObjectShorthandProps> = React_2.ReactChild | React_2.ReactNodeArray | React_2.ReactPortal | number | null | undefined | Props;
|
363
|
-
|
364
|
-
export declare type ShorthandRenderFunction<Props> = (Component: React_2.ElementType<Props>, props: Omit<Props, 'children' | 'as'>) => React_2.ReactNode;
|
365
|
-
|
366
418
|
/**
|
367
419
|
* Checks if the keyboard event should preventDefault() for Enter and Spacebar keys
|
368
420
|
*
|
@@ -370,14 +422,55 @@ export declare type ShorthandRenderFunction<Props> = (Component: React_2.Element
|
|
370
422
|
*/
|
371
423
|
export declare function shouldPreventDefaultOnKeyDown(e: KeyboardEvent | React_2.KeyboardEvent): boolean;
|
372
424
|
|
373
|
-
|
374
|
-
|
375
|
-
|
425
|
+
/**
|
426
|
+
* The props type and shorthand value for a slot. Type is either a single intrinsic element like `'div'`,
|
427
|
+
* or a component like `typeof Button`.
|
428
|
+
*
|
429
|
+
* If a slot needs to support multiple intrinsic element types, use the `AlternateAs` param (see examples below).
|
430
|
+
*
|
431
|
+
* By default, slots can be set to `null` to prevent them from being rendered. If a slot must always be rendered,
|
432
|
+
* wrap with `NonNullable` (see examples below).
|
433
|
+
*
|
434
|
+
* @example
|
435
|
+
* ```
|
436
|
+
* // Intrinsic element examples:
|
437
|
+
* Slot<'div'> // Slot is always div
|
438
|
+
* Slot<'button', 'a'> // Defaults to button, but allows as="a" with anchor-specific props
|
439
|
+
* Slot<'span', 'div' | 'pre'> // Defaults to span, but allows as="div" or as="pre"
|
440
|
+
* NonNullable<Slot<'div'>> // Slot that will always be rendered (can't be set to null by the user)
|
441
|
+
*
|
442
|
+
* // Component examples:
|
443
|
+
* Slot<typeof Button> // Slot is always a Button, and accepts all of Button's Props
|
444
|
+
* NonNullable<Slot<typeof Label>> // Slot is a Label and will always be rendered (can't be set to null by the user)
|
445
|
+
* ```
|
446
|
+
*/
|
447
|
+
export declare type Slot<Type extends keyof JSX.IntrinsicElements | React_2.ComponentType | UnknownSlotProps, AlternateAs extends keyof JSX.IntrinsicElements = never> = IsSingleton<Extract<Type, string>> extends true ? WithSlotShorthandValue<Type extends keyof JSX.IntrinsicElements ? {
|
448
|
+
as?: Type;
|
449
|
+
} & WithSlotRenderFunction<IntrisicElementProps<Type>> : Type extends React_2.ComponentType<infer Props> ? WithSlotRenderFunction<Props> : Type> | {
|
450
|
+
[As in AlternateAs]: {
|
451
|
+
as: As;
|
452
|
+
} & WithSlotRenderFunction<IntrisicElementProps<As>>;
|
453
|
+
}[AlternateAs] | null : 'Error: First parameter to Slot must not be not a union of types. See documentation of Slot type.';
|
376
454
|
|
377
|
-
|
378
|
-
|
455
|
+
/**
|
456
|
+
* Matches any component's Slots type (such as ButtonSlots).
|
457
|
+
*
|
458
|
+
* This should ONLY be used in type templates as in `extends SlotPropsRecord`;
|
459
|
+
* it shouldn't be used as a component's Slots type.
|
460
|
+
*/
|
461
|
+
export declare type SlotPropsRecord = Record<string, UnknownSlotProps | SlotShorthandValue | null | undefined>;
|
462
|
+
|
463
|
+
export declare type SlotRenderFunction<Props> = (Component: React_2.ElementType<Props>, props: Omit<Props, 'children' | 'as'>) => React_2.ReactNode;
|
464
|
+
|
465
|
+
export declare type Slots<S extends SlotPropsRecord> = {
|
466
|
+
[K in keyof S]: ExtractSlotProps<S[K]> extends AsIntrinsicElement<infer As> ? As : ExtractSlotProps<S[K]> extends React_2.ComponentType<infer P> ? React_2.ElementType<NonNullable<P>> : React_2.ElementType<ExtractSlotProps<S[K]>>;
|
379
467
|
};
|
380
468
|
|
469
|
+
/**
|
470
|
+
* The shorthand value of a slot allows specifying its child
|
471
|
+
*/
|
472
|
+
export declare type SlotShorthandValue = React_2.ReactChild | React_2.ReactNodeArray | React_2.ReactPortal;
|
473
|
+
|
381
474
|
export declare const SSRContext: React_2.Context<SSRContextValue>;
|
382
475
|
|
383
476
|
/**
|
@@ -438,6 +531,16 @@ export declare const trProperties: Record<string, number>;
|
|
438
531
|
*/
|
439
532
|
export declare type UnionToIntersection<U> = (U extends unknown ? (x: U) => U : never) extends (x: infer I) => U ? I : never;
|
440
533
|
|
534
|
+
/**
|
535
|
+
* Matches any slot props type.
|
536
|
+
*
|
537
|
+
* This should ONLY be used in type templates as in `extends UnknownSlotProps`;
|
538
|
+
* it shouldn't be used as the type of a slot.
|
539
|
+
*/
|
540
|
+
export declare type UnknownSlotProps = Pick<React_2.HTMLAttributes<HTMLElement>, 'children' | 'className' | 'style'> & {
|
541
|
+
as?: keyof JSX.IntrinsicElements;
|
542
|
+
};
|
543
|
+
|
441
544
|
/**
|
442
545
|
* Hook to store a value and generate callbacks for setting the value to true or false.
|
443
546
|
* The identity of the callbacks will always stay the same.
|
@@ -471,42 +574,37 @@ export declare type UseBooleanCallbacks = {
|
|
471
574
|
export declare function useConst<T>(initialValue: T | (() => T)): T;
|
472
575
|
|
473
576
|
/**
|
474
|
-
* A useState
|
475
|
-
*
|
476
|
-
*
|
577
|
+
* A `useState`-like hook to manage a value that could be either controlled or uncontrolled,
|
578
|
+
* such as a checked state or text input string.
|
579
|
+
*
|
580
|
+
* Unlike `setState`, it's okay to call the returned updater (dispatch) function for either a
|
581
|
+
* controlled or uncontrolled component. Calls will only be respected if the component is uncontrolled.
|
582
|
+
*
|
583
|
+
* @returns Same as [`useState`](https://reactjs.org/docs/hooks-reference.html#usestate): an array
|
584
|
+
* of the current value and an updater (dispatch) function. The updater function is referentially
|
585
|
+
* stable (won't change during the component's lifecycle). It can take either a new value, or a
|
586
|
+
* function which is passed the previous value and returns the new value. Unlike `setState`, calls
|
587
|
+
* to the updater function will only be respected if the component is uncontrolled.
|
588
|
+
* @see https://reactjs.org/docs/uncontrolled-components.html
|
477
589
|
*/
|
478
590
|
export declare const useControllableState: <State>(options: UseControllableStateOptions<State>) => [State, React_2.Dispatch<React_2.SetStateAction<State>>];
|
479
591
|
|
480
592
|
export declare type UseControllableStateOptions<State> = {
|
481
593
|
/**
|
482
|
-
* User
|
594
|
+
* User-provided default state or initializer, for uncontrolled usage.
|
483
595
|
*/
|
484
596
|
defaultState?: State | (() => State);
|
485
597
|
/**
|
486
|
-
* User
|
598
|
+
* User-provided controlled state. `undefined` means internal state will be used.
|
487
599
|
*/
|
488
600
|
state: State | undefined;
|
489
601
|
/**
|
490
|
-
* Used
|
602
|
+
* Used as the initial state if `state` and `defaultState` are both `undefined`.
|
603
|
+
* If `undefined` is the correct initial state, pass that here.
|
491
604
|
*/
|
492
605
|
initialState: State;
|
493
606
|
};
|
494
607
|
|
495
|
-
/**
|
496
|
-
* Hook to manage a value that could be either controlled or uncontrolled, such as a checked state or
|
497
|
-
* text box string.
|
498
|
-
* @param controlledValue - The controlled value passed in the props. This value will always be used if provided,
|
499
|
-
* and the internal state will be updated to reflect it.
|
500
|
-
* @param defaultUncontrolledValue - Initial value for the internal state in the uncontrolled case.
|
501
|
-
* @returns An array of the current value and an updater callback. Like `React.useState`, the updater
|
502
|
-
* callback always has the same identity, and it can take either a new value, or a function which
|
503
|
-
* is passed the previous value and returns the new value.
|
504
|
-
* @see https://reactjs.org/docs/uncontrolled-components.html
|
505
|
-
*/
|
506
|
-
export declare function useControllableValue<TValue, TElement extends HTMLElement>(controlledValue: TValue, defaultUncontrolledValue: DefaultValue<TValue>): Readonly<[TValue, (update: React_2.SetStateAction<TValue>) => void]>;
|
507
|
-
|
508
|
-
export declare function useControllableValue<TValue, TElement extends HTMLElement, TEvent extends React_2.SyntheticEvent<TElement> | undefined>(controlledValue: TValue, defaultUncontrolledValue: DefaultValue<TValue>, onChange: ChangeCallback<TElement, TValue, TEvent>): Readonly<[TValue, (update: React_2.SetStateAction<TValue>, ev?: React_2.FormEvent<TElement>) => void]>;
|
509
|
-
|
510
608
|
/**
|
511
609
|
* https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback
|
512
610
|
*
|
@@ -565,6 +663,25 @@ export declare const useIsomorphicLayoutEffect: typeof React_2.useEffect;
|
|
565
663
|
*/
|
566
664
|
export declare function useIsSSR(): boolean;
|
567
665
|
|
666
|
+
/**
|
667
|
+
* Combine two event callbacks into a single callback function that calls each one in order.
|
668
|
+
*
|
669
|
+
* This is useful to add an event listener to an existing element without overwriting the current listener, if any.
|
670
|
+
*
|
671
|
+
* For example:
|
672
|
+
* ```ts
|
673
|
+
* state.slot.onChange = useMergedCallbacks(state.slot.onChange, ev => {
|
674
|
+
* // Handle onChange
|
675
|
+
* });
|
676
|
+
* ```
|
677
|
+
*
|
678
|
+
* @param callback1 - The first callback to be called
|
679
|
+
* @param callback2 - The second callback to be called
|
680
|
+
*
|
681
|
+
* @returns An event callback that calls the callbacks in order, and is stable between renders
|
682
|
+
*/
|
683
|
+
export declare function useMergedEventCallbacks<Args extends unknown[]>(callback1: ((...args: Args) => void) | undefined, callback2: ((...args: Args) => void) | undefined): (...args: Args) => void;
|
684
|
+
|
568
685
|
/**
|
569
686
|
* React hook to merge multiple React refs (either MutableRefObjects or ref callbacks) into a single ref callback that
|
570
687
|
* updates all provided refs
|
@@ -645,4 +762,21 @@ export declare const useUnmount: (callback: () => void) => void;
|
|
645
762
|
*/
|
646
763
|
export declare const videoProperties: Record<string, number>;
|
647
764
|
|
765
|
+
/**
|
766
|
+
* Helper type for {@link Slot}. Takes the props we want to support for a slot and adds the ability for `children`
|
767
|
+
* to be a render function that takes those props.
|
768
|
+
*/
|
769
|
+
declare type WithSlotRenderFunction<Props extends {
|
770
|
+
children?: unknown;
|
771
|
+
}> = Props & {
|
772
|
+
children?: Props['children'] | SlotRenderFunction<Props>;
|
773
|
+
};
|
774
|
+
|
775
|
+
/**
|
776
|
+
* Helper type for {@link Slot}. Adds shorthand types that are assignable to the slot's `children`.
|
777
|
+
*/
|
778
|
+
declare type WithSlotShorthandValue<Props extends {
|
779
|
+
children?: unknown;
|
780
|
+
}> = Props | Extract<SlotShorthandValue, Props['children']>;
|
781
|
+
|
648
782
|
export { }
|
@@ -1,10 +1,10 @@
|
|
1
1
|
import * as React from 'react';
|
2
|
-
import { AsIntrinsicElement, ComponentState,
|
3
|
-
export declare type Slots<S extends
|
4
|
-
[K in keyof S]
|
2
|
+
import type { AsIntrinsicElement, ComponentState, ExtractSlotProps, SlotPropsRecord, UnionToIntersection } from './types';
|
3
|
+
export declare type Slots<S extends SlotPropsRecord> = {
|
4
|
+
[K in keyof S]: ExtractSlotProps<S[K]> extends AsIntrinsicElement<infer As> ? As : ExtractSlotProps<S[K]> extends React.ComponentType<infer P> ? React.ElementType<NonNullable<P>> : React.ElementType<ExtractSlotProps<S[K]>>;
|
5
5
|
};
|
6
|
-
declare type
|
7
|
-
[K in keyof S]-?:
|
6
|
+
declare type ObjectSlotProps<S extends SlotPropsRecord> = {
|
7
|
+
[K in keyof S]-?: ExtractSlotProps<S[K]> extends AsIntrinsicElement<infer As> ? UnionToIntersection<JSX.IntrinsicElements[As]> : ExtractSlotProps<S[K]> extends React.ComponentType<infer P> ? P : never;
|
8
8
|
};
|
9
9
|
/**
|
10
10
|
* Given the state and an array of slot names, will break out `slots` and `slotProps`
|
@@ -15,15 +15,16 @@ declare type SlotProps<S extends ObjectShorthandPropsRecord> = {
|
|
15
15
|
* Slots will render as null if they are rendered as primitives with undefined children.
|
16
16
|
*
|
17
17
|
* The slotProps will always omit the `as` prop within them, and for slots that are string
|
18
|
-
* primitives, the props will be filtered according
|
19
|
-
* slot is rendered `as: 'a'`, the props will be filtered for acceptable
|
18
|
+
* primitives, the props will be filtered according to the slot type by the type system.
|
19
|
+
* For example, if the slot is rendered `as: 'a'`, the props will be filtered for acceptable
|
20
|
+
* anchor props. Note that this is only enforced at build time by Typescript -- there is no
|
21
|
+
* runtime code filtering props in this function.
|
20
22
|
*
|
21
23
|
* @param state - State including slot definitions
|
22
|
-
* @param slotNames - Name of which props are slots
|
23
24
|
* @returns An object containing the `slots` map and `slotProps` map.
|
24
25
|
*/
|
25
|
-
export declare function getSlots<R extends
|
26
|
+
export declare function getSlots<R extends SlotPropsRecord>(state: ComponentState<R>): {
|
26
27
|
slots: Slots<R>;
|
27
|
-
slotProps:
|
28
|
+
slotProps: ObjectSlotProps<R>;
|
28
29
|
};
|
29
30
|
export {};
|
package/lib/compose/getSlots.js
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
import * as React from 'react';
|
2
|
-
import { nullRender } from './nullRender';
|
3
2
|
import { omit } from '../utils/omit';
|
4
3
|
/**
|
5
4
|
* Given the state and an array of slot names, will break out `slots` and `slotProps`
|
@@ -10,17 +9,19 @@ import { omit } from '../utils/omit';
|
|
10
9
|
* Slots will render as null if they are rendered as primitives with undefined children.
|
11
10
|
*
|
12
11
|
* The slotProps will always omit the `as` prop within them, and for slots that are string
|
13
|
-
* primitives, the props will be filtered according
|
14
|
-
* slot is rendered `as: 'a'`, the props will be filtered for acceptable
|
12
|
+
* primitives, the props will be filtered according to the slot type by the type system.
|
13
|
+
* For example, if the slot is rendered `as: 'a'`, the props will be filtered for acceptable
|
14
|
+
* anchor props. Note that this is only enforced at build time by Typescript -- there is no
|
15
|
+
* runtime code filtering props in this function.
|
15
16
|
*
|
16
17
|
* @param state - State including slot definitions
|
17
|
-
* @param slotNames - Name of which props are slots
|
18
18
|
* @returns An object containing the `slots` map and `slotProps` map.
|
19
19
|
*/
|
20
20
|
|
21
|
-
export function getSlots(state
|
21
|
+
export function getSlots(state) {
|
22
22
|
const slots = {};
|
23
23
|
const slotProps = {};
|
24
|
+
const slotNames = Object.keys(state.components);
|
24
25
|
|
25
26
|
for (const slotName of slotNames) {
|
26
27
|
const [slot, props] = getSlot(state, slotName);
|
@@ -38,7 +39,7 @@ function getSlot(state, slotName) {
|
|
38
39
|
var _a, _b, _c;
|
39
40
|
|
40
41
|
if (state[slotName] === undefined) {
|
41
|
-
return [
|
42
|
+
return [null, undefined];
|
42
43
|
}
|
43
44
|
|
44
45
|
const {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["
|
1
|
+
{"version":3,"sources":["compose/getSlots.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AAEA,SAAS,IAAT,QAAqB,eAArB;AA6BA;;;;;;;;;;;;;;;;AAgBG;;AACH,OAAM,SAAU,QAAV,CACJ,KADI,EACoB;AAKxB,QAAM,KAAK,GAAG,EAAd;AACA,QAAM,SAAS,GAAG,EAAlB;AAEA,QAAM,SAAS,GAAgB,MAAM,CAAC,IAAP,CAAY,KAAK,CAAC,UAAlB,CAA/B;;AACA,OAAK,MAAM,QAAX,IAAuB,SAAvB,EAAkC;AAChC,UAAM,CAAC,IAAD,EAAO,KAAP,IAAgB,OAAO,CAAC,KAAD,EAAQ,QAAR,CAA7B;AACA,IAAA,KAAK,CAAC,QAAD,CAAL,GAAkB,IAAlB;AACA,IAAA,SAAS,CAAC,QAAD,CAAT,GAAsB,KAAtB;AACD;;AACD,SAAO;AAAE,IAAA,KAAF;AAAS,IAAA,SAAS,EAAG;AAArB,GAAP;AACD;;AAED,SAAS,OAAT,CACE,KADF,EAEE,QAFF,EAEa;;;AAEX,MAAI,KAAK,CAAC,QAAD,CAAL,KAAoB,SAAxB,EAAmC;AACjC,WAAO,CAAC,IAAD,EAAO,SAAP,CAAP;AACD;;AACD,QAAM;AAAE,IAAA,QAAF;AAAY,IAAA,EAAE,EAAE,MAAhB;AAAwB,OAAG;AAA3B,MAAoC,KAAK,CAAC,QAAD,CAA/C;AAEA,QAAM,IAAI,GAAI,CAAA,CAAA,EAAA,GAAA,KAAK,CAAC,UAAN,MAAgB,IAAhB,IAAgB,EAAA,KAAA,KAAA,CAAhB,GAAgB,KAAA,CAAhB,GAAgB,EAAA,CAAG,QAAH,CAAhB,MAAiC,SAAjC,IAA8C,OAAO,KAAK,CAAC,UAAN,CAAiB,QAAjB,CAAP,KAAsC,QAApF,GACV,MAAM,KAAI,CAAA,EAAA,GAAA,KAAK,CAAC,UAAN,MAAgB,IAAhB,IAAgB,EAAA,KAAA,KAAA,CAAhB,GAAgB,KAAA,CAAhB,GAAgB,EAAA,CAAG,QAAH,CAApB,CAAN,IAA0C,KADhC,GAEV,KAAK,CAAC,UAAN,CAAiB,QAAjB,CAFJ;;AAIA,MAAI,OAAO,QAAP,KAAoB,UAAxB,EAAoC;AAClC,UAAM,MAAM,GAAG,QAAf;AACA,WAAO,CACL,KAAK,CAAC,QADD,EAEJ;AACC,MAAA,QAAQ,EAAE,MAAM,CAAC,IAAD,EAAO,IAAP;AADjB,KAFI,CAAP;AAMD;;AAED,QAAM,gBAAgB,GAAG,OAAO,IAAP,KAAgB,QAAhB,KAA4B,CAAA,EAAA,GAAA,KAAK,CAAC,QAAD,CAAL,MAAe,IAAf,IAAe,EAAA,KAAA,KAAA,CAAf,GAAe,KAAA,CAAf,GAAe,EAAA,CAAE,EAA7C,CAAzB;AACA,QAAM,SAAS,GAAI,gBAAgB,GAAG,IAAI,CAAC,KAAK,CAAC,QAAD,CAAN,EAAmB,CAAC,IAAD,CAAnB,CAAP,GAAoC,KAAK,CAAC,QAAD,CAA5E;AAEA,SAAO,CAAC,IAAD,EAAO,SAAP,CAAP;AACD","sourcesContent":["import * as React from 'react';\n\nimport { omit } from '../utils/omit';\nimport type {\n AsIntrinsicElement,\n ComponentState,\n ExtractSlotProps,\n SlotPropsRecord,\n SlotRenderFunction,\n UnionToIntersection,\n} from './types';\n\nexport type Slots<S extends SlotPropsRecord> = {\n [K in keyof S]: ExtractSlotProps<S[K]> extends AsIntrinsicElement<infer As>\n ? // for slots with an `as` prop, the slot will be any one of the possible values of `as`\n As\n : ExtractSlotProps<S[K]> extends React.ComponentType<infer P>\n ? React.ElementType<NonNullable<P>>\n : React.ElementType<ExtractSlotProps<S[K]>>;\n};\n\ntype ObjectSlotProps<S extends SlotPropsRecord> = {\n [K in keyof S]-?: ExtractSlotProps<S[K]> extends AsIntrinsicElement<infer As>\n ? // For intrinsic element types, return the intersection of all possible\n // element's props, to be compatible with the As type returned by Slots<>\n UnionToIntersection<JSX.IntrinsicElements[As]>\n : ExtractSlotProps<S[K]> extends React.ComponentType<infer P>\n ? P\n : never;\n};\n\n/**\n * Given the state and an array of slot names, will break out `slots` and `slotProps`\n * collections.\n *\n * The root is derived from a mix of `components` props and `as` prop.\n *\n * Slots will render as null if they are rendered as primitives with undefined children.\n *\n * The slotProps will always omit the `as` prop within them, and for slots that are string\n * primitives, the props will be filtered according to the slot type by the type system.\n * For example, if the slot is rendered `as: 'a'`, the props will be filtered for acceptable\n * anchor props. Note that this is only enforced at build time by Typescript -- there is no\n * runtime code filtering props in this function.\n *\n * @param state - State including slot definitions\n * @returns An object containing the `slots` map and `slotProps` map.\n */\nexport function getSlots<R extends SlotPropsRecord>(\n state: ComponentState<R>,\n): {\n slots: Slots<R>;\n slotProps: ObjectSlotProps<R>;\n} {\n const slots = {} as Slots<R>;\n const slotProps = {} as R;\n\n const slotNames: (keyof R)[] = Object.keys(state.components);\n for (const slotName of slotNames) {\n const [slot, props] = getSlot(state, slotName);\n slots[slotName] = slot as Slots<R>[typeof slotName];\n slotProps[slotName] = props;\n }\n return { slots, slotProps: (slotProps as unknown) as ObjectSlotProps<R> };\n}\n\nfunction getSlot<R extends SlotPropsRecord, K extends keyof R>(\n state: ComponentState<R>,\n slotName: K,\n): readonly [React.ElementType<R[K]> | null, R[K]] {\n if (state[slotName] === undefined) {\n return [null, undefined as R[K]];\n }\n const { children, as: asProp, ...rest } = state[slotName]!;\n\n const slot = (state.components?.[slotName] === undefined || typeof state.components[slotName] === 'string'\n ? asProp || state.components?.[slotName] || 'div'\n : state.components[slotName]) as React.ElementType<R[K]>;\n\n if (typeof children === 'function') {\n const render = children as SlotRenderFunction<R[K]>;\n return [\n React.Fragment,\n ({\n children: render(slot, rest as Omit<R[K], 'children' | 'as'>),\n } as unknown) as R[K],\n ];\n }\n\n const shouldOmitAsProp = typeof slot === 'string' && state[slotName]?.as;\n const slotProps = (shouldOmitAsProp ? omit(state[slotName]!, ['as']) : state[slotName]) as R[K];\n\n return [slot, slotProps];\n}\n"],"sourceRoot":"../src/"}
|
package/lib/compose/index.d.ts
CHANGED
package/lib/compose/index.js
CHANGED
package/lib/compose/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"../src/","sources":["compose/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,oBAAoB,CAAC;AACnC,cAAc,SAAS,CAAC","sourcesContent":["export * from './getSlots';\nexport * from './resolveShorthand';\nexport * from './types';\n"]}
|
@@ -1,12 +1,16 @@
|
|
1
|
-
import type {
|
2
|
-
export declare type ResolveShorthandOptions<Props
|
1
|
+
import type { ReplaceNullWithUndefined, SlotShorthandValue, UnknownSlotProps } from './types';
|
2
|
+
export declare type ResolveShorthandOptions<Props, Required extends boolean = false> = {
|
3
3
|
required?: Required;
|
4
4
|
defaultProps?: Props;
|
5
5
|
};
|
6
|
+
export declare type ResolveShorthandFunction<Props extends UnknownSlotProps = UnknownSlotProps> = {
|
7
|
+
<P extends Props | null>(value: P | SlotShorthandValue | undefined, options?: ResolveShorthandOptions<P, true>): ReplaceNullWithUndefined<P>;
|
8
|
+
<P extends Props | null>(value: P | SlotShorthandValue | undefined, options?: ResolveShorthandOptions<P, boolean>): ReplaceNullWithUndefined<P> | undefined;
|
9
|
+
};
|
6
10
|
/**
|
7
|
-
* Resolves
|
11
|
+
* Resolves shorthands into slot props, to ensure normalization of the signature
|
8
12
|
* being passed down to getSlots method
|
9
|
-
* @param value - the base
|
10
|
-
* @param options - options to resolve
|
13
|
+
* @param value - the base shorthand props
|
14
|
+
* @param options - options to resolve shorthand props
|
11
15
|
*/
|
12
|
-
export declare
|
16
|
+
export declare const resolveShorthand: ResolveShorthandFunction;
|