Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment: use the Interactivity API to do client-side form submissions #49305

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 52 additions & 15 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
"@wordpress/warning": "file:packages/warning",
"@wordpress/widgets": "file:packages/widgets",
"@wordpress/wordcount": "file:packages/wordcount",
"deepsignal": "1.3.0",
"preact": "10.13.1",
"wicg-inert": "3.1.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"sideEffects": [
"build-style/**",
"src/**/*.scss",
"{src,build,build-module}/*/init.js"
"{src,build,build-module}/**/init.js"
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
],
"dependencies": {
"@babel/runtime": "^7.16.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/block-library/src/post-comments-form/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"usesContext": [ "postId", "postType" ],
"supports": {
"interactivity": true,
"anchor": true,
"html": false,
"color": {
Expand Down Expand Up @@ -44,5 +45,6 @@
"wp-block-post-comments-form",
"wp-block-buttons",
"wp-block-button"
]
],
"viewScript": [ "file:./view.min.js" ]
}
10 changes: 8 additions & 2 deletions packages/block-library/src/post-comments-form/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,17 @@ function render_block_core_post_comments_form( $attributes, $content, $block ) {
// to the block is carried along when the comment form is moved to the location
// of the 'Reply' link that the user clicked by Core's `comment-reply.js` script.
$form = str_replace( 'class="comment-respond"', $wrapper_attributes, $form );
$form = str_replace( '</form>', '<div style="color: red;" data-wp-text="state.core.commentsFormError"></div></form>', $form );
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, there's a comment_form_after action that can be used to render stuff here. (Though I'm not sure it's worth using it here as it'd require adding the action before the comment_form() call and removing it after.)

Copy link
Contributor

Choose a reason for hiding this comment

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

On second thought -- if we're not married to this exact location (i.e. right before the </form>, which means after the 'Submit' button), we might also use one of the (many) options to the comment_form function. There is e.g. comment_notes_after which is rendered after the textarea (before the 'Submit' button), and which defaults to the empty string.

Copy link
Member

@westonruter westonruter Mar 29, 2023

Choose a reason for hiding this comment

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

This brings to mind something important: it could be that when the user submits the form, the button could be positioned at the bottom of the viewport. In such a case, the injected error message container may not be visible when it gets populated. The user could then be very frustrated and rage tap the submit button confounded as to why nothing is happening. Perhaps the error message should get displayed as a snackbar with position:fixed or else scrolled into view if not visible.

Copy link
Member

Choose a reason for hiding this comment

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

The snackbar seems like a great idea. The message would also have to get announced when using assistive technologies. In both cases when it’s successful, or it fails.

Copy link
Member Author

Choose a reason for hiding this comment

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

I like the snackbar idea! We can try it to see how it feels and to see how easy it would be to create one with the Interactivity API 🙂


// Enqueue the comment-reply script.
wp_enqueue_script( 'comment-reply' );
// wp_enqueue_script( 'comment-reply' );

return $form;
$tags = new WP_HTML_Tag_Processor( $form );

$tags->next_tag( array( 'tag_name' => 'FORM', 'id' => 'commentform' ) );
$tags->set_attribute( 'data-wp-on.submit', 'actions.core.commentsFormSubmission' );

return $tags->get_updated_html();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const csnMetaTagItemprop = 'wp-client-side-navigation';
export const componentPrefix = 'wp-';
export const directivePrefix = 'data-wp-';
Loading