@rarui/typings 1.0.0-rc.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.md ADDED
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ This package is intended for internal use in creating components with polymorphic typing.
4
+
5
+ ## 2024-03-22 `1.0.0`
6
+
7
+ #### 🎉 New features
8
+
9
+ - Added useRefObjectAsForwardedRef hook used to sync ref object with forwarded ref and allow local access to instance reference with `.current`. ([#4](https://git.rarolabs.com.br/frontend/rarui/pull/4) by [@junior](https://git.rarolabs.com.br/junior))
10
+ - Added types that support dynamic typing of polymorphic components. ([#4](https://git.rarolabs.com.br/frontend/rarui/pull/4) by [@junior](https://git.rarolabs.com.br/junior))
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # `@rarui/typings`
2
+
3
+ This package is intended for internal use in creating components with polymorphic typing.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ $ yarn add @rarui/typings
9
+ # or
10
+ $ npm install @rarui/typings
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ This is an internal utility, not intended for public usage.
@@ -0,0 +1,44 @@
1
+ // Generated by dts-bundle-generator v9.3.1
2
+
3
+ import React$1 from 'react';
4
+ import { ForwardedRef, RefObject } from 'react';
5
+
6
+ export type Merge<P1 = {}, P2 = {}> = Omit<P1, keyof P2> & P2;
7
+ /**
8
+ * Infers the OwnProps if E is a ForwardRefExoticComponentWithAs
9
+ */
10
+ export type OwnProps<E> = E extends PolymorphicForwardRefComponent<any, infer P> ? P : {};
11
+ /**
12
+ * Infers the JSX.IntrinsicElement if E is a ForwardRefExoticComponentWithAs
13
+ */
14
+ export type IntrinsicElement<E> = E extends PolymorphicForwardRefComponent<infer I, any> ? I : never;
15
+ export type ForwardRefExoticComponent<E, OwnProps> = React$1.ForwardRefExoticComponent<Merge<E extends React$1.ElementType ? React$1.ComponentPropsWithRef<E> : never, OwnProps & {
16
+ as?: E;
17
+ }>>;
18
+ export interface PolymorphicForwardRefComponent<IntrinsicElementString, OwnProps = {}> extends ForwardRefExoticComponent<IntrinsicElementString, OwnProps> {
19
+ /**
20
+ * When `as` prop is passed, use this overload.
21
+ * Merges original own props (without DOM props) and the inferred props
22
+ * from `as` element with the own props taking precendence.
23
+ *
24
+ * We explicitly avoid `React.ElementType` and manually narrow the prop types
25
+ * so that events are typed when using JSX.IntrinsicElements.
26
+ */
27
+ <As = IntrinsicElementString>(props: As extends "" ? {
28
+ as: keyof JSX.IntrinsicElements;
29
+ } : As extends React$1.ComponentType<infer P> ? Merge<P, OwnProps & {
30
+ as: As;
31
+ }> : As extends keyof JSX.IntrinsicElements ? Merge<JSX.IntrinsicElements[As], OwnProps & {
32
+ as: As;
33
+ }> : never): React$1.ReactElement | null;
34
+ }
35
+ /**
36
+ * Use a ref object as the imperative handle for a forwarded ref. This can be used to
37
+ * synchronize the ref object with the forwarded ref and allow local access the reference
38
+ * instance with `.current`.
39
+ *
40
+ * **NOTE**: The `refObject` should be passed to the underlying element, NOT the `forwardedRef`.
41
+ */
42
+ export declare function useRefObjectAsForwardedRef<T>(forwardedRef: React$1.ForwardedRef<T>, refObject: React$1.RefObject<T>): void;
43
+
44
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ !function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r(require("react")):"function"==typeof define&&define.amd?define(["react"],r):"object"==typeof exports?exports["@rarui/typings"]=r(require("react")):e["@rarui/typings"]=r(e.react)}(global,(e=>(()=>{"use strict";var r={155:r=>{r.exports=e}},t={};function o(e){var n=t[e];if(void 0!==n)return n.exports;var s=t[e]={exports:{}};return r[e](s,s.exports,o),s.exports}var n={};return(()=>{var e=n;Object.defineProperty(e,"__esModule",{value:!0}),e.useRefObjectAsForwardedRef=void 0;const r=o(155);e.useRefObjectAsForwardedRef=function(e,t){(0,r.useImperativeHandle)(e,(()=>t.current))}})(),n})()));
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@rarui/typings",
3
+ "version": "1.0.0-rc.1",
4
+ "license": "MIT",
5
+ "main": "dist/index.js",
6
+ "sideEffects": false,
7
+ "scripts": {
8
+ "build": "yarn g:webpack",
9
+ "clean": "rm -rf dist",
10
+ "version": "yarn version"
11
+ },
12
+ "homepage": "https://github.com/juniorconquista/boilerplate-design-system",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/juniorconquista/boilerplate-design-system.git"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/juniorconquista/boilerplate-design-system/issues"
19
+ },
20
+ "devDependencies": {
21
+ "@rarui/webpack": "workspace:^"
22
+ },
23
+ "stableVersion": "0.0.0"
24
+ }
package/src/index.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { ForwardedRef, RefObject, useImperativeHandle } from "react";
2
+
3
+ /**
4
+ * Use a ref object as the imperative handle for a forwarded ref. This can be used to
5
+ * synchronize the ref object with the forwarded ref and allow local access the reference
6
+ * instance with `.current`.
7
+ *
8
+ * **NOTE**: The `refObject` should be passed to the underlying element, NOT the `forwardedRef`.
9
+ */
10
+ export function useRefObjectAsForwardedRef<T>(
11
+ forwardedRef: ForwardedRef<T>,
12
+ refObject: RefObject<T>,
13
+ ): void {
14
+ useImperativeHandle<T | null, T | null>(
15
+ forwardedRef,
16
+ () => refObject.current,
17
+ );
18
+ }
19
+
20
+ export type {
21
+ PolymorphicForwardRefComponent,
22
+ OwnProps,
23
+ IntrinsicElement,
24
+ Merge,
25
+ } from "./index.types";
@@ -0,0 +1,70 @@
1
+ /**
2
+ * This file is originally from `@radix-ui/react-polymorphic` before the package
3
+ * was deprecated. The original source for this lived in the URL below.
4
+ *
5
+ * @see https://github.com/radix-ui/primitives/blob/17ffcb7aaa42cbd36b3c210ba86d7d73d218e5be/packages/react/polymorphic/src/polymorphic.ts
6
+ */
7
+
8
+ import React from "react";
9
+
10
+ /* -------------------------------------------------------------------------------------------------
11
+ * Utility types
12
+ * -----------------------------------------------------------------------------------------------*/
13
+ type Merge<P1 = {}, P2 = {}> = Omit<P1, keyof P2> & P2;
14
+
15
+ /**
16
+ * Infers the OwnProps if E is a ForwardRefExoticComponentWithAs
17
+ */
18
+ type OwnProps<E> =
19
+ E extends PolymorphicForwardRefComponent<any, infer P> ? P : {};
20
+
21
+ /**
22
+ * Infers the JSX.IntrinsicElement if E is a ForwardRefExoticComponentWithAs
23
+ */
24
+ type IntrinsicElement<E> =
25
+ E extends PolymorphicForwardRefComponent<infer I, any> ? I : never;
26
+
27
+ type ForwardRefExoticComponent<E, OwnProps> = React.ForwardRefExoticComponent<
28
+ Merge<
29
+ E extends React.ElementType ? React.ComponentPropsWithRef<E> : never,
30
+ OwnProps & { as?: E }
31
+ >
32
+ >;
33
+
34
+ /* -------------------------------------------------------------------------------------------------
35
+ * PolymorphicForwardRefComponent
36
+ * -----------------------------------------------------------------------------------------------*/
37
+
38
+ interface PolymorphicForwardRefComponent<
39
+ IntrinsicElementString,
40
+ OwnProps = {},
41
+ /**
42
+ * Extends original type to ensure built in React types play nice
43
+ * with polymorphic components still e.g. `React.ElementRef` etc.
44
+ */
45
+ > extends ForwardRefExoticComponent<IntrinsicElementString, OwnProps> {
46
+ /**
47
+ * When `as` prop is passed, use this overload.
48
+ * Merges original own props (without DOM props) and the inferred props
49
+ * from `as` element with the own props taking precendence.
50
+ *
51
+ * We explicitly avoid `React.ElementType` and manually narrow the prop types
52
+ * so that events are typed when using JSX.IntrinsicElements.
53
+ */
54
+ <As = IntrinsicElementString>(
55
+ props: As extends ""
56
+ ? { as: keyof JSX.IntrinsicElements }
57
+ : As extends React.ComponentType<infer P>
58
+ ? Merge<P, OwnProps & { as: As }>
59
+ : As extends keyof JSX.IntrinsicElements
60
+ ? Merge<JSX.IntrinsicElements[As], OwnProps & { as: As }>
61
+ : never,
62
+ ): React.ReactElement | null;
63
+ }
64
+
65
+ export type {
66
+ PolymorphicForwardRefComponent,
67
+ OwnProps,
68
+ IntrinsicElement,
69
+ Merge,
70
+ };
@@ -0,0 +1,11 @@
1
+ import path from "path";
2
+ import { configuration } from "@rarui/webpack";
3
+
4
+ const config = {
5
+ output: {
6
+ path: path.resolve(__dirname, "dist"),
7
+ library: "@rarui/typings",
8
+ },
9
+ };
10
+
11
+ export default () => configuration.getConfiguration(config);