index.d.mts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import * as React from "react";
  2. declare const NODES: readonly ["a", "button", "div", "form", "h2", "h3", "img", "input", "label", "li", "nav", "ol", "p", "span", "svg", "ul"];
  3. type PropsWithoutRef<P> = P extends any ? ('ref' extends keyof P ? Pick<P, Exclude<keyof P, 'ref'>> : P) : P;
  4. export type ComponentPropsWithoutRef<T extends React.ElementType> = PropsWithoutRef<React.ComponentProps<T>>;
  5. type Primitives = {
  6. [E in typeof NODES[number]]: PrimitiveForwardRefComponent<E>;
  7. };
  8. export type PrimitivePropsWithRef<E extends React.ElementType> = React.ComponentPropsWithRef<E> & {
  9. asChild?: boolean;
  10. };
  11. interface PrimitiveForwardRefComponent<E extends React.ElementType> extends React.ForwardRefExoticComponent<PrimitivePropsWithRef<E>> {
  12. }
  13. export const Primitive: Primitives;
  14. /**
  15. * Flush custom event dispatch
  16. * https://github.com/radix-ui/primitives/pull/1378
  17. *
  18. * React batches *all* event handlers since version 18, this introduces certain considerations when using custom event types.
  19. *
  20. * Internally, React prioritises events in the following order:
  21. * - discrete
  22. * - continuous
  23. * - default
  24. *
  25. * https://github.com/facebook/react/blob/a8a4742f1c54493df00da648a3f9d26e3db9c8b5/packages/react-dom/src/events/ReactDOMEventListener.js#L294-L350
  26. *
  27. * `discrete` is an important distinction as updates within these events are applied immediately.
  28. * React however, is not able to infer the priority of custom event types due to how they are detected internally.
  29. * Because of this, it's possible for updates from custom events to be unexpectedly batched when
  30. * dispatched by another `discrete` event.
  31. *
  32. * In order to ensure that updates from custom events are applied predictably, we need to manually flush the batch.
  33. * This utility should be used when dispatching a custom event from within another `discrete` event, this utility
  34. * is not nessesary when dispatching known event types, or if dispatching a custom type inside a non-discrete event.
  35. * For example:
  36. *
  37. * dispatching a known click 👎
  38. * target.dispatchEvent(new Event(‘click’))
  39. *
  40. * dispatching a custom type within a non-discrete event 👎
  41. * onScroll={(event) => event.target.dispatchEvent(new CustomEvent(‘customType’))}
  42. *
  43. * dispatching a custom type within a `discrete` event 👍
  44. * onPointerDown={(event) => dispatchDiscreteCustomEvent(event.target, new CustomEvent(‘customType’))}
  45. *
  46. * Note: though React classifies `focus`, `focusin` and `focusout` events as `discrete`, it's not recommended to use
  47. * this utility with them. This is because it's possible for those handlers to be called implicitly during render
  48. * e.g. when focus is within a component as it is unmounted, or when managing focus on mount.
  49. */
  50. export function dispatchDiscreteCustomEvent<E extends CustomEvent>(target: E['target'], event: E): void;
  51. export const Root: Primitives;
  52. //# sourceMappingURL=index.d.ts.map