I'm using SASS in my project, can you provide guidance on customizing the UI with @aws-amplify/ui

I've recently taken over a Next.js (React) project from another developer and they've used .scss files (SASS) for styling. The specific project I'm working on can be found here https://github.com/codebushi/nextjs-starter-dimension

My task now is to implement an authentication flow in the project using @aws-amplify/ui-react. While everything functions properly, I am looking to customize the UI style. According to the documentation, this customization can be achieved by setting CSS variables within :root in globals.css. Here's an example:

:root {
  --amplify-primary-color: #ff6347;
  --amplify-primary-tint: #ff7359;
  --amplify-primary-shade: #e0573e;
}

For further reference, you can check out the documentation here:

Although I have limited knowledge of SASS beyond the basics, I am seeking guidance on how to replicate setting these variables in :root.

Additional Details

This is my next.config.js:

module.exports = {
  webpack: (config, { dev }) => {
    config.module.rules.push(
      {
        test: /\.scss$/,
        use: ['raw-loader', 'sass-loader']
      }
    )
    return config
  }
}

Below is the code snippet defining the Amplify elements in my authentication page:

import { useState, useEffect } from 'react'
import { AuthState, onAuthUIStateChange } from '@aws-amplify/ui-components';
import { AmplifyAuthenticator, AmplifyAuthContainer, AmplifySignUp, AmplifySignIn, AmplifySignOut } from '@aws-amplify/ui-react';
import Router from 'next/router'

const Auth = (props) => {
  const [authState, setAuthState] = useState();
  const [user, setUser] = useState();

  useEffect(() => {
      return onAuthUIStateChange((nextAuthState, authData) => {
          setAuthState(nextAuthState);
          setUser(authData);
      });
  }, []);

  if (authState === AuthState.SignedIn && user) {
    Router.push('https://mywebsite')
    return <p>Redirecting...</p>
  }

  return (
    <AmplifyAuthContainer>
        <AmplifyAuthenticator usernameAlias="email">
          <AmplifySignUp
            slot="sign-up"
            usernameAlias="email"
            formFields={[
              {
                type: "email",
                label: "Email Address *",
                placeholder: "Enter your email address",
                inputProps: { required: true },
              },
              {
                type: "password",
                label: "Password *",
                placeholder: "Enter your password",
                inputProps: { required: true },
              },
            ]} 
          />
          <AmplifySignIn slot="sign-in" usernameAlias="email" />
        </AmplifyAuthenticator>
    </AmplifyAuthContainer>
  );
}

export default Auth;

Following advice provided by Sean W below, I attempted creating an _app.js with:

import '../styles/global.scss'

const App = ({ Component, pageProps }) => {
  return <Component {...pageProps} />
}

export default App;

and included the following content in the global.scss file:

:root {
  --amplify-primary-color: #ff6347;
  --amplify-primary-tint: #ff7359;
  --amplify-primary-shade: #e0573e;
}

Unfortunately, it appears that the CSS variables are not being replaced as expected.

Answer â„–1

To incorporate the styles into your pages, a convenient method is to establish a new SCSS file and include it within a custom _app.js.

By default, Next provides SASS support - you simply need to install the sass npm package without requiring a custom next.config. This could potentially be causing some of your issues.

The file, global.scss, contains:

:root{
  --amplify-primary-color: #ff6347;
  --amplify-primary-shade: #e0573e;
}

//scoped & redeclared for an element with id #__next
:root #__next {
  --amplify-primary-color: #ff6347;
  --amplify-primary-shade: #e0573e;
}

In situations where Amplify overrides styles that were initially defined for the :root, you will need to ensure that your custom styles take precedence over the default Amplify CSS variables. To achieve this, redeclare them in a CSS rule that has higher specificity than :root, as shown in the example above - while considering the order of precedence

Additionally, Amplify grants the ability to directly target components.

amplify-authenticator {
  --amplify-primary-color: #ff6347;
  background: var(--amplify-primary-color);
  padding: 5px;
}

// scoped & redeclared 
:root #__next amplify-sign-in{
  --amplify-primary-color: #ff6347;
}

The amplification elements that can be specifically targeted include:

  • amplify-authenticator
  • amplify-sign-in
  • amplify-confirm-sign-in
  • amplify-sign-up
  • amplify-confirm-sign-up
  • amplify-forgot-password
  • amplify-require-new-password
  • amplify-verify-contact
  • amplify-totp-setup

To import your file, navigate to pages/_app.js

import 'path/to/global.scss';
const App = ({ Component, pageProps }) => <Component {...pageProps} />;
export default App;

Answer â„–2

One simple line of code can create global variables, similar to :root variables.

$color = #c0ff33 !important;

These variables can be used for any content in css and sass. In larger projects, it's beneficial to organize all variable declarations in a separate file called variables.scss. This allows for easy changes to multiple variables at once by simply including the file with

@import "./variables.scss"
at the beginning of your main file.

We hope this information proves useful! Best of luck with your stylish endeavors!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Customize the background color of MaterialUI KeyboardDateTimePicker without altering the padding and layout of the component

Is it possible to alter the background color of KeyboardDateTimePicker components without affecting the padding and layout? I attempted to set the background property to style={{background: "rgb(232, 241, 250)"}}, but when combined with inputVari ...

Integrating threejs dynamically into a Create React App environment

When I am dynamically loading Three.js, the variable THREE is not found. I have created a React project using create-react-app and copied the three js file into the public folder. Here's the directory structure: src public ├── js │ └─┠...

How can I incorporate a personalized SVG path to serve as a cursor on a webpage?

Is there a way to enhance the functionality of binding the 'mousemove' event to a div and moving it around the page while hiding the real cursor? Specifically, can we change the shape of the circle to an SVG path and drag the SVG path around the ...

Error encountered while executing the create-react-app build version: Uncaught TypeError - Unable to access the property 'mountComponent' as it is undefined

Steps to Reproduce While my create-react-app functions perfectly in development mode, I encounter a blank page with this console error when running the built version using (npm run build). Uncaught TypeError: Cannot read property 'mountComponent&apo ...

I am facing an issue with MUI DatePicker that is causing issues with rendering in my project. I have also included a

Check out my codesandbox example showcasing MUI's Datepicker in action: https://codesandbox.io/s/proud-pond-fb4rm?file=/src/App.js *Update: I've also included my index.tsx code snippet: import React from 'react'; import ReactDOM from ...

Arranging divs within list items in a single row

My goal is to create a dropdown menu within an <li> element of <ul>. Here's what I have so far: HTML: <ul class="navigationTable"> <li>1</li> <li>2</li> <li><div id="comittee"> <div id= ...

Customizing the background color of a Material-UI checkbox

I am currently working with Material UI checkboxes. I want to customize the background color of the checkbox, but setting the root background to white creates a rounded shape that doesn't match my intended design. Is there a way to change the shape of ...

Adjust the appearance using Timeout within a React component

I am facing a challenge with updating the style in React - specifically, the opacity of a Div element is not updating despite successfully updating the opacity in a timeout event. import React from 'react'; function PortItem(props){ let style ...

Modifying React npm module code within the Node module directory

Hey there! I'm curious to know if it's doable to modify the code within the npm modules located in the modules folder. From what I understand, this isn't the best way to go about it. Are there any alternative methods that could lead to the s ...

Tips for setting up a full-size image with nextJS and the <Image /> component

Upgrading NextJS to the latest version has resulted in some errors when using the Image component: // import Image from 'next/image' <div style={Object.assign({}, styles.slide, style)} key={key}> <Image src={src} alt="&quo ...

What is the best way to emphasize specific text within a material-ui TextField component?

Is there a way to emphasize specific parts of the content within a TextField component without actually selecting them? For example, like this: https://i.stack.imgur.com/37po0.png I attempted using a <span> with a class in the value property, but ...

Troubleshooting Google Web Fonts

I seem to be using code similar to what I used before, but it appears that the fonts are not loading. <html> <head> <script src="http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:extralight,light,regular,bold"></s ...

I am noticing multiple quantities of the same item being added to my shopping

Recently, I encountered a problem with my online shop that seems to be related to the Javascript. The issue arises when I add an item to my shopping cart from this particular page: . Initially, when I click 'Add to Cart,' the item is correctly ad ...

Guide on aligning svg and button horizontally while maintaining vertical alignment

I found this interesting code snippet: .float-left { float: left !important; } .float-right { float: right !important; } .container { padding: 1rem; } .clearfix { clear: both; } .has-border { border: 2px solid #000; } .d-inline-block { ...

Override the class inside the ul div

I am utilizing Dreamweaver's Spy menu without any sub items. By using a background image for "not active", another for "rollover" and attempting to apply one for "active". I have created a class for "active" and assigned it to one of the ul items. How ...

What is the best way to utilize CSS to center an element in relation to the scroll bar?

For a group project, I successfully centered a div on a webpage. However, I ran into an issue where the website's contents are centered taking the scroll bar width into consideration. This means that the web page contents adjust to be centered without ...

Conditionally style a ReactJS table based on the previous value

My current React JS app utilizes the Material-UI Table component, and it looks like this: I am seeking to modify the border style between the last row of one date and the first row of another date, such as between Date A and Date B, similar to this illust ...

Modify the css based on the user's input

<html lang="en"> <head> <link rel="stylesheet" href="style.css" /> <li id="visa"> <section class="credit-card visa gr-visa"> <div class="logo">visa</div> <form> <h2>Payment ...

Adjusting the visibility of a div as you scroll

I'm trying to achieve a fade-in and fade-out effect on div elements when scrolling over them by adjusting their opacity. However, I'm facing difficulties in getting it to work properly. The issue lies in the fact that my div elements are positio ...

Asynchronous retrieval of reference value from Firebase Firestore in ReactJS

Encountering a peculiar bug in TypeScript-JavaScript where I have a Model class in TypeScript and a ReactJS Component in JS. The issue arises when dealing with a list of Promo Objects, each containing a "_listCompte" property which holds a list of Compte O ...