Skip to content

Add link to analytics options in submenu and make all settings sections linkable#5216

Merged
westonruter merged 35 commits intodevelopfrom
feature/5190-settings-screen-anchor-links
Aug 14, 2020
Merged

Add link to analytics options in submenu and make all settings sections linkable#5216
westonruter merged 35 commits intodevelopfrom
feature/5190-settings-screen-anchor-links

Conversation

@johnwatkins0
Copy link
Copy Markdown
Contributor

@johnwatkins0 johnwatkins0 commented Aug 10, 2020

Summary

Fixes #5190

In #5155 we removed the Analytics menu page and placed the analytics option on the settings screen. This restores that menu item with a link to the Analytics section of the menu page.

Additional changes:

  • Passed the URL hash into the root settings screen component so that it can be used to scroll to sections after the UI loads
  • Moved the drawers on the settings screen up to the root settings screen component so that all the handling of IDs/anchors can be in one file
  • Created an AnalyticsOptionsSubmenu service class, which is a stripped-down version of the class that was removed in Add Analytics option to settings screen #5155. It inserts a link into the AMP admin menu in the second position.
  • Created a get_menu_slug method in the OptionsMenu class for consumption in AnalyticsOptionsSubmenu.

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@google-cla google-cla bot added the cla: yes Signed the Google CLA label Aug 10, 2020
@johnwatkins0 johnwatkins0 marked this pull request as ready for review August 11, 2020 15:31
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Aug 11, 2020

Plugin builds for 249fb03 are ready 🛎️!

@johnwatkins0 johnwatkins0 added the WS:UX Work stream for UX/Front-end label Aug 11, 2020
…190-settings-screen-anchor-links

* 'develop' of github.com:ampproject/amp-wp:
  Revert "Replace astra with neve for testing since astra is suspended"
  Unset `$_SERVER['REQUEST_URI']`
  Fix lint error
  Default to localhost if `$_SERVER['HTTP_HOST']` is not set
  Remove check for WP CLI context
  Default parsed URL host to localhost if in WP CLI context
  Default parsed URL host to localhost if in WP CLI context
  Add null as return type
  Return null if the URL cannot be parsed when in a WP CLI environment
  Padding: adjust padding on mobile-width close button
  Ensure non-AMP link in paired browsing does not redirect to AMP version
  Update dependency @wordpress/data to v4.22.3
  Update ReaderThemes::get_themes() to remove any themes that lack slug
  Also make name optional, falling back to slug
  Make theme description optional for Reader themes
  Make homepage a optional field for Reader themes
  Wizard: move close button to the top in mobile view
  Defer mobile redirection to end of template_redirect action
  Fix test referencing core theme
  Abort post-processing if an expected template was not rendered
* Settings page application root.
*
* @param {Object} props Component props.
* @param {string} props.section The initially focused section.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe focusedSection would be more clear?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in e41c971

const focusedSection = document.getElementById( section );

if ( focusedSection ) {
focusedSection.scrollIntoView();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would smooth scrolling be desirable?

Suggested change
focusedSection.scrollIntoView();
focusedSection.scrollIntoView( { behavior: 'smooth' } );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Alternatively, for accessibility, I think what is needed is to actually focus() on the first focusable element.

So this should be looking for the first button or input that is inside the focusedSection and then focus() on it. This will automatically scroll and add the necessary keyboard focus.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in e41c971d9 with select elements also included

[ $old_menu[0] ],
[
[
__( 'AMP Analytics Options', 'amp' ),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This needs to be shortened to just Analytics:

image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in b28aa90

array_slice( $old_menu, 1 )
);

$submenu[ $this->parent_menu_slug ] = array_values( $new_menu ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should not $submenu be modified with add_submenu_page() rather than doing this and the above?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, you're right. Updated in 0299a03. Didn't realize it would be so simple to link to the parent menu page with a hash appended.

<ErrorContextProvider>
<Providers>
<Root optionsRestPath={ OPTIONS_REST_PATH } />
<Root section={ global.location.hash.replace( /^#/, '' ) } />
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this can be improved… namely, if I am already on the AMP Settings screen and click the admin menu item, then I am jumped down the screen without the section expanding. Therefore, it would be preferable if there was also a hashchange event that would re-render this tree whenever it fires. This will ensure that the section always reflects what is currently in the location.hash.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I did this in 754aadd but it was surprisingly complicated to get it to work in all the scenarios (initial load of the page, changing the hash in the URL location bar, clicking the Analytics link in the menu), and I'm not sure how I feel about how much code I had to add. It's working, though.

Comment on lines +98 to +100
*
* @param {Object} props Component props.
* @param {string} props.focusedSection The initially focused focusedSection.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
*
* @param {Object} props Component props.
* @param {string} props.focusedSection The initially focused focusedSection.

Comment on lines +224 to +226
Root.propTypes = {
focusedSection: PropTypes.string,
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Root.propTypes = {
focusedSection: PropTypes.string,
};

<h2 id="advanced-settings">
{ __( 'Advanced Settings', 'amp' ) }
</h2>
<MobileRedirection />
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not put the ID for the section here instead of inside of the component?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I like having all the IDs at this level. Updated in 5e7b5de

id="supported-templates"
initialOpen={ 'supported-templates' === focusedSection }
>
<SupportedTemplates />
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same question as above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In this case the ID is set just above on the AMPDrawer component.

import { getPageError } from '@wordpress/e2e-test-utils';

function createURLWithHash( WPPath, query = '', hash = '' ) {
const url = new URL( 'http://localhost:8890' );
Copy link
Copy Markdown
Contributor

@pierlon pierlon Aug 13, 2020

Choose a reason for hiding this comment

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

This URL should be available via the environment variable WP_BASE_URL, so no need to hard code it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in 92ec1f3

* @param {string} query String to be serialized as query portion of URL.
* @param {string} hash URL hash.
*/
export async function visitAdminPageWithHash( adminPath, query, hash = null ) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If defaulting hash to null is still wanted I'd recommend updating the docblock to include null, otherwise I'd recommend defaulting to an empty string:

Suggested change
export async function visitAdminPageWithHash( adminPath, query, hash = null ) {
export async function visitAdminPageWithHash( adminPath, query, hash = '' ) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in 92ec1f3

johnwatkins0 and others added 2 commits August 12, 2020 19:59
Co-authored-by: Pierre Gordon <16200219+pierlon@users.noreply.github.com>
* @param WP_Styles $wp_styles The WP_Styles instance for the current page.
*/
public function register_shimmed_styles( $wp_styles ) {
if ( version_compare( get_bloginfo( 'version' ), '5.4', '<' ) ) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unrelated change: Noticed some undesirable CSS effects when working in 5.2 locally, so this will force the polyfilled wp-components styles. Note this only runs on AMP screens.

@google-cla
Copy link
Copy Markdown

google-cla bot commented Aug 14, 2020

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added cla: no Has not signed the Google CLA and removed cla: yes Signed the Google CLA labels Aug 14, 2020
@pierlon
Copy link
Copy Markdown
Contributor

pierlon commented Aug 14, 2020

@googlebot I consent.

@pierlon
Copy link
Copy Markdown
Contributor

pierlon commented Aug 14, 2020

Some tests were modifying the global $submenu without restoring it's original value, so I've fixed that in 70e517b. I've also updated OptionsMenuTest::test_add_menu_items to perform the correct assertions.

@google-cla google-cla bot added cla: yes Signed the Google CLA and removed cla: no Has not signed the Google CLA labels Aug 14, 2020
@pierlon
Copy link
Copy Markdown
Contributor

pierlon commented Aug 14, 2020

On a larger viewport the scroll to the Analytics section doesn't seem to have much have an effect:

@johnwatkins0
Copy link
Copy Markdown
Contributor Author

On a larger viewport the scroll to the Analytics section doesn't seem to have much have an effect:

That's true, but it's just because the analytics section is last on the page so it can't scroll any further than that. I suppose the workarounds would be to move that section up or add a bit of extra padding to the bottom of the page.

@pierlon
Copy link
Copy Markdown
Contributor

pierlon commented Aug 14, 2020

Could we focus the drawer when selected by hash? Similar to how GitHub does it, where if I link to a comment it adds a border around it:

image

@westonruter
Copy link
Copy Markdown
Member

Could we focus the drawer when selected by hash? Similar to how GitHub does it, where if I link to a comment it adds a border around it:

I'd say that would be an optional enhancement. GitHub styles this via:

:target .timeline-comment {
    border-color: #2188ff;
    box-shadow: 0 0 0 0.2em #c8e1ff;
}

However, the drawers depend on box-shadow to achieve their effect. If we do any such highlighting, I'd say perhaps we go with an initial faint yellow that fades out (e.g. via a CSS animation).

But yeah, I'd say this is not really necessary. After all, the first focusable element will have the outline. I suppose the the alternative would be remove the :not(.components-panel__body-toggle) so that the drawer button gets the border which would have a similar effect to what GitHub is doing:

image

--- a/assets/src/settings-page/index.js
+++ b/assets/src/settings-page/index.js
@@ -105,7 +105,7 @@ function scrollFocusedSectionIntoView( focusedSectionId ) {
 		return;
 	}
 
-	const firstInput = focusedSectionElement.querySelector( 'input, select, textarea, button:not(.components-panel__body-toggle)' );
+	const firstInput = focusedSectionElement.querySelector( 'input, select, textarea, button' );
 
 	if ( firstInput ) {
 		firstInput.focus();

@westonruter
Copy link
Copy Markdown
Member

I noticed that when going to /wp-admin/admin.php?page=amp-options#reader-themes-drawer it doesn't cause the Reader theme drawer to open initially.

@johnwatkins0
Copy link
Copy Markdown
Contributor Author

a856c71 adds scrolling to the reader themes drawer if it's on the page (note I changed that HTML ID from reader-themes-drawer to reader-themes). I also removed that :not(.components-panel__body-toggle) in the selector, so the toggle button will always be focused for these drawers.

Another thing, I noticed that element.scrollIntoView({behavior: smooth}) seemed to work much more reliably than focusElement.focus({behavior: smooth}) so I updated it to use scrollIntoView on the main element and then focus the focus element without smooth scroll.

@westonruter
Copy link
Copy Markdown
Member

focusElement.focus({behavior: smooth})

Oh, I didn't even the focus() method as supporting {behavior:smooth}: https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/focus

So, good to switch to scrollIntoView 👍

}

#reader-themes-drawer {
#reader-themes {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for making this change. I was going to suggest it but I didn't want to nit pick… too much. 😊

@johnwatkins0
Copy link
Copy Markdown
Contributor Author

Oh, I didn't even the focus() method as supporting {behavior:smooth}:

Huh, you're right. I could've sworn I learned this was a thing recently, but I'm no longer able to find it 🤷‍♂️ No wonder it didn't work.

@westonruter
Copy link
Copy Markdown
Member

For some reason deeplinking to #reader-themes is not causing scrolling to occur. If I try navigating in a new tab to #analytics I get scrolled down, but not if I go to #reader-themes. If, however, I put the scroll logic in a 100ms timeout then the Reader Themes will scroll into view. It's strange because the Reader Themes section does exist in the DOM and it is being rendered. Not a blocker since this section is near the top anyway and it is still getting expanded, but it is still a mystery.

@westonruter westonruter added this to the v2.0 milestone Aug 14, 2020
@westonruter westonruter merged commit b246864 into develop Aug 14, 2020
@westonruter westonruter deleted the feature/5190-settings-screen-anchor-links branch August 14, 2020 19:46
@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Aug 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelogged Whether the issue/PR has been added to release notes. cla: yes Signed the Google CLA WS:UX Work stream for UX/Front-end

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Restore Analytics admin menu item to link to jump to expanded Analytics drawer on settings screen

4 participants