I am currently utilizing a react color picker to create a personalized 16 color gradient. The process involves selecting a color from a dropdown menu and then using the color picker to make adjustments. This action updates an array that dictates the styling of each box in the dropdown. Ultimately, my objective is to modify the background of each entry in the dropdown to reflect the respective color in the gradient.
const gradientColors = ['#000000', '#000000', '#000000', '#000000', '#000000', '#000000', '#000000', '#000000', '#000000', '#000000', '#000000', '#000000', '#000000', '#000000', '#000000', '#000000'];
function ChangeCustomGradientColor(eventKey, color) {
gradientColors[eventKey] = color;
}
function App() {
const [color, setColor] = useState('#fff')
const [showColorPicker, setShowColorPicker] = useState(false)
const [eKey, setEventKey] = useState('');
const eventKeySelect=(e)=>{
setEventKey(e)
setShowColorPicker(showColorPicker => !showColorPicker)
}
return (
<div className="App" id="top">
<header className="App-header" id="mid">
<Dropdown onSelect={eventKeySelect}>
<Dropdown.Toggle variant="success" id="custom-color-change">
Change Custom Gradient Color
</Dropdown.Toggle>
<Dropdown.Menu >
<Dropdown.Item id="cust0" eventKey="0" style={{backgroundColor:gradientColors[0]}}>1</Dropdown.Item>
<Dropdown.Item id="cust1" eventKey="1" style={{backgroundColor:gradientColors[1]}}>2</Dropdown.Item>
...
...
...
<Dropdown.Item id="cust15" eventKey="15" style={{backgroundColor:gradientColors[15]}}>16</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
{
showColorPicker && (
<ChromePicker
disableAlpha={true}
color={color}
onChangeComplete={updatedColor => ChangeCustomGradientColor(eKey, updatedColor)}
/>
)}
</header>
</div>
)
}
In addition to the above approach, I have attempted to utilize the getElementByID
method in my ChangeCustomGradientColor
function, as shown below:
function ChangeCustomGradientColor(eventKey, color) {
let elementID = "cust" + eventKey;
document.getElementByID(elementID).style.backgroundColor = color;
}
While I have made progress through trial and error, I have reached a point where I recognize the importance of dedicating time to thorough JavaScript education. Any guidance or assistance would be greatly appreciated.