@atom-learning/components 2.66.6 → 2.66.7

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 +1,2 @@
1
1
  export * from './navigatorActions.types';
2
+ export * from './polymorphicComponentProp.types';
@@ -0,0 +1,60 @@
1
+ /**
2
+ * The goal of these types is to make sure that our polymorphic components accept
3
+ * the correct attributes, and any ref passed onto the polymorphic component has
4
+ * the correct type.
5
+ *
6
+ * For example, when you pass `as="a"`, it should accept an `href` attribute and,
7
+ * the `ref` should be of type `HTMLAnchorElement`.
8
+ *
9
+ * If you pass `as="button"` instead, it should not accept `href` and the `ref`
10
+ * should be of type `HTMLButtonElement`.
11
+ *
12
+ *
13
+ * Taken from:
14
+ * https://blog.logrocket.com/build-strongly-typed-polymorphic-components-react-typescript/
15
+ */
16
+ /// <reference types="react" />
17
+ /**
18
+ * A generic that is used to capture the `as` prop value
19
+ */
20
+ declare type AsProp<C extends React.ElementType> = {
21
+ as?: C;
22
+ };
23
+ /**
24
+ * A type of the `as` prop, plus any custom defined props.
25
+ */
26
+ declare type PropsToOmit<C extends React.ElementType, P> = keyof (AsProp<C> & P);
27
+ /**
28
+ * Adds a typed `as` prop to a list of props.
29
+ */
30
+ declare type PolymorphicComponentProp<C extends React.ElementType, Props = Record<string, unknown>> = React.PropsWithChildren<Props & AsProp<C>> & Omit<React.ComponentPropsWithoutRef<C>, PropsToOmit<C, Props>>;
31
+ /**
32
+ * Type to capture the `ref` prop type from the element type
33
+ * passed into the `as` prop.
34
+ */
35
+ export declare type PolymorphicRef<C extends React.ElementType> = React.ComponentPropsWithRef<C>['ref'];
36
+ /**
37
+ * Usage example:
38
+ *
39
+ * ```tsx
40
+ * type MyComponentProps<C extends React.ElementType> = PolymorphicComponentPropWithRef<
41
+ * C,
42
+ * { myProp: string }
43
+ * >
44
+ *
45
+ * const MyComponent: <C extends React.ElementType = 'span'>(
46
+ * props: MyComponentProps<C>
47
+ * ) => React.ReactElement | null = React.forwardRef(
48
+ * <C extends React.ElementType = 'span'>(
49
+ * { children, as, myProp }: MyComponentProps<C>,
50
+ * ref?: MyComponentProps<C>['ref']
51
+ * ) => {
52
+ * ...
53
+ * }
54
+ * )
55
+ * ```
56
+ */
57
+ export declare type PolymorphicComponentPropWithRef<C extends React.ElementType, Props = Record<string, unknown>> = PolymorphicComponentProp<C, Props> & {
58
+ ref?: PolymorphicRef<C>;
59
+ };
60
+ export {};
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "main": "dist/index.cjs.js",
5
5
  "typings": "dist/index.d.ts",
6
6
  "module": "dist/index.js",
7
- "version": "2.66.6",
7
+ "version": "2.66.7",
8
8
  "description": "",
9
9
  "files": [
10
10
  "dist"