Nenhuma Descrição

customize-preview.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* global twentytwentyoneGetHexLum, jQuery */
  2. ( function() {
  3. // Add listener for the "background_color" control.
  4. wp.customize( 'background_color', function( value ) {
  5. value.bind( function( to ) {
  6. var lum = twentytwentyoneGetHexLum( to ),
  7. isDark = 127 > lum,
  8. textColor = ! isDark ? 'var(--global--color-dark-gray)' : 'var(--global--color-light-gray)',
  9. tableColor = ! isDark ? 'var(--global--color-light-gray)' : 'var(--global--color-dark-gray)',
  10. stylesheetID = 'twentytwentyone-customizer-inline-styles',
  11. stylesheet,
  12. styles;
  13. // Modify the html & body classes depending on whether this is a dark background or not.
  14. if ( isDark ) {
  15. document.body.classList.add( 'is-dark-theme' );
  16. document.documentElement.classList.add( 'is-dark-theme' );
  17. document.body.classList.remove( 'is-light-theme' );
  18. document.documentElement.classList.remove( 'is-light-theme' );
  19. document.documentElement.classList.remove( 'respect-color-scheme-preference' );
  20. } else {
  21. document.body.classList.remove( 'is-dark-theme' );
  22. document.documentElement.classList.remove( 'is-dark-theme' );
  23. document.body.classList.add( 'is-light-theme' );
  24. document.documentElement.classList.add( 'is-light-theme' );
  25. if ( wp.customize( 'respect_user_color_preference' ).get() ) {
  26. document.documentElement.classList.add( 'respect-color-scheme-preference' );
  27. }
  28. }
  29. // Toggle the white background class.
  30. if ( 225 <= lum ) {
  31. document.body.classList.add( 'has-background-white' );
  32. } else {
  33. document.body.classList.remove( 'has-background-white' );
  34. }
  35. stylesheet = jQuery( '#' + stylesheetID );
  36. styles = '';
  37. // If the stylesheet doesn't exist, create it and append it to <head>.
  38. if ( ! stylesheet.length ) {
  39. jQuery( '#twenty-twenty-one-style-inline-css' ).after( '<style id="' + stylesheetID + '"></style>' );
  40. stylesheet = jQuery( '#' + stylesheetID );
  41. }
  42. // Generate the styles.
  43. styles += '--global--color-primary:' + textColor + ';';
  44. styles += '--global--color-secondary:' + textColor + ';';
  45. styles += '--global--color-background:' + to + ';';
  46. styles += '--button--color-background:' + textColor + ';';
  47. styles += '--button--color-text:' + to + ';';
  48. styles += '--button--color-text-hover:' + textColor + ';';
  49. styles += '--table--stripes-border-color:' + tableColor + ';';
  50. styles += '--table--stripes-background-color:' + tableColor + ';';
  51. // Add the styles.
  52. stylesheet.html( ':root{' + styles + '}' );
  53. } );
  54. } );
  55. }() );