@@ -53,6 +53,10 @@ interface Props {
5353 onValueChange ?: ( value : string , options : object ) => void ;
5454 style ?: StyleProp < ViewStyle > ;
5555 testID ?: string ;
56+ /**
57+ * Give the ColorPalette a background color
58+ */
59+ backgroundColor ?: string ;
5660}
5761export type ColorPaletteProps = Props ;
5862
@@ -81,7 +85,8 @@ class ColorPalette extends PureComponent<Props, State> {
8185 static defaultProps = {
8286 numberOfRows : DEFAULT_NUMBER_OF_ROWS ,
8387 usePagination : true ,
84- loop : true
88+ loop : true ,
89+ backgroundColor : Colors . $backgroundDefault
8590 } ;
8691
8792 constructor ( props : Props ) {
@@ -219,30 +224,31 @@ class ColorPalette extends PureComponent<Props, State> {
219224 return ( margin - 0.001 ) / 2 ;
220225 }
221226
222- scrollToSelected = ( ) => setTimeout ( ( ) => {
223- const { scrollable, currentPage} = this . state ;
224-
225- if ( scrollable && this . selectedColorIndex !== undefined && this . itemsRefs . current ) {
226- // The this.selectedColorIndex layout doesn't update on time
227- // so we use this.selectedColorIndex - 1 and add an offset of 1 Swatch
228- const childRef : any = this . itemsRefs . current [ this . selectedColorIndex - 1 ] ?. current ;
229-
230- if ( childRef ) {
231- const childLayout = childRef . getLayout ( ) ;
232- const leftMargins = this . getHorizontalMargins ( this . selectedColorIndex ) . marginLeft ;
233- const childX = childLayout . x + childLayout . width + SWATCH_MARGIN + leftMargins + SWATCH_SIZE ;
234- if ( childX > this . containerWidth ) {
235- this . scrollBar ?. current ?. scrollTo ( {
236- x : childX + HORIZONTAL_PADDING - this . containerWidth ,
237- y : 0 ,
238- animated : false
239- } ) ;
227+ scrollToSelected = ( ) =>
228+ setTimeout ( ( ) => {
229+ const { scrollable, currentPage} = this . state ;
230+
231+ if ( scrollable && this . selectedColorIndex !== undefined && this . itemsRefs . current ) {
232+ // The this.selectedColorIndex layout doesn't update on time
233+ // so we use this.selectedColorIndex - 1 and add an offset of 1 Swatch
234+ const childRef : any = this . itemsRefs . current [ this . selectedColorIndex - 1 ] ?. current ;
235+
236+ if ( childRef ) {
237+ const childLayout = childRef . getLayout ( ) ;
238+ const leftMargins = this . getHorizontalMargins ( this . selectedColorIndex ) . marginLeft ;
239+ const childX = childLayout . x + childLayout . width + SWATCH_MARGIN + leftMargins + SWATCH_SIZE ;
240+ if ( childX > this . containerWidth ) {
241+ this . scrollBar ?. current ?. scrollTo ( {
242+ x : childX + HORIZONTAL_PADDING - this . containerWidth ,
243+ y : 0 ,
244+ animated : false
245+ } ) ;
246+ }
247+ } else if ( this . usePagination ) {
248+ this . carousel ?. current ?. goToPage ( this . selectedPage || currentPage , false ) ;
240249 }
241- } else if ( this . usePagination ) {
242- this . carousel ?. current ?. goToPage ( this . selectedPage || currentPage , false ) ;
243250 }
244- }
245- } , 100 )
251+ } , 100 ) ;
246252
247253 onContentSizeChange = ( contentWidth : number ) => {
248254 this . setState ( {
@@ -331,32 +337,32 @@ class ColorPalette extends PureComponent<Props, State> {
331337 }
332338
333339 renderScrollableContent ( ) {
334- const { containerStyle, ...others } = this . props ;
340+ const { containerStyle, backgroundColor , ...others } = this . props ;
335341 const { scrollable, contentWidth} = this . state ;
336342
337343 return (
338344 < ScrollBar
339345 ref = { this . scrollBar }
340- style = { [ containerStyle , styles . scrollContainer ] }
346+ style = { [ containerStyle , { backgroundColor } ] }
341347 scrollEnabled = { scrollable }
342348 onContentSizeChange = { this . onContentSizeChange }
343349 height = { SCROLLABLE_HEIGHT }
344350 containerProps = { { width : ! scrollable ? contentWidth : undefined } }
345351 gradientHeight = { SCROLLABLE_HEIGHT - 12 }
346- gradientColor = { Colors . $backgroundDefault }
352+ gradientColor = { backgroundColor }
347353 >
348354 { this . renderPalette ( others , styles . scrollContent , this . colors , 0 ) }
349355 </ ScrollBar >
350356 ) ;
351357 }
352358
353359 renderPaginationContent ( ) {
354- const { containerStyle, loop, ...others } = this . props ;
360+ const { containerStyle, loop, backgroundColor , ...others } = this . props ;
355361 const { currentPage} = this . state ;
356362 const colorGroups = _ . chunk ( this . colors , this . itemsPerPage ) ;
357363
358364 return (
359- < View center style = { [ containerStyle , styles . paginationContainer ] } >
365+ < View center style = { [ containerStyle , styles . paginationContainer , { backgroundColor } ] } >
360366 < Carousel loop = { loop } onChangePage = { this . onChangePage } ref = { this . carousel } >
361367 { _ . map ( colorGroups , ( colorsPerPage , index ) => {
362368 return this . renderPalette ( others , { ...styles . page , width : this . containerWidth } , colorsPerPage , index ) ;
@@ -390,12 +396,8 @@ const styles = StyleSheet.create({
390396 } ,
391397 paginationContainer : {
392398 flex : 1 ,
393- backgroundColor : Colors . $backgroundDefault ,
394399 paddingBottom : VERTICAL_PADDING
395400 } ,
396- scrollContainer : {
397- backgroundColor : Colors . $backgroundDefault
398- } ,
399401 page : {
400402 flexWrap : 'wrap'
401403 } ,
0 commit comments