How do I change the body background color with TypeScript? #5048
-
I want to change the body background color for both light and dark modes. I found this on Sandbox, but it doesn't work anymore on 1.X. What can I do? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 7 replies
-
|
Beta Was this translation helpful? Give feedback.
-
Thanks. In my browser it got overridden by other CSS values. I don't know where to start debugging. Any ideas? |
Beta Was this translation helpful? Give feedback.
-
inside index.html, find body tag. Then |
Beta Was this translation helpful? Give feedback.
-
You can change the background color with Typescript as shown below. Add the following to your file where you create your theme
The full file looks like the following:
|
Beta Was this translation helpful? Give feedback.
-
@AoverK if you wrap your code in import { StyleFunctionProps } from '@chakra-ui/theme-tools'
styles: {
global: (props: StyleFunctionProps) => ({
body: {
color: 'default',
bg: '#FFFFFF',
},
}),
}, import type { AppProps } from 'next/app'
import { Center, ChakraProvider, extendTheme, Spinner } from '@chakra-ui/react'
import { theme as chakraTheme } from "@chakra-ui/pro-theme";
import { StyleFunctionProps } from '@chakra-ui/theme-tools'
const theme = extendTheme(
{
colors: {
...chakraTheme.colors,
brand: chakraTheme.colors.blue
},
styles: {
global: (props: StyleFunctionProps) => ({
body: {
color: 'default',
bg: '#FFFFFF',
},
}),
},
components: {
Button: {
// 1. We can update the base styles
/* baseStyle: {
fontWeight: 'bold', // Normally, it is "semibold"
}, */
// 2. We can add a new button size or extend existing
/* sizes: {
xl: {
h: '56px',
fontSize: 'lg',
px: '32px',
},
}, */
// 3. We can add a new visual variant
variants: {
/* 'with-shadow': {
bg: 'red.400',
boxShadow: '0 0 2px 2px #efdfde',
}, */
// 4. We can override existing variants
outline: {
width: "140px",
borderColor: "#2962FF",
color: "#2962FF",
_hover: {
borderColor: "#0634B5",
color: "#0634B5"
}
},
/* primary: (props: StyleFunctionProps) => ({
bgColor: props.borderColor === "#2962FF"? 'red.300' : 'red.500',
color: props.color === "#2962FF"? 'red.300' : 'red.500'
}), */
primary:{
width: "140px",
bgColor: "#2962FF",
_hover: {
bgColor: "#0634B5",
}
},
/* outline: {
borderColor="#2962FF",
color="#2962FF"
} */
/* (props: StyleFunctionProps) => ({
bg: props.colorMode === 'dark' ? 'red.300' : 'red.500',
}), */
// 5. We can add responsive variants
sm: {
bg: 'teal.500',
fontSize: 'md',
},
},
// 6. We can overwrite defaultProps
defaultProps: {
/* size: 'lg', */ // default is md
/* variant: 'sm', */ // default is solid
// colorScheme: 'green', // default is gray
_hover: {
color: "#0634B5",
}
},
},
},
},
chakraTheme
);
function MyApp({ Component, pageProps }: AppProps) {
return (
<ChakraProvider theme={theme}>
<Component {...pageProps} />
</ChakraProvider>
)
}
export default MyApp |
Beta Was this translation helpful? Give feedback.
ChakraProvider
:Demo