@@ -25,6 +25,12 @@ type AdditionalColorPickerProps = ColorProps & {
2525
2626type ColorPickerProps = ColorProps & { additional ?: boolean } ;
2727
28+ type CustomColorEntry = {
29+ key : string ;
30+ name : string ;
31+ hex : string ;
32+ } ;
33+
2834const defaultColor = '#663399' ;
2935
3036const colorPalette = [
@@ -174,41 +180,48 @@ const CodePreview = ({
174180 ) ;
175181} ;
176182
183+ let uniqColorKey = 0 ;
184+
177185const CustomColors = ( {
178186 customColors,
179187 setCustomColors,
180188} : {
181- customColors : [ string , string ] [ ] ;
182- setCustomColors : React . Dispatch < React . SetStateAction < [ string , string ] [ ] > > ;
189+ customColors : CustomColorEntry [ ] ;
190+ setCustomColors : React . Dispatch < React . SetStateAction < CustomColorEntry [ ] > > ;
183191} ) => {
184192 const addCustomColor = ( ) => {
185- const newName = `custom${ customColors . length } ` ;
186- setCustomColors ( ( colors ) => [ ...colors , [ newName , defaultColor ] ] ) ;
193+ const newColor = {
194+ key : `custom${ ++ uniqColorKey } ` ,
195+ name : `custom${ customColors . length } ` ,
196+ hex : defaultColor ,
197+ } ;
198+ setCustomColors ( ( colors ) => [ ...colors , newColor ] ) ;
187199 } ;
188200
189- const setCustomColor = ( index : number , hex : string ) => {
201+ const setCustomColor = ( key : string , hex : string ) => {
190202 setCustomColors ( ( colors ) =>
191- colors . map ( ( color , i ) => ( i === index ? [ color [ 0 ] , hex ] : color ) )
203+ colors . map ( ( color ) => ( color . key === key ? { ... color , hex } : color ) )
192204 ) ;
193205 } ;
194206
195- const setCustomName = ( index : number , name : string ) => {
207+ const setCustomName = ( key : string , name : string ) => {
196208 setCustomColors ( ( colors ) =>
197- colors . map ( ( color , i ) => ( i === index ? [ name , color [ 1 ] ] : color ) )
209+ colors . map ( ( color ) => ( color . key === key ? { ... color , name } : color ) )
198210 ) ;
199211 } ;
200212
201- const deleteCustomColor = ( index : number ) =>
202- setCustomColors ( ( colors ) => colors . filter ( ( _c , i ) => i !== index ) ) ;
213+ const deleteCustomColor = ( key : string ) =>
214+ setCustomColors ( ( colors ) => colors . filter ( ( color ) => key !== color . key ) ) ;
203215
204216 return (
205217 < >
206- { customColors . map ( ( [ name , color ] , index ) => (
218+ { customColors . map ( ( { key , name, hex } , index ) => (
207219 < CustomColor
208- key = { `${ index } ${ color } ` }
220+ key = { key }
221+ uniqKey = { key }
209222 name = { name }
210223 index = { index }
211- color = { color }
224+ color = { hex }
212225 setCustomColor = { setCustomColor }
213226 deleteCustomColor = { deleteCustomColor }
214227 setCustomName = { setCustomName }
@@ -233,22 +246,24 @@ const CustomColor = ({
233246 name,
234247 color,
235248 index,
249+ uniqKey,
236250 setCustomColor,
237251 deleteCustomColor,
238252 setCustomName,
239253} : {
240254 name : string ;
241255 color : string ;
256+ uniqKey : string ;
242257 index : number ;
243- setCustomColor : ( index : number , hex : string ) => void ;
244- deleteCustomColor : ( index : number ) => void ;
245- setCustomName : ( index : number , name : string ) => void ;
258+ setCustomColor : ( key : string , hex : string ) => void ;
259+ deleteCustomColor : ( key : string ) => void ;
260+ setCustomName : ( key : string , name : string ) => void ;
246261} ) => {
247262 // const [colorName, setColorName] = useState<string>(name);
248263
249264 const onNameChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
250265 event . preventDefault ( ) ;
251- setCustomName ( index , event . target . value ) ;
266+ setCustomName ( uniqKey , event . target . value ) ;
252267 } ;
253268
254269 if ( name ) {
@@ -265,12 +280,12 @@ const CustomColor = ({
265280 < td >
266281 < ColorPicker
267282 color = { color }
268- setColor = { ( hex ) => setCustomColor ( index , hex ) }
283+ setColor = { ( hex ) => setCustomColor ( uniqKey , hex ) }
269284 additional
270285 />
271286 < button
272287 className = "color-picker-button-cancel"
273- onClick = { ( ) => deleteCustomColor ( index ) }
288+ onClick = { ( ) => deleteCustomColor ( uniqKey ) }
274289 >
275290 ✕
276291 </ button >
@@ -290,7 +305,7 @@ const CustomColor = ({
290305 />
291306 < button
292307 className = "color-picker-button-cancel"
293- onClick = { ( ) => deleteCustomColor ( index ) }
308+ onClick = { ( ) => deleteCustomColor ( uniqKey ) }
294309 >
295310 ✕
296311 </ button >
@@ -305,15 +320,15 @@ const DynamicColorTheme = () => {
305320 const [ primary , setPrimary ] = useState < string > ( defaultColor ) ;
306321 const [ secondary , setSecondary ] = useState < string | undefined > ( ) ;
307322 const [ tertiary , setTertiary ] = useState < string | undefined > ( ) ;
308- const [ customColors , setCustomColors ] = useState < [ string , string ] [ ] > ( [ ] ) ;
323+ const [ customColors , setCustomColors ] = useState < CustomColorEntry [ ] > ( [ ] ) ;
309324
310325 const darkMode = isDark ? 'dark' : 'light' ;
311326 const baseTheme = hexThemeFromColor ( primary ) ;
312327 const themes = prepareThemes ( {
313328 primary,
314329 secondary,
315330 tertiary,
316- custom : customColors ,
331+ custom : customColors . map ( ( { name , hex } ) => [ name , hex ] ) ,
317332 } ) ;
318333
319334 return (
0 commit comments