praxis-kit 4.0.0 → 4.0.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.
@@ -1,8 +1,364 @@
1
- import { ElementType, AnyRecord, EmptyRecord, VariantMap, RecipeMap, ClassPluginFactory, FactoryOptions, ExtractPluginProps } from '@praxis-kit/core';
2
- export { AnyFactoryOptions } from '@praxis-kit/core';
3
- import { FilterPredicate } from '@praxis-kit/adapter-utils';
4
- export { defineContractComponent } from '@praxis-kit/adapter-utils';
5
- import { Diagnostics } from '@praxis-kit/diagnostics';
1
+ import { Except, RequireAtLeastOne, Simplify, ReadonlyDeep } from 'type-fest';
2
+
3
+ type StringMap<T = unknown> = Record<string, T>;
4
+ type AnyRecord = StringMap<unknown>;
5
+
6
+ type IntrinsicTag = keyof HTMLElementTagNameMap;
7
+
8
+ type ElementType = IntrinsicTag | (string & {});
9
+
10
+ declare const KNOWN_ARIA_ROLES: readonly ["alert", "alertdialog", "application", "article", "banner", "blockquote", "button", "caption", "cell", "checkbox", "code", "columnheader", "combobox", "complementary", "contentinfo", "definition", "deletion", "dialog", "document", "emphasis", "feed", "figure", "form", "generic", "grid", "gridcell", "group", "heading", "img", "insertion", "link", "list", "listbox", "listitem", "log", "main", "marquee", "math", "menu", "menubar", "menuitem", "menuitemcheckbox", "menuitemradio", "meter", "navigation", "none", "note", "option", "paragraph", "presentation", "progressbar", "radio", "radiogroup", "region", "row", "rowgroup", "rowheader", "scrollbar", "search", "searchbox", "separator", "slider", "spinbutton", "status", "strong", "subscript", "superscript", "switch", "tab", "table", "tablist", "tabpanel", "term", "textbox", "time", "timer", "toolbar", "tooltip", "tree", "treegrid", "treeitem"];
11
+ type KnownAriaRole = (typeof KNOWN_ARIA_ROLES)[number];
12
+
13
+ type Booleanish = boolean | 'true' | 'false';
14
+ type ClassName = string | string[];
15
+ type EmptyRecord = Record<never, never>;
16
+ type NonEmptyArray<T> = [T, ...T[]];
17
+ type Numberish = number | `${number}`;
18
+ type Primitive = string | number | boolean;
19
+ type AriaRole = KnownAriaRole | (string & {});
20
+ type IntrinsicProps = AnyRecord & {
21
+ role?: AriaRole;
22
+ };
23
+ type TagMap = Partial<Record<IntrinsicTag | (string & {}), ClassName>>;
24
+
25
+ declare enum DiagnosticCategory {
26
+ Contract = 0,
27
+ HTML = 1,
28
+ ARIA = 2,
29
+ Composition = 3,
30
+ Rendering = 4,
31
+ Accessibility = 5,
32
+ Performance = 6,
33
+ Internal = 7,
34
+ Deprecation = 8,
35
+ Lint = 9
36
+ }
37
+
38
+ declare enum DiagnosticCode {
39
+ MissingRequiredChild = "COMP1001",
40
+ InvalidParent = "COMP1002",
41
+ InvalidChild = "COMP1003",
42
+ UnexpectedChild = "COMP1004",
43
+ AmbiguousChild = "COMP1005",
44
+ CardinalityMin = "COMP1006",
45
+ CardinalityMax = "COMP1007",
46
+ PositionViolation = "COMP1008",
47
+ AllowedAsViolation = "COMP1009",
48
+ SlotExclusive = "SLOT1001",
49
+ SlotSingleChild = "SLOT1002",
50
+ SlotDiscardedChildren = "SLOT1003",
51
+ SlotRenderFn = "SLOT1004",
52
+ MissingAriaRelationship = "ARIA2001",
53
+ AriaViolation = "ARIA2002",
54
+ AriaAttributeInvalid = "ARIA2003",
55
+ AriaMissingLiveRegion = "ARIA2004",
56
+ AriaMissingAtomic = "ARIA2005",
57
+ AriaRelevantInvalidToken = "ARIA2006",
58
+ AriaRelevantSuperseded = "ARIA2007",
59
+ AriaInvalidRole = "ARIA2008",
60
+ AriaMissingAccessibleName = "ARIA2009",
61
+ AriaAttributeOnPresentational = "ARIA2010",
62
+ AriaHiddenOnFocusable = "ARIA2011",
63
+ AriaRequiredProperty = "ARIA2012",
64
+ AriaInvalidAttributeValue = "ARIA2013",
65
+ AriaRedundantLevelAttribute = "ARIA2014",
66
+ InvalidHeadingHierarchy = "HTML3001",
67
+ HtmlEmptyRole = "HTML3002",
68
+ HtmlImplicitRoleRedundant = "HTML3003",
69
+ HtmlImplicitRoleOverride = "HTML3004",
70
+ HtmlStandaloneRegionOverride = "HTML3005",
71
+ HtmlLandmarkRoleOverride = "HTML3006",
72
+ HtmlInvalidChild = "HTML3007",
73
+ InvalidRenderingTarget = "RENDER4001",
74
+ LintDeadCompoundKey = "LINT5001",
75
+ LintDeadCompoundValue = "LINT5002",
76
+ LintDeadCompoundNonLiteral = "LINT5003",
77
+ LintMissingStrict = "LINT5004",
78
+ LintInvalidDefaultKey = "LINT5005",
79
+ LintInvalidDefaultValue = "LINT5006",
80
+ LintInvalidDefaultNonLiteral = "LINT5007",
81
+ LintNegativeMin = "LINT5008",
82
+ LintNegativeMax = "LINT5009",
83
+ LintMaxLessThanMin = "LINT5010",
84
+ LintZeroMax = "LINT5011",
85
+ LintMultipleFirst = "LINT5012",
86
+ LintMultipleLast = "LINT5013",
87
+ LintMinSumExceedsCapacity = "LINT5014",
88
+ LintCardinalityViolation = "LINT5015",
89
+ LintAriaTagOverride = "LINT5016",
90
+ ContractUnknownVariantDim = "COMP1010",
91
+ ContractUnknownVariantValue = "COMP1011",
92
+ ContractUnknownRecipeKey = "COMP1012",
93
+ ContractInvalidVariantValue = "COMP1013",
94
+ TailwindMultipleDisplayProps = "CSS6001",
95
+ TailwindReservedLayoutLiteral = "CSS6002",
96
+ TailwindDeadVariantClass = "CSS6003",
97
+ PluginInvalidShape = "PLUGIN7001",
98
+ PluginPipelineReturnType = "PLUGIN7002",
99
+ InternalError = "INTERNAL9000"
100
+ }
101
+
102
+ interface SourcePosition {
103
+ line: number;
104
+ col: number;
105
+ }
106
+ interface SourceLocation {
107
+ file: string;
108
+ start: SourcePosition;
109
+ end?: SourcePosition;
110
+ }
111
+
112
+ declare enum Severity$1 {
113
+ Debug = 0,
114
+ Info = 1,
115
+ Warning = 2,
116
+ Error = 3,
117
+ Fatal = 4
118
+ }
119
+
120
+ interface DiagnosticSuggestion {
121
+ title: string;
122
+ description?: string;
123
+ fix?: string;
124
+ }
125
+
126
+ type Context = AnyRecord;
127
+ type Metadata = AnyRecord;
128
+ interface Diagnostic {
129
+ code: DiagnosticCode;
130
+ severity: Severity$1;
131
+ category: DiagnosticCategory;
132
+ message: string;
133
+ rationale?: string;
134
+ component?: string;
135
+ contract?: string;
136
+ location?: SourceLocation;
137
+ suggestions?: DiagnosticSuggestion[];
138
+ context?: Context;
139
+ metadata?: Metadata;
140
+ }
141
+
142
+ declare enum Enforcement {
143
+ Ignore = 0,
144
+ Report = 1,
145
+ Throw = 2
146
+ }
147
+ interface DiagnosticPolicy {
148
+ resolve(diagnostic: Diagnostic): Enforcement;
149
+ }
150
+
151
+ interface DiagnosticReporter {
152
+ report(diagnostic: Diagnostic): void;
153
+ }
154
+
155
+ type DiagnosticInput = Except<Diagnostic, 'severity'>;
156
+ declare class Diagnostics {
157
+ private readonly reporter;
158
+ private readonly policy;
159
+ readonly active: boolean;
160
+ constructor(reporter: DiagnosticReporter, policy?: DiagnosticPolicy);
161
+ report(diagnostic: Diagnostic): Diagnostic;
162
+ warn(input: DiagnosticInput): Diagnostic;
163
+ error(input: DiagnosticInput): Diagnostic;
164
+ info(input: DiagnosticInput): Diagnostic;
165
+ }
166
+
167
+ type MinMax = {
168
+ min: number;
169
+ max: number;
170
+ };
171
+ type CardinalityInput = Partial<MinMax>;
172
+
173
+ type ChildRuleMatch<T, U extends T = T> = (child: T) => child is U;
174
+
175
+ type ChildRulePosition = 'first' | 'last' | 'any';
176
+
177
+ type ChildRuleInput<T = unknown, U extends T = T> = {
178
+ name: string;
179
+ match: ChildRuleMatch<T, U>;
180
+ cardinality?: CardinalityInput;
181
+ position?: ChildRulePosition;
182
+ /**
183
+ * Optional component-type reference for O(1) dispatch index.
184
+ * When provided for every rule, the matcher reads child.type instead of
185
+ * calling every match function on every child (O(n×m) → O(n+m)).
186
+ */
187
+ type?: unknown;
188
+ };
189
+
190
+ type ValidResult = {
191
+ valid: true;
192
+ };
193
+
194
+ type RequireAtLeastOneIfNotEmpty<T> = keyof T extends never ? EmptyRecord : RequireAtLeastOne<T>;
195
+ type CompoundVariantConditionValue<V extends VariantMap, K extends keyof V> = VariantKey<V, K> | NonEmptyArray<VariantKey<V, K>>;
196
+ type CompoundVariantConditions<V extends VariantMap> = Simplify<{
197
+ [K in keyof V]: CompoundVariantConditionValue<V, K>;
198
+ }>;
199
+ type CompoundVariantRequiredConditions<V extends VariantMap> = RequireAtLeastOneIfNotEmpty<CompoundVariantConditions<V>>;
200
+ type CompoundVariantBase<V extends VariantMap> = keyof V extends never ? EmptyRecord : CompoundVariantRequiredConditions<V>;
201
+ type CompoundVariant<V extends VariantMap> = CompoundVariantBase<V> & {
202
+ class: VariantValue;
203
+ };
204
+
205
+ interface CVACompounds<V extends VariantMap> {
206
+ compoundVariants?: readonly CompoundVariant<V>[];
207
+ }
208
+
209
+ interface CVADefaults<V extends VariantMap> {
210
+ defaultVariants?: DefaultVariants<V>;
211
+ }
212
+
213
+ interface CVAVariants<V extends VariantMap> {
214
+ variants?: V;
215
+ }
216
+
217
+ type StringToBoolean<T> = T extends 'true' | 'false' ? boolean : T;
218
+ type VariantValue = string | string[];
219
+ type VariantStates<K extends string = string> = Record<K, VariantValue>;
220
+ type VariantMap<V extends string = string, K extends string = string> = Record<V, VariantStates<K>>;
221
+ type VariantKey<V extends VariantMap, K extends keyof V> = StringToBoolean<keyof V[K] & string>;
222
+ /**
223
+ * A partial selection of variant states authored at factory definition time.
224
+ *
225
+ * Uses `keyof V[K]` directly (not `VariantKey`) so TypeScript can eagerly
226
+ * resolve the union at constraint-check time without deferred conditional types.
227
+ */
228
+ type VariantSelection<V extends VariantMap> = {
229
+ [K in keyof V]?: keyof V[K];
230
+ };
231
+ type NormalizedVariantValue<K extends string> = string extends K ? Primitive : K extends 'true' | 'false' ? Booleanish : K extends `${number}` ? Numberish : K;
232
+ type DefaultVariants<V extends VariantMap> = {
233
+ [K in keyof V]?: NormalizedVariantValue<keyof V[K] & string>;
234
+ };
235
+ /**
236
+ * A static, immutable map of named presets to partial variant selections.
237
+ *
238
+ * Presets are named bundles of variant props that callers activate by key,
239
+ * avoiding the need to repeat variant combinations at each call site.
240
+ */
241
+ type RecipeMap<V extends VariantMap = VariantMap> = Readonly<Record<string, VariantSelection<V>>>;
242
+ type RecipeTarget<TVariants extends VariantMap = VariantMap> = VariantSelection<TVariants>;
243
+
244
+ interface BaseClassOptions {
245
+ baseClassName?: ClassName;
246
+ }
247
+
248
+ type ClassPipelineFn = (tag: unknown, props: AnyRecord, className?: ClassName, recipe?: string) => string;
249
+
250
+ interface RecipeOptions<TVariants extends VariantMap = VariantMap> {
251
+ recipeMap?: Record<string, RecipeTarget<TVariants>>;
252
+ }
253
+
254
+ interface TagMapOptions {
255
+ tagMap?: TagMap;
256
+ }
257
+
258
+ type CompositionOptions<TVariants extends VariantMap = VariantMap> = Simplify<TagMapOptions & RecipeOptions<TVariants>>;
259
+
260
+ type CVASystemOptions<TVariants extends VariantMap = VariantMap> = Simplify<CVAVariants<TVariants> & CVADefaults<TVariants> & CVACompounds<TVariants>>;
261
+
262
+ type StyleOptions<TVariants extends VariantMap = VariantMap> = Simplify<BaseClassOptions & CVASystemOptions<TVariants>>;
263
+
264
+ type ClassPipelineOptions<TVariants extends VariantMap = VariantMap> = Simplify<StyleOptions<TVariants> & CompositionOptions<TVariants>>;
265
+
266
+ type OwnedPropKeys = ReadonlySet<string>;
267
+
268
+ type ClassPlugin<TProps extends AnyRecord = EmptyRecord> = Readonly<{
269
+ pipeline: ClassPipelineFn;
270
+ ownedKeys?: OwnedPropKeys;
271
+ readonly _pluginProps?: TProps;
272
+ }>;
273
+
274
+ type ClassPluginFactory<TProps extends AnyRecord = EmptyRecord> = <V extends VariantMap>(options: ClassPipelineOptions<V>, diagnostics: Diagnostics) => ClassPlugin<TProps>;
275
+ type ExtractPluginProps<TPlugin extends ClassPluginFactory<AnyRecord> | undefined> = TPlugin extends ClassPluginFactory<infer T> ? string extends keyof T ? EmptyRecord : T : EmptyRecord;
276
+
277
+ type AriaContext = {
278
+ readonly tag: IntrinsicTag;
279
+ readonly implicitRole: AriaRole | undefined;
280
+ readonly effectiveRole: string | undefined;
281
+ readonly props: ReadonlyDeep<IntrinsicProps>;
282
+ };
283
+
284
+ type RemoveAttributeFixKind = `removeAttribute:${string}`;
285
+ type InjectLiveFixKind = `injectLive:${string}`;
286
+ type FixKind = 'removeRole' | 'setRole' | 'normalizeRelevantAll' | RemoveAttributeFixKind | InjectLiveFixKind;
287
+
288
+ type AriaFixResult = {
289
+ applied: false;
290
+ next: ReadonlyDeep<IntrinsicProps>;
291
+ } | {
292
+ applied: true;
293
+ next: ReadonlyDeep<IntrinsicProps>;
294
+ previous: ReadonlyDeep<IntrinsicProps>;
295
+ };
296
+ type AriaFix = {
297
+ readonly kind: FixKind;
298
+ readonly priority?: number;
299
+ readonly source?: string;
300
+ readonly apply: (context: AriaContext) => AriaFixResult;
301
+ };
302
+
303
+ type Severity = 'error' | 'warning' | (string & {});
304
+
305
+ type AriaInvalidBase<M extends string = string> = {
306
+ valid: false;
307
+ severity: Severity;
308
+ message?: M;
309
+ attribute?: string;
310
+ diagnostic?: DiagnosticInput;
311
+ };
312
+ type AriaInvalidWithFix<M extends string = string> = AriaInvalidBase<M> & {
313
+ fixable: true;
314
+ fix: AriaFix;
315
+ };
316
+ type AriaInvalidWithoutFix<M extends string = string> = AriaInvalidBase<M> & {
317
+ fixable: false;
318
+ };
319
+ type AriaInvalidResult<M extends string = string> = AriaInvalidWithFix<M> | AriaInvalidWithoutFix<M>;
320
+ type AriaResult = ValidResult | AriaInvalidResult;
321
+
322
+ type AriaRule<C extends AriaContext = AriaContext> = (context: C) => readonly AriaResult[];
323
+
324
+ type PropNormalizer = (props: Readonly<AnyRecord & IntrinsicProps>) => Partial<AnyRecord & IntrinsicProps>;
325
+
326
+ type EnforcementOptions<TAllowed extends ElementType = ElementType> = {
327
+ readonly diagnostics?: Diagnostics;
328
+ readonly aria?: readonly AriaRule[];
329
+ readonly children?: readonly ChildRuleInput[];
330
+ readonly props?: readonly PropNormalizer[];
331
+ /** Restricts the `as` prop to this set of tags. Violations route through diagnostics. */
332
+ readonly allowedAs?: readonly TAllowed[];
333
+ };
334
+
335
+ type StylingOptions<V extends Readonly<VariantMap> = Readonly<EmptyRecord>, TPreset extends RecipeMap<V> = Readonly<EmptyRecord>, TPlugin extends ClassPluginFactory<AnyRecord> | undefined = ClassPluginFactory<AnyRecord> | undefined> = {
336
+ readonly base?: ClassName;
337
+ readonly variants?: V;
338
+ readonly defaults?: Partial<DefaultVariants<V>>;
339
+ readonly compounds?: readonly CompoundVariant<V>[];
340
+ readonly presets?: TPreset;
341
+ readonly tags?: Readonly<TagMap>;
342
+ readonly plugin?: TPlugin;
343
+ readonly precomputedClasses?: Readonly<Record<string, string>>;
344
+ };
345
+
346
+ type NormalizeFn<Props extends AnyRecord = AnyRecord> = {
347
+ normalize(props: Readonly<Props & IntrinsicProps>): Props & IntrinsicProps;
348
+ }['normalize'];
349
+ type AnyFactoryOptions = FactoryOptions<ElementType, AnyRecord, VariantMap, RecipeMap<VariantMap>, ClassPluginFactory<AnyRecord> | undefined>;
350
+ type FactoryOptions<TDefault extends ElementType = ElementType, Props extends AnyRecord = EmptyRecord, V extends Readonly<VariantMap> = Readonly<EmptyRecord>, TPreset extends RecipeMap<V> = Readonly<EmptyRecord>, TPlugin extends ClassPluginFactory<AnyRecord> | undefined = ClassPluginFactory<AnyRecord> | undefined, TAllowed extends ElementType = ElementType> = {
351
+ readonly tag?: TDefault;
352
+ readonly name?: string;
353
+ readonly defaults?: Partial<NoInfer<Props>>;
354
+ readonly normalize?: NormalizeFn<NoInfer<Props>>;
355
+ readonly styling?: StylingOptions<V, TPreset, TPlugin>;
356
+ readonly enforcement?: EnforcementOptions<TAllowed>;
357
+ };
358
+
359
+ type FilterPredicate = (key: string, variantKeys: ReadonlySet<string>) => boolean;
360
+
361
+ declare function defineContractComponent<O extends FactoryOptions>(options: O): <R>(factory: (options: O) => R) => R;
6
362
 
7
363
  /**
8
364
  * Options accepted by createContractComponent in the web adapter.
@@ -75,4 +431,4 @@ declare function createContractComponent<TDefault extends ElementType, TProps ex
75
431
  */
76
432
  declare function renderToString(component: WebContractComponent, props?: UnknownProps, innerHTML?: string): string;
77
433
 
78
- export { type WebContractComponent, type WebFactoryOptions, createContractComponent, renderToString };
434
+ export { type AnyFactoryOptions, type WebContractComponent, type WebFactoryOptions, createContractComponent, defineContractComponent, renderToString };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "praxis-kit",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "./react": {
@@ -130,8 +130,8 @@
130
130
  "vite": "^8.0.16",
131
131
  "vue": "^3.5.38",
132
132
  "@praxis-kit/adapter-utils": "0.0.0",
133
- "@praxis-kit/core": "0.0.0",
134
133
  "@praxis-kit/diagnostics": "0.0.0",
134
+ "@praxis-kit/core": "0.0.0",
135
135
  "@praxis-kit/primitive": "0.0.0"
136
136
  },
137
137
  "publishConfig": {
@@ -171,7 +171,7 @@
171
171
  "node": ">=18"
172
172
  },
173
173
  "scripts": {
174
- "build": "tsup",
174
+ "build": "rm -rf dist && tsup",
175
175
  "dev": "tsup --watch",
176
176
  "lint": "eslint . --fix",
177
177
  "lint:check": "eslint .",
@@ -1,144 +0,0 @@
1
- import { ComponentType, PropsWithChildren, ReactElement, JSX, Ref, ReactNode } from 'react';
2
- import { ElementType, VariantMap, RecipeMap, EmptyRecord, ClassPluginFactory, AnyRecord, FactoryOptions, IntrinsicTag, PolymorphicGenerics, DefaultOf, PropsOf, VariantProps, VariantsOf, AllowedOf, ClassName, RecipeOf } from '@praxis-kit/core';
3
- import { ComponentDefinition } from '@praxis-kit/runtime';
4
- import { Simplify, NonEmptyTuple } from 'type-fest';
5
-
6
- type UnknownProps = Record<string, unknown>;
7
- type SlotComponent = ComponentType<UnknownProps>;
8
-
9
- /**
10
- * Props passed to the `render` callback — the component's resolved className,
11
- * filtered own props, and ref. Spread these onto the target element.
12
- *
13
- * Typed loosely to accommodate any element tag the user chooses.
14
- */
15
- type RenderCallbackProps = Readonly<Record<string, unknown>>;
16
-
17
- type SlottableProps = PropsWithChildren;
18
- declare function Slottable({ children }: SlottableProps): ReactElement;
19
-
20
- /** Structural subset of `CompiledComponentArtifact` consumed by the React adapter. */
21
- interface CompiledArtifact {
22
- readonly definition: ComponentDefinition;
23
- readonly precomputed?: {
24
- readonly variantLookup?: Record<string, string>;
25
- };
26
- }
27
- /**
28
- * Extends FactoryOptions with React-specific configuration.
29
- * slotComponent is intentionally not in core — it is a React rendering concern.
30
- */
31
- type ReactFactoryOptions<TDefault extends ElementType, Props extends UnknownProps, Variants extends Readonly<VariantMap>, TPreset extends RecipeMap<Variants> = Readonly<EmptyRecord>, TPlugin extends ClassPluginFactory<AnyRecord> | undefined = ClassPluginFactory<AnyRecord> | undefined, TAllowed extends ElementType = ElementType> = FactoryOptions<TDefault, Props, Variants, TPreset, TPlugin, TAllowed> & {
32
- /** Component used to render the asChild slot. Defaults to the built-in Slot. */
33
- slotComponent?: SlotComponent;
34
- /**
35
- * Return true for any prop key that should be consumed but not forwarded to the DOM.
36
- * The adapter strips nothing by default — implementations decide what is safe to drop.
37
- * Receives `runtime.options.variantKeys` as a convenience if needed.
38
- */
39
- filterProps?: (key: string, variantKeys: ReadonlySet<string>) => boolean;
40
- /** Pre-compiled artifact from `@praxis-kit/runtime`'s compiler. When provided, replaces the stub
41
- * definition and enables the precomputed variant lookup fast path. */
42
- artifact?: CompiledArtifact;
43
- };
44
-
45
- /** Maps a core ElementType to its DOM instance type for ref inference. */
46
- type ElementRef<T extends ElementType> = T extends IntrinsicTag ? HTMLElementTagNameMap[T] : unknown;
47
- /** @internal React JSX intrinsic props for a given element type. */
48
- type IntrinsicJSXProps<T extends ElementType> = T extends IntrinsicTag ? JSX.IntrinsicElements[T] : UnknownProps;
49
- /**
50
- * @internal
51
- * Removes index signatures (e.g. `[key: string]: T`) from a type, keeping only
52
- * explicitly named properties. Used to prevent fallback-to-constraint generics
53
- * like `Record<string, unknown>` or `Readonly<VariantMap>` from producing a
54
- * `[key: string]: T` member that makes `keyof` resolve to `string` — which
55
- * would cause `Omit<IntrinsicJSXProps<TAs>, string>` to remove all HTML props.
56
- */
57
- type StripIndexSignature<T> = {
58
- [K in keyof T as string extends K ? never : K]: T[K];
59
- };
60
- type ComponentProps<G extends PolymorphicGenerics> = StripIndexSignature<PropsOf<G>>;
61
- type ComponentVariants<G extends PolymorphicGenerics> = StripIndexSignature<VariantProps<VariantsOf<G>>>;
62
- type OwnedProps<G extends PolymorphicGenerics> = ComponentProps<G> & ComponentVariants<G>;
63
- /**
64
- * @internal
65
- * Polymorphic machinery props. Separated from `OwnedProps` so the two concerns
66
- * are distinct: user-defined props vs. the tag/ref/class system.
67
- *
68
- * `asChild` and `children` are excluded — typed separately in the discriminated
69
- * union below so each render mode can enforce different child constraints.
70
- */
71
- type PolymorphicControlProps<G extends PolymorphicGenerics, TAs extends ElementType> = {
72
- as?: TAs & AllowedOf<G>;
73
- className?: ClassName | undefined;
74
- recipe?: keyof RecipeOf<G>;
75
- ref?: Ref<ElementRef<TAs>>;
76
- };
77
- /** @internal Full set of props owned by the component — used as the Omit key in IntrinsicPropsWithoutOwned. */
78
- type ControlProps<G extends PolymorphicGenerics, TAs extends ElementType> = OwnedProps<G> & PolymorphicControlProps<G, TAs>;
79
- type IntrinsicPropsWithoutOwned<G extends PolymorphicGenerics, TAs extends ElementType> = Omit<IntrinsicJSXProps<TAs>, keyof ControlProps<G, TAs> | 'children'>;
80
- type SharedProps<G extends PolymorphicGenerics, TAs extends ElementType> = IntrinsicPropsWithoutOwned<G, TAs> & ControlProps<G, TAs>;
81
- /** Discriminant for the normal render path: `asChild` absent or false, any children. */
82
- type NormalRenderMode = {
83
- asChild?: false;
84
- children?: ReactNode | undefined;
85
- };
86
- /**
87
- * Discriminant for the slot render path. One or more `ReactElement` children required.
88
- * `as` is forbidden — combining `as` with `asChild` is a runtime invariant violation.
89
- */
90
- type SlotRenderMode = {
91
- asChild: true;
92
- as?: never;
93
- children: ReactElement | NonEmptyTuple<ReactElement>;
94
- };
95
- /**
96
- * Discriminant for the render-prop path. The callback receives all resolved props
97
- * (className, ref, filtered component props) and returns the target element.
98
- *
99
- * This is the output form for the compile-time `asChild` transform: keeps the same
100
- * rendering flexibility as `asChild` without the `cloneElement` cost at runtime.
101
- * Unlike `asChild`, the render callback does not auto-merge conflicting event
102
- * handlers — spread position determines precedence.
103
- *
104
- * ```tsx
105
- * <Button render={(p) => <a href="/home" {...p} />} size="lg" />
106
- * ```
107
- */
108
- type CallbackRenderMode = {
109
- render: (props: RenderCallbackProps) => ReactElement;
110
- asChild?: never;
111
- children?: never;
112
- };
113
- /**
114
- * Props for the normal render path. HTML attributes are inferred from `TAs`.
115
- *
116
- * `Omit + intersection` (not `Merge`) is used so TypeScript can infer `TAs` from
117
- * the `as` prop value; control props win on key conflicts.
118
- */
119
- type PolymorphicProps<G extends PolymorphicGenerics, TAs extends ElementType = DefaultOf<G>> = Simplify<SharedProps<G, TAs> & NormalRenderMode>;
120
- /**
121
- * Props for the slot render path (`asChild: true`). One or more `ReactElement`
122
- * children are required. Multiple children are permitted for the Slottable
123
- * sibling pattern where one child is a `<Slottable>` wrapper.
124
- */
125
- type PolymorphicWithAsChild<G extends PolymorphicGenerics, TAs extends ElementType = DefaultOf<G>> = Simplify<SharedProps<G, TAs> & SlotRenderMode>;
126
- type PolymorphicWithRender<G extends PolymorphicGenerics, TAs extends ElementType = DefaultOf<G>> = Simplify<SharedProps<G, TAs> & CallbackRenderMode>;
127
- /**
128
- * A polymorphic component that infers HTML attributes and ref type from the `as` prop.
129
- *
130
- * Three call signatures form a discriminated union:
131
- * - `render` present → render-prop path; no Slot or cloneElement at runtime
132
- * - `asChild: true` → slot path; exactly one `ReactElement` child required
133
- * - `asChild?: false` → normal path; any `ReactNode` children accepted
134
- */
135
- type PolymorphicComponent<G extends PolymorphicGenerics> = {
136
- <TAs extends ElementType = DefaultOf<G>>(props: PolymorphicWithRender<G, TAs>): ReactElement;
137
- <TAs extends ElementType = DefaultOf<G>>(props: PolymorphicWithAsChild<G, TAs>): ReactElement;
138
- <TAs extends ElementType = DefaultOf<G>>(props: PolymorphicProps<G, TAs>): ReactElement;
139
- displayName?: string;
140
- };
141
-
142
- declare function mergeRefs<T>(...refs: (Ref<T> | null | undefined)[]): Ref<T> | null;
143
-
144
- export { type ElementRef as E, type PolymorphicComponent as P, type ReactFactoryOptions as R, Slottable as S, type UnknownProps as U, type PolymorphicProps as a, type PolymorphicWithAsChild as b, type PolymorphicWithRender as c, type RenderCallbackProps as d, type SlottableProps as e, mergeRefs as m };