-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v3): support
asChild
composition (#8302)
* feat: support as child composition * refactor: simplify types * chore: deprecate as props * refactor: rename vars
- Loading branch information
1 parent
42a006a
commit 8296ad3
Showing
8 changed files
with
196 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@chakra-ui/react": minor | ||
--- | ||
|
||
Add support for `asChild` in chakra factory |
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,23 @@ | ||
# v3 Migration Guide | ||
|
||
## Changed | ||
|
||
## Added | ||
|
||
### `asChild` prop | ||
|
||
Removed support for `as` prop due to the type complexity involved. | ||
|
||
**Action:** Replace `asChild` in `chakra` factory and existing components. | ||
|
||
```tsx | ||
import { Button } from "@chakra-ui/react" | ||
|
||
const Demo = () => { | ||
return ( | ||
<Button asChild> | ||
<a href="#">Child</a> | ||
</Button> | ||
) | ||
} | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { callAll } from "@chakra-ui/utils" | ||
|
||
interface Props { | ||
[key: string]: any | ||
} | ||
|
||
const clsx = (...args: (string | undefined)[]) => | ||
args | ||
.map((str) => str?.trim?.()) | ||
.filter(Boolean) | ||
.join(" ") | ||
|
||
type TupleTypes<T extends any[]> = T[number] | ||
|
||
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ( | ||
k: infer I, | ||
) => void | ||
? I | ||
: never | ||
|
||
const eventRegex = /^on[A-Z]/ | ||
|
||
export function mergeProps<T extends Props>( | ||
...args: T[] | ||
): UnionToIntersection<TupleTypes<T[]>> { | ||
let result: Props = {} | ||
|
||
for (let props of args) { | ||
for (let key in result) { | ||
if ( | ||
eventRegex.test(key) && | ||
typeof result[key] === "function" && | ||
typeof props[key] === "function" | ||
) { | ||
result[key] = callAll(result[key], props[key]) | ||
continue | ||
} | ||
|
||
if (key === "className" || key === "class") { | ||
result[key] = clsx(result[key], props[key]) | ||
continue | ||
} | ||
|
||
if (key === "style") { | ||
result[key] = Object.assign({}, result[key] ?? {}, props[key] ?? {}) | ||
continue | ||
} | ||
|
||
result[key] = props[key] !== undefined ? props[key] : result[key] | ||
} | ||
|
||
// Add props from b that are not in a | ||
for (let key in props) { | ||
if (result[key] === undefined) { | ||
result[key] = props[key] | ||
} | ||
} | ||
} | ||
|
||
return result as any | ||
} |
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