Twenty_Twenty_One_Dark_Mode
- Source
wp-content/themes/twentytwentyone/classes/class-twenty-twenty-one-dark-mode.php:13
This class is in charge of Dark Mode.
Compatibility
- WordPress
- core
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0).
Methods · 12
- __construct()Instantiates the object.
- editor_custom_color_variables()Enqueues editor custom color variables & scripts.
- enqueue_scripts()Enqueues scripts and styles.
- customize_controls_enqueue_scripts()Enqueues scripts for the customizer.
- customizer_controls()Registers customizer options.
- html_classes()Calculates classes for the main element.
- admin_body_classes()Adds a class to the element in the editor to accommodate dark-mode.
- switch_should_render()Determines if we want to print the dark-mode switch or not.
- the_switch()Adds night/day switch.
- the_html()Prints the dark-mode switch HTML.
- the_script()Prints the dark-mode switch script.
- add_privacy_policy_content()Adds information to the privacy policy.
Source code
class Twenty_Twenty_One_Dark_Mode { /** * Instantiates the object. * * @since Twenty Twenty-One 1.0 */ public function __construct() { // Enqueue assets for the block-editor. if ( is_admin() ) { add_action( 'enqueue_block_assets', array( $this, 'editor_custom_color_variables' ) ); } // Add styles for dark-mode. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); // Add scripts for customizer controls. add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_scripts' ) ); // Add customizer controls. add_action( 'customize_register', array( $this, 'customizer_controls' ) ); // Add HTML classes. add_filter( 'twentytwentyone_html_classes', array( $this, 'html_classes' ) ); // Add classes to <body> in the dashboard. add_filter( 'admin_body_class', array( $this, 'admin_body_classes' ) ); // Add the switch on the frontend & customizer. add_action( 'wp_footer', array( $this, 'the_switch' ) ); // Add the privacy policy content. add_action( 'admin_init', array( $this, 'add_privacy_policy_content' ) ); } /** * Enqueues editor custom color variables & scripts. * * @since Twenty Twenty-One 1.0 * * @return void */ public function editor_custom_color_variables() { if ( ! $this->switch_should_render() ) { return; } $background_color = get_theme_mod( 'background_color', 'D1E4DD' ); $should_respect_color_scheme = get_theme_mod( 'respect_user_color_preference', false ); if ( $should_respect_color_scheme && Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( $background_color ) > 127 ) { // Add Dark Mode variable overrides. wp_add_inline_style( 'twenty-twenty-one-custom-color-overrides', '.is-dark-theme.is-dark-theme .editor-styles-wrapper { --global--color-background: var(--global--color-dark-gray); --global--color-primary: var(--global--color-light-gray); --global--color-secondary: var(--global--color-light-gray); --button--color-text: var(--global--color-background); --button--color-text-hover: var(--global--color-secondary); --button--color-text-active: var(--global--color-secondary); --button--color-background: var(--global--color-secondary); --button--color-background-active: var(--global--color-background); --global--color-border: #9ea1a7; --table--stripes-border-color: rgba(240, 240, 240, 0.15); --table--stripes-background-color: rgba(240, 240, 240, 0.15); }' ); } wp_enqueue_script( 'twentytwentyone-dark-mode-support-toggle', get_template_directory_uri() . '/assets/js/dark-mode-toggler.js', array(), wp_get_theme()->get( 'Version' ), array( 'in_footer' => true ) ); wp_enqueue_script( 'twentytwentyone-editor-dark-mode-support', get_template_directory_uri() . '/assets/js/editor-dark-mode-support.js', array( 'twentytwentyone-dark-mode-support-toggle' ), wp_get_theme()->get( 'Version' ), array( 'in_footer' => true ) ); } /** * Enqueues scripts and styles. * * @since Twenty Twenty-One 1.0 * * @return void */Changelog
Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-content/themes/twentytwentyone/classes/class-twenty-twenty-one-dark-mode.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.