Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
react-polymorphic-box
Advanced tools
Building blocks for strongly typed polymorphic components in React.
Building blocks for strongly typed polymorphic components in React.
Popularized by Styled Components v4, the as
prop allows changing the HTML tag rendered by a component, e.g.:
import { Box } from 'react-polymorphic-box';
import { Link } from 'react-router-dom';
<Box as="a" href="https://github.com/kripod">GitHub</Box>
<Box as={Link} to="/about">About</Box>
While this pattern has been encouraged by several libraries, typings had lacked support for polymorphism, missing benefits like:
as
propA Heading
component can demonstrate the effectiveness of polymorphism:
<Heading color="rebeccapurple">Heading</Heading>
<Heading as="h3">Subheading</Heading>
Custom components like the previous one may utilize the package as shown below.
import React from 'react';
import { Box, PolymorphicComponentProps } from 'react-polymorphic-box';
// Component-specific props should be specified separately
export interface HeadingOwnProps {
color?: string;
}
// Merge own props with others inherited from the underlying element type
export type HeadingProps<
E extends React.ElementType
> = PolymorphicComponentProps<E, HeadingOwnProps>;
// An HTML tag or a different React component can be rendered by default
const defaultElement = 'h2';
export function Heading<E extends React.ElementType = typeof defaultElement>({
color,
style,
...restProps
}: HeadingProps<E>): JSX.Element {
// The `as` prop may be overridden by the passed props
return <Box as={defaultElement} style={{ color, ...style }} {...restProps} />;
}
Library authors should consider encapsulating reusable components, passing a ref through each of them:
import React from 'react';
import { Box } from 'react-polymorphic-box';
export const Heading = React.forwardRef(
<E extends React.ElementType = typeof defaultElement>(
{ ref, color, style, ...restProps }: HeadingProps<E>,
innerRef: typeof ref,
) => {
return (
<Box
ref={innerRef}
as={defaultElement}
style={{ color, ...style }}
{...restProps}
/>
);
},
) as <E extends React.ElementType = typeof defaultElement>(
props: HeadingProps<E>,
) => JSX.Element;
FAQs
Building blocks for strongly typed polymorphic components in React.
The npm package react-polymorphic-box receives a total of 9,622 weekly downloads. As such, react-polymorphic-box popularity was classified as popular.
We found that react-polymorphic-box demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.