-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Modal Manager block and started general modal interactivity.
- Loading branch information
Showing
16 changed files
with
288 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 3, | ||
"name": "gatherpress/modal-manager", | ||
"version": "1.0.0", | ||
"title": "Modal Manager", | ||
"category": "gatherpress", | ||
"icon": "external", | ||
"example": {}, | ||
"description": "Manage modals and their triggers with ease.", | ||
"attributes": {}, | ||
"supports": { | ||
"html": false, | ||
"interactivity": true | ||
}, | ||
"textdomain": "gatherpress", | ||
"editorScript": "file:./index.js", | ||
"viewScriptModule": "file:./view.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks'), 'version' => '1097ba5f24d0a9952920'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php return array('dependencies' => array('@wordpress/interactivity'), 'version' => '78bbf04e2bbfe6dd79f3', 'type' => 'module'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-data'), 'version' => 'f0f47369380caa31bad3'); | ||
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-data'), 'version' => 'd1d1ac2fd0477b184bb0'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
/** | ||
* The "RSVP" class manages the RSVP block and its variations, | ||
* primarily transforming block content and preparing it for output. | ||
* | ||
* @package GatherPress\Core | ||
* @since 1.0.0 | ||
*/ | ||
|
||
namespace GatherPress\Core\Blocks; | ||
|
||
// Exit if accessed directly. | ||
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore | ||
|
||
use GatherPress\Core\Block; | ||
use GatherPress\Core\Traits\Singleton; | ||
use WP_HTML_Tag_Processor; | ||
|
||
/** | ||
* Class responsible for managing the "RSVP" block and its variations, | ||
* including dynamic transformations and enhancements for interactive functionality. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
class Modal_Manager { | ||
/** | ||
* Enforces a single instance of this class. | ||
*/ | ||
use Singleton; | ||
|
||
/** | ||
* Class constructor. | ||
* | ||
* This method initializes the object and sets up necessary hooks. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
protected function __construct() { | ||
$this->setup_hooks(); | ||
} | ||
|
||
/** | ||
* Set up hooks for various purposes. | ||
* | ||
* This method adds hooks for different purposes as needed. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @return void | ||
*/ | ||
protected function setup_hooks(): void { | ||
add_filter( 'render_block', array( $this, 'inject_modal_behavior' ), 10, 2 ); | ||
} | ||
|
||
/** | ||
* Injects modal interactivity behavior into block content. | ||
* | ||
* This method enhances `core/button` blocks with specific classes by injecting | ||
* attributes necessary for modal interactivity. It supports both `<button>` | ||
* and `<a>` elements and applies the corresponding interactivity attributes | ||
* based on the class names `gatherpress--open-modal` and `gatherpress--close-modal`. | ||
* | ||
* If a block contains the `gatherpress--open-modal` class, it adds attributes | ||
* to handle opening the modal. Similarly, for the `gatherpress--close-modal` | ||
* class, it adds attributes for closing the modal. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @param string $block_content The HTML content of the block. | ||
* @param array $block The parsed block data. | ||
* | ||
* @return string The updated block content with interactivity attributes. | ||
*/ | ||
public function inject_modal_behavior( string $block_content, array $block ): string { | ||
$block_instance = Block::get_instance(); | ||
|
||
if ( | ||
'core/button' === $block['blockName'] && | ||
isset( $block['attrs']['className'] ) | ||
) { | ||
$tag = new WP_HTML_Tag_Processor( $block_content ); | ||
$button_tag = $block_instance->locate_button_tag( $tag, 'button' ); | ||
|
||
if ( empty( $button_tag ) ) { | ||
$tag = new WP_HTML_Tag_Processor( $block_content ); | ||
$button_tag = $block_instance->locate_button_tag( $tag, 'a' ); | ||
} | ||
|
||
if ( | ||
$button_tag && | ||
false !== strpos( $block['attrs']['className'], 'gatherpress--open-modal' ) | ||
) { | ||
$button_tag->set_attribute( 'data-wp-interactive', 'gatherpress/modal' ); | ||
$button_tag->set_attribute( 'data-wp-on--click', 'actions.openModal' ); | ||
} | ||
|
||
if ( | ||
$button_tag && | ||
false !== strpos( $block['attrs']['className'], 'gatherpress--close-modal' ) | ||
) { | ||
$button_tag->set_attribute( 'data-wp-interactive', 'gatherpress/modal' ); | ||
$button_tag->set_attribute( 'data-wp-on--click', 'actions.closeModal' ); | ||
} | ||
|
||
$block_content = $tag->get_updated_html(); | ||
} | ||
|
||
return $block_content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 3, | ||
"name": "gatherpress/modal-manager", | ||
"version": "1.0.0", | ||
"title": "Modal Manager", | ||
"category": "gatherpress", | ||
"icon": "external", | ||
"example": {}, | ||
"description": "Manage modals and their triggers with ease.", | ||
"attributes": {}, | ||
"supports": { | ||
"html": false, | ||
"interactivity": true | ||
}, | ||
"textdomain": "gatherpress", | ||
"editorScript": "file:./index.js", | ||
"viewScriptModule": "file:./view.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* WordPress dependencies. | ||
*/ | ||
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor'; | ||
|
||
const Edit = () => { | ||
const blockProps = useBlockProps(); | ||
|
||
const TEMPLATE = [ | ||
['gatherpress/modal', {}, [['gatherpress/modal-content', {}]]], | ||
]; | ||
|
||
return ( | ||
<div {...blockProps}> | ||
<InnerBlocks template={TEMPLATE} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Edit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* WordPress dependencies. | ||
*/ | ||
import { registerBlockType } from '@wordpress/blocks'; | ||
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies. | ||
*/ | ||
import edit from './edit'; | ||
import metadata from './block.json'; | ||
|
||
/** | ||
* Edit component for the GatherPress Modal Manager block. | ||
* | ||
* This component renders the edit view of the GatherPress Modal Manager block. | ||
* The block acts as a container for managing modals and their triggers. | ||
* It includes functionality for setting up modals and dynamically handling | ||
* their content and visibility, providing users with an interactive experience. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @return {JSX.Element} The rendered React component for editing the block. | ||
*/ | ||
registerBlockType(metadata, { | ||
edit, | ||
save: () => { | ||
return ( | ||
<div {...useBlockProps.save()}> | ||
<InnerBlocks.Content /> | ||
</div> | ||
); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* WordPress dependencies. | ||
*/ | ||
import { store, getElement } from '@wordpress/interactivity'; | ||
|
||
store('gatherpress/modal', { | ||
actions: { | ||
openModal(event) { | ||
event.preventDefault(); | ||
|
||
const element = getElement(); | ||
const modalManager = element.ref.closest( | ||
'.wp-block-gatherpress-modal-manager' | ||
); | ||
|
||
if (modalManager) { | ||
const modal = modalManager.querySelector( | ||
'.wp-block-gatherpress-modal' | ||
); | ||
|
||
if (modal) { | ||
modal.classList.add('gatherpress--is-visible'); | ||
} | ||
} | ||
}, | ||
closeModal(event) { | ||
event.preventDefault(); | ||
|
||
const element = getElement(); | ||
const modalManager = element.ref.closest( | ||
'.wp-block-gatherpress-modal-manager' | ||
); | ||
|
||
if (modalManager) { | ||
const modal = modalManager.querySelector( | ||
'.wp-block-gatherpress-modal' | ||
); | ||
|
||
if (modal) { | ||
modal.classList.remove('gatherpress--is-visible'); | ||
} | ||
} | ||
}, | ||
}, | ||
}); |
Oops, something went wrong.