Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add additional props support #16

Merged
merged 2 commits into from
Dec 23, 2023
Merged

feat: add additional props support #16

merged 2 commits into from
Dec 23, 2023

Conversation

gregberge
Copy link
Owner

@gregberge gregberge commented Dec 23, 2023

Additional props

To avoid unnecessary wrappers that just pass on some props to the rendered component or element, you can use the .attrs constructor. It allows you to attach additional props (or "attributes") to a component.

Usage

Create an input of type "checkbox" with twc:

const Checkbox = twc.input.attrs({
  type: "checkbox",
})`appearance-none size-4 border-2 border-blue-500 rounded-sm bg-white`;

Adapting attributes based on props

attrs accepts a function to generate attributes based on input props.

In this example, we create an anchor that accepts an $external prop and adds target and rel attributes based on its value.

import { twc, TwcComponentProps } from "react-twc";

type AnchorProps = TwcComponentProps<"a"> & { $external?: boolean };

// Accept an $external prop that adds `target` and `rel` attributes if present
const Anchor = twc.a.attrs<AnchorProps>((props) =>
  props.$external ? { target: "_blank", rel: "noopener noreferrer" } : {},
)`appearance-none size-4 border-2 border-blue-500 rounded-sm bg-white`;

render(
  <Anchor $external href="https://cva.style">Class Variance Authority</Link>
);

Copy link

vercel bot commented Dec 23, 2023

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 23, 2023 3:24pm

@gregberge gregberge merged commit 5b67de9 into main Dec 23, 2023
6 checks passed
@gregberge gregberge deleted the additional-props branch December 23, 2023 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant