Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T345056: dark mode support #109

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* global wikipediapreview_init_options */
import { getColorScheme } from './link/utils';

wikipediaPreview.init( {
root: document,
/* eslint-disable-next-line camelcase */
/* eslint-disable camelcase */
detectLinks: !! wikipediapreview_init_options.detectLinks,
prefersColorScheme: getColorScheme( !! wikipediapreview_init_options.darkmode ),
/* eslint-enable camelcase */
} );
12 changes: 12 additions & 0 deletions src/link/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ export const isTextNearTheEdge = ( anchor ) => {
( scrollWidth - anchorXPosition.right ) / scrollWidth < 0.2
);
};

export const getColorScheme = ( userSetPostMeta ) => {
// TODO: add extra check for document.body.style background color?
if (
window.matchMedia( '(prefers-color-scheme: dark)' ).matches ||
userSetPostMeta
) {
return 'dark';
}

return 'detect';
};
12 changes: 12 additions & 0 deletions src/postmeta/wp-postmeta-detectlinks.js
medied marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ const WikipediaPreviewPostMetaDetectLinks = ( { postMeta, setPostMeta } ) => {
checked={ postMeta.wikipediapreview_detectlinks }
/>
</PanelRow>
<PanelRow>
<ToggleControl
label={ __(
'Enable dark mode',
'wikipedia-preview'
) }
onChange={ ( value ) =>
setPostMeta( { wikipediapreview_darkmode: value } )
}
checked={ postMeta.wikipediapreview_darkmode }
/>
</PanelRow>
</PluginDocumentSettingPanel>
);
};
Expand Down
15 changes: 15 additions & 0 deletions wikipediapreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function wikipediapreview_enqueue_scripts() {
if ( isset( $post->ID ) ) {
$options = array(
'detectLinks' => get_post_meta( $post->ID, 'wikipediapreview_detectlinks', true ),
'darkmode' => get_post_meta( $post->ID, 'wikipediapreview_darkmode', true ),
);
wp_localize_script( 'wikipedia-preview-init', 'wikipediapreview_init_options', $options );
}
Expand Down Expand Up @@ -141,6 +142,19 @@ function register_detectlinks_postmeta() {
register_post_meta( $all_post_types, $meta_name, $options );
}

function register_darkmode_postmeta() {
$all_post_types = '';
$meta_name = 'wikipediapreview_darkmode';
$options = array(
'show_in_rest' => true,
'auth_callback' => true,
'single' => true,
'type' => 'boolean',
'default' => true, // it could default to false when the gutenburg support is released
);
register_post_meta( $all_post_types, $meta_name, $options );
}

function make_link( $text, $url ) {
return '<a target="_BLANK" href="' . esc_url( $url ) . '">' . $text . '</a>';
}
Expand All @@ -165,6 +179,7 @@ function add_meta_links( $links_array, $plugin_file_name, $plugin_data, $status
add_action( 'enqueue_block_editor_assets', 'wikipediapreview_guten_enqueue' );
add_action( 'init', 'myguten_set_script_translations' );
add_action( 'init', 'register_detectlinks_postmeta' );
add_action( 'init', 'register_darkmode_postmeta' );

require __DIR__ . '/banner.php';
require __DIR__ . '/intro.php';
Loading