praxis-kit 7.4.0 → 7.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import { RequireAtLeastOne, Simplify, ReadonlyDeep, OmitIndexSignature } from 'type-fest';
2
2
  import { Diagnostics, DiagnosticInput, DiagnosticsMode } from '../_shared/diagnostics.js';
3
3
  import { ComponentChildren, VNode, ComponentType, JSX, Ref } from 'preact';
4
+ import { HasGenerics, Mode, PickMode } from '@praxis-kit/contract-props';
4
5
 
5
6
  /**
6
7
  * A string-keyed object whose values are of type `T`.
@@ -72,6 +73,19 @@ type MergeRecords<A extends object, B extends object> = IsEmptyRecord<A> extends
72
73
  type IntrinsicTag = keyof HTMLElementTagNameMap;
73
74
 
74
75
  type ElementType = IntrinsicTag | (string & {});
76
+ /**
77
+ * Resolves a component's default tag to its real DOM interface — `HTMLDialogElement` for
78
+ * `'dialog'`, `HTMLDetailsElement` for `'details'`, and so on — falling back to `HTMLElement`
79
+ * for custom-element tags or anything not in `HTMLElementTagNameMap`. Used to type
80
+ * `FactoryOptions.onElement`'s `element` param so component authors get direct, correctly-typed
81
+ * access to tag-specific native members (`dialogEl.showModal()`) without an unsafe cast.
82
+ *
83
+ * The fallback is `HTMLElement`, not the more generic `Element` — every tag reachable through
84
+ * `IntrinsicTag` extends it, and so does every custom element per spec, so members `HTMLElement`
85
+ * itself declares (`showPopover()`/`hidePopover()`/`togglePopover()`, the `popover` attribute)
86
+ * stay directly accessible even for tags with no dedicated entry in `HTMLElementTagNameMap`.
87
+ */
88
+ type ElementForTag<TDefault extends ElementType> = TDefault extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[TDefault] : HTMLElement;
75
89
 
76
90
  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"];
77
91
  type KnownAriaRole = (typeof KNOWN_ARIA_ROLES)[number];
@@ -176,20 +190,75 @@ type DefaultVariants<V extends VariantMap> = {
176
190
  * Presets are named bundles of variant props that callers activate by key,
177
191
  * avoiding the need to repeat variant combinations at each call site.
178
192
  */
179
- type RecipeMap<V extends VariantMap = VariantMap> = Readonly<Record<string, VariantSelection<V>>>;
193
+ type RecipeMap<V extends VariantMap = VariantMap> = Readonly<StringMap<VariantSelection<V>>>;
180
194
 
181
195
  type RecipeTarget<TVariants extends VariantMap = VariantMap> = VariantSelection<TVariants>;
182
196
 
183
- interface PolymorphicGenerics<TDefault extends ElementType = ElementType, Props extends AnyRecord = AnyRecord, Variants extends Readonly<VariantMap> = Readonly<VariantMap>, TPreset extends RecipeMap<Variants> = Readonly<EmptyRecord>, TAllowed extends ElementType = ElementType> {
197
+ /**
198
+ * The framework-neutral descriptor for a single praxis-kit component's contract — every render
199
+ * mechanism (tag resolution, prop merging, classes, ARIA) and every framework adapter's
200
+ * component type is built from this one shape. Deliberately just data: five type parameters and
201
+ * their corresponding properties, with no notion of JSX, call signatures, refs, or any
202
+ * framework-specific rendering concern. Each adapter (React, Vue, Svelte, Solid, Lit, Web) builds
203
+ * its own idiomatic component type on top of a `PolymorphicGenerics<...>` instantiation — see
204
+ * `PolymorphicComponent<G>` (`adapters/react/src/shared/types/polymorphic-props.ts`) for the
205
+ * React example — rather than this interface knowing anything about any of them.
206
+ *
207
+ * Use the `*Of<T>` accessor aliases below (`DefaultOf<G>`, `PropsOf<G>`, etc.) to read a single
208
+ * field back out of an already-resolved `G`, instead of indexing `G['default']` etc. directly at
209
+ * call sites — same rationale as any accessor: the property name stays an implementation detail,
210
+ * and every reader benefits together if it ever needs to change.
211
+ */
212
+ interface PolymorphicGenerics<
213
+ /**
214
+ * The element/tag this component renders as when the consumer doesn't override it via `as`
215
+ * (`AllowedOf<G>` permitting) — e.g. `'button'`, `'div'`. Defaults to the widest `ElementType`
216
+ * so a generic `PolymorphicGenerics` reference (with nothing else specified) still compiles.
217
+ */
218
+ TDefault extends ElementType = ElementType,
219
+ /**
220
+ * The props this specific component declares — its own contract, before variants are mixed
221
+ * in. Defaults to `AnyRecord` for the same "still compiles unspecified" reason as `TDefault`.
222
+ */
223
+ Props extends AnyRecord = AnyRecord,
224
+ /**
225
+ * This component's variant definitions (e.g. `{ intent: { primary: ..., ghost: ... } }`).
226
+ * Constrained to `Readonly<VariantMap>` — not the wider `AnyRecord` — specifically so `TPreset`
227
+ * below can be expressed as `RecipeMap<Variants>` and get real per-variant-key checking,
228
+ * instead of falling back to an unconstrained `RecipeMap<VariantMap>`.
229
+ */
230
+ Variants extends Readonly<VariantMap> = Readonly<VariantMap>,
231
+ /**
232
+ * Named presets (`RecipeMap<Variants>`) — bundles of variant selections a consumer activates
233
+ * by key instead of repeating the same variant combination at every call site. Tied to
234
+ * `Variants`, not `AnyRecord`, precisely so a preset can only ever select keys/values that
235
+ * `Variants` actually defines — an invalid preset is a type error, not a silent no-op.
236
+ * Defaults to `Readonly<EmptyRecord>` (no presets), which is the common case: a component can
237
+ * have variants without necessarily defining any named presets over them, and most don't.
238
+ */
239
+ TPreset extends RecipeMap<Variants> = Readonly<EmptyRecord>,
240
+ /**
241
+ * The set of elements/tags a consumer is allowed to switch to via `as`. Defaults to the widest
242
+ * `ElementType`, under which `AllowedOf<G>` imposes no restriction at all (see
243
+ * `PolymorphicControlProps.as`'s own comment in the React adapter for the concrete effect this
244
+ * has at a component's actual call site).
245
+ */
246
+ TAllowed extends ElementType = ElementType> {
184
247
  default: TDefault;
185
248
  props: Props;
186
249
  variants: Variants;
187
250
  preset: TPreset;
188
251
  allowed: TAllowed;
189
252
  }
253
+ /** This component's variant definitions. See `PolymorphicGenerics`'s `Variants` parameter. */
190
254
  type VariantsOf<T extends PolymorphicGenerics> = T['variants'];
255
+ /** This component's named presets. See `PolymorphicGenerics`'s `TPreset` parameter. */
191
256
  type RecipeOf<T extends PolymorphicGenerics> = T['preset'];
257
+ /** The element/tag this component renders as by default. See `PolymorphicGenerics`'s `TDefault`
258
+ * parameter. */
192
259
  type DefaultOf<T extends PolymorphicGenerics> = T['default'];
260
+ /** This component's own declared props, before variants are mixed in. See `PolymorphicGenerics`'s
261
+ * `Props` parameter. */
193
262
  type PropsOf<T extends PolymorphicGenerics> = T['props'];
194
263
 
195
264
  type RequireAtLeastOneIfNotEmpty<T> = keyof T extends never ? EmptyRecord : RequireAtLeastOne<T>;
@@ -222,7 +291,7 @@ interface BaseClassOptions {
222
291
  type ClassPipelineFn = (tag: unknown, props: AnyRecord, className?: ClassName, recipe?: string) => string | undefined;
223
292
 
224
293
  interface RecipeOptions<TVariants extends VariantMap = VariantMap> {
225
- recipeMap?: Record<string, RecipeTarget<TVariants>>;
294
+ recipeMap?: StringMap<RecipeTarget<TVariants>>;
226
295
  }
227
296
 
228
297
  interface TagMapOptions {
@@ -391,7 +460,7 @@ type StylingOptions<V extends Readonly<VariantMap> = Readonly<EmptyRecord>, TPre
391
460
  * combination, skipping runtime class computation entirely when a match is found. Normally
392
461
  * generated by a build-time class-extraction plugin rather than hand-authored.
393
462
  */
394
- readonly precomputedClasses?: Readonly<Record<string, string>>;
463
+ readonly precomputedClasses?: Readonly<StringMap<string>>;
395
464
  };
396
465
 
397
466
  type NormalizeFn<Props extends AnyRecord = AnyRecord> = {
@@ -437,13 +506,23 @@ type FactoryOptions<TDefault extends ElementType = ElementType, Props extends An
437
506
  * no prop-based equivalent), not for anything expressible as a plain
438
507
  * prop.
439
508
  *
509
+ * `element` is typed to the real DOM interface of every tag the rendered
510
+ * element could actually be — `TDefault` plus whatever `enforcement.allowed`
511
+ * permits via `as` (`HTMLDialogElement` for `tag: 'dialog'`,
512
+ * `HTMLDetailsElement` for `tag: 'details'`, and so on) — no cast needed to
513
+ * reach tag-specific members. A component that leaves `allowed`
514
+ * unconstrained (any tag reachable via `as`) falls back to `HTMLElement`,
515
+ * which still covers members every element shares (`showPopover()` and
516
+ * friends); restrict `enforcement.allowed` to the tags `onElement`
517
+ * actually knows how to handle to get real narrowing.
518
+ *
440
519
  * `getProps` returns the instance's *current* resolved props at call
441
520
  * time — read it from inside a listener registered once at mount, rather
442
521
  * than re-subscribing on every prop change.
443
522
  *
444
523
  * Return a cleanup function to run when the instance unmounts.
445
524
  */
446
- readonly onElement?: (element: Element, getProps: () => Readonly<Props>) => void | (() => void);
525
+ readonly onElement?: (element: ElementForTag<TDefault | TAllowed>, getProps: () => Readonly<Props>) => void | (() => void);
447
526
  };
448
527
 
449
528
  declare function defineContractComponent<O extends FactoryOptions>(options: O): <R>(factory: (options: O) => R) => R;
@@ -497,8 +576,37 @@ type PolymorphicComponent<G extends PolymorphicGenerics> = {
497
576
  * inference for tools such as Storybook and `ComponentProps`.
498
577
  */
499
578
  (props: PolymorphicProps<G, DefaultOf<G>>): AnyVNode;
579
+ /**
580
+ * Type-only; never assigned at runtime. See `HasGenerics<G>` (`@praxis-kit/contract-props`) for
581
+ * the full rationale — kept as an inline field rather than `HasGenerics<G> & {...}` because
582
+ * intersecting it onto this callable type changes how `PolymorphicComponent<any>` resolves
583
+ * against concrete instantiations (confirmed for React's identical shape,
584
+ * `adapters/react/src/shared/types/polymorphic-props.test.ts`); structurally identical to
585
+ * `HasGenerics<G>` either way, which is what lets `ContractProps` constrain against it.
586
+ */
587
+ readonly __generics?: G;
500
588
  displayName?: string;
501
589
  };
590
+ /**
591
+ * Recovers a built `PolymorphicComponent<G>`'s prop shape for a specific render mode, from
592
+ * outside the file that built it — the missing piece `ComponentProps<typeof Component>` can't
593
+ * provide, since it always resolves against `PolymorphicComponent`'s normal-mode fallback
594
+ * overload (see that type's own doc comment).
595
+ *
596
+ * No `'render'` mode — Preact has no render-callback render strategy, unlike React.
597
+ *
598
+ * ```tsx
599
+ * const Container = createContractComponent({ tag: 'div', name: 'Container', /* ... *\/ })
600
+ *
601
+ * // Normal-mode props (equivalent to ComponentProps<typeof Container>, but works for asChild too):
602
+ * type ContainerProps = ContractProps<typeof Container>
603
+ *
604
+ * // A wrapper that always renders Container with asChild — ComponentProps<typeof Container>
605
+ * // fails here ("Type 'true' is not assignable to type 'false'"); ContractProps doesn't.
606
+ * type ContainerAsChildProps = ContractProps<typeof Container, 'asChild'>
607
+ * ```
608
+ */
609
+ type ContractProps<T extends HasGenerics<PolymorphicGenerics>, M extends Exclude<Mode, 'render'> = 'normal'> = T extends HasGenerics<infer G extends PolymorphicGenerics> ? PickMode<M, PolymorphicProps<G, DefaultOf<G>>, PolymorphicWithAsChild<G, DefaultOf<G>>, never> : never;
502
610
 
503
611
  /**
504
612
  * Creates a polymorphic Preact component with praxis-kit contracts applied.
@@ -525,4 +633,4 @@ declare function createContractComponent<TDefault extends ElementType, Props ext
525
633
  readonly subComponents?: TSubComponents;
526
634
  }): MergeRecords<PolymorphicComponent<PolymorphicGenerics<TDefault, MergeRecords<Props, ExtractPluginProps<TPlugin>>, Variants, TPreset>>, TSubComponents>;
527
635
 
528
- export { type AnyFactoryOptions, type ElementRef, type ElementType, type EmptyRecord, type PolymorphicComponent, type PolymorphicGenerics, type PolymorphicProps, type PolymorphicWithAsChild, type PreactFactoryOptions, Slottable, createContractComponent, defineContractComponent };
636
+ export { type AnyFactoryOptions, type ContractProps, type ElementRef, type ElementType, type EmptyRecord, type PolymorphicComponent, type PolymorphicGenerics, type PolymorphicProps, type PolymorphicWithAsChild, type PreactFactoryOptions, Slottable, createContractComponent, defineContractComponent };