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

WIP - Try/hovered template part tracking #25382

Closed
wants to merge 5 commits into from

Conversation

Addison-Stavlo
Copy link
Contributor

@Addison-Stavlo Addison-Stavlo commented Sep 16, 2020

Description

Exploration to go along with #25320 (alternative approaches discussed in #25469 - more detail on this approach there as "Solution 2").

How has this been tested?

Screenshots

Types of changes

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

Comment on lines 1654 to 1657
export function documentSettingsDropdown(
state = { hoveredEntities: [] },
action
) {
Copy link
Contributor Author

@Addison-Stavlo Addison-Stavlo Sep 16, 2020

Choose a reason for hiding this comment

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

This extra level may not be necessary, but I was thinking it may be helpful if we need other items tracked in central state than just the hovered items. (Ex - Dectecting if a template part has an inner block selected may be easier from the template parts end, since we could dispatch the selected template part block's ID to this store given a hasSelectedInnerBlock in the edit component itself.)

Also, this may not be the correct store.. perhaps a different store or a new one specifically for the document settings dropdown may make more sense?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I could definitely see needing a store for document settings at some point. I also agree that it probably isn't the right store. Depending on how things go, I think a nested reducer in the editor stores might be the most obvious place 🤔

Comment on lines +107 to +110
<div
onMouseEnter={ () => addHoveredEntity( clientId ) }
onMouseLeave={ () => removeHoveredEntity( clientId ) }
>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This didn't work on the BlockWrapper, the onMouseEnter worked there, but onMouseLeave was getting overridden.

Adding this ad-hoc doesn't seem bad given we don't have many entities we need to highlight in the document settings dropdown. However, if we did wan't this functionality to be more reusable maybe we could add these handlers via a supports hook similar to the style hooks? 🤔

Comment on lines 82 to 93
const { hoveredEntityIds, getBlock } = useSelect( ( select ) => {
const { getHoveredEntities, getBlock } = select( 'core/block-editor' );

return {
hoveredEntityIds: getHoveredEntities(),
getBlock,
};
} );

const hoveredName =
getBlock( hoveredEntityIds[ 0 ] )?.attributes.slug || '';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is all that was changed in this file, please disregard the extra linting changes below.

Similarly, we would try to update this to show the title instead of the slug where available.

@github-actions
Copy link

github-actions bot commented Sep 16, 2020

Size Change: +306 B (0%)

Total Size: 1.2 MB

Filename Size Change
build/block-library/index.js 135 kB +90 B (0%)
build/components/index.js 201 kB -7 B (0%)
build/edit-site/index.js 19.2 kB +223 B (1%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.14 kB 0 B
build/annotations/index.js 3.67 kB 0 B
build/api-fetch/index.js 3.41 kB 0 B
build/autop/index.js 2.83 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 8.53 kB 0 B
build/block-directory/style-rtl.css 943 B 0 B
build/block-directory/style.css 942 B 0 B
build/block-editor/index.js 128 kB 0 B
build/block-editor/style-rtl.css 11.1 kB 0 B
build/block-editor/style.css 11.1 kB 0 B
build/block-library/editor-rtl.css 8.59 kB 0 B
build/block-library/editor.css 8.59 kB 0 B
build/block-library/style-rtl.css 7.6 kB 0 B
build/block-library/style.css 7.59 kB 0 B
build/block-library/theme-rtl.css 741 B 0 B
build/block-library/theme.css 741 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 47.8 kB 0 B
build/components/style-rtl.css 15.5 kB 0 B
build/components/style.css 15.4 kB 0 B
build/compose/index.js 9.68 kB 0 B
build/core-data/index.js 12.2 kB 0 B
build/data-controls/index.js 1.28 kB 0 B
build/data/index.js 8.55 kB 0 B
build/date/index.js 31.9 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 4.48 kB 0 B
build/edit-navigation/index.js 10.7 kB 0 B
build/edit-navigation/style-rtl.css 868 B 0 B
build/edit-navigation/style.css 871 B 0 B
build/edit-post/index.js 305 kB 0 B
build/edit-post/style-rtl.css 6.24 kB 0 B
build/edit-post/style.css 6.22 kB 0 B
build/edit-site/style-rtl.css 3.13 kB 0 B
build/edit-site/style.css 3.13 kB 0 B
build/edit-widgets/index.js 16.4 kB 0 B
build/edit-widgets/style-rtl.css 2.75 kB 0 B
build/edit-widgets/style.css 2.75 kB 0 B
build/editor/editor-styles-rtl.css 492 B 0 B
build/editor/editor-styles.css 493 B 0 B
build/editor/index.js 45.3 kB 0 B
build/editor/style-rtl.css 3.8 kB 0 B
build/editor/style.css 3.8 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.71 kB 0 B
build/format-library/style-rtl.css 547 B 0 B
build/format-library/style.css 548 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 621 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 711 B 0 B
build/keyboard-shortcuts/index.js 2.52 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.12 kB 0 B
build/list-reusable-blocks/style-rtl.css 476 B 0 B
build/list-reusable-blocks/style.css 476 B 0 B
build/media-utils/index.js 5.32 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.4 kB 0 B
build/nux/style-rtl.css 671 B 0 B
build/nux/style.css 668 B 0 B
build/plugins/index.js 2.56 kB 0 B
build/primitives/index.js 1.41 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 13.9 kB 0 B
build/server-side-render/index.js 2.77 kB 0 B
build/shortcode/index.js 1.69 kB 0 B
build/token-list/index.js 1.27 kB 0 B
build/url/index.js 4.06 kB 0 B
build/viewport/index.js 1.85 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

@Addison-Stavlo
Copy link
Contributor Author

Overall I am thinking I like this approach better than the previous, just given that we aren't adding an unnecessary handler to every block and our parsing on the header side can be a bit more simple and run less often.

'Add block',
'Generic label for block inserter button'
<>
<div className="edit-site-header">{ hoveredName }</div>
Copy link
Contributor Author

@Addison-Stavlo Addison-Stavlo Sep 16, 2020

Choose a reason for hiding this comment

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

Again this just just the temporary ugly thing to show the hovered name for now. 😆 lines below this are unchanged.

Copy link
Member

@noahtallen noahtallen left a comment

Choose a reason for hiding this comment

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

It's so hard to choose! Some thoughts:

  • I do like that this is a more limited change.
  • I don't think the block-editor store makes sense here. If we are making a publicly accessible API for block hover states, then I definitely think that the other PR is better in that regard.
  • I'm not a big fan of having to add hover handler wrappers to every block we want to add this too..

I wonder if a combination would be better? Like letting blocks opt-in to hover tracking, and then using the store from the other PR?

I think if we do end up going this route, it might make sense to just use some reducers/actions in edit-site for the time being. (Since we aren't designing it around a general use case, but something specific to us..)

@@ -1106,3 +1106,17 @@ export function setHasControlledInnerBlocks(
clientId,
};
}

export function addHoveredEntity( clientId ) {
Copy link
Member

Choose a reason for hiding this comment

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

I feel like we want to avoid mingling the "entity" concept with the block editor. I think the idea of an "inner block controller" is similar (the "inner block controller" is more of an "entity block controller", but the entity stuff is outside of the scope of the block-editor package.)

@Addison-Stavlo
Copy link
Contributor Author

Addison-Stavlo commented Sep 16, 2020

I don't think the block-editor store makes sense here.

Agreed.

I'm not a big fan of having to add hover handler wrappers to every block we want to add this too..

How many blocks do we really need to add this to? Template Part and maybe Post Content seems to be the consensus currently, so it doesn't seem too horrible.

I wonder if a combination would be better? Like letting blocks opt-in to hover tracking,

If we did see the need to make this more reusable, I could see allowing blocks to 'opt-in' through the supports field in the blocks.json. Then these handlers could potentially be added with a hook similar to the style hooks. Would that make sense as a way to create the opt-in? but is that something we should currently be pursuing given there aren't plans to use this sort of thing beyond 1 or 2 blocks?

and then using the store from the other PR?

Im not following why the store from the other PR would be more acceptable? There doesn't seem to be much of a need for vague hover tracking, and if it is opt-in only then coming across selectors like getHoveredBlocks could be a bit misleading.

@noahtallen
Copy link
Member

Im not following why the store from the other PR would be more acceptable

I think the thing is, when we have a selector getHoveredBlock, a public consumer (e.g. plugin developer) would expect it to work with any hovered block. I can imagine wanting that as a plugin developer myself, if I was working on something to do with hovers...

My main point is that if we created getHoveredBlock with an implementation like this doesn't really hold true. It would return nothing most of the time, which isn't what you'd expect as a developer trying to use that kind of selector.

I think it's different if this was instead setTobBarHoverItem or something more explicit like that. Because the "contract" for that is a lot more straightforward and explicit than setting the hovered block. And there's no other expectation for what it does.

I think if we want to be general and do a block-editor-level store to contain this information, then we should go all in, and track every block, so that it actually is general for arbitrary blocks.

I think if we want it to be not general and only specific to what we need, maybe it makes sense to have it actually be tightly-coupled with the site editor store for now.

Does that make sense at all?

@Addison-Stavlo
Copy link
Contributor Author

Does that make sense at all?

Totally! Thanks for elaborating. Something like setToolbarHoverItem would be a much better term (I often struggle with naming things 🤣 ). What would it make sense to call this thing? setDocumentSettingsDropdownHoverItem seems pretty long. lol, but it is more explicit. Im not sure if 'toolbar' totally makes sense either, but it is better than the vague term I am using.

Im happy coupling it to the site editor for now. Going all in and adding tracking to every block could be cumbersome, unnecessary, and/or stall us from moving forward as it is a bit of a larger change that effects a lot more than just the template part and the site editor. While it is neat to be able to track the all hovered items from external components, I don't think there is a large demand for this ability?

@noahtallen
Copy link
Member

While it is neat to be able to track the all hovered items from external components, I don't think there is a large demand for this ability?

🤷 Not sure!

Im happy coupling it to the site editor for now

Yeah, I think I am too. The only unknown is: I'm not sure how common or "best practice" it is to hook blocks to the editor UI like this.

setDocumentSettingsDropdownHoverItem seems pretty long

true! How about setThatLittleBitOfTextThatShowsUpUnderTheDocumentTitle,ButI'mOnlyHoveringItForNow? 😆

Maybe.. setDocumentActionsSecondaryItem or setDocumentActionsLabel or setDocumentActionsSublabel? Names are definitely hard!

@Addison-Stavlo
Copy link
Contributor Author

Yeah, I think I am too. The only unknown is: I'm not sure how common or "best practice" it is to hook blocks to the editor UI like this.

Its not. There may be some grief over it, but it is non-breaking and compatible with both editors. 🤷‍♀️ Maybe we could set up our own little store core/document-dropdown or something similar. Or maybe there is a more fitting store, core/interface sounds good but I remember it as not being fitting for this kind of thing either.

setThatLittleBitOfTextThatShowsUpUnderTheDocumentTitle,ButI'mOnlyHoveringItForNow

Sounds good to me! setDocumentActionsHoveredLabel maybe? or setDocumentActionsPrimarySublabel making sense as the 'primary sublabel' is for hovered items of interest and 'secondary sublabel' for selected items of interest.

@jeyip
Copy link
Contributor

jeyip commented Sep 18, 2020

The behavior here works as described in Chrome, Edge, Safari, and IE11. ⭐️

There's a little bit of wonkiness in Firefox where initially hovering a template part causes the refresh icon in the browser toolbar to quickly toggle between "refresh" and "cancel refresh". 🤷‍♂️ Probably not your PR, but it is a little distracting.

@jeyip
Copy link
Contributor

jeyip commented Sep 18, 2020

I think if we want to be general and do a block-editor-level store to contain this information, then we should go all in, and track every block, so that it actually is general for arbitrary blocks.

I think if we want it to be not general and only specific to what we need, maybe it makes sense to have it actually be tightly-coupled with the site editor store for now.

I don't have much to add to the conversation, but I agree with the direction of the PR.

Im happy coupling it to the site editor for now. Going all in and adding tracking to every block could be cumbersome, unnecessary, and/or stall us from moving forward as it is a bit of a larger change that effects a lot more than just the template part and the site editor.

Agreed!

@Addison-Stavlo
Copy link
Contributor Author

Closing this exploration, as we will work out the solution in #25348

@Addison-Stavlo Addison-Stavlo self-assigned this Sep 25, 2020
@aristath aristath deleted the try/hovered-template-part-tracking branch November 10, 2020 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants