-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from gregberge/additional-props
feat: add additional props support
- Loading branch information
Showing
11 changed files
with
204 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/* | ||
!/dist/**/*.{ts,js} | ||
!/dist/index.d.ts | ||
!/dist/index.mjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
{ | ||
"as-child-prop": "asChild prop", | ||
"class-name-prop": "className prop" | ||
"class-name-prop": "className prop", | ||
"refs": "Refs", | ||
"styling-any-component": "Styling any component", | ||
"additional-props": "Additional props", | ||
"adapting-based-on-props": "Adapting based on props" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 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`: | ||
|
||
```ts | ||
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. | ||
|
||
```ts | ||
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> | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters