wpcom-customize-preview.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * File wpcom-customize-preview.js.
  3. *
  4. * Instantly live-update customizer settings in the preview for improved user experience,
  5. * targeting the updates needed to hide the page title on the homepage on WordPress.com.
  6. *
  7. * This file needs to avoid ESNext syntax to work in older browsers
  8. */
  9. // From https://gist.github.com/xenozauros/f6e185c8de2a04cdfecf
  10. function hexToHSL( hex ) {
  11. var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec( hex );
  12. r = parseInt( result[1], 16 );
  13. g = parseInt( result[2], 16 );
  14. b = parseInt( result[3], 16 );
  15. r /= 255, g /= 255, b /= 255;
  16. var max = Math.max( r, g, b ), min = Math.min( r, g, b );
  17. var h, s, l = ( max + min ) / 2;
  18. if( max == min ){
  19. h = s = 0; // achromatic
  20. } else {
  21. var d = max - min;
  22. s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
  23. switch( max ){
  24. case r: h = ( g - b ) / d + ( g < b ? 6 : 0 ); break;
  25. case g: h = ( b - r ) / d + 2; break;
  26. case b: h = ( r - g ) / d + 4; break;
  27. }
  28. h /= 6;
  29. }
  30. var HSL = new Object();
  31. HSL['h'] = h;
  32. HSL['s'] = s;
  33. HSL['l'] = l;
  34. return HSL;
  35. }
  36. function changeColorLuminescence( hex, amount ) {
  37. var hsl = hexToHSL( hex );
  38. return 'hsl( ' + hsl.h * 360 + ',' + hsl.s * 100 + '%,' + ( hsl.l * 100 + amount ) + '%)';
  39. }
  40. ( function( $ ) {
  41. // Hide Front Page Title
  42. wp.customize( 'hide_front_page_title', function( value ) {
  43. value.bind( function( to ) {
  44. if ( true === to ) {
  45. $( 'body' ).addClass( 'hide-homepage-title' );
  46. } else {
  47. $( 'body' ).removeClass( 'hide-homepage-title' );
  48. }
  49. } );
  50. } );
  51. // Since the plugin handles customizer preview updates via the postMessage transport,
  52. // we need to manually override the "extra CSS" when a user selects a different color palette.
  53. wp.customize( 'colors_manager[colors]', function( value ) {
  54. value.bind( function( to ) {
  55. var background = to.bg;
  56. var foreground = to.txt;
  57. var primary = to.link;
  58. var secondary = to.fg1;
  59. var tertiary = to.fg2;
  60. var foregroundLowContrast = changeColorLuminescence( foreground, 10 );
  61. var foregroundHighContrast = changeColorLuminescence( foreground, -10 );
  62. var primaryHover = changeColorLuminescence( primary, 10 );
  63. var secondaryHover = changeColorLuminescence( secondary, 10 );
  64. const extraCSS = ':root, body {' +
  65. '--global--color-background: ' + background + ';' +
  66. '--global--color-background-high-contrast: ' + background + ';' +
  67. '--global--color-foreground: ' + foreground + ';' +
  68. '--global--color-foreground-low-contrast: ' + foregroundLowContrast + ';' +
  69. '--global--color-foreground-high-contrast: ' + foregroundHighContrast + ';' +
  70. '--global--color-primary: ' + primary + ';' +
  71. '--global--color-primary-hover: ' + primaryHover + ';' +
  72. '--global--color-secondary: ' + secondary + ';' +
  73. '--global--color-secondary-hover: ' + secondaryHover + ';' +
  74. '--global--color-tertiary: ' + tertiary + ';' +
  75. '--global--color-border: ' + tertiary + ';' +
  76. '}';
  77. // Append an extra style element that overrides the previous extra CSS
  78. if ( $('#custom-colors-extra-css').length ) {
  79. $( '#custom-colors-extra-css' ).html( extraCSS );
  80. } else {
  81. $( 'head' ).append( '<style id="custom-colors-extra-css">' + extraCSS + '</style>' );
  82. }
  83. if ( typeof cssVars !== 'undefined' ) {
  84. cssVars( {} );
  85. }
  86. } );
  87. } );
  88. } )( jQuery );