Customize colors of withAuthenticator HOC component - aws-amplify

I am trying to customize the colors in withAuthenticator HOC aws-amplifier login screen.
I followed:
https://aws-amplify.github.io/docs/js/authentication#using-components-in-react
and also read:
https://medium.com/#coryschimmoeller/customizing-the-authentication-experience-of-amplifys-withauthenticator-e6f2089ff469
import { AmplifyTheme } from 'aws-amplify-react';
const myTheme = {
...AmplifyTheme,
BackgroundColor: { color: 'blue',backgroundColor: 'blue' },
button: { color: 'blue',backgroundColor: 'blue' },
amazonSignInButton: { color: 'blue',backgroundColor: 'blue' },
signInButton: { backgroundColor: 'blue' , color: 'blue'}
};
...
//export default App;
export default withAuthenticator(App, myTheme );
amplify still renders the AWS default look and feel. I doesn't make any difference what I put in myTheme, looks like as if it is ignored completely.
Thanks for any feedback in advance.

You need to adress the different elements like so:
import { AmplifyTheme } from "aws-amplify-react";
const authTheme = {
...AmplifyTheme,
sectionHeader:{
...AmplifyTheme.sectionHeader,
color:"red",
},
formSection: {
...AmplifyTheme.formSection,
backgroundColor: "green",
},
sectionFooter: {
...AmplifyTheme.sectionFooter,
backgroundColor: "purple"
},
button: {
...AmplifyTheme.button,
backgroundColor: "blue"
}
}
export default withAuthenticator(App, { theme: authTheme });
If you are not sure about the names of the different elements you can look them up in the developer console of your browser. It´s a bit tedious but i haven´t found a documentation so far

Taken from the documentation:
Web
const MyTheme = {
signInButtonIcon: { 'display': 'none' },
googleSignInButton: { 'backgroundColor': 'red', 'borderColor': 'red' }
}
<Authenticator theme={MyTheme} />
Web components reference
React Native
import { AmplifyTheme } from 'aws-amplify-react-native';
const MySectionHeader = Object.assign({}, AmplifyTheme.sectionHeader, { background: 'orange' });
const MyTheme = Object.assign({}, AmplifyTheme, { sectionHeader: MySectionHeader });
<Authenticator theme={MyTheme} />
React Native components reference
Since the question is about withAuthenticator specifically, the previous examples apply to that too:
export default withAuthenticator(App, false, [], null, MyTheme);

Related

Why is the custom color in tailwind not defined in NextJS production stage

I created a custom color on tailwind in next js. On localhost the defined color appears fine, but when I deploy to vercel the color doesn't appear.
here's the picture localhost
production in vercel
tailwind.config.js
const colors = require('tailwindcss/colors');
module.exports = {
purge: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}'
],
darkMode: false, // or 'media' or 'class'
theme: {
colors: {
transparent: 'transparent',
current: 'currentColor',
black: {
DEFAULT: '#23232D'
},
white: colors.white,
gray: {
DEFAULT: '#A1A1A1',
},
...
}
},
variants: {
extend: {},
},
plugins: [],
}
ButtonColor/index.js
import PropTypes from 'prop-types';
import { motion } from 'framer-motion';
function ButtonColor({ color, isOpen, onClick }) {
const variants = {
open: { width: '100%' },
closed: { width: '50%' },
}
return (
<motion.div
className={`bg-${color} h-6 cursor-pointer`}
onClick={onClick}
animate={isOpen ? "open" : "closed"}
variants={variants}
>
</motion.div>
)
}
ButtonColor.propTypes = {
color: PropTypes.string.isRequired,
isOpen: PropTypes.bool.isRequired,
onClick: PropTypes.func.isRequired,
}
export default ButtonColor;
Any solutions for this case? thanks.
You can't use string concatenation to create CSS class names because PurgeCSS won't know to preserve your custom classes during the build process.
className={`${color === 'red' ? 'bg-red' : 'bg-blue'} h-6 cursor-pointer`}
Alternatively, you can create a custom global CSS/SCSS file. In that file, define your styles using tailwindcss directives.
global.{css|scss}
.button {
#apply h-6;
#apply cursor-pointer;
&.red{
#apply bg-red-700 dark:bg-red-900;
#apply text-white;
#apply hover:bg-red-800 dark:hover:bg-red-800;
}
&.gray {
#apply bg-gray-300 dark:bg-gray-600;
#apply text-gray-900 dark:text-gray-200;
#apply hover:bg-gray-400 dark:hover:bg-gray-500;
}
}
<motion.button className="button"> ...
Side note - motion.div should be motion.button

Access Button Primary Color In React

I am trying to provide overrides in the theme for buttons that are contained, primary and hovered. I tried this but it doesn't work
CODESANDBOX LINK CLICK HERE
theme/overrides/MuiButton.js
import palette from '../palette';
export default {
contained: {
backgroundColor: '#FFFFFF',
'&.primary': {
'&:hover': {
backgroundColor: palette.primary.dark,
},
},
},
};
theme/overrides/index.js
import MuiButton from "./MuiButton";
export default {
MuiButton
};
theme/index.js
import { createMuiTheme } from "#material-ui/core";
import palette from "./palette";
import typography from "./typography";
import overrides from "./overrides";
const theme = createMuiTheme({
palette,
typography,
overrides,
zIndex: {
appBar: 1200,
drawer: 1100
}
});
export default theme;
The best resource for determining how to appropriately override the default styles in your theme, is to look at how the default styles are defined.
From https://github.com/mui-org/material-ui/blob/v4.11.0/packages/material-ui/src/Button/Button.js#L138:
containedPrimary: {
color: theme.palette.primary.contrastText,
backgroundColor: theme.palette.primary.main,
'&:hover': {
backgroundColor: theme.palette.primary.dark,
// Reset on touch devices, it doesn't add specificity
'#media (hover: none)': {
backgroundColor: theme.palette.primary.main,
},
},
},
Translating this approach to the code in your question would look like this:
import palette from '../palette';
export default {
containedPrimary: {
backgroundColor: '#FFFFFF',
'&:hover': {
backgroundColor: palette.primary.dark,
},
},
};

How to Overwrite CSS of an external [third-party] component using Material-UI-React?

I'm trying to overwrite the default CSS of an external component which isn't developed in Material-UI or my project. In styled-components, I can just take the root classes and replace them with my custom CSS. How do I do the same with Material-UI-React?
.ace-tm .ace_variable {
color : red
}
Suppose I've to replace those two classes with the new color property, how do I do it in Material styles?
This is what I've tried with no luck!
const Styles = {
" & ace-tm": {
"& ace_variable": {
color: red,
fontSize: "16px"
},
}
};
I'm using withStyles to later inject them in the components.
I just found this and thought I'd share the solution for posterity:
const GlobalCss = withStyles((theme) => ({
'#global': {
'.ace-tm .ace_variable': {
color: 'red',
},
},
}))(() => null)
const SomeComponent = () => {
return (
<>
<GlobalCss />
<h1>Hey Jude</h1>
<SomeComponentWhoseCSSWillBeModified />
</>
}
Read more on this here: https://material-ui.com/styles/advanced/#global-css

Dynamically swap css in react

Im generating two css files from my sass files with defined color variables
Lets call them dark.css and light.css
Now what i want to do is dynamically swap these two css to change themes
So far I have something like this
...
componentDidUpdate() {
if (this.props.colorScheme === 'dark') {
require('../../../static/css/style-dark.css');
} else {
require('../../../static/css/style-light.css');
}
}
}
...
Above doesnt really work.Well it wroks ..partially
The problem is that theme is switched only once e.g default one is light then it can be changed to dark but after that no matter what i cant change it back to light
Had somebady similar problem? maybe this isnt a right way at all so any ideas are apprecited
I think you can do something like that...
Create a js styles file instead of css styles file.
example file1.js
export default = ({
backgroundColor: 'red',
color: 'blue',
paddingTop: 20,
paddingBottom: 20,
});
// file 1
//export default({ <-- replace this line in your file
const file1 = {
backgroundColor: 'red',
color: 'blue',
paddingTop: 20,
paddingBottom: 20,
};
// file 2
//export default({ <-- replace this line in your file
const file2 = {
backgroundColor: 'yellow',
color: 'black',
paddingTop: 40,
paddingBottom: 40,
};
class Dashboard extends React.Component {
constructor(props) {
super(props);
this.state = {
theme: 1,
styles: {...file1},
};
}
changeTheme = () => {
this.setState({
styles: this.state.theme === 1 ? {...file2} : {...file1},
theme: this.state.theme === 1 ? 2 : 1,
});
};
render() {
return (
<div style={this.state.styles}>
Hello
<button onClick={this.changeTheme}>
Change Theme
</button>
</div>
);
}
}
ReactDOM.render(
<Dashboard />,
document.querySelector('#app')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.1/umd/react-dom.production.min.js"></script>
<div id="app"></div>

Using common classes with withStyles and material ui

I am using React with TypeScript and Material UI. So I have the the following layout
<MuiThemeProvider theme={muiTheme}>
<My Component/>
</MuiThemeProvider>
And then in my component I have something similar to
let styles: any = ({ palette }: any) => ({
root: {
marginTop: 10
}
});
export default withStyles(styles)(MyComponent);
What would be the preferred way to go if I want to share the root class among multiple components? Can I extend Material UI theme when I am creating it using createMuiTheme?
Any advise will be highly appreciated
Here is how I define my custom theme in Material-ui
const Theme = createMuiTheme({
typography: {
fontSize: 18,
},
palette: {
primary: {
light: '#40bbbf',
main: '#032B43',
dark: '#0b777b',
},
},
customCommonClass: {
textAlign: center,
},
})
Theme.overrides = {
MuiListItemIcon: {
root: {
marginRight: 0,
},
},
MuiListItem: {
dense: {
paddingTop: '4px',
paddingBottom: '4px',
},
},
}
There are 3 parts within this creation:
the first I will define some defaults of my theme (for the fontSize, and palette color as an example)
The second I create some global class customCommonClass that I can use in all styled components down the road (your question)
I override some class of the material ui components.
hope this can help

Resources