<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Food Blogger Mania &#187; DOM</title>
	<atom:link href="https://foodbloggermania.it/tag/ricette/dom/feed/" rel="self" type="application/rss+xml" />
	<link>https://foodbloggermania.it</link>
	<description>Food Blogger Mania</description>
	<lastBuildDate>Thu, 30 Apr 2026 20:56:13 +0000</lastBuildDate>
	<language>it-IT</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>Ovosodo Cook</dc:creator>
				<category><![CDATA[Emilia Romagna]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[Loading React]]></category>
		<category><![CDATA[Performant Optimizing]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-2/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-2/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>Mariapia</dc:creator>
				<category><![CDATA[Basilicata]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Query Loop]]></category>
		<category><![CDATA[Unlike Alpine]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-2/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-2/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-3/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-3/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>fucinaidee</dc:creator>
				<category><![CDATA[Veneto]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[Mario Santos]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-3/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-3/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-4/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-4/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>ricette da coinquiline</dc:creator>
				<category><![CDATA[Campania]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-4/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-4/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-5/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-5/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>Ragazze conTorte</dc:creator>
				<category><![CDATA[Lazio]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-5/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-5/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-6/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-6/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>Pani cunzatu</dc:creator>
				<category><![CDATA[Sicilia]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-6/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-6/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-7/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-7/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>Alveare Delle Delizie</dc:creator>
				<category><![CDATA[Friuli Venezia Giulia]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-7/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-7/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-8/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-8/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>BucciaDiArancia</dc:creator>
				<category><![CDATA[Piemonte]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-8/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-8/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-9/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-9/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>claudialuca90</dc:creator>
				<category><![CDATA[Sicilia]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-9/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-9/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-10/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-10/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>Gemma archeologa tra i fornelli.</dc:creator>
				<category><![CDATA[Toscana]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-10/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-10/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making 43% of the Web More Dynamic with the WordPress Interactivity API</title>
		<link>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-11/</link>
		<comments>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-11/#comments</comments>
		<pubDate>Wed, 17 Apr 2024 12:00:00 +0000</pubDate>
		<dc:creator>santox89</dc:creator>
				<category><![CDATA[Campania]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[aria]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Image Block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unlike Alpine]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-11/</guid>
		<description><![CDATA[Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place&#160;<a href="https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-11/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Creating rich, engaging, and interactive website experiences is a simple way to surprise, delight, and attract attention from website readers and users. Dynamic interactivity like instant search, form handling, and client-side “app-like” navigation where elements can persist across routes, all without a full page reload, can make the web a more efficient and interesting place for all.</p>
<p>But creating those experiences on WordPress hasn’t always been the easiest or most straightforward, often requiring complex JavaScript framework setup and maintenance.&nbsp;</p>
<p>Now, with the Interactivity API, WordPress developers have a standardized way for doing that, all built directly into core.&nbsp;</p>
<p>The Interactivity API started as <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/block-interactivity-experiments/">an experimental plugin in early 2022</a>, became <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">an official proposal in March 2023</a>, and was finally <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.org/documentation/wordpress-version/version-6-5/#bring-interactions-to-blocks-with-the-interactivity-api">merged into WordPress core with the release of WordPress 6.5 on April 2, 2024</a>. <strong>It provides an easier, standardized way for WordPress developers to create rich, interactive user experiences with their blocks on the front-end.</strong></p>
<h2>ELI5: The Interactivity API and the Image Block</h2>
<p>Several core WordPress blocks, including the Query Loop, Image, and Search blocks, have already adopted the Interactivity API. The <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/support/wordpress-editor/blocks/image-block/">Image block</a>, in particular, is a great way to show off the Interactivity API in action.&nbsp;</p>
<p>At its core, the Image blocks allow you to add an image to a post or page. When a user clicks on an image in a post or page, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/index.php#L196">the Interactivity API launches a lightbox</a> showing a high-resolution version of the image.</p>
<p>The rendering of the Image block is handled server-side. The client-side interactivity, handling resizing and opening the lightbox, is now done with the new API that comes bundled with WordPress. You can bind the client-side interactivity simply by adding the <code>wp-on--click directive</code> to the image element, referencing the <code>showLightbox</code> action in <code>view.js</code>.</p>
<p>You might say, “But I could easily do this with some JavaScript!” With the Interactivity API, the code is compact and declarative, and you get the context (local state) to handle the lightbox, resizing, side effects, and all of the other needed work <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/blob/e1842bb8dadd224ccf2e7499bcff781afb9ce499/packages/block-library/src/image/view.js#L38">here in the store object</a>.</p>
<div>
<pre>
actions: {
			showLightbox() {
				const ctx = getContext();

				// Bails out if the image has not loaded yet.
				if ( ! ctx.imageRef?.complete ) {
					return;
				}

				// Stores the positons of the scroll to fix it until the overlay is
				// closed.
				state.scrollTopReset = document.documentElement.scrollTop;
				state.scrollLeftReset = document.documentElement.scrollLeft;

				// Moves the information of the expaned image to the state.
				ctx.currentSrc = ctx.imageRef.currentSrc;
				imageRef = ctx.imageRef;
				buttonRef = ctx.buttonRef;
				state.currentImage = ctx;
				state.overlayEnabled = true;

				// Computes the styles of the overlay for the animation.
				callbacks.setOverlayStyles();
			},
...

</pre>
</div>
<p>The lower-level implementation details, like keeping the server and client side in sync, just work; developers no longer need to account for them.</p>
<p>This functionality is possible using vanilla JavaScript, by selecting the element via a query selector, reading data attributes, and manipulating the DOM. But it’s far less elegant, and up until now, there hasn’t been a standardized way in WordPress of handling interactive events like these.</p>
<p>With the Interactivity API, <strong>developers have a predictable way to provide interactivity to users on the front-end</strong>. You don’t have to worry about lower-level code for adding interactivity; it’s there in WordPress for you to start using today. Batteries <em>are</em> included.</p>
<h2>How is the Interactivity API different from Alpine, React, or Vue?</h2>
<p>Prior to merging the Interactivity API into WordPress core, developers would typically reach for a JavaScript framework to add dynamic features to the user-facing parts of their websites. This approach worked just fine, so why was there a need to standardize it?</p>
<p><strong>At its core, the Interactivity API is a lightweight JavaScript library that standardizes the way developers can build interactive HTML elements on WordPress sites.</strong></p>
<p>Mario Santos, a developer on the WordPress core team, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">wrote in the Interactivity API proposal</a> that, “With a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.”</p>
<p>The team saw that the gap between what’s possible and what’s practical grew as sites became more complex. The more complex a user experience developers wanted to build, the more blocks needed to interact with each other, and the more difficult it became to build and maintain sites. Developers would spend a lot of time making sure that the client-side and server-side code played nicely together.</p>
<p>For a large open-source project with several contributors, having an agreed-upon standard and native way of providing client-side interactivity speeds up development and greatly improves the developer experience.</p>
<p><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2024/02/19/merge-announcement-interactivity-api/">Five goals shaped the core development team’s decisions</a> as they built the API:&nbsp;</p>
<ol>
<li><strong>Block-first and PHP-first: </strong>Prioritizing blocks for building sites and server side rendering for better SEO and performance. Combining the best for user and developer experience.</li>
<li><strong>Backward-compatible: </strong>Ensuring compatibility with both classic and block themes and optionally with other JavaScript frameworks, though it’s advised to use the API as the primary method. It also works with hooks and internationalization.</li>
<li><strong>Declarative and reactive: </strong>Using declarative code to define interactions, listening for changes in data, and updating only relevant parts of the DOM accordingly.</li>
<li><strong>Performant: </strong>Optimizing runtime performance to deliver a fast and lightweight user experience.</li>
<li><strong>Send less JavaScript: </strong>Reduce the overall amount of JavaScript being sent on the page by providing a common framework that blocks can reuse.&nbsp; So the more that blocks leverage the Interactivity API, the less JavaScript will be sent overall.</li>
</ol>
<p>Other goals are on the horizon, including improvements to client-side navigation, as you can <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/pull/59707">see in this PR</a>.</p>
<h3>Interactivity API vs. Alpine</h3>
<p>The Interactivity API shares a few similarities to <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://alpinejs.dev/">Alpine</a>—a lightweight JavaScript library that allows developers to build interactions into their web projects, often used in WordPress and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://laravel.com/">Laravel</a> projects.</p>
<p>Similar to Alpine, the Interactivity API uses directives directly in HTML and both play nicely with PHP. <em>Unlike</em> Alpine, the Interactivity API is designed to seamlessly integrate with WordPress <em>and</em> support server-side rendering of its directives.</p>
<p>With the interactivity API, you can easily generate the view from the server in PHP, and <em>then</em> add client-side interactivity. <strong>This results in less duplication, and its support in WordPress core will lead to less architectural decisions currently required by developers.&nbsp;</strong></p>
<p>So while Alpine and the Interactivity API share a broadly similar goal—making it easy for web developers to add interactive elements to a webpage—the Interactivity API is even more plug-and-play for WordPress developers.</p>
<h3>Interactivity API vs. React and Vue</h3>
<p>Many developers have opted for <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://react.dev/">React</a> when adding interactivity to WordPress sites because, in the modern web development stack, React is the go-to solution for declaratively handling DOM interactivity. This is familiar territory, and we’re used to using React and JSX when adding custom blocks for Gutenberg.</p>
<p>Loading React on the client side can be done, but it leaves you with many decisions: “How should I handle routing? How do I work with the context between PHP and React? What about server-side rendering?”</p>
<p>Part of the goal in developing the Interactivity API was the need to <strong>write as little as little JavaScript as possible</strong>, leaving the heavy lifting to PHP, and only shipping JavaScript when necessary.</p>
<p>The core team also saw issues with how these frameworks worked in conjunction with WordPress. Developers can use JavaScript frameworks like React and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://vuejs.org/">Vue</a> to render a block on the front-end that they server-rendered in PHP, for example, but this requires logic duplication and risks exposure to issues with WordPress hooks.</p>
<p>For these reasons, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#why-preact">among others</a>, the core team preferred <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://preactjs.com/">Preact</a>—a smaller UI framework that requires less JavaScript to download and execute without sacrificing performance. Think of it like React with fewer calories.</p>
<p>Luis Herranz, a WordPress Core contributor from Automattic, outlines more details on Alpine vs the Interactivity API’s usage of Preact with a thin layer of directives on top of it <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#comment-44595">in this comment on the original proposal</a>.</p>
<p><strong>Preact only loads if the page source contains an interactive block</strong>, meaning it is not loaded until it’s needed, aligning with the idea of shipping as little JavaScript as possible (and shipping <em>no</em> JavaScript as a default).</p>
<p>In the original Interactivity API proposal, you can see the <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/#approaches-considered">run-down and comparison of several frameworks</a> and why Preact was chosen over the others.</p>
<h2>What does the new Interactivity API provide to WordPress developers?</h2>
<p>In addition to providing a standardized way to render interactive elements client-side, the Interactivity API also provides developers with directives and a more straightforward way of creating a store object to handle state, side effects, and actions.</p>
<figure><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/"><img width="1084" height="2048" src="https://en-blog.files.wordpress.com/2024/04/wordpress-interactivity-api-standard-table.png" alt="a table showing the differences of developing interactive elements on WordPress with and without a standard" class="wp-image-60877" style="width:840px;height:auto" /></a><br />
<figcaption><em>Graphic from <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: The Interactivity API – A better developer experience in building interactive blocks on WordPress.org</a></em></figcaption>
</figure>
<h3>Directives</h3>
<p>Directives, a special set of data attributes, allow you to extend HTML markup. You can share data between the server-side-rendered blocks and the client-side, bind values, add click events, and much more. The Interactivity API reference <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">lists all the available directives</a>.</p>
<p>These directives are typically added in the block’s <code>render.php</code> file, and they support all of the WordPress APIs, including <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/actions/">actions</a>, <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/plugins/hooks/filters/">filters</a>, and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/reference/functions/translations_api/">core translation</a> APIs.&nbsp;</p>
<p>Here’s the render file of a sample block. Notice the click event (<code>data-wp-on--click="actions.toggle"</code>), and how we bind the value of the aria-expanded attributes via directives.</p>
<div>
<pre>
&lt;div
	&lt;?php echo get_block_wrapper_attributes(); ?&gt;
	data-wp-interactive=&quot;create-block&quot;
	&lt;?php echo wp_interactivity_data_wp_context( array( &#039;isOpen&#039; =&gt; false ) ); ?&gt;
	data-wp-watch=&quot;callbacks.logIsOpen&quot;
&gt;
	&lt;button
		data-wp-on--click=&quot;actions.toggle&quot;
		data-wp-bind--aria-expanded=&quot;context.isOpen&quot;
		aria-controls=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
	&gt;
		&lt;?php esc_html_e( &#039;Toggle&#039;, &#039;my-interactive-block&#039; ); ?&gt;
	&lt;/button&gt;

	&lt;p
		id=&quot;&lt;?php echo esc_attr( $unique_id ); ?&gt;&quot;
		data-wp-bind--hidden=&quot;!context.isOpen&quot;
	&gt;
		&lt;?php
			esc_html_e( &#039;My Interactive Block - hello from an interactive block!&#039;, &#039;my-interactive-block&#039; );
		?&gt;
	&lt;/p&gt;
&lt;/div&gt;

</pre>
</div>
<p>Do you need to dynamically update an element&#8217;s inner text? The Interactivity API allows you to use <code>data-wp-text</code> on an element, just like you can use v-text in Vue.</p>
<p>You can bind a value to a boolean or string using <code>wp-bind–</code> or hook up a click event by using <code>data-wp-on–click</code> on the element. This means you can write PHP and HTML and sprinkle in directives to add interactivity in a declarative way.</p>
<h3>Handling state, side effects, and actions</h3>
<p>The second stage of adding interactivity is to create a store, which is usually done in your <code>view.js</code> file. In the store, you’ll have access to the same context as in your <code>render.php</code> file.</p>
<p>In the store object, you define actions responding to user interactions. These actions can update the local context or global state, which then re-renders and updates the connected HTML element. You can also define side effects/callbacks, which are similar to actions, but they respond to state changes instead of direct user actions.</p>
<div>
<pre>
import { store, getContext } from &#039;@wordpress/interactivity&#039;;

store( &#039;create-block&#039;, {
	actions: {
		toggle: () =&gt; {
			const context = getContext();
			context.isOpen = ! context.isOpen;
		},
	},
	callbacks: {
		logIsOpen: () =&gt; {
			const { isOpen } = getContext();
			// Log the value of `isOpen` each time it changes.
			console.log( `Is open: ${ isOpen }` );
		},
	},
} );
</pre>
</div>
<h2>Try it out for yourself</h2>
<p>The Interactivity API is production-ready and already running on WordPress.com! With any WordPress.com plan, you’ll have access to the core blocks built on top of the Interactivity API.&nbsp;</p>
<p>If you want to build your own interactive blocks, you can scaffold an interactive block by running the below code in your terminal:</p>
<div>
<pre>
npx @wordpress/create-block@latest my-interactive-block --template @wordpress/create-block-interactive-template 
</pre>
</div>
<p>This will give you an example interactive block, with directives and state handling set up.&nbsp;</p>
<p>You can then play around with this locally, using <code>wp-env</code>, using a staging site, or by uploading the plugin directly to your site running <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/hosting/?ref=blog#pricing-grid">a plugin-eligible WordPress.com plan</a>.&nbsp;</p>
<p>If you want a seamless experience between your local dev setup and your WordPress.com site, try using it with <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wordpress.com/blog/2024/03/18/github-deployments/">our new GitHub Deployments</a> feature! Developing custom blocks is the perfect use case for this new tool.</p>
<p>The best way to learn something new is to start building. To kick things off, you may find the following resources a good starting point:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/news/2024/04/11/a-first-look-at-the-interactivity-api/">A first look at the Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://wpmovies.dev/">Interactivity API WP Movies demo</a> and <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/wp-movies-demo/blob/main/README.md">demo video</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/issues/60219">Follow along with this task</a> for improvements coming to the Interactivity API</li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#list-of-directives">Block editor reference</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/">Proposal: Interactivity API</a></li>
<li><a target="_blank" rel="nofollow" href="/redirect.php?URL=https://github.com/WordPress/gutenberg/discussions/55642">GitHub issue for showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/making-43-of-the-web-more-dynamic-with-the-wordpress-interactivity-api-11/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Affettatrici Fac: Migliori 3 modelli a confronto con caratteristiche foto e prezzi</title>
		<link>https://foodbloggermania.it/ricetta/affettatrici-fac-migliori-3-modelli-a-confronto-con-caratteristiche-foto-e-prezzi/</link>
		<comments>https://foodbloggermania.it/ricetta/affettatrici-fac-migliori-3-modelli-a-confronto-con-caratteristiche-foto-e-prezzi/#comments</comments>
		<pubDate>Fri, 01 May 2020 16:56:04 +0000</pubDate>
		<dc:creator>deliziadivina</dc:creator>
				<category><![CDATA[Campania]]></category>
		<category><![CDATA[avanzi]]></category>
		<category><![CDATA[carni]]></category>
		<category><![CDATA[ce]]></category>
		<category><![CDATA[dell]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[FAC]]></category>
		<category><![CDATA[Italy]]></category>
		<category><![CDATA[misura]]></category>

		<guid isPermaLink="false">https://foodbloggermania.it/ricetta/affettatrici-fac-migliori-3-modelli-a-confronto-con-caratteristiche-foto-e-prezzi/</guid>
		<description><![CDATA[Le affettatrici Fac garantiscono la qualità eccellente tipica del Made in Italy. Si tratta infatti di un’azienda italiana, fondata nel 1963, che lavora con passione e dedizione, realizzando prodotti sul territorio italiano. Tutte le affettatrici Fac vengono realizzate in lega di alluminio, ossia MG3, di cui la componente principale è il magnesio il quale risulta&#160;<a href="https://foodbloggermania.it/ricetta/affettatrici-fac-migliori-3-modelli-a-confronto-con-caratteristiche-foto-e-prezzi/" class="read-more">Continua a leggere..</a>]]></description>
			<content:encoded><![CDATA[<p>Le <strong>affettatrici Fac</strong> garantiscono la <strong>qualità eccellente tipica del Made in Italy</strong>. Si tratta infatti di un’azienda italiana, fondata nel 1963, che lavora con passione e dedizione, realizzando prodotti sul territorio italiano. Tutte le affettatrici Fac vengono realizzate in lega di alluminio, ossia MG3, di cui la componente principale è il magnesio il quale risulta altamente resistente alla corrosione, durevole nel tempo, duttile, facile da lavorare.</p>
<p>Tutti i modelli di <strong>affettatrici Fac</strong> sono accuratamente <strong>lucidate a anodizzate</strong>, attraverso l’applicazione di uno strato ossido protettivo, applicato sull’alluminio trattato. Ogni affettatrice viene infatti montata artigianalmente da operai specializzati. Solo per alcuni particolari (come lo stampo dei piedini o i fori della capottina) vengono utilizzati i robot.</p>
<p>Oltre alla funzionalità, l&#8217;azienda Fac <strong>cura per ogni modello anche il design e l&#8217;immagine complessiva</strong>. L&#8217;obiettivo è quello di offrire ai propri clienti in prodotto non solo utile e altamente performante ma anche esteticamente gradevole, in grado di decorare e impreziosire la cucina.</p>
<div>
<div>
<p>    <span>Bestseller No. 1</span></p>
<div>
        <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B010HOPULM?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1&amp;keywords=fac%20affettatrice" title="FAC F 250 E DOM affettatrice Elettrico Acciaio inossidabile 185 W" rel="nofollow" target="_blank"><br />
            <img class="aawp-product__image" src="https://m.media-amazon.com/images/I/311aKgA9zGL._SL160_.jpg" alt="FAC F 250 E DOM affettatrice Elettrico Acciaio inossidabile 185 W" /><br />
        </a></p></div>
<div>
        <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B010HOPULM?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1&amp;keywords=fac%20affettatrice" title="FAC F 250 E DOM affettatrice Elettrico Acciaio inossidabile 185 W" rel="nofollow" target="_blank"><br />
            FAC F 250 E DOM affettatrice Elettrico Acciaio inossidabile 185 W        </a></p>
<div>
                    </div>
</p></div>
<div>
<div>
<p>            <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/gp/prime/?tag=deliziadivina-21" title="Amazon Prime" rel="nofollow" target="_blank"></a>        </div>
<p>                <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B010HOPULM?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1&amp;keywords=fac%20affettatrice" title="Vedi Prezzo su Amazon" target="_blank" rel="nofollow">Vedi Prezzo su Amazon</a>
            </div>
</div>
<div>
<p>    <span>Bestseller No. 2</span></p>
<div>
        <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B01LYGLVI5?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1&amp;keywords=fac%20affettatrice" title="Fac 6195001CEDO1 Affettatrice Elettrica, Rosso" rel="nofollow" target="_blank"><br />
            <img class="aawp-product__image" src="https://m.media-amazon.com/images/I/51iR3fApZAL._SL160_.jpg" alt="Fac 6195001CEDO1 Affettatrice Elettrica, Rosso" /><br />
        </a></p></div>
<div>
        <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B01LYGLVI5?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1&amp;keywords=fac%20affettatrice" title="Fac 6195001CEDO1 Affettatrice Elettrica, Rosso" rel="nofollow" target="_blank"><br />
            Fac 6195001CEDO1 Affettatrice Elettrica, Rosso        </a></p>
<div>
<ul>
<li>Esclusivo uso domestico-ce uso domestico</li>
</ul></div>
</p></div>
<div>
<div>
<p>            <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/gp/prime/?tag=deliziadivina-21" title="Amazon Prime" rel="nofollow" target="_blank"></a>        </div>
<p>                <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B01LYGLVI5?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1&amp;keywords=fac%20affettatrice" title="Vedi Prezzo su Amazon" target="_blank" rel="nofollow">Vedi Prezzo su Amazon</a>
            </div>
</div>
<div>
<p>    <span>Bestseller No. 3</span></p>
<div>
        <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B01EKHGF5W?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1&amp;keywords=fac%20affettatrice" title="S220Ce Domest.Affettat.22Cm Fa" rel="nofollow" target="_blank"><br />
            <img class="aawp-product__image" src="https://m.media-amazon.com/images/I/41m4MeZN8PL._SL160_.jpg" alt="S220Ce Domest.Affettat.22Cm Fa" /><br />
        </a></p></div>
<div>
        <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B01EKHGF5W?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1&amp;keywords=fac%20affettatrice" title="S220Ce Domest.Affettat.22Cm Fa" rel="nofollow" target="_blank"><br />
            S220Ce Domest.Affettat.22Cm Fa        </a></p>
<div>
                    </div>
</p></div>
<div>
<div>
<p>            <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/gp/prime/?tag=deliziadivina-21" title="Amazon Prime" rel="nofollow" target="_blank"></a>        </div>
<p>                <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B01EKHGF5W?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1&amp;keywords=fac%20affettatrice" title="Vedi Prezzo su Amazon" target="_blank" rel="nofollow">Vedi Prezzo su Amazon</a>
            </div>
</div>
</div>
<h2>Affettatrici Fac 3 modelli a confronto &#8211; caratteristiche e prezzi</h2>
<ul>
<li>
<h3>FAC F 250 E DOM affettatrice in acciaio inox</h3>
</li>
</ul>
<p><img class="size-medium wp-image-594 aligncenter" src="https://deliziadivina.it/wp-content/uploads/2020/05/FAC-F-250-E-DOM-affettatrice-in-acciaio-inox-300x278.jpg" alt="FAC F 250 E DOM affettatrice in acciaio inox" width="300" height="278" />Realizzata <strong>in acciaio inossidabile</strong>, come le <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://deliziadivina.it/migliori-affettatrici-professionali/"><strong>migliori affettatrici</strong> </a>questa<strong> affettatrice Fac 250 elettrica</strong> ha una potenza di <strong>185 Watt</strong>. Il design minimale e semplice, tipico dell&#8217;azienda, conferisce a questo elettrodomestico un aspetto versatile, perfetto per ogni cucina. Tale strumento dispone di un <strong>sistema affila lame</strong> incluso nella confezione e di un sistema di protezione per garantire massima sicurezza durante e dopo l&#8217;utilizzo.</p>
<p>In questo modo, anche i meno esperti <strong>potranno utilizzare lo strumento senza il rischio di ferirsi</strong>. Il <strong>diametro della lama è di 25 cm</strong> mentre l’intera struttura misura 52 cm, 40 cm e 38 cm (profondità, larghezza e altezza). Grazie<strong> all&#8217;affettatrice FAC F 250 E DOM</strong> è possibile affettare qualunque alimento si desidera, dalla carne congelata, agli affettati passando per le verdure. Infatti, la qualità della <strong>lama in acciaio</strong> consente di lavorare anche le carni più dure. Lo strumento richiede una manutenzione minima e piuttosto semplice.</p>
<p>Insieme al prodotto viene inoltre fornito uno strumento apposito per la manutenzione della lama, eseguibile a casa propria, senza la necessità di contattare un esperto. In questo modo è possibile mantenere il dispositivo e le sue componenti, garantendone un <strong>ottimo funzionamento anche nel corso degli anni</strong>. Non solo, la particolare struttura in acciaio inossidabile permette di pulire facilmente il prodotto per garantire massima igiene.</p>
<div>
<div>
<div>
        <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B010HOPULM?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1" title="FAC F 250 E DOM affettatrice Elettrico Acciaio inossidabile 185 W" rel="nofollow" target="_blank"><br />
            <img class="aawp-product__image" src="https://m.media-amazon.com/images/I/311aKgA9zGL._SL160_.jpg" alt="FAC F 250 E DOM affettatrice Elettrico Acciaio inossidabile 185 W" /><br />
        </a></p></div>
<div>
        <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B010HOPULM?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1" title="FAC F 250 E DOM affettatrice Elettrico Acciaio inossidabile 185 W" rel="nofollow" target="_blank"><br />
            FAC F 250 E DOM affettatrice Elettrico Acciaio inossidabile 185 W        </a></p>
<div>
                    </div>
</p></div>
<div>
<div>
<p>            <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/gp/prime/?tag=deliziadivina-21" title="Amazon Prime" rel="nofollow" target="_blank"></a>        </div>
<p>                <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B010HOPULM?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1" title="Vedi Prezzo su Amazon" target="_blank" rel="nofollow">Vedi Prezzo su Amazon</a>
            </div>
</div>
</div>
<ul>
<li>
<h3>Affettatrice Fac 6195001CEDO1</h3>
</li>
</ul>
<p><strong>L&#8217;affettatrice Fac</strong> 6195001CEDO1, viene realizzata in<strong> lega di alluminio anodizzato</strong>, che ne garantisce un&#8217;ottima durata nel tempo. Tale materiale risulta, infatti, particolarmente robusto e durevole. Pur considerando questa caratteristica, se ne consiglia un esclusivo uso domestico. Lo strumento offre la possibilità di<strong> regolare lo spessore del taglio</strong>, il quale può variare da 0 mm a 15 mm.</p>
<p>All’interno della confezione dell’affettatrice Fac 6195001CEDO1 è presente uno <strong>strumento affila lama</strong> e il <strong>piatto per gli avanzi</strong>, montato su cuscinetti a sfere, che ne garantisce una facile pulizia. <strong>La lama misura 195 mm</strong> mentre l’intera struttura misura 52 cm, 36 cm e 31,5 cm (larghezza, profondità e altezza). <strong>L&#8217;elevata qualità dei materiali</strong> e della lama garantiscono alte prestazioni e un risultato professionale. La lama è infatti studiata per essere utilizzata per insaccati di ogni genere ma anche per salumi pesanti, formaggi, arrosti, verdure, pesce, carni dure, ecc.</p>
<div>
<div>
<div>
        <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B009E53UH2?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1" title="Fac 6195001CEDO1 Affettatrice Elettrica, Alluminio" rel="nofollow" target="_blank"><br />
            <img class="aawp-product__image" src="https://m.media-amazon.com/images/I/51x6KVPqOVL._SL160_.jpg" alt="Fac 6195001CEDO1 Affettatrice Elettrica, Alluminio" /><br />
        </a></p></div>
<div>
        <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B009E53UH2?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1" title="Fac 6195001CEDO1 Affettatrice Elettrica, Alluminio" rel="nofollow" target="_blank"><br />
            Fac 6195001CEDO1 Affettatrice Elettrica, Alluminio        </a></p>
<div>
<ul>
<li>Esclusivo uso domestico-ce uso domestico</li>
</ul></div>
</p></div>
<div>
<div></div>
<p>                <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B009E53UH2?tag=deliziadivina-21&amp;linkCode=osi&amp;th=1&amp;psc=1" title="Vedi Prezzo su Amazon" target="_blank" rel="nofollow">Vedi Prezzo su Amazon</a>
            </div>
</div>
</div>
<ul>
<li>
<h3><b>Fac 6220001CEDO1 </b></h3>
</li>
</ul>
<p>Questo modello di <strong>affettatrice Fac</strong>, è <strong>realizzata in alluminio</strong> verniciato (trattato anodicamente) di colore rosso. Considerata la struttura e la lama, se ne consiglia esclusivamente l&#8217;uso domestico.<strong> Lo spessore del taglio può essere regolato da 0 mm a 15 mm</strong>. Lo strumento risulta, infatti, molto sensibile pertanto garantisce regolazione decimale dello spessore in base alle esigenze di ogni consumatore.</p>
<p>Acquistandola questo <strong>modello di affettatrice</strong> è possibile ottenere (all&#8217;interno della confezione) anche <strong>l’affilatore per la lama</strong> (facilmente montabile, garantisce l’efficienza dell’utilizzo a lungo). Questo semplice strumento consente di svolgere la manutenzione dell&#8217;elettrodomestico a casa propria senza la necessità di contattare un esperto. La Fac 6220001CEDO1 è facile ed estremamente veloce da pulire.</p>
<p>Per garantire un <strong>costante igiene</strong> dopo l’utilizzo <strong>è sufficiente detergere le zone removibili</strong> con acqua e sapone per i piatti e utilizzare un panno umido e un detergente per le componenti fisse. Il piatto per la raccolta di quanto tagliato è disposto su cuscinetti a sfere per renderlo scorrevole e utile anche al taglio di salumi pensati. Non solo, <strong>si adatta anche a formaggi, carni dure, pesce e molto altro</strong>. Infine, la dimensione ridotta e il design compatto la rendono facile da spostare o da riporre all&#8217;interno degli scaffali della cucina.</p>
<div>
<div>
<div>
        <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B01LYSA9JW?tag=deliziadivina-21&amp;linkCode=ogi&amp;th=1&amp;psc=1" title="Fac 6220001CEDO1 Affettatrice Elettrica, Rosso" rel="nofollow" target="_blank"><br />
            <img class="aawp-product__image" src="https://m.media-amazon.com/images/I/51HE8sW-1pL._SL160_.jpg" alt="Fac 6220001CEDO1 Affettatrice Elettrica, Rosso" /><br />
        </a></p></div>
<div>
        <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B01LYSA9JW?tag=deliziadivina-21&amp;linkCode=ogi&amp;th=1&amp;psc=1" title="Fac 6220001CEDO1 Affettatrice Elettrica, Rosso" rel="nofollow" target="_blank"><br />
            Fac 6220001CEDO1 Affettatrice Elettrica, Rosso        </a></p>
<div>
<ul>
<li>Esclusivo uso domestico-ce uso domestico</li>
</ul></div>
</p></div>
<div>
<div>
<p>            <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/gp/prime/?tag=deliziadivina-21" title="Amazon Prime" rel="nofollow" target="_blank"></a>        </div>
<p>                <a target="_blank" rel="nofollow" href="/redirect.php?URL=https://www.amazon.it/dp/B01LYSA9JW?tag=deliziadivina-21&amp;linkCode=ogi&amp;th=1&amp;psc=1" title="Vedi Prezzo su Amazon" target="_blank" rel="nofollow">Vedi Prezzo su Amazon</a>
            </div>
</div>
</div>
<p>L&#8217;articolo <a target="_blank" rel="nofollow" rel="nofollow" href="/redirect.php?URL=https://deliziadivina.it/affettatrici-fac-migliori-prezzi/">Affettatrici Fac: Migliori 3 modelli a confronto con caratteristiche foto e prezzi</a> proviene da <a target="_blank" rel="nofollow" rel="nofollow" href="/redirect.php?URL=https://deliziadivina.it">DeliziaDivina &#8211; Solo il meglio per la tua cucina</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://foodbloggermania.it/ricetta/affettatrici-fac-migliori-3-modelli-a-confronto-con-caratteristiche-foto-e-prezzi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- Wp Fastest Cache: XML Content -->