Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
508c70e
add lightbox support to gallery block
madhusudhand Jul 15, 2024
98bba76
disable and hide next/prev icons based on device
madhusudhand Jul 25, 2024
cc48519
remove view.js from gallery
madhusudhand Jul 25, 2024
c2611d0
fix a lint issue
madhusudhand Jul 25, 2024
e09b8ee
rename isGallery to hasNavigation and imageInit method
madhusudhand Jul 26, 2024
4d4a509
adjust button opacity when disabled
madhusudhand Jul 26, 2024
5dbfaed
address accessiability of lightbox navigation
madhusudhand Jul 26, 2024
ccbe9dc
fix accessibility: dynamic aria-label based on current image
madhusudhand Aug 13, 2024
5e0df68
Try to fix a11y issues
t-hamano Aug 16, 2024
0579c8e
Improve screen reader reading
t-hamano Aug 22, 2024
dfcca2f
move translations to server
madhusudhand Aug 29, 2024
408cf80
reset gallery_id after each gallery processing
madhusudhand Aug 30, 2024
3716092
fix php lint errors
madhusudhand Aug 30, 2024
f14acc0
Merge remote-tracking branch 'origin/trunk' into gallery/lightbox
luisherranz Dec 6, 2024
1077e73
Use block context to pass down a gallery ID
luisherranz Dec 6, 2024
3387ac0
Merge branch 'trunk' into gallery/lightbox
luisherranz Feb 19, 2025
fd51250
Fix frontend implementation
luisherranz Feb 19, 2025
016ae8d
Merge branch 'trunk' into gallery/lightbox
t-hamano Feb 21, 2025
d229233
Fix Screen reader double reading
t-hamano Feb 21, 2025
e68f3a2
Don't hide lightbox when hitting Enter on prev/next buttons
t-hamano Feb 21, 2025
4e0b31a
Match visual order with DOM order
t-hamano Feb 21, 2025
24524c6
Do not apply opacity to focus styles on disabled prev/next buttons
t-hamano Feb 21, 2025
c7608c3
Add a tooltip to navigation button
t-hamano Feb 21, 2025
230ab34
Add `cursor:pointer` to navigation buttons
t-hamano Feb 23, 2025
a9aa62b
Add focus trap
t-hamano Feb 23, 2025
7ede46a
Add "Lightbox navigation" option
t-hamano Feb 23, 2025
deeaf40
Restore aria-live/aria-atomic
t-hamano Feb 23, 2025
4f1cde2
Ajust z-index values
t-hamano Feb 23, 2025
1da0337
Regenerate fixtures
t-hamano Feb 23, 2025
0ed7d4c
Fix PHP lint error
t-hamano Feb 23, 2025
424de4b
Merge branch 'trunk' into gallery/lightbox
t-hamano Aug 25, 2025
206bae2
Show toggle only when lightbox.allowEditing is enabled
t-hamano Aug 25, 2025
c005efb
Fix: add data-wp-watch--inert
t-hamano Aug 25, 2025
1a6f7de
Remove tooltip from previous and next buttons
t-hamano Aug 25, 2025
fa14a03
Enable Lightbox by default
t-hamano Aug 25, 2025
1997e51
Add some reset CSS to the navigation buttons so they aren't affected …
t-hamano Aug 26, 2025
dac54a3
Fix since tag
t-hamano Aug 26, 2025
f849597
Add control to show visible text fo three buttons
t-hamano Sep 4, 2025
a082fbf
Enhance text of Enlarge button
t-hamano Sep 4, 2025
f96f5ea
Regenerate fixtures
t-hamano Sep 4, 2025
38b5ab6
Make navigation button appearance more configurable
t-hamano Sep 5, 2025
3d29bb4
Update fixtures
t-hamano Sep 5, 2025
0b16ca9
Remove aria-label attirbute if value is empty
t-hamano Sep 5, 2025
fd265f0
Update help text
t-hamano Sep 5, 2025
3fadb5c
Simplify logic
t-hamano Sep 5, 2025
8c0beb2
Merge branch 'trunk' into gallery/lightbox
t-hamano Sep 18, 2025
a738ef0
Merge branch 'trunk' into gallery/lightbox
t-hamano Sep 24, 2025
7b4faa9
Fix: navigationButtonType doesn't work properly when there are multip…
t-hamano Sep 24, 2025
a994601
Output aria-label only when there is no visible text
t-hamano Sep 24, 2025
f774cdd
Reduce icon size when button has text
t-hamano Sep 24, 2025
e3e40ca
Merge branch 'trunk' into gallery/lightbox
t-hamano Oct 1, 2025
5ba5421
Loop through the gallery
t-hamano Oct 1, 2025
b868202
Disable button type selection UI when there is no lightbox image
t-hamano Oct 1, 2025
9589dd6
Add mobile swipe action
t-hamano Oct 1, 2025
a0a4c8d
Merge branch 'trunk' into gallery/lightbox
t-hamano Oct 5, 2025
52f9d9d
Tweak lightbox button style
t-hamano Oct 5, 2025
5872e7d
Apply randomize order setting to lightbox images
t-hamano Oct 5, 2025
c321439
Merge branch 'trunk' into gallery/lightbox
t-hamano Oct 7, 2025
2705bb8
Merge branch 'trunk' into gallery/lightbox
t-hamano Oct 17, 2025
189a2cc
Merge branch 'trunk' into gallery/lightbox
t-hamano Nov 15, 2025
7c4aff6
Fix unused state
t-hamano Nov 15, 2025
80ef11e
Merge branch 'trunk' into gallery/lightbox
t-hamano Nov 28, 2025
843cdd5
Update @since docblock to 7.0.0
t-hamano Nov 28, 2025
40a7328
Use sentence case in label
t-hamano Nov 28, 2025
3fe7d2d
Adjust size of close and navigation buttons
t-hamano Nov 28, 2025
97b18a4
Merge branch 'trunk' into gallery/lightbox
t-hamano Dec 23, 2025
f5635d6
Merge branch 'trunk' into gallery/lightbox
t-hamano Jan 7, 2026
5d43756
Remove navigation button wrapper div
t-hamano Jan 7, 2026
2e2f025
Merge branch 'trunk' into gallery/lightbox
t-hamano Feb 2, 2026
369a31c
Make navigation button responsive
t-hamano Feb 2, 2026
3080967
Aligning icons and text
t-hamano Feb 2, 2026
b9df731
Change navigation button position
t-hamano Feb 2, 2026
b7343c8
Use ToggleGroupControl for navigation button type control
t-hamano Feb 3, 2026
c1fe18a
Merge branch 'trunk' into gallery/lightbox
t-hamano Feb 3, 2026
711cf86
Merge branch 'trunk' into gallery/lightbox
t-hamano Feb 5, 2026
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ Display multiple images in a rich gallery. ([Source](https://github.com/WordPres
- **Category:** media
- **Allowed Blocks:** core/image
- **Supports:** align, anchor, color (background, gradients, ~~text~~), interactivity (clientNavigation), layout (default, ~~allowEditing~~, ~~allowInheriting~~, ~~allowSwitching~~), spacing (blockGap, margin, padding), units (em, px, rem, vh, vw), ~~html~~
- **Attributes:** allowResize, aspectRatio, caption, columns, fixedHeight, ids, imageCrop, images, linkTarget, linkTo, randomOrder, shortCodeTransforms, sizeSlug
- **Attributes:** allowResize, aspectRatio, caption, columns, fixedHeight, ids, imageCrop, images, linkTarget, linkTo, navigationButtonType, randomOrder, shortCodeTransforms, sizeSlug

## Group

Expand Down
9 changes: 8 additions & 1 deletion packages/block-library/src/gallery/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"name": "core/gallery",
"title": "Gallery",
"category": "media",
"usesContext": [ "galleryId" ],
"allowedBlocks": [ "core/image" ],
"description": "Display multiple images in a rich gallery.",
"keywords": [ "images", "photos" ],
Expand Down Expand Up @@ -60,6 +61,11 @@
},
"default": []
},
"navigationButtonType": {
"type": "string",
"default": "icon",
"enum": [ "icon", "text", "both" ]
},
"shortCodeTransforms": {
"type": "array",
"items": {
Expand Down Expand Up @@ -112,7 +118,8 @@
"providesContext": {
"allowResize": "allowResize",
"imageCrop": "imageCrop",
"fixedHeight": "fixedHeight"
"fixedHeight": "fixedHeight",
"navigationButtonType": "navigationButtonType"
},
"supports": {
"anchor": true,
Expand Down
65 changes: 65 additions & 0 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
MenuItem,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
ToolbarDropdownMenu,
PanelBody,
} from '@wordpress/components';
Expand Down Expand Up @@ -94,6 +96,20 @@ const LINK_OPTIONS = [
noticeText: __( 'None' ),
},
];
const NAVIGATION_BUTTON_TYPE_OPTIONS = [
{
label: __( 'Icon' ),
value: 'icon',
},
{
label: __( 'Text' ),
value: 'text',
},
{
label: __( 'Both' ),
value: 'both',
},
];
const ALLOWED_MEDIA_TYPES = [ 'image' ];

const PLACEHOLDER_TEXT = Platform.isNative
Expand Down Expand Up @@ -134,6 +150,7 @@ export default function GalleryEdit( props ) {
: LINK_OPTIONS;

const {
navigationButtonType,
columns,
imageCrop,
randomOrder,
Expand Down Expand Up @@ -205,6 +222,16 @@ export default function GalleryEdit( props ) {

const newImages = useGetNewImages( images, imageData );

// Check if there is at least one image with lightbox enabled
const hasLightboxImages = lightboxSetting?.enabled
? images.filter(
( image ) =>
image.attributes?.lightbox?.enabled === undefined ||
image.attributes?.lightbox?.enabled === true
).length > 0
: images.filter( ( image ) => image.attributes.lightbox?.enabled )
.length > 0;

const themeOptions = themeRatios?.map( ( { name, ratio } ) => ( {
label: name,
value: ratio,
Expand Down Expand Up @@ -644,6 +671,7 @@ export default function GalleryEdit( props ) {
label={ __( 'Settings' ) }
resetAll={ () => {
setAttributes( {
navigationButtonType: 'icon',
columns: undefined,
imageCrop: true,
randomOrder: false,
Expand Down Expand Up @@ -779,6 +807,43 @@ export default function GalleryEdit( props ) {
/>
</ToolsPanelItem>
) }
<ToolsPanelItem
label={ __( 'Navigation button type' ) }
isShownByDefault
hasValue={ () => navigationButtonType !== 'icon' }
onDeselect={ () =>
setAttributes( {
navigationButtonType: 'icon',
} )
}
>
{ hasLightboxImages && (
<ToggleGroupControl
label={ __( 'Navigation button type' ) }
value={ navigationButtonType }
onChange={ ( value ) =>
setAttributes( {
navigationButtonType: value,
} )
}
isBlock
__next40pxDefaultSize
help={ __(
'Adjust the appearance of buttons in the lightbox.'
) }
>
{ NAVIGATION_BUTTON_TYPE_OPTIONS.map(
( option ) => (
<ToggleGroupControlOption
key={ option.value }
value={ option.value }
label={ option.label }
/>
)
) }
</ToggleGroupControl>
) }
</ToolsPanelItem>
</ToolsPanel>
) }
{ Platform.isNative && (
Expand Down
97 changes: 89 additions & 8 deletions packages/block-library/src/gallery/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,35 @@ function block_core_gallery_data_id_backcompatibility( $parsed_block ) {

add_filter( 'render_block_data', 'block_core_gallery_data_id_backcompatibility' );

/**
* Adds a unique ID to the gallery block context.
*
* @since 7.0.0
*
* @param array $context Default context.
* @param array $parsed_block Block being rendered, filtered by render_block_data.
* @return array Filtered context.
*/
function block_core_gallery_render_context( $context, $parsed_block ) {
if ( 'core/gallery' === $parsed_block['blockName'] ) {
$context['galleryId'] = uniqid();
}
return $context;
}

add_filter( 'render_block_context', 'block_core_gallery_render_context', 10, 2 );

/**
* Renders the `core/gallery` block on the server.
*
* @since 6.0.0
*
* @param array $attributes Attributes of the block being rendered.
* @param string $content Content of the block being rendered.
* @param string $content Content of the block being rendered.
* @param array $block The block instance being rendered.
* @return string The content of the block being rendered.
*/
function block_core_gallery_render( $attributes, $content ) {
function block_core_gallery_render( $attributes, $content, $block ) {
// Adds a style tag for the --wp--style--unstable-gallery-gap var.
// The Gallery block needs to recalculate Image block width based on
// the current gap setting in order to maintain the number of flex columns
Expand Down Expand Up @@ -116,11 +135,63 @@ function block_core_gallery_render( $attributes, $content ) {

wp_style_engine_get_stylesheet_from_css_rules(
$gallery_styles,
array(
'context' => 'block-supports',
array( 'context' => 'block-supports' )
);

// Gets all image IDs from the state that match this gallery's ID.
$state = wp_interactivity_state( 'core/image' );
$gallery_id = $block->context['galleryId'] ?? null;
$image_ids = array();
if ( isset( $gallery_id ) && isset( $state['metadata'] ) ) {
foreach ( $state['metadata'] as $image_id => $metadata ) {
if ( isset( $metadata['galleryId'] ) && $metadata['galleryId'] === $gallery_id ) {
$image_ids[] = $image_id;
}
}
}

// Randomize image IDs when `randomOrder` setting is enabled
if ( ! empty( $attributes['randomOrder'] ) && ! empty( $image_ids ) ) {
shuffle( $image_ids );
}

$processed_content->set_attribute( 'data-wp-interactive', 'core/gallery' );
$processed_content->set_attribute(
'data-wp-context',
wp_json_encode(
array( 'galleryId' => $gallery_id ),
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
)
);

// Populates the aria label for each image in the gallery.
if ( ! empty( $image_ids ) ) {
if ( 1 <= count( $image_ids ) ) {
for ( $i = 0; $i < count( $image_ids ); $i++ ) {
$image_id = $image_ids[ $i ];
$alt = $state['metadata'][ $image_id ]['alt'];
wp_interactivity_state(
'core/image',
array(
'metadata' => array(
$image_id => array(
'customAriaLabel' => empty( $alt )
/* translators: %1$s: current image index, %2$s: total number of images */
? sprintf( __( 'Enlarged image %1$s of %2$s' ), $i + 1, count( $image_ids ) )
/* translators: %1$s: current image index, %2$s: total number of images, %3$s: Image alt text */
: sprintf( __( 'Enlarged image %1$s of %2$s: %3$s' ), $i + 1, count( $image_ids ), $alt ),
/* translators: %1$s: current image index, %2$s: total number of images */
'triggerButtonAriaLabel' => sprintf( __( 'Enlarge %1$s of %2$s' ), $i + 1, count( $image_ids ) ),
// This metadata is used to store the order of the images in the gallery.
'order' => $i,
),
),
)
);
}
}
}

// The WP_HTML_Tag_Processor class calls get_updated_html() internally
// when the instance is treated as a string, but here we explicitly
// convert it to a string.
Expand Down Expand Up @@ -151,13 +222,22 @@ function block_core_gallery_render( $attributes, $content ) {
}
$image_blocks = $matches[0];

// Randomize the order of Image blocks.
shuffle( $image_blocks );
// Reorder image blocks according to the randomized `$image_ids` order
$reordered_blocks = array();
foreach ( $image_ids as $image_id ) {
foreach ( $image_blocks as $block ) {
if ( strpos( $block, $image_id ) !== false ) {
$reordered_blocks[] = $block;
break;
}
}
}

$i = 0;
$content = preg_replace_callback(
$pattern,
static function () use ( $image_blocks, &$i ) {
$new_image_block = $image_blocks[ $i ];
static function () use ( $reordered_blocks, &$i ) {
$new_image_block = $reordered_blocks[ $i ];
++$i;
return $new_image_block;
},
Expand All @@ -166,6 +246,7 @@ static function () use ( $image_blocks, &$i ) {

return $content;
}

/**
* Registers the `core/gallery` block on server.
*
Expand Down
4 changes: 3 additions & 1 deletion packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
"allowResize",
"imageCrop",
"fixedHeight",
"navigationButtonType",
"postId",
"postType",
"queryId"
"queryId",
"galleryId"
],
"description": "Insert an image to make a visual statement.",
"keywords": [ "img", "photo", "picture" ],
Expand Down
Loading
Loading