• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

Need help with "JS" to add a close button for Color Picker

BuddhikaDesh20

New member
Jan 12, 2021
7
2
3
Hey, Could Anyone Be Able To Help Me To Add This Functionality by With Adding A Close Button For The Selected Colors From The Color Picker.

This Is What Happen, Click On The "Plus" Icon "Color Picker" Popup
project-reactjs-s (2).png

Then It'll Show The Selected Colors In Circles, Like In The Bottom
project-reactjs-s (3).png

The Issue Is Unable To Remove This Selected Colors, Could Anyone Tell How Can I Be Able To Add A Close Button Above The Selected Color Circles.

This Is The Code :
JavaScript:
                            <div className="col">
                                <div className="form-group add-products">
                                    <label htmlFor="productPrice">Product Colors</label>
                                    <div>
                                        {colors.length > 0 &&
                                        <CirclePicker colors={colors}/>
                                        }

                                        <div className="form-check form-check-inline">
                                            <button type="button" className="btn rounded-circle"
                                                    style={{padding: "0px 0px"}}
                                                    onClick={() => setDisplayColorPicker(!displayColorPicker)}>
                                                <img src={PlusButton} alt=""/>
                                            </button>
                                            {displayColorPicker ?
                                                <div style={{position: "absolute", zIndex: "2"}}>
                                                    <div style={{
                                                        position: 'fixed',
                                                        top: '0px',
                                                        right: '0px',
                                                        bottom: '0px',
                                                        left: '0px'
                                                    }} onClick={() => {
                                                        if (!colors.includes(color)) {
                                                            setColors(values => [...values, color]);
                                                        }
                                                        setDisplayColorPicker(false);
                                                    }} />
                                                    <ChromePicker color={color} onChangeComplete={handleColorChange}/>
                                                </div>
                                                : null
                                            }
                                        </div>

                                    </div>
                                </div>
                            </div>


Thank You !
 
Depending how big it is and if u got twitchy hands and fast mouse, ignore this, but u cwould just have it close itself after brief few MS after u move ur mouse away or off of it . clicking works too. make sure to add keyboard binds like to catch Escape press, or X or something l. toggle / on off button

Not sure what kind of pseudo JS format that is, as i live under a rock, but assuming its emmet/brackets idk.. i like i though, very angular like, need to look into it. ANYHOO

u wanna add another html element, div will do, styled as button, or even a button,, place it above other elements, and/or set Z-index really high just in case. then within that just like a bove edit its onclick and and add there setDisplayColorPicker(false);
 
  • Like
Reactions: BuddhikaDesh20
try placing just after this line or within that div

tyle={{position: "absolute", zIndex: 2 (the one that comes after the << {displayColorPicker ? >>
 
  • Like
Reactions: BuddhikaDesh20
A,lso consider if it doesnt already, adding code to detect screen size, or user device view, width/height, and ensure it falls within that based on currenty X/Y global window position relative to screen (and not local, relative to the current html body - which could extend off screen
 
  • Like
Reactions: BuddhikaDesh20
Hey, Could Anyone Be Able To Help Me To Add This Functionality by With Adding A Close Button For The Selected Colors From The Color Picker.

This Is What Happen, Click On The "Plus" Icon "Color Picker" Popup
project-reactjs-s (2).png

Then It'll Show The Selected Colors In Circles, Like In The Bottom
project-reactjs-s (3).png

The Issue Is Unable To Remove This Selected Colors, Could Anyone Tell How Can I Be Able To Add A Close Button Above The Selected Color Circles.

This Is The Code :
JavaScript:
                            <div className="col">
                                <div className="form-group add-products">
                                    <label htmlFor="productPrice">Product Colors</label>
                                    <div>
                                        {colors.length > 0 &&
                                        <CirclePicker colors={colors}/>
                                        }

                                        <div className="form-check form-check-inline">
                                            <button type="button" className="btn rounded-circle"
                                                    style={{padding: "0px 0px"}}
                                                    onClick={() => setDisplayColorPicker(!displayColorPicker)}>
                                                <img src={PlusButton} alt=""/>
                                            </button>
                                            {displayColorPicker ?
                                                <div style={{position: "absolute", zIndex: "2"}}>
                                                    <div style={{
                                                        position: 'fixed',
                                                        top: '0px',
                                                        right: '0px',
                                                        bottom: '0px',
                                                        left: '0px'
                                                    }} onClick={() => {
                                                        if (!colors.includes(color)) {
                                                            setColors(values => [...values, color]);
                                                        }
                                                        setDisplayColorPicker(false);
                                                    }} />
                                                    <ChromePicker color={color} onChangeComplete={handleColorChange}/>
                                                </div>
                                                : null
                                            }
                                        </div>

                                    </div>
                                </div>
                            </div>


Thank You !


To add a close button above the selected color circles which will allow users to remove a chosen color, you need to modify the CirclePicker component to include a close button on each color. If the CirclePicker component does not natively support this functionality, you'll have to implement it yourself.

Here is a conceptual example of how you might update your code to include a close button for each color:
{colors.length > 0 &&
colors.map((selectedColor, index) => (
<div key={index} style={{ position: 'relative', display: 'inline-block', marginRight: '8px' }}>
<div
style={{
backgroundColor: selectedColor,
width: '24px',
height: '24px',
borderRadius: '50%',
border: '1px solid black'
}}
/>
<button
type="button"
className="btn"
style={{
position: 'absolute',
top: '-5px',
right: '-5px',
padding: '0',
backgroundColor: 'transparent',
border: 'none',
cursor: 'pointer'
}}
onClick={() => handleRemoveColor(index)}>
✕ {/* This can be replaced with an icon or image */}
</button>
</div>
))
}



And the handleRemoveColor function might look something like this:
function handleRemoveColor(index) {
// Create a new array without the color at the given index
const newColors = colors.filter((_, colorIndex) => index !== colorIndex);
setColors(newColors);
}



You would also need to ensure that handleColorChange and handleRemoveColor functions are properly bound in the context they are used.

This code assumes that colors is an array of color strings. The map function is used to create a list of color divs with a close button. When the close button is clicked, handleRemoveColor is called with the index of the color to be removed, which filters out the selected color from the colors array.
 
  • Like
Reactions: BuddhikaDesh20
ahhh im struggling to conceptualize that except for when u have a set of color boxes added to panel on them being selected, and then on hovering a close/remove button appearing on each.that would make sense but has nothing to do with picker, as i suggested make a empty div, 10px x 10px, place it within the main picker div, try to align it to the top right of parent div or wherervever u want close button, give it a red fill, then see if that appears in a good place in testing, and if so then u can just add the oncwlick (or ideally on mouse up ) add the close method,

if for some reason u want this mass close biuttons tjhen do the exact same thing just place the div within the div or element where the respective colour appears and align it so. But if u want it to close just that colour instead of the picker, werll then ur gonna have to writ eur own method similar to the selectPicker close method that targets specific child element -- sounds more confusing than it is. only thing to keep in mind is each element that needs to be closed needs to be uniquely referenced either by its index within a collection or array, or ideally by a unique id assigned on creation, so ur for loop u mention above u would set that id there, or just have the close action on its parent element, which would be the colour box - at least how i understand it. try find the code of that selectpicker conmponent and create something similar, U could get fancy and override it or have a 2nd argument which if defined would close a specific colour box rather than picker itseslf, and within code u would have a condition, if arg2 not null then close the element with that an id == arg2.. on close button of colours, have onclick selectPicker(false,Id_or_name)
 
Last edited:
  • Like
Reactions: BuddhikaDesh20
ahhh im struggling to conceptualize that except for when u have a set of color boxes added to panel on them being selected, and then on hovering a close/remove button appearing on each.that would make sense but has nothing to do with picker, as i suggested make a empty div, 10px x 10px, place it within the main picker div, try to align it to the top right of parent div or wherervever u want close button, give it a red fill, then see if that appears in a good place in testing, and if so then u can just add the oncwlick (or ideally on mouse up ) add the close method,

if for some reason u want this mass close biuttons tjhen do the exact same thing just place the div within the div or element where the respective colour appears and align it so. But if u want it to close just that colour instead of the picker, werll then ur gonna have to writ eur own method similar to the selectPicker close method that targets specific child element -- sounds more confusing than it is. only thing to keep in mind is each element that needs to be closed needs to be uniquely referenced either by its index within a collection or array, or ideally by a unique id assigned on creation, so ur for loop u mention above u would set that id there, or just have the close action on its parent element, which would be the colour box - at least how i understand it. try find the code of that selectpicker conmponent and create something similar, U could get fancy and override it or have a 2nd argument which if defined would close a specific colour box rather than picker itseslf, and within code u would have a condition, if arg2 not null then close the element with that an id == arg2.. on close button of colours, have onclick selectPicker(false,Id_or_name)


It seems you're looking to add a close button to each selected color in a color picker, with functionality to remove that specific color from the selection. Based on your description, here is a conceptual breakdown of how you can approach this:

  1. Create a Close Button Component:
    • You'll want to create a small close button that can be absolutely positioned within the parent color element.
    • The button should have an onClick event that triggers a function to remove the selected color.
  2. Modify the Color Display to Include the Close Button:
    • For each color that is displayed, you'll wrap it in a parent div that will also contain the close button.
    • You can position the close button absolutely within this div so that it appears in the top-right corner of the color element.
  3. Handle the Close Button Click:
    • The function that is triggered by the close button should remove the color from the state that holds the selected colors.
    • If you're using an array to hold colors, you can filter out the color that matches the one to be removed and then update the state with the new array.
Here is a conceptual example of how your component could be structured:
// ... other component code ...

// This function will be called when the close button is clicked.
// It should receive the color to be removed.
const removeColor = colorToRemove => {
setColors(colors.filter(color => color !== colorToRemove));
};

// ... other component code ...

// In your render or return method:
{colors.map((color, index) => (
<div key={index} style={{ position: 'relative', display: 'inline-block' }}>
{/* This is your color display */}
<div style={{ backgroundColor: color, width: '24px', height: '24px', borderRadius: '50%' }} />

{/* This is the close button */}
<button
style={{
position: 'absolute',
top: 0,
right: 0,
width: '10px',
height: '10px',
borderRadius: '50%',
backgroundColor: 'red',
border: 'none',
cursor: 'pointer'
}}
onClick={() => removeColor(color)} // Pass the color to be removed
>
X {/* Or use an icon/image */}
</button>
</div>
))}

// ... other component code ...



In this example, removeColor is a function that removes a color from the colors array. The map function is used to iterate over the colors array and render each color with a close button. When the close button is clicked, removeColor is called with the color to remove.

Make sure to apply the correct styling and positioning to make the close button appear where you want it. The styling provided is just a starting point and will likely need adjustments to fit your design.

Lastly, ensure that the state updates correctly and that your application logic handles the removal of colors as intended.
 
  • Like
Reactions: BuddhikaDesh20

Forum statistics

Threads
79,466
Messages
1,143,014
Members
248,602
Latest member
Tryghuiirrf
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock