|
| 1 | +# Chakra Factory |
| 2 | + |
| 3 | +Chakra factory serves as an **object of chakra enabled JSX elements**, and also |
| 4 | +**a function that can be used to enable custom component** to receive chakra's |
| 5 | +style props. |
| 6 | + |
| 7 | +```bash |
| 8 | +import { chakra } from "@chakra-ui/vue-next" |
| 9 | +``` |
| 10 | +
|
| 11 | +## Chakra JSX Elements |
| 12 | +
|
| 13 | +Create base HTML elements with theme-aware style props using `chakra.<element>` |
| 14 | +notation. For example, if you want a plain HTML button with the ability to pass |
| 15 | +chakra styles, you can write `<chakra.button />`. |
| 16 | +
|
| 17 | +```jsx |
| 18 | +<chakra.button |
| 19 | + px="3" |
| 20 | + py="2" |
| 21 | + bg="green.200" |
| 22 | + rounded="md" |
| 23 | + :_hover="{ bg: "green.300" }" |
| 24 | +> |
| 25 | + Click me |
| 26 | +</chakra.button> |
| 27 | +``` |
| 28 | +
|
| 29 | +This reduces the need to create custom component wrappers and name them. This |
| 30 | +syntax is available for common HTML elements. See the reference for the full |
| 31 | +[list of elements](https://github.com/chakra-ui/chakra-ui/blob/main/packages/system/src/system.utils.ts#L9) |
| 32 | +supported. |
| 33 | +
|
| 34 | +```jsx |
| 35 | +<chakra.h1 fontSize="lg"> Heading </chakra.h1> |
| 36 | +``` |
| 37 | +
|
| 38 | +## Chakra factory function |
| 39 | +
|
| 40 | +This is a function that converts **non-chakra components** or **jsx element** to |
| 41 | +chakra-enabled components so you can pass style props to them. |
| 42 | +
|
| 43 | +Consider a package called `my-awesome-components`, let's use the chakra factory |
| 44 | +function to make it possible to pass style props. |
| 45 | +
|
| 46 | +The function will infer the prop types from the wrapped component and also add |
| 47 | +chakra style props. |
| 48 | +
|
| 49 | +```vue |
| 50 | +<template> |
| 51 | + <ChakraAwesomeComponent bg="red.200" fontSize="12px" /> |
| 52 | +</template> |
| 53 | +
|
| 54 | +<script setup> |
| 55 | + import { chakra } from "@chakra-ui/vue-next" |
| 56 | + import awesomeComponent from "my-awesome-components" |
| 57 | +
|
| 58 | + const ChakraAwesomeComponent = chakra(awesomeComponent) |
| 59 | +</script> |
| 60 | +``` |
| 61 | +
|
| 62 | +> Considering that Chakra uses `emotion` under the hood, ensure the non-chakra |
| 63 | +> component accepts `className` as props for this to work correctly |
| 64 | +
|
| 65 | +### Attaching styles |
| 66 | +
|
| 67 | +In some instances, you might need to attach specific styles to the component |
| 68 | +wrapped in the chakra factory |
| 69 | +
|
| 70 | +```jsx |
| 71 | +const ChakraAwesomeComponent = chakra(awesomeComponent, { |
| 72 | + baseStyle: { |
| 73 | + bg: "papayawhip", |
| 74 | + color: "red.500" |
| 75 | + }, |
| 76 | +}) |
| 77 | +``` |
| 78 | +
|
| 79 | +You can also use the chakra factory on jsx-elements as well. |
| 80 | +
|
| 81 | +```jsx |
| 82 | +const Card = chakra("div", { |
| 83 | + baseStyle: { |
| 84 | + shadow: "lg", |
| 85 | + rounded: "lg", |
| 86 | + bg: "white" |
| 87 | + }, |
| 88 | +}) |
| 89 | +``` |
| 90 | +
|
| 91 | +## On DOM elements |
| 92 | +If you wish to use the Chakra Factory on all dom elements to create base HTML elements |
| 93 | +with theme-aware style props using `chakra.<element>` notation. |
| 94 | +
|
| 95 | +In your `main.ts` file make sure to import the chakra object from the '@chakra-ui/vue-next' package. |
| 96 | +Use a forEach() function to loop over all the dom elements and push them through the factory function (chakra()). |
| 97 | +
|
| 98 | +```jsx |
| 99 | +import { createApp } from 'vue' |
| 100 | +import App from './App.vue' |
| 101 | +import ChakraUIVuePlugin, { chakra } from '@chakra-ui/vue-next' |
| 102 | +import { domElements } from '@chakra-ui/vue-system' |
| 103 | +
|
| 104 | +const app = createApp(App).use(ChakraUIVuePlugin) |
| 105 | +
|
| 106 | +domElements.forEach((tag) => { |
| 107 | + app.component(`chakra.${tag}`, chakra(tag)) |
| 108 | +}) |
| 109 | +// For example now `chakra.img` can be styled with style props. |
| 110 | +
|
| 111 | +app.mount('#app') |
| 112 | +``` |
0 commit comments