gregberge / twc

Create reusable React + Tailwind CSS components in one line ✨
https://react-twc.vercel.app
MIT License
1.35k stars 31 forks source link

feat: support react-aria-components render prop #20

Closed gregberge closed 10 months ago

gregberge commented 10 months ago

Fix #11

Render props

Some libraries like react-aria-components or Remix accepts a function as className. tsc supports render props out of the box.

Usage

In this example, we use a render props to change the class of a react-aria-components button when the button is pressed.

import { twc } from 'react-twc'
import { Button as AriaButton, ButtonProps } from "react-aria-components";

const Button = twc(AriaButton)<ButtonProps>(
  (props) => (({ isPressed })) =>
    isPressed ? "bg-gray-700" : "bg-gray-500",
);

export default () => (
  <Button>Click me</Button>
);
vercel[bot] commented 10 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
twc ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 24, 2023 0:38am
izznatsir commented 10 months ago

This approach is brilliant!

gregberge commented 10 months ago

@devongovett could you please give a review on this? What do you think?

https://twc-o3f01nfwe-argos-ci.vercel.app/docs/integrations/react-aria-components

gregberge commented 10 months ago

@izznatsir final implementation, please review :)

gregberge commented 10 months ago

I finally found a way to support it without any adapter 🎉

ducan-ne commented 10 months ago

@gregberge Do you think this API could be way better?

const Button = twc(AriaButton)<ButtonProps>(
  (props, { isPressed }) =>
    isPressed ? "bg-gray-700" : "bg-gray-500",
);

I'm uncertain about the technical aspect that is possible