-
-
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: allow all dom elements for the chakra factory (#5508)
* feat: replace hardcoded element list with proxy * chore: polish * chore: rename interface * docs: add example
- Loading branch information
1 parent
be02865
commit e5e0f25
Showing
5 changed files
with
74 additions
and
97 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,13 @@ | ||
--- | ||
"@chakra-ui/system": minor | ||
--- | ||
|
||
Allow all `JSX.IntrinsicElements` for the chakra factory. This allows to use | ||
[every DOM element](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/30a2f70db2f9ac223fd923ff1f8bcc175c082fd0/types/react/index.d.ts#L3111-L3288) | ||
with the shorthand version: | ||
|
||
```jsx live=false | ||
<chakra.header>Header</chakra.header> | ||
<chakra.main>Main</chakra.main> | ||
<chakra.footer>Many more</chakra.footer> | ||
``` |
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,37 @@ | ||
import { DOMElements } from "./system.utils" | ||
import { ChakraStyledOptions, HTMLChakraComponents, styled } from "./system" | ||
import { As, ChakraComponent } from "./system.types" | ||
|
||
type ChakraFactory = { | ||
<T extends As, P = {}>( | ||
component: T, | ||
options?: ChakraStyledOptions, | ||
): ChakraComponent<T, P> | ||
} | ||
|
||
function factory() { | ||
const cache = new Map<DOMElements, ChakraComponent<DOMElements>>() | ||
|
||
return new Proxy(styled, { | ||
/** | ||
* @example | ||
* const Div = chakra("div") | ||
* const WithChakra = chakra(AnotherComponent) | ||
*/ | ||
apply(target, thisArg, argArray: [DOMElements, ChakraStyledOptions]) { | ||
return styled(...argArray) | ||
}, | ||
/** | ||
* @example | ||
* <chakra.div /> | ||
*/ | ||
get(_, element: DOMElements) { | ||
if (!cache.has(element)) { | ||
cache.set(element, styled(element)) | ||
} | ||
return cache.get(element) | ||
}, | ||
}) as ChakraFactory & HTMLChakraComponents | ||
} | ||
|
||
export const 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
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
e5e0f25
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: