Skip to content

Commit

Permalink
feat: change forwardref api
Browse files Browse the repository at this point in the history
  • Loading branch information
nafees nazik committed Mar 24, 2022
1 parent 2539d60 commit 3be46ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ const Render = () => {
<summary>with ref forwarding</summary>

```tsx
import * as React from "react"
import { styled } from "react-cva";
import { useRef } from "react";

const Button = styled("button", true)("test", {
const Button = styled("button", React.forwardRef)("test", {
variants: {
margin: { 0: "m-0", 2: "m-2", 4: "m-4", 8: "m-8" },
padding: { 0: "p-0", 2: "p-2", 4: "p-4", 8: "p-8" },
Expand Down Expand Up @@ -101,9 +102,10 @@ const Render = () => {
<summary>with tailwind css</summary>

```tsx
import * as React from "react"
import { styled } from "react-cva";

const Button = styled("button", true)("button", {
const Button = styled("button", React.forwardRef)("button", {
variants: {
intent: {
primary: [
Expand Down Expand Up @@ -147,10 +149,11 @@ const Render = () => {
<summary>with css modules</summary>

```tsx
import * as React from "react"
import { styled } from "react-cva";
import style from "./button.module.css";

const Button = styled("button", true)(style.base, {
const Button = styled("button", React.forwardRef)(style.base, {
variants: {
intent: {
primary: style.primary,
Expand Down Expand Up @@ -189,13 +192,13 @@ const Render = () => {
Builds a `styled` component

```ts
const Component = styled("div",true)("base", options);
const Component = styled("div",React.forwardRef)("base", options);
```

#### Parameters

1. `div`: tag type (`HtmlElement`)
2. `true` should forward prop (`boolean` or `undefined`)
2. `React.forwardRef`:a forward ref function (`function` or `undefined`)
3. `base`: the base class name (`string`, `string[]` or `null`)
4. `options` _(optional)_
- `variants`: your variants schema
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export { VariantProps, ClassProp, VariantsSchema, VariantsConfig, ClassValue }

export type IntrinsicElementsKeys = keyof JSX.IntrinsicElements

type ForwardRefFunction = {
(props: any, ref?: any): any
}

type VariantOBJ<Variants> =
| (Variants extends VariantsSchema
? VariantsConfig<Variants> & ClassProp
Expand All @@ -20,7 +24,7 @@ type VariantOBJ<Variants> =

export function styled<T extends IntrinsicElementsKeys>(
Tag: T,
forwardRef?: boolean,
forwardRef?: ForwardRefFunction,
) {
return function wrapper<Variants extends VariantsSchema>(
base?: ClassValue,
Expand Down Expand Up @@ -75,6 +79,6 @@ export function styled<T extends IntrinsicElementsKeys>(
return React.createElement(_as, _props)
}

return forwardRef ? React.forwardRef(StyledWrapper) : StyledWrapper
return forwardRef ? forwardRef(StyledWrapper) : StyledWrapper
}
}

0 comments on commit 3be46ac

Please # to comment.