Creating a dynamic and unique border design in MUI

Currently, I am working with React 17 and MUI version 5.6. My component requires the ability to dynamically add new borders to the parent container based on user input. While I have come across CSS tutorials that demonstrate how to achieve this effect using non-MUI techniques, these resources only provide solutions with statically coded borders rather than ones that can be added upon user demand.

If you'd like to see an example of a working sandbox for adding a single border, feel free to check out this link. Any assistance or guidance on implementing support for multiple borders would be greatly appreciated.

Answer №1

If you are facing an issue, this solution may help. Here is the functional code that allows the user to choose colors using buttons, although you can also achieve the same outcome with dropdown menus. View the code here

import { Box } from "@mui/material";
import Button from "@mui/material/Button";
import { useState } from "react";
import "./styles.css";

    export default function App() {
      const [colors, setColors] = useState([]);
      const [pixel, setPixel] = useState(6);
      const addColor = async (color) => {
        setPixel(pixel + 2);
        let randomColor = `0 0 0 ${pixel}px ${color}`;
        setColors([...colors, randomColor]);
      };
    
      return (
        <div
          style={{
            width: "100vw",
            height: "100vh",
            display: "flex",
            justifyContent: "center",
            alignItems: "center",
            flexDirection: "column"
          }}
        >
          <Box
            sx={{
              width: "50%",
              height: "20%",
              border: 3,
              boxShadow: colors.join(",")
            }}
          ></Box>
          <div style={{ marginTop: 100 }}>
            <Button onClick={() => addColor("#FFFF00")}>Yellow</Button>
            <Button onClick={() => addColor("#FF002B")}>Red</Button>
            <Button onClick={() => addColor("#0000FF")}>Blue</Button>
            <Button onClick={() => addColor("#2BFF00")}>Green</Button>
          </div>
        </div>
      );
    }

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

trigger an onChange event when initializing the value

I'm currently working on creating a select field that will automatically choose an Entry when there's only one option available. The issue I'm facing is that the onChange event isn't triggered when setting the value in the list. Is ther ...

The Material UI button feature neglects to account for custom CSS styles when attempting to override the default settings

Utilizing a custom bootstrap css styles in my react app, I am seeking to enhance the default material ui components with the bootstrap styles. import React, {useState} from 'react'; import 'cg-bootstrap/core/build/cg-bootstrap-standard.css&a ...

Getting Started with Material UI's Default Components

After working with bootstrap, I decided to explore the material ui framework as well. However, when trying to use the default MUI component without customization, such as the card component, I encountered some difficulties. Here is an example of one of th ...

The CSS class is specifically assigned to a single element

After every two seconds, my JavaScript function is triggered and based on certain logic, I aim to add a CSS class with an effect to an HTML element on the page. var counter = 0; $interval(function () { counter++; ...

Dark Mode in Next.js - Struggling to maintain the dark theme on page reloads as it keeps reverting to light mode

I've encountered an issue with the theme hook in my next.js application. Every time I reload the page, there is a slight flicker effect as the dark theme briefly appears before switching back to the light theme. Upon checking the local storage, it sho ...

Display different screens depending on whether the user is authenticated in React Native

My goal is to dynamically render a React Native screen based on different conditions: 1) when the app is loading while checking authentication status, 2) if the user is already authenticated and show the home screen, or 3) if the user is not authenticated ...

Implementing server-side API calls in REACT

Seeking to boost SEO and performance, I am working on rendering my React app server-side. However, as most of the content comes from an API that is currently being called client-side, the server is returning a mostly empty HTML template. Is there a way to ...

Is it possible to use React hooks to set state with a string that includes HTML elements?

I am attempting to update the state using a JSON file value that includes HTML content. The code snippet looks like this: ... const quote = [quote, setQuote] = useState(''); const quotes = [ { quote: "Some text." }, { quote: &a ...

Ensure that the main div remains centered on the page even when the window size is adjusted

Here is the code snippet: <div id="root"> <div id="child1">xxxx</div> <div id="child2">yyyy</div> </div> CSS : #root{ width: 86%; margin: 0 auto; } #root div { width: 50%; float: left; border: ...

What is the best way to implement a 'TextField' in Flutter with the Label positioned inside the TextField's border?

Can someone help me figure out how to modify the TextField Widget in Flutter so that its label is displayed within its border? Are there any properties in the 'TextField' that can accomplish this, or is there another method I could use? I'm ...

Ensuring the same height for the navigation bar and logo with aligned vertical text

I'm in the process of recreating a simple navigation menu and I've reached the section where I am encountering an issue with the list items. The orange li element is not filling up 100% of the width, and I also need the links to align in the midd ...

Blending React Router and Redux resulted in my redirection coming to a halt

I need to integrate Redux with React Router for my project. Initially, I tried using React Router alone and it worked fine when I clicked on images, they redirected correctly. Following the redux tutorial, now when I click on images, the URL changes (e.g. ...

next-pwa is failing to generate service worker files

I've been working on a project using react.js and next.js, and I recently decided to turn it into a PWA app by implementing next-pwa. Following the instructions provided in the pwa-next docs, when I try running npm run build, no workbox-*.js or sw.js ...

What sort of JavaScript WYSIWYG text editor provides formula support?

Looking for a Javascript rich text editor that offers formula selection in the toolbar. Any recommendations? ...

Set the class of an element dynamically using ng-class and detect changes with ng-change

I want the input field to initially have the class .form-control-error. When the user clicks on the field and starts typing, I would like it to change to .form-control-success. I attempted the following code but couldn't get it to update. The ng-chan ...

When Webpack presets are utilized, class properties undergo a transformation

As I was testing my React project on webpack-dev-server, I encountered an issue with using the classfield syntax. The error occurred during state initialization. client:162 ./src/containers/App.js Module build failed (from ./node_modules/babel-loader/lib/ ...

The node and npm versions are identical, yet the package.lock file differs

Our team is working on a React project with the same Node version (18.16.1) and NPM version (9.5.1). However, when I clone the repository and run `npm install`, I am getting a package-lock file that differs from the rest of the team's. What could be c ...

Why are Chakra UI components not responsive to small screen sizes?

Exploring the world of Chakra UI with my brand new NextJs project built using typescript has been quite interesting. I decided to play around with custom styles for the Default Button component, but encountered some issues with applying small size styles a ...

Issue with Express server routing: only the home route is functional and other routes are inaccessible

I have been diving into the world of GraphQL in combination with React, but I've hit a roadblock with routing problems. While my application works smoothly on the home route "localhost:4000/", any other route such as "localhost:4000/song" throws a &ap ...

The CSS background cannot be blurred

After constructing a basic HTML page, I decided to incorporate the following CSS code: html { background: url("photourl") no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover ...