-
-
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: replace hardcoded element list with proxy
- Loading branch information
1 parent
d4d18b7
commit 76dc3c8
Showing
5 changed files
with
67 additions
and
96 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,28 @@ | ||
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, { | ||
apply(target, thisArg, argArray: [DOMElements, ChakraStyledOptions]) { | ||
return styled(...argArray) | ||
}, | ||
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