customize-controls.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* global twentyTwentyBgColors, twentyTwentyColor, jQuery, wp, _ */
  2. /**
  3. * Customizer enhancements for a better user experience.
  4. *
  5. * Contains extra logic for our Customizer controls & settings.
  6. *
  7. * @since Twenty Twenty 1.0
  8. */
  9. ( function() {
  10. // Wait until the customizer has finished loading.
  11. wp.customize.bind( 'ready', function() {
  12. // Add a listener for accent-color changes.
  13. wp.customize( 'accent_hue', function( value ) {
  14. value.bind( function( to ) {
  15. // Update the value for our accessible colors for all areas.
  16. Object.keys( twentyTwentyBgColors ).forEach( function( context ) {
  17. var backgroundColorValue;
  18. if ( twentyTwentyBgColors[ context ].color ) {
  19. backgroundColorValue = twentyTwentyBgColors[ context ].color;
  20. } else {
  21. backgroundColorValue = wp.customize( twentyTwentyBgColors[ context ].setting ).get();
  22. }
  23. twentyTwentySetAccessibleColorsValue( context, backgroundColorValue, to );
  24. } );
  25. } );
  26. } );
  27. // Add a listener for background-color changes.
  28. Object.keys( twentyTwentyBgColors ).forEach( function( context ) {
  29. wp.customize( twentyTwentyBgColors[ context ].setting, function( value ) {
  30. value.bind( function( to ) {
  31. // Update the value for our accessible colors for this area.
  32. twentyTwentySetAccessibleColorsValue( context, to, wp.customize( 'accent_hue' ).get(), to );
  33. } );
  34. } );
  35. } );
  36. } );
  37. /**
  38. * Updates the value of the "accent_accessible_colors" setting.
  39. *
  40. * @since Twenty Twenty 1.0
  41. *
  42. * @param {string} context The area for which we want to get colors. Can be for example "content", "header" etc.
  43. * @param {string} backgroundColor The background color (HEX value).
  44. * @param {number} accentHue Numeric representation of the selected hue (0 - 359).
  45. *
  46. * @return {void}
  47. */
  48. function twentyTwentySetAccessibleColorsValue( context, backgroundColor, accentHue ) {
  49. var value, colors;
  50. // Get the current value for our accessible colors, and make sure it's an object.
  51. value = wp.customize( 'accent_accessible_colors' ).get();
  52. value = ( _.isObject( value ) && ! _.isArray( value ) ) ? value : {};
  53. // Get accessible colors for the defined background-color and hue.
  54. colors = twentyTwentyColor( backgroundColor, accentHue );
  55. // Sanity check.
  56. if ( colors.getAccentColor() && 'function' === typeof colors.getAccentColor().toCSS ) {
  57. // Update the value for this context.
  58. value[ context ] = {
  59. text: colors.getTextColor(),
  60. accent: colors.getAccentColor().toCSS(),
  61. background: backgroundColor
  62. };
  63. // Get borders color.
  64. value[ context ].borders = colors.bgColorObj
  65. .clone()
  66. .getReadableContrastingColor( colors.bgColorObj, 1.36 )
  67. .toCSS();
  68. // Get secondary color.
  69. value[ context ].secondary = colors.bgColorObj
  70. .clone()
  71. .getReadableContrastingColor( colors.bgColorObj )
  72. .s( colors.bgColorObj.s() / 2 )
  73. .toCSS();
  74. }
  75. // Change the value.
  76. wp.customize( 'accent_accessible_colors' ).set( value );
  77. // Small hack to save the option.
  78. wp.customize( 'accent_accessible_colors' )._dirty = true;
  79. }
  80. }( jQuery ) );