Skip to content

Commit 232f6f0

Browse files
authored
Site screenshot: Add responsive image support to the screenshot block (#175)
* Site grid: Update breakpoint for 3-column display Fixes #171 * Site screenshot: Add size as a separate parameter from type * Images: Add extra sizes for desktop screenshot * Site screenshot: Add responsive image support to the screenshot block Fixes #156 * Use a "location" attribute to pass better information to block * Set the `location` attribute when displaying screenshots * Add a readme to the screenshot block
1 parent 5366829 commit 232f6f0

File tree

11 files changed

+104
-12
lines changed

11 files changed

+104
-12
lines changed

source/wp-content/themes/wporg-showcase-2022/functions.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ function setup_theme() {
6262
add_image_size( 'screenshot-desktop', 2044, 1150, array( 'center', 'top' ) );
6363
add_image_size( 'screenshot-mobile', 750, 1334, array( 'center', 'top' ) );
6464

65+
// More desktop sizes to support responsive images in the grid.
66+
// In production these will be generated using Photon CDN, but we can set
67+
// them up as real sizes for local support too.
68+
// Images in the grid are display from 340-740px wide. This set of sizes
69+
// should cover 1x and 2x resolution.
70+
$desktop_ratio = 1150 / 2044;
71+
add_image_size( 'screenshot-desktop-1400', 1400, 1400 * $desktop_ratio, array( 'center', 'top' ) );
72+
add_image_size( 'screenshot-desktop-1100', 1100, 1100 * $desktop_ratio, array( 'center', 'top' ) );
73+
add_image_size( 'screenshot-desktop-800', 800, 800 * $desktop_ratio, array( 'center', 'top' ) );
74+
add_image_size( 'screenshot-desktop-500', 500, 500 * $desktop_ratio, array( 'center', 'top' ) );
75+
6576
// Add tonesque support so that Jetpack loads the class.
6677
add_theme_support( 'tonesque' );
6778

@@ -470,7 +481,7 @@ function jetpack_related_posts_display( $markup, $post_id, $related_posts, $bloc
470481
<!-- wp:query {"queryId":2,"query":{"perPage":3,"include":<?php echo esc_attr( wp_json_encode( $ids ) ); ?>,"inherit":false},"align":"wide","layout":{"type":"constrained","wideSize":"1760px"}} -->
471482
<div class="wp-block-query alignwide">
472483
<!-- wp:post-template {"align":"wide","style":{"spacing":{"blockGap":"var:preset|spacing|40"}},"layout":{"type":"grid","columnCount":3},"className":"wporg-related-posts"} -->
473-
<!-- wp:wporg/site-screenshot {"isLink":true,"lazyLoad":true} /-->
484+
<!-- wp:wporg/site-screenshot {"isLink":true,"lazyLoad":true,"location":"row"} /-->
474485

475486
<!-- wp:group {"style":{"spacing":{"margin":{"top":"var:preset|spacing|10"}}},"layout":{"type":"flex","flexWrap":"nowrap","justifyContent":"space-between"}} -->
476487
<div class="wp-block-group" style="margin-top:var(--wp--preset--spacing--10)">

source/wp-content/themes/wporg-showcase-2022/patterns/_site-grid-featured.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<!-- /wp:heading -->
4040

4141
<!-- wp:post-template {"align":"wide","style":{"spacing":{"blockGap":"var:preset|spacing|40"}},"layout":{"type":"grid","columnCount":3}} -->
42-
<!-- wp:wporg/site-screenshot {"isLink":true,"lazyLoad":true} /-->
42+
<!-- wp:wporg/site-screenshot {"isLink":true,"lazyLoad":true,"location":"grid"} /-->
4343

4444
<!-- wp:group {"style":{"spacing":{"margin":{"top":"var:preset|spacing|10"}}},"layout":{"type":"flex","flexWrap":"nowrap","justifyContent":"space-between"}} -->
4545
<div class="wp-block-group" style="margin-top:var(--wp--preset--spacing--10)">

source/wp-content/themes/wporg-showcase-2022/patterns/_site-grid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<!-- wp:query-title {"type":"archive","className":"screen-reader-text"} /-->
3939

4040
<!-- wp:post-template {"align":"wide","style":{"spacing":{"blockGap":"var:preset|spacing|40"}},"layout":{"type":"grid","columnCount":3}} -->
41-
<!-- wp:wporg/site-screenshot {"isLink":true,"lazyLoad":true} /-->
41+
<!-- wp:wporg/site-screenshot {"isLink":true,"lazyLoad":true,"location":"grid"} /-->
4242

4343
<!-- wp:group {"style":{"spacing":{"margin":{"top":"var:preset|spacing|10"}}},"layout":{"type":"flex","flexWrap":"nowrap","justifyContent":"space-between"}} -->
4444
<div class="wp-block-group" style="margin-top:var(--wp--preset--spacing--10)">

source/wp-content/themes/wporg-showcase-2022/patterns/_site-hero.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<span aria-hidden="true" class="wp-block-cover__background has-background-dim-0 has-background-dim"></span>
4343
<img class="wp-block-cover__image-background" alt="" src="<?php echo esc_url( get_theme_file_uri( 'images/dots-hero.svg' ) . '?v=' . time() ); ?>" style="object-position:0% 0%" data-object-fit="contain" data-object-position="0% 0%"/>
4444
<div class="wp-block-cover__inner-container">
45-
<!-- wp:wporg/site-screenshot {"isLink":true} /-->
45+
<!-- wp:wporg/site-screenshot {"isLink":true,"location":"hero"} /-->
4646
</div>
4747
</div>
4848
<!-- /wp:cover -->

source/wp-content/themes/wporg-showcase-2022/patterns/screenshot-auditor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@
4141
<h3 class="has-small-font-size"><?php esc_attr_e( 'Desktop', 'wporg' ); ?></h3>
4242
<!-- /wp:heading -->
4343

44-
<!-- wp:wporg/site-screenshot {"isLink":true,"type":"desktop"} /--></div>
44+
<!-- wp:wporg/site-screenshot {"isLink":true,"type":"desktop","location":"header"} /--></div>
4545
<!-- /wp:column -->
4646

4747
<!-- wp:column {"width":"25%"} -->
4848
<div class="wp-block-column" style="flex-basis:25%"><!-- wp:heading {"level":3,"fontSize":"small"} -->
4949
<h3 class="has-small-font-size"><?php esc_attr_e( 'Mobile', 'wporg' ); ?></h3>
5050
<!-- /wp:heading -->
5151

52-
<!-- wp:wporg/site-screenshot {"isLink":true,"type":"mobile"} /--></div>
52+
<!-- wp:wporg/site-screenshot {"isLink":true,"type":"mobile","location":"header"} /--></div>
5353
<!-- /wp:column --></div>
5454
<!-- /wp:columns --></div>
5555
<!-- /wp:group -->

source/wp-content/themes/wporg-showcase-2022/src/site-screenshot/block.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
"type": "string",
2222
"default": "desktop",
2323
"enum": [ "desktop", "mobile" ]
24+
},
25+
"location": {
26+
"type": "string",
27+
"default": "grid",
28+
"enum": [ "grid", "hero", "header", "row" ]
2429
}
2530
},
2631
"supports": {

source/wp-content/themes/wporg-showcase-2022/src/site-screenshot/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ function init() {
3030
*
3131
* @param WP_Post $post
3232
* @param string $type
33+
* @param string $size
3334
* @return string
3435
*/
35-
function get_site_screenshot_src( $post, $type = 'desktop' ) {
36+
function get_site_screenshot_src( $post, $type = 'desktop', $size = 'screenshot-desktop' ) {
3637
$screenshot_url = false;
3738
$media_id = get_post_meta( $post->ID, 'screenshot-' . $type, true );
3839
$cache_key = '20230913'; // To break out of cached image.
3940

40-
$size = 'screenshot-' . $type;
4141
$all_sizes = wp_get_registered_image_subsizes();
4242
if ( ! isset( $all_sizes[ $size ] ) ) {
4343
return null;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Site Screenshots
2+
3+
Display the screenshot for the current post (site). This uses the uploaded image if available, otherwise falls back to using the `domain` to take and load a screenshot from mshots.
4+
5+
The mshots behavior uses the [Interactivity API](https://github.com/WordPress/gutenberg/blob/trunk/packages/interactivity/docs/1-getting-started.md) to provide a CSS-controlled loading state.
6+
7+
When a local image is used, the block may use responsive images, depending on the location.
8+
9+
This block also controls loading the panel into the Document Settings sidebar for uploading the desktop & mobile images, and setting the custom background color.
10+
11+
## Attributes
12+
13+
### `isLink`: boolean
14+
15+
- Default: false
16+
17+
If true, will wrap the screenshot in a link to the current post.
18+
19+
### `lazyLoad`: boolean
20+
21+
- Default: false
22+
23+
Controls the `loading` attribute on the `img` tag. This is currently not used for mshots images.
24+
25+
### `type`: "desktop" | "mobile"
26+
27+
- Default: "desktop"
28+
29+
Controls which image to show/capture. When `desktop`, it uses the image saved as `screenshot-desktop` or captures from mshots at 1920x1080. When `mobile`, it uses the image saves as `screenshot-mobile` or captures from mshots at 375x667.
30+
31+
### `location`: "grid" | "hero" | "header" | "row"
32+
33+
- Default: "grid"
34+
35+
This indicates to the block where in the layout this will be rendered. This is used to control the responsive image hints.
36+
37+
- grid: the main site grid, on homepage or archives, which displays at 3, 2, or 1 column depending on screen size.
38+
- hero: the homepage header, does not use responsive images.
39+
- header: single site page header, does not use responsive images.
40+
- row: the "related sites" section, which displays at 3 or 1 column depending on screen size.
41+

source/wp-content/themes/wporg-showcase-2022/src/site-screenshot/render.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,45 @@
1313
$has_link = isset( $attributes['isLink'] ) && true == $attributes['isLink'];
1414
$is_lazyload = isset( $attributes['lazyLoad'] ) && true === $attributes['lazyLoad'];
1515

16-
$screenshot = get_site_screenshot_src( $current_post, $attributes['type'] );
16+
$img_size = ( 'desktop' === $attributes['type'] ) ? 'screenshot-desktop' : 'screenshot-mobile';
17+
$screenshot = get_site_screenshot_src( $current_post, $attributes['type'], $img_size );
1718
$is_mshots = str_contains( $screenshot, 'mshots' );
1819

1920
$classname = 'is-size-' . esc_attr( $attributes['type'] );
2021
if ( $has_link ) {
2122
$classname .= ' is-linked-image';
2223
}
2324

25+
$has_responsive_images = ! $is_mshots && 'desktop' === $attributes['type'] && ( 'grid' === $attributes['location'] || 'row' === $attributes['location'] );
26+
27+
// If the block needs responsive images, set up more image URLs & sizes attribute.
28+
if ( $has_responsive_images ) {
29+
$screenshot = get_site_screenshot_src( $current_post, $attributes['type'], 'screenshot-desktop-1100' );
30+
$screenshot_srcset = get_site_screenshot_src( $current_post, $attributes['type'], 'screenshot-desktop-500' ) . ' 500w, ';
31+
$screenshot_srcset .= get_site_screenshot_src( $current_post, $attributes['type'], 'screenshot-desktop-800' ) . ' 800w, ';
32+
$screenshot_srcset .= get_site_screenshot_src( $current_post, $attributes['type'], 'screenshot-desktop-1100' ) . ' 1100w, ';
33+
$screenshot_srcset .= get_site_screenshot_src( $current_post, $attributes['type'], 'screenshot-desktop-1400' ) . ' 1400w';
34+
35+
// Set up the sizes attribute. The value here should reflect the width of
36+
// the column, i.e., the displayed with of the image.
37+
// On very large screens, columns are a fixed size.
38+
$sizes = '(min-width: 1920px) 533px,';
39+
// Handle dynamic column sizes. This math is not really reflective of the
40+
// layout, but it works well. Real math would be tricky due to the scaling
41+
// column gap value (--wp--preset--spacing--40).
42+
// See https://css-tricks.com/a-guide-to-the-responsive-images-syntax-in-html/#aa-being-more-chill-about-sizes
43+
if ( 'grid' === $attributes['location'] ) {
44+
$sizes .= '(min-width: 1601px) calc(25vw + 30px),';
45+
$sizes .= '(min-width: 801px) calc(50vw - 125px),';
46+
} else {
47+
// In "row", this stays 3-column until flipping to one-column.
48+
$sizes .= '(min-width: 801px) calc(25vw + 30px),';
49+
}
50+
// One column—this one's actually accurate! At one column, we only need to
51+
// account for site padding & border width.
52+
$sizes .= 'calc(100vw - 60px)';
53+
}
54+
2455
// Initial state to pass to Interactivity API.
2556
// This handles the image data (used to load image from mshots) and current
2657
// state information (like errors).
@@ -56,6 +87,10 @@ class="wporg-site-screenshot__mshot-container"
5687
<?php else : ?>
5788
<img
5889
src="<?php echo esc_url( $screenshot ); ?>"
90+
<?php if ( $has_responsive_images ) : ?>
91+
srcset="<?php echo esc_attr( $screenshot_srcset ); ?>"
92+
sizes="<?php echo esc_attr( $sizes ); ?>"
93+
<?php endif; ?>
5994
alt="<?php echo the_title_attribute( array( 'echo' => false ) ); ?>"
6095
loading="<?php echo $is_lazyload ? 'lazy' : 'eager'; ?>"
6196
/>

source/wp-content/themes/wporg-showcase-2022/src/style/style.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ a:where(:not(.wp-element-button)):focus-visible {
152152
}
153153
}
154154

155-
@media (max-width: 1440px) {
155+
@media (max-width: 1600px) {
156156
.wp-block-post-template.is-layout-grid.columns-3:not(.wporg-related-posts) {
157157
grid-template-columns: repeat(2, minmax(0, 1fr));
158158
}

source/wp-content/themes/wporg-showcase-2022/templates/single.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
<div class="wp-block-group alignfull has-feature-color-background has-background" style="border-bottom-color:var(--wp--preset--color--black-opacity-15);border-bottom-style:solid;border-bottom-width:1px;background-color:#f6f6f6;margin-bottom:var(--wp--preset--spacing--50);padding-top:var(--wp--preset--spacing--60);padding-right:var(--wp--preset--spacing--edge-space);padding-left:var(--wp--preset--spacing--edge-space)">
77
<!-- wp:group {"style":{"spacing":{"blockGap":"var:preset|spacing|60"}},"layout":{"type":"flex","flexWrap":"nowrap","justifyContent":"center","verticalAlignment":"bottom"}} -->
88
<div class="wp-block-group">
9-
<!-- wp:wporg/site-screenshot /-->
9+
<!-- wp:wporg/site-screenshot {"location":"header"} /-->
1010

11-
<!-- wp:wporg/site-screenshot {"type":"mobile"} /-->
11+
<!-- wp:wporg/site-screenshot {"type":"mobile","location":"header"} /-->
1212
</div>
1313
<!-- /wp:group -->
1414
</div>

0 commit comments

Comments
 (0)