|
| 1 | +module.exports = { |
| 2 | + App: `import { Box, SimpleGrid, IconButton, useColorMode, TableContainer, Table, Tr, Td, Th, Thead, Tbody, Tfoot, TableCaption, } from "@chakra-ui/react"; |
| 3 | +import { FaMoon, FaSun, FaPhone } from "react-icons/fa"; |
| 4 | +
|
| 5 | +export default function App() { |
| 6 | + const { toggleColorMode, colorMode } = useColorMode(); |
| 7 | + return ( |
| 8 | + <Box position="relative" h="100vh"> |
| 9 | + <TableContainer padding="1rem"> |
| 10 | + <Table> |
| 11 | + <TableCaption placement="top">XL Size Rounded Variant Table</TableCaption> |
| 12 | + <Thead> |
| 13 | + <Tr><Th>Month</Th><Th isNumeric>Spend</Th></Tr> |
| 14 | + </Thead> |
| 15 | + <Tbody> |
| 16 | + <Tr><Td>January</Td><Td isNumeric>100</Td></Tr> |
| 17 | + <Tr><Td>February</Td><Td isNumeric>100</Td></Tr> |
| 18 | + <Tr><Td>March</Td><Td isNumeric>100</Td></Tr> |
| 19 | + <Tr><Td>April</Td><Td isNumeric>100</Td></Tr> |
| 20 | + </Tbody> |
| 21 | + <Tfoot> |
| 22 | + <Tr><Th>Total</Th><Th isNumeric>400</Th></Tr> |
| 23 | + </Tfoot> |
| 24 | + </Table> |
| 25 | + </TableContainer> |
| 26 | + |
| 27 | + <TableContainer padding="1rem"> |
| 28 | + <Table variant="striped" size="sm"> |
| 29 | + <TableCaption placement="top">Size: Small, Variant: Striped</TableCaption> |
| 30 | + <Thead> |
| 31 | + <Tr><Th>Month</Th><Th isNumeric>Spend</Th></Tr> |
| 32 | + </Thead> |
| 33 | + <Tbody> |
| 34 | + <Tr><Td>January</Td><Td isNumeric>100</Td></Tr> |
| 35 | + <Tr><Td>February</Td><Td isNumeric>100</Td></Tr> |
| 36 | + <Tr><Td>March</Td><Td isNumeric>100</Td></Tr> |
| 37 | + <Tr><Td>April</Td><Td isNumeric>100</Td></Tr> |
| 38 | + </Tbody> |
| 39 | + <Tfoot> |
| 40 | + <Tr><Th>Total</Th><Th isNumeric>400</Th></Tr> |
| 41 | + </Tfoot> |
| 42 | + </Table> |
| 43 | + </TableContainer> |
| 44 | +
|
| 45 | + <IconButton |
| 46 | + aria-label="toggle theme" |
| 47 | + rounded="full" |
| 48 | + size="xs" |
| 49 | + position="fixed" |
| 50 | + top={4} |
| 51 | + left={4} |
| 52 | + onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} |
| 53 | + /> |
| 54 | + </Box> |
| 55 | + ); |
| 56 | +}`, Index: `import * as React from "react"; |
| 57 | +import { createRoot } from "react-dom/client"; |
| 58 | +import { ChakraProvider, extendTheme } from "@chakra-ui/react"; |
| 59 | +
|
| 60 | +import App from "./App"; |
| 61 | +import { tableTheme } from "./theme/components/Table"; |
| 62 | +
|
| 63 | +const theme = extendTheme({ |
| 64 | + components: { |
| 65 | + Table: tableTheme, |
| 66 | + } |
| 67 | +}); |
| 68 | +
|
| 69 | +const container = document.getElementById("root"); |
| 70 | +const root = createRoot(container!); |
| 71 | +root.render( |
| 72 | + <ChakraProvider theme={theme}> |
| 73 | + <App /> |
| 74 | + </ChakraProvider> |
| 75 | +);`, TableTheme: `import { tableAnatomy as parts } from "@chakra-ui/anatomy"; |
| 76 | +import { |
| 77 | + createMultiStyleConfigHelpers, |
| 78 | + defineStyle, |
| 79 | +} from "@chakra-ui/styled-system" |
| 80 | +
|
| 81 | +const { definePartsStyle, defineMultiStyleConfig } = |
| 82 | + createMultiStyleConfigHelpers(parts.keys) |
| 83 | +
|
| 84 | +// default base style from the Table theme |
| 85 | +const baseStyle = definePartsStyle({ |
| 86 | + table: { |
| 87 | + fontVariantNumeric: "lining-nums tabular-nums", |
| 88 | + borderCollapse: "collapse", |
| 89 | + width: "full", |
| 90 | + }, |
| 91 | + th: { |
| 92 | + fontFamily: "heading", |
| 93 | + fontWeight: "bold", |
| 94 | + textTransform: "uppercase", |
| 95 | + letterSpacing: "wider", |
| 96 | + textAlign: "start", |
| 97 | + }, |
| 98 | + td: { |
| 99 | + textAlign: "start", |
| 100 | + }, |
| 101 | + caption: { |
| 102 | + mt: 4, |
| 103 | + fontFamily: "heading", |
| 104 | + textAlign: "center", |
| 105 | + fontWeight: "medium", |
| 106 | + }, |
| 107 | +}) |
| 108 | +
|
| 109 | +const variantStriped = definePartsStyle((props) => { |
| 110 | + return { |
| 111 | + td: { |
| 112 | + fontFamily: "mono", // change font family to mono |
| 113 | + } |
| 114 | + } |
| 115 | +}) |
| 116 | +
|
| 117 | +const variantSimple = definePartsStyle((props) => { |
| 118 | + return { |
| 119 | + td: { |
| 120 | + fontWeight: "semibold", // change font weight to semibold |
| 121 | + }, |
| 122 | + } |
| 123 | +}) |
| 124 | +
|
| 125 | +// Defining a custom variant |
| 126 | +const variantRounded = definePartsStyle((props) => { |
| 127 | + const { colorScheme: c, colorMode } = props; |
| 128 | +
|
| 129 | + return { |
| 130 | + tr: { |
| 131 | + "td:first-child": { |
| 132 | + borderTopLeftRadius: "full", |
| 133 | + borderBottomLeftRadius: "full" |
| 134 | + }, |
| 135 | + "td:last-child": { |
| 136 | + borderTopRightRadius: "full", |
| 137 | + borderBottomRightRadius: "full" |
| 138 | + }, |
| 139 | + }, |
| 140 | + th: { |
| 141 | + "&[data-is-numeric=true]": { |
| 142 | + textAlign: "end" |
| 143 | + } |
| 144 | + }, |
| 145 | + td: { |
| 146 | + "&[data-is-numeric=true]": { |
| 147 | + textAlign: "end" |
| 148 | + } |
| 149 | + }, |
| 150 | + caption: { |
| 151 | + color: colorMode === "light" ? \`\${c}.600\` : \`\${c}.100\`, |
| 152 | + }, |
| 153 | + tbody: { |
| 154 | + tr: { |
| 155 | + "&:nth-of-type(odd)": { |
| 156 | + "th, td": { |
| 157 | + borderBottomWidth: "1px", |
| 158 | + borderColor: colorMode === "light" ? \`\${c}.100\` : \`\${c}.700\`, |
| 159 | + }, |
| 160 | + td: { |
| 161 | + background: colorMode === "light" ? \`\${c}.100\` : \`\${c}.700\`, |
| 162 | + } |
| 163 | + }, |
| 164 | + "&:nth-of-type(even)": { |
| 165 | + "th, td": { |
| 166 | + borderBottomWidth: "1px", |
| 167 | + borderColor: colorMode === "light" ? \`\${c}.300\` : \`\${c}.600\`, |
| 168 | + }, |
| 169 | + td: { |
| 170 | + background: colorMode === "light" ? \`\${c}.300\` : \`\${c}.600\`, |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + }, |
| 175 | + tfoot: { |
| 176 | + tr: { |
| 177 | + "&:last-of-type": { |
| 178 | + th: { borderBottomWidth: 0 } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + }; |
| 183 | +}); |
| 184 | +
|
| 185 | +const variants = { |
| 186 | + simple: variantSimple, |
| 187 | + striped: variantStriped, |
| 188 | + rounded: variantRounded, |
| 189 | +} |
| 190 | +
|
| 191 | +const xl = defineStyle({ |
| 192 | + fontSize: 'lg', |
| 193 | + px: '4', |
| 194 | + h: '12', |
| 195 | +}) |
| 196 | +
|
| 197 | +const sizes = { |
| 198 | + xl: definePartsStyle({ |
| 199 | + td: xl, |
| 200 | + th: xl, |
| 201 | + }), |
| 202 | +} |
| 203 | +
|
| 204 | +export const tableTheme = defineMultiStyleConfig({ |
| 205 | + baseStyle, |
| 206 | + variants, |
| 207 | + sizes, |
| 208 | + defaultProps: { |
| 209 | + size: "xl", |
| 210 | + variant: "rounded", |
| 211 | + colorScheme: "gray" |
| 212 | + }, |
| 213 | +})`, |
| 214 | +} |
0 commit comments