Skip to content

Commit

Permalink
Sending RGBA colors to Stripe Appearance API
Browse files Browse the repository at this point in the history
  • Loading branch information
gpressutto5 committed Sep 17, 2024
1 parent a9955a1 commit 58ffb40
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 49 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-dark-mode-stripe-appearance
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Fixed CC form input fields appearance when using RGBA
5 changes: 2 additions & 3 deletions client/checkout/upe-styles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { upeRestrictedProperties } from './upe-styles';
import {
generateHoverRules,
generateOutlineStyle,
maybeConvertRGBAtoRGB,
dashedToCamelCase,
isColorLight,
getBackgroundColor,
Expand Down Expand Up @@ -380,8 +379,8 @@ export const getFieldStyles = (
for ( let i = 0; i < styles.length; i++ ) {
const camelCase = dashedToCamelCase( styles[ i ] );
if ( validProperties.includes( camelCase ) ) {
filteredStyles[ camelCase ] = maybeConvertRGBAtoRGB(
styles.getPropertyValue( styles[ i ] )
filteredStyles[ camelCase ] = styles.getPropertyValue(
styles[ i ]
);
}
}
Expand Down
26 changes: 0 additions & 26 deletions client/checkout/upe-styles/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,32 +100,6 @@ describe( 'UPE Utilities to generate UPE styles', () => {
} );
} );

test( 'maybeConvertRGBAtoRGB returns valid colors', () => {
const hex = '#ffffff';
const color = 'red';
const rgb = 'rgb(1, 2, 3)';
const rgbNoSpaces = 'rgb(1,2,3)';
const rgba = 'rgba(1, 2, 3, 1)';
const rgbaNoSpaces = 'rgba(1,2,3,1)';
const shadow = 'rgb(1,2,3) 0px 1px 1px 0px';
const shadowTransparent = 'rgba(1,2,3,1) 0px 1px 1px 0px';
const pixel = '0px';

expect( upeUtils.maybeConvertRGBAtoRGB( hex ) ).toEqual( hex );
expect( upeUtils.maybeConvertRGBAtoRGB( color ) ).toEqual( color );
expect( upeUtils.maybeConvertRGBAtoRGB( rgb ) ).toEqual( rgb );
expect( upeUtils.maybeConvertRGBAtoRGB( rgbNoSpaces ) ).toEqual(
rgbNoSpaces
);
expect( upeUtils.maybeConvertRGBAtoRGB( rgba ) ).toEqual( rgb );
expect( upeUtils.maybeConvertRGBAtoRGB( rgbaNoSpaces ) ).toEqual( rgb );
expect( upeUtils.maybeConvertRGBAtoRGB( shadow ) ).toEqual( shadow );
expect( upeUtils.maybeConvertRGBAtoRGB( shadowTransparent ) ).toEqual(
shadowTransparent
);
expect( upeUtils.maybeConvertRGBAtoRGB( pixel ) ).toEqual( pixel );
} );

test( 'isColorLight returns valid brightness values', () => {
const white = '#ffffff';
const black = '#000000';
Expand Down
20 changes: 0 additions & 20 deletions client/checkout/upe-styles/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,6 @@ export const dashedToCamelCase = ( string ) => {
} );
};

/**
* Converts rgba to rgb format, since Stripe Appearances API does not accept rgba format for background.
*
* @param {string} color CSS color value.
* @return {string} Accepted CSS color value.
*/
export const maybeConvertRGBAtoRGB = ( color ) => {
const colorParts = color.match(
/^rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(0?(\.\d+)?|1?(\.0+)?)\s*\)$/
);
if ( colorParts ) {
const alpha = colorParts[ 4 ] || 1;
const newColorParts = colorParts.slice( 1, 4 ).map( ( part ) => {
return Math.round( part * alpha + 255 * ( 1 - alpha ) );
} );
color = `rgb(${ newColorParts.join( ', ' ) })`;
}
return color;
};

/**
* Searches through array of CSS selectors and returns first visible background color.
*
Expand Down

0 comments on commit 58ffb40

Please sign in to comment.