Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@leafygreen-ui/polymorphic

Package Overview
Dependencies
Maintainers
6
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@leafygreen-ui/polymorphic - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

dist/InferredPolymorphic/InferredPolymorphic.d.ts

6

CHANGELOG.md
# @leafygreen-ui/polymorphic
## 1.1.0
### Minor Changes
- 9f06c9495: Adds `InferredPolymorphic` factory function. Adds additional generic prop to `Polymorphic` and `InferredPolymorphic` to set a default as prop
## 1.0.0

@@ -4,0 +10,0 @@

2

dist/esm/index.js

@@ -1,2 +0,2 @@

import e,{forwardRef as t}from"react";import{render as r}from"@testing-library/react";var n=function(t){return e.useRef(null)};function a(e){var t=function(e){return e||"div"}(e);return{Component:t,ref:n()}}function o(e,t){return e=function(e,t){return e||(e="string"==typeof(null==t?void 0:t.href)?"a":"div"),e}(e,t),a(e)}function i(){return i=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},i.apply(this,arguments)}function s(e,t){if(null==e)return{};var r,n,a=function(e,t){if(null==e)return{};var r,n,a={},o=Object.keys(e);for(n=0;n<o.length;n++)r=o[n],t.indexOf(r)>=0||(a[r]=e[r]);return a}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(n=0;n<o.length;n++)r=o[n],t.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(e,r)&&(a[r]=e[r])}return a}var p=["children"];var l=["as","children"],c=function(t,r){var n=t.as,o=t.children,p=s(t,l),c=a(n).Component;return e.createElement(c,i({},p,{ref:r}),o)},u=e.forwardRef(c);u.displayName="Polymorph";var f=function(e,r){var n,a=1===e.length?e:t(e);return a.displayName=null!==(n=null!=r?r:e.displayName)&&void 0!==n?n:"PolymorphicComponent",a};expect.extend({toBePolymorphic:function(t){var n,a=function(){var t=jest.fn(),r=e.forwardRef((function(r,n){var a=r.children,o=s(r,p);return t(),e.createElement("span",i({"data-testid":"wrapper"},o,{ref:n}),a)}));return r.displayName="Wrapper",{Wrapper:r,wrapperDidRender:t}}(),o=a.Wrapper,l=r(e.createElement(e.Fragment,null,e.createElement(t,{as:"div","data-testid":"test-1"}),";",e.createElement(t,{as:"span","data-testid":"test-2"}),";",e.createElement(t,{as:"a",href:"mongodb.design","data-testid":"test-3"}),";",e.createElement(t,{as:o,"data-testid":"test-4"}),";")),c=l.getByTestId,u=l.unmount,f=c("test-1"),m=c("test-2"),d=c("test-3"),y=c("test-4"),v="string"==typeof t?t:null!==(n=t.displayName)&&void 0!==n?n:"The component";return"div"===f.tagName.toLowerCase()&&"span"===m.tagName.toLowerCase()&&"a"===d.tagName.toLowerCase()&&"span"===y.tagName.toLowerCase()?(u(),{pass:!0,message:function(){return"Component `".concat(v,"` is polymorphic")}}):(u(),{pass:!1,message:function(){return"Component `".concat(v,"` is not polymorphic")}})}});export{u as Polymorph,f as Polymorphic,o as useInferredPolymorphic,a as usePolymorphic,n as usePolymorphicRef};
import r,{forwardRef as n}from"react";function e(){return e=Object.assign||function(r){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&(r[t]=e[t])}return r},e.apply(this,arguments)}function t(r,n){if(null==r)return{};var e,t,o=function(r,n){if(null==r)return{};var e,t,o={},u=Object.keys(r);for(t=0;t<u.length;t++)e=u[t],n.indexOf(e)>=0||(o[e]=r[e]);return o}(r,n);if(Object.getOwnPropertySymbols){var u=Object.getOwnPropertySymbols(r);for(t=0;t<u.length;t++)e=u[t],n.indexOf(e)>=0||Object.prototype.propertyIsEnumerable.call(r,e)&&(o[e]=r[e])}return o}var o=function(n){return r.useRef(null)};function u(r){var n=function(r){return r||"div"}(r);return{Component:n,ref:o()}}var i=["as","children"],l=function(n,o){var l=n.as,a=n.children,f=t(n,i),c=u(l).Component;return r.createElement(c,e({},f,{ref:o}),a)},a=r.forwardRef(l);a.displayName="Polymorph";var f=function(r,e){var t,o=1===r.length?r:n(r);return o.displayName=null!==(t=null!=e?e:r.displayName)&&void 0!==t?t:"PolymorphicComponent",o},c=function(r,n){return f(r,n)};function p(r,n){return r=function(r,n){return r||(r="string"==typeof(null==n?void 0:n.href)?"a":"div"),r}(r,n),u(r)}export{c as InferredPolymorphic,a as Polymorph,f as Polymorphic,p as useInferredPolymorphic,u as usePolymorphic,o as usePolymorphicRef};
//# sourceMappingURL=index.js.map

@@ -1,2 +0,2 @@

export { AdvancedPolymorphic, AdvancedPolymorphicWithRef, ExampleInferred, ExamplePolymorphic, ExamplePolymorphicWithRef, RestrictedExample, } from './Polymorphic.example';
export { AdvancedPolymorphic, AdvancedPolymorphicWithRef, ExampleInferred, ExampleInferredDefaultButton, ExamplePolymorphic, ExamplePolymorphicWithRef, RestrictedExample, StyledExample, } from './Polymorphic.example';
//# sourceMappingURL=index.d.ts.map

@@ -1,3 +0,3 @@

export { type InferredPolymorphicComponentType, type InferredPolymorphicProps, useInferredPolymorphic, } from './InferredPolymorphic';
export { InferredPolymorphic, type InferredPolymorphicComponentType, type InferredPolymorphicProps, useInferredPolymorphic, } from './InferredPolymorphic';
export { Polymorph, Polymorphic, type PolymorphicAs, type PolymorphicComponentType, type PolymorphicProps, type PolymorphicPropsWithRef, type PolymorphicRef, usePolymorphic, usePolymorphicRef, } from './Polymorphic';
//# sourceMappingURL=index.d.ts.map

@@ -1,2 +0,2 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("@testing-library/react")):"function"==typeof define&&define.amd?define(["exports","react","@testing-library/react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@leafygreen-ui/polymorphic"]={},e.React,e.react)}(this,(function(e,t,r){"use strict";function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=n(t),o=function(e){return a.default.useRef(null)};function i(e){var t=function(e){return e||"div"}(e);return{Component:t,ref:o()}}function l(){return l=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},l.apply(this,arguments)}function u(e,t){if(null==e)return{};var r,n,a=function(e,t){if(null==e)return{};var r,n,a={},o=Object.keys(e);for(n=0;n<o.length;n++)r=o[n],t.indexOf(r)>=0||(a[r]=e[r]);return a}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(n=0;n<o.length;n++)r=o[n],t.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(e,r)&&(a[r]=e[r])}return a}var f=["children"];var s=["as","children"],c=function(e,t){var r=e.as,n=e.children,o=u(e,s),f=i(r).Component;return a.default.createElement(f,l({},o,{ref:t}),n)},p=a.default.forwardRef(c);p.displayName="Polymorph";expect.extend({toBePolymorphic:function(e){var t,n=function(){var e=jest.fn(),t=a.default.forwardRef((function(t,r){var n=t.children,o=u(t,f);return e(),a.default.createElement("span",l({"data-testid":"wrapper"},o,{ref:r}),n)}));return t.displayName="Wrapper",{Wrapper:t,wrapperDidRender:e}}(),o=n.Wrapper,i=r.render(a.default.createElement(a.default.Fragment,null,a.default.createElement(e,{as:"div","data-testid":"test-1"}),";",a.default.createElement(e,{as:"span","data-testid":"test-2"}),";",a.default.createElement(e,{as:"a",href:"mongodb.design","data-testid":"test-3"}),";",a.default.createElement(e,{as:o,"data-testid":"test-4"}),";")),s=i.getByTestId,c=i.unmount,p=s("test-1"),d=s("test-2"),m=s("test-3"),y=s("test-4"),h="string"==typeof e?e:null!==(t=e.displayName)&&void 0!==t?t:"The component";return"div"===p.tagName.toLowerCase()&&"span"===d.tagName.toLowerCase()&&"a"===m.tagName.toLowerCase()&&"span"===y.tagName.toLowerCase()?(c(),{pass:!0,message:function(){return"Component `".concat(h,"` is polymorphic")}}):(c(),{pass:!1,message:function(){return"Component `".concat(h,"` is not polymorphic")}})}}),e.Polymorph=p,e.Polymorphic=function(e,r){var n,a=1===e.length?e:t.forwardRef(e);return a.displayName=null!==(n=null!=r?r:e.displayName)&&void 0!==n?n:"PolymorphicComponent",a},e.useInferredPolymorphic=function(e,t){return e=function(e,t){return e||(e="string"==typeof(null==t?void 0:t.href)?"a":"div"),e}(e,t),i(e)},e.usePolymorphic=i,e.usePolymorphicRef=o,Object.defineProperty(e,"__esModule",{value:!0})}));
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self)["@leafygreen-ui/polymorphic"]={},e.React)}(this,(function(e,r){"use strict";function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var t=n(r);function o(){return o=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var n=arguments[r];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(e[t]=n[t])}return e},o.apply(this,arguments)}function f(e,r){if(null==e)return{};var n,t,o=function(e,r){if(null==e)return{};var n,t,o={},f=Object.keys(e);for(t=0;t<f.length;t++)n=f[t],r.indexOf(n)>=0||(o[n]=e[n]);return o}(e,r);if(Object.getOwnPropertySymbols){var f=Object.getOwnPropertySymbols(e);for(t=0;t<f.length;t++)n=f[t],r.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}var u=function(e){return t.default.useRef(null)};function i(e){var r=function(e){return e||"div"}(e);return{Component:r,ref:u()}}var l=["as","children"],a=function(e,r){var n=e.as,u=e.children,a=f(e,l),c=i(n).Component;return t.default.createElement(c,o({},a,{ref:r}),u)},c=t.default.forwardRef(a);c.displayName="Polymorph";var p=function(e,n){var t,o=1===e.length?e:r.forwardRef(e);return o.displayName=null!==(t=null!=n?n:e.displayName)&&void 0!==t?t:"PolymorphicComponent",o};e.InferredPolymorphic=function(e,r){return p(e,r)},e.Polymorph=c,e.Polymorphic=p,e.useInferredPolymorphic=function(e,r){return e=function(e,r){return e||(e="string"==typeof(null==r?void 0:r.href)?"a":"div"),e}(e,r),i(e)},e.usePolymorphic=i,e.usePolymorphicRef=u,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=index.js.map

@@ -0,3 +1,4 @@

export { InferredPolymorphic } from './InferredPolymorphic';
export type { InferredPolymorphicComponentType, InferredPolymorphicProps, } from './InferredPolymorphic.types';
export { useInferredPolymorphic } from './useInferredPolymorphic';
//# sourceMappingURL=index.d.ts.map

@@ -1,2 +0,2 @@

import { AnchorProps, PolymorphicComponentType } from '../Polymorphic/Polymorphic.types';
import { AnchorProps, PolymorphicAs, PolymorphicComponentType } from '../Polymorphic/Polymorphic.types';
/**

@@ -13,3 +13,3 @@ * Wrapping props in this type ensures that if `href` is defined,

*/
export declare type InferredPolymorphicComponentType<XP = {}> = PolymorphicComponentType<InferredPolymorphicProps<XP>>;
export declare type InferredPolymorphicComponentType<XP = {}, DefaultAs extends PolymorphicAs = PolymorphicAs> = PolymorphicComponentType<InferredPolymorphicProps<XP>, DefaultAs>;
//# sourceMappingURL=InferredPolymorphic.types.d.ts.map

@@ -5,9 +5,2 @@ export { Polymorph } from './Polymorph';

export type { PolymorphicAs, PolymorphicComponentType, PolymorphicProps, PolymorphicPropsWithRef, PolymorphicRef, } from './Polymorphic.types';
declare global {
namespace jest {
interface Matchers<R> {
toBePolymorphic(): R;
}
}
}
//# sourceMappingURL=index.d.ts.map

@@ -1,2 +0,2 @@

import { PolymorphicComponentType, PolymorphicRenderFunction } from './Polymorphic.types';
import { PolymorphicAs, PolymorphicComponentType, PolymorphicRenderFunction } from './Polymorphic.types';
/**

@@ -13,3 +13,3 @@ * Factory function that returns a polymorphic component.

*/
export declare const Polymorphic: <XP extends object = {}>(render: PolymorphicRenderFunction<XP>, displayName?: string | undefined) => PolymorphicComponentType<XP>;
export declare const Polymorphic: <XP extends object = {}, DefaultAs extends PolymorphicAs = PolymorphicAs>(render: PolymorphicRenderFunction<XP, DefaultAs>, displayName?: string | undefined) => PolymorphicComponentType<XP, DefaultAs>;
//# sourceMappingURL=Polymorphic.d.ts.map

@@ -23,3 +23,3 @@ /**

*/
declare type PropsToOmit<T extends PolymorphicAs, P> = keyof (P & AsProp<T>);
export declare type PropsToOmit<T extends PolymorphicAs, P> = keyof (P & AsProp<T>);
/**

@@ -36,3 +36,3 @@ * Ensures `href` is required for anchors

*/
declare type InheritedProps<T extends PolymorphicAs> = T extends 'a' ? AnchorProps : ComponentPropsWithoutRef<T>;
export declare type InheritedProps<T extends PolymorphicAs> = T extends 'a' ? AnchorProps : ComponentPropsWithoutRef<T>;
/**

@@ -59,6 +59,5 @@ * The basic props for the Polymorphic component.

* Differs from `PolymorphicComponentType` only by the `propTypes` attribute
*
*/
export interface PolymorphicRenderFunction<XP = {}> {
<T extends PolymorphicAs>(props: PolymorphicPropsWithRef<T, XP>, ref: PolymorphicRef<T>): ReactElement | null;
export interface PolymorphicRenderFunction<XP = {}, DefaultAs extends PolymorphicAs = PolymorphicAs> {
<T extends PolymorphicAs = DefaultAs>(props: PolymorphicPropsWithRef<T, XP>, ref: PolymorphicRef<T>): ReactElement | null;
displayName?: string;

@@ -76,8 +75,7 @@ propTypes?: never;

*/
export interface PolymorphicComponentType<XP = {}> {
<T extends PolymorphicAs>(props: PolymorphicPropsWithRef<T, XP>, ref: PolymorphicRef<T>): ReactElement | null;
export interface PolymorphicComponentType<XP = {}, DefaultAs extends PolymorphicAs = PolymorphicAs> {
<T extends PolymorphicAs = DefaultAs>(props: PolymorphicPropsWithRef<T, XP>, ref: PolymorphicRef<T>): ReactElement | null;
displayName?: string;
propTypes?: WeakValidationMap<PropsWithoutRef<PolymorphicPropsWithRef<PolymorphicAs, XP>> & RefAttributes<any>>;
}
export {};
//# sourceMappingURL=Polymorphic.types.d.ts.map
{
"name": "@leafygreen-ui/polymorphic",
"version": "1.0.0",
"version": "1.1.0",
"description": "LeafyGreen UI Kit Polymorphic",

@@ -5,0 +5,0 @@ "main": "./dist/index.js",

@@ -21,2 +21,3 @@ # Polymorphic

If the logic defining the `as` prop is defined internally within your component, use the standalone `Polymorph` component.
A simple example of this may be rendering an element as a header or paragraph text depending on certain props passed in.

@@ -29,3 +30,3 @@ ```tsx

const MyComponent = (props: MyProps) => {
const shouldRenderAs = 'button';
const shouldRenderAs = props.isHeader ? 'h1' : 'p';
return <Polymorph as={shouldRenderAs} {...props} />;

@@ -38,2 +39,3 @@ };

If you want to expose `as` as a prop of your component, use the `Polymorphic` factory function and related hooks.
This is likely the more common use case, since it allows you to create new polymorphic components (such as a `Button` component that can render as a link, etc.)

@@ -89,2 +91,22 @@ Note that any inherited props will be indeterminate in the factory function, since the value `as` is not known. (i.e. the attributes of `rest` in the following example are unknown).

#### Inferred `as` with a default value
Sometimes, when developing a component that uses Polymorphic, you'll want to set a default value for the `as` prop.
For example, when creating a Button component, you may want to have the `as` prop default to the HTML `button` element.
To set a default value for the inferred as value, you'll need to provide the default value both to TypeScript and React:
```tsx
export const MyInferredComponentWitDefault = InferredPolymorphic<
ExampleProps,
'button'
>(({ as = 'button' as PolymorphicAs, title, ...rest }) => {
const { Component, ref } = useInferredPolymorphic(as, rest);
return (
<Component ref={ref} {...rest}>
{title}
</Component>
);
});
```
## With Emotion `styled` API

@@ -91,0 +113,0 @@

@@ -5,5 +5,7 @@ export {

ExampleInferred,
ExampleInferredDefaultButton,
ExamplePolymorphic,
ExamplePolymorphicWithRef,
RestrictedExample,
StyledExample,
} from './Polymorphic.example';
export {
InferredPolymorphic,
type InferredPolymorphicComponentType,

@@ -3,0 +4,0 @@ type InferredPolymorphicProps,

@@ -0,1 +1,2 @@

export { InferredPolymorphic } from './InferredPolymorphic';
export type {

@@ -2,0 +3,0 @@ InferredPolymorphicComponentType,

import {
AnchorProps,
PolymorphicAs,
PolymorphicComponentType,

@@ -20,3 +21,5 @@ } from '../Polymorphic/Polymorphic.types';

*/
export type InferredPolymorphicComponentType<XP = {}> =
PolymorphicComponentType<InferredPolymorphicProps<XP>>;
export type InferredPolymorphicComponentType<
XP = {},
DefaultAs extends PolymorphicAs = PolymorphicAs,
> = PolymorphicComponentType<InferredPolymorphicProps<XP>, DefaultAs>;

@@ -1,2 +0,1 @@

import { toBePolymorphic } from '../utils/Polymorphic.testutils';
export { Polymorph } from './Polymorph';

@@ -12,13 +11,1 @@ export { Polymorphic } from './Polymorphic';

} from './Polymorphic.types';
expect.extend({
toBePolymorphic,
});
declare global {
namespace jest {
interface Matchers<R> {
toBePolymorphic(): R;
}
}
}
import { forwardRef } from 'react';
import {
PolymorphicAs,
PolymorphicComponentType,

@@ -19,11 +20,15 @@ PolymorphicRenderFunction,

*/
export const Polymorphic = <XP extends object = {}>(
render: PolymorphicRenderFunction<XP>,
export const Polymorphic = <
XP extends object = {},
DefaultAs extends PolymorphicAs = PolymorphicAs,
>(
render: PolymorphicRenderFunction<XP, DefaultAs>,
displayName?: string,
): PolymorphicComponentType<XP> => {
): PolymorphicComponentType<XP, DefaultAs> => {
// Here we only know the additional props,
// but don' t know what `as` is, or what props to inherit
// but we don't know what `as` is, or what props to inherit
// (i.e. we don't know if `as="button"`, and if `type` is a valid prop)
// If no `ref` arg was passed in, we use the plain render function
const PolyComponent: PolymorphicComponentType<XP> =
const PolyComponent: PolymorphicComponentType<XP, DefaultAs> =
render.length === 1 ? render : forwardRef(render);

@@ -30,0 +35,0 @@

@@ -37,3 +37,3 @@ /**

*/
type PropsToOmit<T extends PolymorphicAs, P> = keyof (P & AsProp<T>);
export type PropsToOmit<T extends PolymorphicAs, P> = keyof (P & AsProp<T>);

@@ -52,3 +52,3 @@ /**

*/
type InheritedProps<T extends PolymorphicAs> = T extends 'a'
export type InheritedProps<T extends PolymorphicAs> = T extends 'a'
? AnchorProps

@@ -87,6 +87,8 @@ : ComponentPropsWithoutRef<T>;

* Differs from `PolymorphicComponentType` only by the `propTypes` attribute
*
*/
export interface PolymorphicRenderFunction<XP = {}> {
<T extends PolymorphicAs>(
export interface PolymorphicRenderFunction<
XP = {},
DefaultAs extends PolymorphicAs = PolymorphicAs,
> {
<T extends PolymorphicAs = DefaultAs>(
props: PolymorphicPropsWithRef<T, XP>,

@@ -98,3 +100,2 @@ ref: PolymorphicRef<T>,

}
/**

@@ -109,4 +110,7 @@ * An explicit definition of the component type

*/
export interface PolymorphicComponentType<XP = {}> {
<T extends PolymorphicAs>(
export interface PolymorphicComponentType<
XP = {},
DefaultAs extends PolymorphicAs = PolymorphicAs,
> {
<T extends PolymorphicAs = DefaultAs>(
props: PolymorphicPropsWithRef<T, XP>,

@@ -113,0 +117,0 @@ ref: PolymorphicRef<T>,

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc