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

Experiment: Add lightbox to Image block using directives #49621

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ebba7b4
Add interactivity runtime
luisherranz Apr 21, 2023
af59781
Add it to the image block
luisherranz Apr 21, 2023
cca3d60
Add a separate webpack config
luisherranz Apr 21, 2023
160a029
Make sure the runtime is imported only once
luisherranz Apr 21, 2023
7f16f27
Use sideEffects instead of init
luisherranz Apr 21, 2023
f4b2ee8
Move script registration to a general file
luisherranz Apr 21, 2023
378b041
Add `defer` to the interactivity scripts
luisherranz Apr 21, 2023
409d161
Revert changes of the image block
luisherranz Apr 21, 2023
29e11ab
Fix init import name
luisherranz Apr 21, 2023
f4b6c0a
Move and refactor the interactive scritps registration
gziolo Apr 28, 2023
3d94473
Fix code style violations
gziolo Apr 28, 2023
6ae760f
Use `wp-interactivity-` prefix for script handles
gziolo Apr 28, 2023
9d6869e
Improve the matcher for side effects in `package.json`
gziolo Apr 28, 2023
c6d02d8
Add custom useSignalEffect
DAreRodz May 4, 2023
af55917
Call `init` after `store` has been initialized
DAreRodz May 4, 2023
7c1f2d1
Add lightbox to image block
artemiomorales Apr 3, 2023
9507199
Add logic for hiding lightbox on esc key press and overlay click
artemiomorales Apr 3, 2023
3f89d9b
Improve styles and add note to add conditional for lightbox markup
artemiomorales Apr 5, 2023
cb7f89a
Add editor UI and attribute for toggling lightbox
artemiomorales Apr 13, 2023
437d873
Remove image translation animation and add fade instead
artemiomorales Apr 19, 2023
7bd6b25
Add accessibility; clean up styles; fix bug regarding ref in directives
artemiomorales Apr 21, 2023
109344c
Configure image to use new Interactivity API runtime included in Gute…
artemiomorales Apr 26, 2023
f95a2cd
Remove viewScript from image config
artemiomorales Apr 26, 2023
e37e228
Add Portal directive to Interactivity API runtime
artemiomorales Apr 26, 2023
e17e564
Set scrim to site background color
artemiomorales Apr 26, 2023
f481a35
Remove extraneous image CSS declaration
artemiomorales Apr 26, 2023
848a8fa
Improve aria labeling
artemiomorales May 2, 2023
2204a50
Code cleanup; simplify syntax, consolidate code
artemiomorales May 3, 2023
6244e14
Refactor code, remove event listeners, consolidate logic
artemiomorales May 3, 2023
c388721
Fix formatting in SCSS file
artemiomorales May 3, 2023
9754edb
Change CheckboxControl to a ToggleControl; update API docs
artemiomorales May 3, 2023
b337272
Update wp_enqueue_script to correctly add interactivity runtime
artemiomorales May 3, 2023
ee679e6
Fix linter errors
artemiomorales May 4, 2023
ddbf32c
Update to use core.image namespace
artemiomorales May 4, 2023
f82d8f5
Pause closing of lightbox slightly when using the mousewheel
artemiomorales May 4, 2023
8ce00cd
Rename portal directive to 'wp-body' and remove unused reference
artemiomorales May 4, 2023
1427e8a
Add internal dependencies flag; update comment
artemiomorales May 4, 2023
58b0ef5
Remove extraneous code
artemiomorales May 4, 2023
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 @@ -339,7 +339,7 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre
- **Name:** core/image
- **Category:** media
- **Supports:** anchor, color (~~background~~, ~~text~~), filter (duotone)
- **Attributes:** align, alt, caption, height, href, id, linkClass, linkDestination, linkTarget, rel, sizeSlug, title, url, width
- **Attributes:** align, alt, caption, enableLightbox, height, href, id, linkClass, linkDestination, linkTarget, rel, sizeSlug, title, url, width

## Latest Comments

Expand Down
50 changes: 50 additions & 0 deletions lib/experimental/interactivity-api/script-loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Updates the script-loader.php file
*
* @package gutenberg
*/

/**
* Registers interactivity runtime and vendor scripts to use with interactive blocks.
*
* @param WP_Scripts $scripts WP_Scripts instance.
*/
function gutenberg_register_interactivity_scripts( $scripts ) {
gutenberg_override_script(
$scripts,
'wp-interactivity-runtime',
gutenberg_url(
'build/block-library/interactive-blocks/interactivity.min.js'
),
array( 'wp-interactivity-vendors' )
);

gutenberg_override_script(
$scripts,
'wp-interactivity-vendors',
gutenberg_url(
'build/block-library/interactive-blocks/vendors.min.js'
)
);
}
add_action( 'wp_default_scripts', 'gutenberg_register_interactivity_scripts', 10, 1 );

/**
* Adds the "defer" attribute to all the interactivity script tags.
*
* @param string $tag The generated script tag.
* @param string $handle The script's registered handle.
*
* @return string The modified script tag.
*/
function gutenberg_interactivity_scripts_add_defer_attribute( $tag, $handle ) {
if ( 0 === strpos( $handle, 'wp-interactivity-' ) ) {
$p = new WP_HTML_Tag_Processor( $tag );
$p->next_tag( array( 'tag' => 'script' ) );
$p->set_attribute( 'defer', true );
return $p->get_updated_html();
}
return $tag;
}
add_filter( 'script_loader_tag', 'gutenberg_interactivity_scripts_add_defer_attribute', 10, 2 );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
require __DIR__ . '/experimental/block-editor-settings.php';
require __DIR__ . '/experimental/blocks.php';
require __DIR__ . '/experimental/interactivity-api/script-loader.php';
require __DIR__ . '/experimental/navigation-theme-opt-in.php';
require __DIR__ . '/experimental/kses.php';
require __DIR__ . '/experimental/l10n.php';
Expand Down
86 changes: 74 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
"@wordpress/warning": "file:packages/warning",
"@wordpress/widgets": "file:packages/widgets",
"@wordpress/wordcount": "file:packages/wordcount",
"deepsignal": "1.3.0",
"preact": "10.13.2",
"wicg-inert": "3.1.2"
},
"devDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
"sideEffects": [
"build-style/**",
"src/**/*.scss",
"{src,build,build-module}/*/init.js"
"{src,build,build-module}/*/init.js",
"{src,build,build-module}/utils/interactivity/index.js"
],
"dependencies": {
"@babel/runtime": "^7.16.0",
"@preact/signals": "^1.1.3",
"@wordpress/a11y": "file:../a11y",
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/autop": "file:../autop",
Expand Down Expand Up @@ -63,12 +65,14 @@
"change-case": "^4.1.2",
"classnames": "^2.3.1",
"colord": "^2.7.0",
"deepsignal": "^1.3.0",
"escape-html": "^1.0.3",
"fast-average-color": "^9.1.1",
"fast-deep-equal": "^3.1.3",
"lodash": "^4.17.21",
"memize": "^1.1.0",
"micromodal": "^0.4.10",
"preact": "^10.13.2",
"remove-accents": "^0.4.2"
},
"peerDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
"source": "attribute",
"selector": "figure > a",
"attribute": "target"
},
"enableLightbox": {
"type": "boolean"
}
},
"supports": {
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export function ImageEdit( {
width,
height,
sizeSlug,
enableLightbox = true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure but, shouldn't we define the default value in the block.json instead of here? Something like:

"enableLightbox": {
    "type": "boolean",
    "default": true
}

} = attributes;
const [ temporaryURL, setTemporaryURL ] = useState();

Expand Down Expand Up @@ -242,6 +243,7 @@ export function ImageEdit( {
...mediaAttributes,
...additionalAttributes,
linkDestination,
enableLightbox,
} );
}

Expand Down
21 changes: 21 additions & 0 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
TextareaControl,
TextControl,
ToolbarButton,
ToggleControl,
__experimentalHeading as Heading,
} from '@wordpress/components';
import { useViewportMatch, usePrevious } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
Expand Down Expand Up @@ -96,6 +98,7 @@ export default function Image( {
height,
linkTarget,
sizeSlug,
enableLightbox,
} = attributes;
const imageRef = useRef();
const prevCaption = usePrevious( caption );
Expand Down Expand Up @@ -458,6 +461,24 @@ export default function Image( {
</>
}
/>
<Heading
style={ {
textTransform: 'uppercase',
fontWeight: '500',
fontSize: '11px',
} }
>
Behaviors
</Heading>
<ToggleControl
label="Lightbox"
checked={ enableLightbox }
onChange={ () => {
setAttributes( {
enableLightbox: ! enableLightbox,
} );
} }
/>
</InspectorControls>
</>
);
Expand Down
Loading