2020-04-01 14:58:50 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useState, useEffect, useCallback } from '@wordpress/element';
|
|
|
|
|
|
|
|
/**
|
2020-04-02 17:04:15 +00:00
|
|
|
* @typedef {import('../stripe-utils/type-defs').StripeElementOptions} StripeElementOptions
|
2020-04-01 14:58:50 +00:00
|
|
|
*/
|
|
|
|
|
2020-04-30 10:18:12 +00:00
|
|
|
/**
|
|
|
|
* Returns the CSS value of the specified property in the document element.
|
|
|
|
*
|
|
|
|
* @param {string} property Name of the property to retrieve the style
|
|
|
|
* value from.
|
|
|
|
* @param {string} defaultValue Fallback value if the value for the property
|
|
|
|
* could not be retrieved.
|
|
|
|
*
|
|
|
|
* @return {string} The style value of that property in the document element.
|
|
|
|
*/
|
|
|
|
const getDocumentStyle = ( property, defaultValue ) => {
|
|
|
|
let documentStyle = {};
|
|
|
|
if (
|
|
|
|
typeof window === 'object' &&
|
|
|
|
typeof window.getComputedStyle === 'function'
|
|
|
|
) {
|
|
|
|
documentStyle = window.getComputedStyle( document.documentElement );
|
|
|
|
}
|
|
|
|
return documentStyle[ property ] || defaultValue;
|
|
|
|
};
|
|
|
|
|
2020-04-01 14:58:50 +00:00
|
|
|
/**
|
|
|
|
* Default options for the stripe elements.
|
|
|
|
*/
|
|
|
|
const elementOptions = {
|
|
|
|
style: {
|
|
|
|
base: {
|
|
|
|
iconColor: '#666EE8',
|
|
|
|
color: '#31325F',
|
2020-04-30 10:18:12 +00:00
|
|
|
fontSize: getDocumentStyle( 'fontSize', '16px' ),
|
|
|
|
lineHeight: 1.375, // With a font-size of 16px, line-height will be 22px.
|
2020-04-01 14:58:50 +00:00
|
|
|
'::placeholder': {
|
|
|
|
color: '#fff',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
classes: {
|
|
|
|
focus: 'focused',
|
|
|
|
empty: 'empty',
|
|
|
|
invalid: 'has-error',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A custom hook handling options implemented on the stripe elements.
|
|
|
|
*
|
|
|
|
* @param {Object} [overloadedOptions] An array of extra options to merge with
|
|
|
|
* the options provided for the element.
|
|
|
|
*
|
|
|
|
* @return {StripeElementOptions} The stripe element options interface
|
|
|
|
*/
|
|
|
|
export const useElementOptions = ( overloadedOptions ) => {
|
|
|
|
const [ isActive, setIsActive ] = useState( false );
|
|
|
|
const [ options, setOptions ] = useState( {
|
|
|
|
...elementOptions,
|
|
|
|
...overloadedOptions,
|
|
|
|
} );
|
|
|
|
const [ error, setError ] = useState( '' );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
const color = isActive ? '#CFD7E0' : '#fff';
|
|
|
|
|
|
|
|
setOptions( ( prevOptions ) => {
|
|
|
|
const showIcon =
|
|
|
|
typeof prevOptions.showIcon !== 'undefined'
|
|
|
|
? { showIcon: isActive }
|
|
|
|
: {};
|
|
|
|
return {
|
|
|
|
...options,
|
|
|
|
style: {
|
|
|
|
...options.style,
|
|
|
|
base: {
|
|
|
|
...options.style.base,
|
|
|
|
'::placeholder': {
|
|
|
|
color,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
...showIcon,
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
}, [ isActive ] );
|
|
|
|
|
|
|
|
const onActive = useCallback(
|
|
|
|
( isEmpty ) => {
|
|
|
|
if ( ! isEmpty ) {
|
|
|
|
setIsActive( true );
|
|
|
|
} else {
|
|
|
|
setIsActive( ( prevActive ) => ! prevActive );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[ setIsActive ]
|
|
|
|
);
|
|
|
|
return { options, onActive, error, setError };
|
|
|
|
};
|