Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@rpxl/recast
Advanced tools
Build components once. Use everywhere.
Recast is a fundamentally different approach to building React components to maximise reusability.
Creating component libraries is a challenging and time-consuming task. Even the seemingly straightforward process of developing a sensible button component can lead to a daunting proliferation of props, primarily driven by the need for theming. Consider the numerous instances where theme-related props and styles are embedded in a component - such as variant: "primary" | "secondary" | "tertiary" or size: "sm" | "md" | "lg". This tight coupling of component props with theme requirements not only results in an ever-expanding list of props but also presents a significant hurdle to reusing components across projects without duplicating code purely for the purposes of theming.
Imagine being able to liberate your component primitives from theme dependencies, allowing them to be written once and used across projects.
Recast is not just a collection of small utilities; it is an approach/pattern to building truly reusable component primitives by abstracting the theme layer from the internal workings of a component.
The specific values that a Recast "primitive" can receive are not specified within the component, instead these are defined by wrapping the component with a styles definition that will form the theme API.
Recast is for any individual/team who wants to build a truly reusable component library that can be used across projects without duplicating code purely for the purposes of theming.
npm install @rpxl/recast
import { recast } from "@rpxl/recast";
const Button = recast(ButtonPrimitive, {
base: "bg-blue-500 text-white px-4 py-2 rounded",
variants: {
size: {
sm: "text-sm",
md: "text-base",
lg: "text-lg",
},
},
});
<Button size="md">Click me</Button>
Base styles are applied to all instances of the component:
const Button = recast(ButtonPrimitive, {
base: "bg-blue-500 text-white px-4 py-2 rounded",
});
Variants allow you to define different style variations:
const Button = recast(ButtonPrimitive, {
base: "bg-blue-500 text-white px-4 py-2 rounded",
variants: {
size: {
sm: "text-sm",
md: "text-base",
lg: "text-lg",
},
color: {
primary: "bg-blue-500",
secondary: "bg-gray-500",
},
},
});
Modifiers are boolean props that can be applied to change styles:
const Button = recast(ButtonPrimitive, {
base: "bg-blue-500 text-white px-4 py-2 rounded",
modifiers: {
disabled: "opacity-50 cursor-not-allowed",
active: "ring-2 ring-blue-300",
},
});
Recast supports responsive styling out of the box when used with the @rpx/tailwind-recast-plugin
. This is an opt-in feature that is tightly coupled with Tailwind CSS:
<Button size={{ default: "sm", md: "lg" }}>Responsive Button</Button>
Note: To enable responsive styling, you need to install and configure the
@rpx/tailwind-recast-plugin
. This plugin integrates Recast with Tailwind CSS, allowing you to use Tailwind's responsive breakpoints in your Recast components.
Apply styles based on specific combinations of variants and modifiers:
const Button = recast(ButtonPrimitive, {
base: "bg-blue-500 text-white px-4 py-2 rounded",
variants: {
size: {
sm: "text-sm",
lg: "text-lg",
},
},
modifiers: {
disabled: "opacity-50",
},
conditionals: [
{
variants: { size: "lg" },
modifiers: ["disabled"],
className: "border-2 border-red-500",
},
],
});
Recast excels at managing complex React component structures. For example, a slider component composed of multiple elements can have styles applied independently to each part.
Set default values for variants and modifiers:
const Button = recast(ButtonPrimitive, {
base: "bg-blue-500 text-white px-4 py-2 rounded",
variants: {
size: {
sm: "text-sm",
md: "text-base",
lg: "text-lg",
},
},
defaults: {
variants: { size: "md" },
},
});
Recast can be used alongside other styling solutions like Tailwind CSS or CSS-in-JS libraries. This flexibility allows you to integrate Recast into your existing projects seamlessly.
The main function for creating Recast components:
recast(Component, styles: RecastStyles): RecastComponent
The structure for defining styles in Recast:
interface RecastStyles {
base?: string | string[];
variants?: Record<string, Record<string, string | string[]>>;
modifiers?: Record<string, string | string[]>;
conditionals?: Array<{
variants?: Record<string, string>;
modifiers?: string[];
className: string | string[];
}>;
defaults?: {
variants?: Record<string, string>;
modifiers?: string[];
};
}
Common issues when using Recast:
We welcome contributions to Recast! Here's how you can help:
main
.Please refer to the CONTRIBUTING.md file for more detailed information.
Recent changes and updates to Recast:
For a complete list of changes, please refer to the CHANGELOG.md file.
For full documentation, visit here.
Recast draws inspiration from several excellent projects in the CSS-in-JS and variant-based styling ecosystem:
We're grateful to these projects for pushing the boundaries of component styling and inspiring aspects of Recast's design.
FAQs
Unknown package
We found that @rpxl/recast demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.