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

[RNMobile] Introduce Context Constants to Native #35510

Closed

Conversation

SiobhyB
Copy link
Contributor

@SiobhyB SiobhyB commented Oct 11, 2021

Gutenberg Mobile: wordpress-mobile/gutenberg-mobile#4099

Description

The Gallery block was recently refactored, for both the web and the mobile (native) apps, in order to pull in image blocks via the InnerBlocks API.

As part of this refactor for the mobile apps, the following native-specific block contexts were introduced in order to style the gallery's inner image blocks:

Similarly, as described in #33531, an allowResize context was introduced on the web-side to disable the image block's resize option for galleries.


Problem: Additional contexts for customizing child blocks are going to be necessary.

On the native side, for example, the settings icon is included within the block, unlike on the web, where the block's settings are separated out into the sidebar. The ability to hide the settings icon is therefore likely to be a fairly common use case for parent blocks. There are also likely to be other valid use cases for customizing inner blocks in the future.

Adding additional contexts isn't an ideal or scaleable solution. This has also been discussed in #33531, with some suggestions outlined there.


Proposal: With this PR, the idea of introducing context constants, as initially suggested here, is built upon.

usesContextConstants is provided as an alternative to usesContext and providesContextConstants as an alternative to providesContext.

The aim is to explicitly indicate the purpose of these contexts and that they aren't serialised, as would normally be the case with contexts. It is also hoped that the introduction of context constants will make it easier to add these types of contexts in the future.


Note: This PR is native-specific but a separate web-specific PR can be made, building on the work done here, if approved.

How has this been tested?

Prequisite: Enable the flag for the refactored Gallery block. on mobile. At the time of writing, the way to enable the block on the Gutenberg Mobile demo app is to comment out this section of code.

With the refactored Gallery block enabled, we should ensure that the changes introduced with this PR don't have any adverse effects:

  • Navigate to the editor within either the Android or iOS app and add the Gallery block.
  • Add a selection of images to the block. Verify that the images appear, which will in turn verify that the new fixedHeight context constant is working and hasn't caused any regressions (no images would be visible if the context wasn't working).
  • Select the cog/gear icon to access the block's settings. Toggle the "crop images" tab on and off to verify that the images within the gallery change from being cropped, to not. This will confirm that the imageCrop context is still working as expected.

Screenshots

The following screencast shows the images going from cropped to uncropped, as expected, to show the imageCrop context is working as intended following the changes in this PR:

Screen.Recording.2021-10-11.at.17.57.15.mov

Types of changes

This PR includes changes to code and the overall approach that's taken to adding contexts when needed to change inner blocks on the native-side, with the following high-level overview:

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • 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 (please manually search all *.native.js files for terms that need renaming or removal).

With this commit, the 'packages/blocks/src/api/registration.js'  file is duplicated into a 'native' version. This is necessary as we'll be making native-specific changes to the metadata that's registered alongside a block.
@SiobhyB SiobhyB added [Feature] Nested / Inner Blocks Anything related to the experience of nested/inner blocks inside a larger container, like Group or P Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change) [Block] Image Affects the Image Block [Block] Gallery Affects the Gallery Block - used to display groups of images labels Oct 11, 2021
@github-actions
Copy link

github-actions bot commented Oct 11, 2021

Size Change: +59 B (0%)

Total Size: 1.08 MB

Filename Size Change
build/block-editor/index.min.js 135 kB +15 B (0%)
build/block-library/index.min.js 155 kB +23 B (0%)
build/blocks/index.min.js 46 kB +21 B (0%)
ℹ️ View Unchanged
Filename Size
build/a11y/index.min.js 931 B
build/admin-manifest/index.min.js 1.09 kB
build/annotations/index.min.js 2.7 kB
build/api-fetch/index.min.js 2.21 kB
build/autop/index.min.js 2.08 kB
build/blob/index.min.js 459 B
build/block-directory/index.min.js 6.2 kB
build/block-directory/style-rtl.css 1.01 kB
build/block-directory/style.css 1.01 kB
build/block-editor/default-editor-styles-rtl.css 378 B
build/block-editor/default-editor-styles.css 378 B
build/block-editor/style-rtl.css 14.1 kB
build/block-editor/style.css 14 kB
build/block-library/blocks/archives/editor-rtl.css 61 B
build/block-library/blocks/archives/editor.css 60 B
build/block-library/blocks/archives/style-rtl.css 65 B
build/block-library/blocks/archives/style.css 65 B
build/block-library/blocks/audio/editor-rtl.css 58 B
build/block-library/blocks/audio/editor.css 58 B
build/block-library/blocks/audio/style-rtl.css 111 B
build/block-library/blocks/audio/style.css 111 B
build/block-library/blocks/audio/theme-rtl.css 125 B
build/block-library/blocks/audio/theme.css 125 B
build/block-library/blocks/block/editor-rtl.css 161 B
build/block-library/blocks/block/editor.css 161 B
build/block-library/blocks/button/editor-rtl.css 470 B
build/block-library/blocks/button/editor.css 470 B
build/block-library/blocks/button/style-rtl.css 560 B
build/block-library/blocks/button/style.css 560 B
build/block-library/blocks/buttons/editor-rtl.css 309 B
build/block-library/blocks/buttons/editor.css 309 B
build/block-library/blocks/buttons/style-rtl.css 317 B
build/block-library/blocks/buttons/style.css 317 B
build/block-library/blocks/calendar/style-rtl.css 207 B
build/block-library/blocks/calendar/style.css 207 B
build/block-library/blocks/categories/editor-rtl.css 84 B
build/block-library/blocks/categories/editor.css 83 B
build/block-library/blocks/categories/style-rtl.css 79 B
build/block-library/blocks/categories/style.css 79 B
build/block-library/blocks/code/style-rtl.css 90 B
build/block-library/blocks/code/style.css 90 B
build/block-library/blocks/code/theme-rtl.css 131 B
build/block-library/blocks/code/theme.css 131 B
build/block-library/blocks/columns/editor-rtl.css 206 B
build/block-library/blocks/columns/editor.css 205 B
build/block-library/blocks/columns/style-rtl.css 497 B
build/block-library/blocks/columns/style.css 496 B
build/block-library/blocks/cover/editor-rtl.css 546 B
build/block-library/blocks/cover/editor.css 547 B
build/block-library/blocks/cover/style-rtl.css 1.17 kB
build/block-library/blocks/cover/style.css 1.17 kB
build/block-library/blocks/embed/editor-rtl.css 488 B
build/block-library/blocks/embed/editor.css 488 B
build/block-library/blocks/embed/style-rtl.css 417 B
build/block-library/blocks/embed/style.css 417 B
build/block-library/blocks/embed/theme-rtl.css 124 B
build/block-library/blocks/embed/theme.css 124 B
build/block-library/blocks/file/editor-rtl.css 300 B
build/block-library/blocks/file/editor.css 300 B
build/block-library/blocks/file/style-rtl.css 255 B
build/block-library/blocks/file/style.css 255 B
build/block-library/blocks/file/view.min.js 322 B
build/block-library/blocks/freeform/editor-rtl.css 2.44 kB
build/block-library/blocks/freeform/editor.css 2.44 kB
build/block-library/blocks/gallery/editor-rtl.css 977 B
build/block-library/blocks/gallery/editor.css 982 B
build/block-library/blocks/gallery/style-rtl.css 1.6 kB
build/block-library/blocks/gallery/style.css 1.59 kB
build/block-library/blocks/gallery/theme-rtl.css 122 B
build/block-library/blocks/gallery/theme.css 122 B
build/block-library/blocks/group/editor-rtl.css 159 B
build/block-library/blocks/group/editor.css 159 B
build/block-library/blocks/group/style-rtl.css 57 B
build/block-library/blocks/group/style.css 57 B
build/block-library/blocks/group/theme-rtl.css 78 B
build/block-library/blocks/group/theme.css 78 B
build/block-library/blocks/heading/style-rtl.css 114 B
build/block-library/blocks/heading/style.css 114 B
build/block-library/blocks/home-link/style-rtl.css 247 B
build/block-library/blocks/home-link/style.css 247 B
build/block-library/blocks/html/editor-rtl.css 332 B
build/block-library/blocks/html/editor.css 333 B
build/block-library/blocks/image/editor-rtl.css 731 B
build/block-library/blocks/image/editor.css 730 B
build/block-library/blocks/image/style-rtl.css 502 B
build/block-library/blocks/image/style.css 505 B
build/block-library/blocks/image/theme-rtl.css 124 B
build/block-library/blocks/image/theme.css 124 B
build/block-library/blocks/latest-comments/style-rtl.css 284 B
build/block-library/blocks/latest-comments/style.css 284 B
build/block-library/blocks/latest-posts/editor-rtl.css 137 B
build/block-library/blocks/latest-posts/editor.css 137 B
build/block-library/blocks/latest-posts/style-rtl.css 528 B
build/block-library/blocks/latest-posts/style.css 527 B
build/block-library/blocks/list/style-rtl.css 94 B
build/block-library/blocks/list/style.css 94 B
build/block-library/blocks/media-text/editor-rtl.css 266 B
build/block-library/blocks/media-text/editor.css 263 B
build/block-library/blocks/media-text/style-rtl.css 493 B
build/block-library/blocks/media-text/style.css 490 B
build/block-library/blocks/more/editor-rtl.css 431 B
build/block-library/blocks/more/editor.css 431 B
build/block-library/blocks/navigation-link/editor-rtl.css 642 B
build/block-library/blocks/navigation-link/editor.css 642 B
build/block-library/blocks/navigation-link/style-rtl.css 94 B
build/block-library/blocks/navigation-link/style.css 94 B
build/block-library/blocks/navigation-submenu/editor-rtl.css 299 B
build/block-library/blocks/navigation-submenu/editor.css 299 B
build/block-library/blocks/navigation-submenu/style-rtl.css 195 B
build/block-library/blocks/navigation-submenu/style.css 195 B
build/block-library/blocks/navigation-submenu/view.min.js 343 B
build/block-library/blocks/navigation/editor-rtl.css 1.81 kB
build/block-library/blocks/navigation/editor.css 1.81 kB
build/block-library/blocks/navigation/style-rtl.css 1.71 kB
build/block-library/blocks/navigation/style.css 1.7 kB
build/block-library/blocks/navigation/view.min.js 2.74 kB
build/block-library/blocks/nextpage/editor-rtl.css 395 B
build/block-library/blocks/nextpage/editor.css 395 B
build/block-library/blocks/page-list/editor-rtl.css 377 B
build/block-library/blocks/page-list/editor.css 377 B
build/block-library/blocks/page-list/style-rtl.css 198 B
build/block-library/blocks/page-list/style.css 198 B
build/block-library/blocks/paragraph/editor-rtl.css 157 B
build/block-library/blocks/paragraph/editor.css 157 B
build/block-library/blocks/paragraph/style-rtl.css 273 B
build/block-library/blocks/paragraph/style.css 273 B
build/block-library/blocks/post-author/style-rtl.css 175 B
build/block-library/blocks/post-author/style.css 176 B
build/block-library/blocks/post-comments-form/style-rtl.css 347 B
build/block-library/blocks/post-comments-form/style.css 347 B
build/block-library/blocks/post-comments/style-rtl.css 492 B
build/block-library/blocks/post-comments/style.css 493 B
build/block-library/blocks/post-content/style-rtl.css 56 B
build/block-library/blocks/post-content/style.css 56 B
build/block-library/blocks/post-excerpt/editor-rtl.css 73 B
build/block-library/blocks/post-excerpt/editor.css 73 B
build/block-library/blocks/post-excerpt/style-rtl.css 69 B
build/block-library/blocks/post-excerpt/style.css 69 B
build/block-library/blocks/post-featured-image/editor-rtl.css 396 B
build/block-library/blocks/post-featured-image/editor.css 397 B
build/block-library/blocks/post-featured-image/style-rtl.css 156 B
build/block-library/blocks/post-featured-image/style.css 156 B
build/block-library/blocks/post-template/editor-rtl.css 99 B
build/block-library/blocks/post-template/editor.css 98 B
build/block-library/blocks/post-template/style-rtl.css 391 B
build/block-library/blocks/post-template/style.css 392 B
build/block-library/blocks/post-terms/style-rtl.css 73 B
build/block-library/blocks/post-terms/style.css 73 B
build/block-library/blocks/post-title/style-rtl.css 60 B
build/block-library/blocks/post-title/style.css 60 B
build/block-library/blocks/preformatted/style-rtl.css 103 B
build/block-library/blocks/preformatted/style.css 103 B
build/block-library/blocks/pullquote/editor-rtl.css 198 B
build/block-library/blocks/pullquote/editor.css 198 B
build/block-library/blocks/pullquote/style-rtl.css 378 B
build/block-library/blocks/pullquote/style.css 378 B
build/block-library/blocks/pullquote/theme-rtl.css 167 B
build/block-library/blocks/pullquote/theme.css 167 B
build/block-library/blocks/query-pagination-numbers/editor-rtl.css 122 B
build/block-library/blocks/query-pagination-numbers/editor.css 121 B
build/block-library/blocks/query-pagination/editor-rtl.css 262 B
build/block-library/blocks/query-pagination/editor.css 255 B
build/block-library/blocks/query-pagination/style-rtl.css 234 B
build/block-library/blocks/query-pagination/style.css 231 B
build/block-library/blocks/query/editor-rtl.css 131 B
build/block-library/blocks/query/editor.css 132 B
build/block-library/blocks/quote/style-rtl.css 187 B
build/block-library/blocks/quote/style.css 187 B
build/block-library/blocks/quote/theme-rtl.css 223 B
build/block-library/blocks/quote/theme.css 226 B
build/block-library/blocks/rss/editor-rtl.css 202 B
build/block-library/blocks/rss/editor.css 204 B
build/block-library/blocks/rss/style-rtl.css 289 B
build/block-library/blocks/rss/style.css 288 B
build/block-library/blocks/search/editor-rtl.css 165 B
build/block-library/blocks/search/editor.css 165 B
build/block-library/blocks/search/style-rtl.css 397 B
build/block-library/blocks/search/style.css 398 B
build/block-library/blocks/search/theme-rtl.css 64 B
build/block-library/blocks/search/theme.css 64 B
build/block-library/blocks/separator/editor-rtl.css 99 B
build/block-library/blocks/separator/editor.css 99 B
build/block-library/blocks/separator/style-rtl.css 250 B
build/block-library/blocks/separator/style.css 250 B
build/block-library/blocks/separator/theme-rtl.css 172 B
build/block-library/blocks/separator/theme.css 172 B
build/block-library/blocks/shortcode/editor-rtl.css 474 B
build/block-library/blocks/shortcode/editor.css 474 B
build/block-library/blocks/site-logo/editor-rtl.css 770 B
build/block-library/blocks/site-logo/editor.css 770 B
build/block-library/blocks/site-logo/style-rtl.css 165 B
build/block-library/blocks/site-logo/style.css 165 B
build/block-library/blocks/site-tagline/editor-rtl.css 86 B
build/block-library/blocks/site-tagline/editor.css 86 B
build/block-library/blocks/site-title/editor-rtl.css 84 B
build/block-library/blocks/site-title/editor.css 84 B
build/block-library/blocks/social-link/editor-rtl.css 177 B
build/block-library/blocks/social-link/editor.css 177 B
build/block-library/blocks/social-links/editor-rtl.css 824 B
build/block-library/blocks/social-links/editor.css 823 B
build/block-library/blocks/social-links/style-rtl.css 1.32 kB
build/block-library/blocks/social-links/style.css 1.32 kB
build/block-library/blocks/spacer/editor-rtl.css 307 B
build/block-library/blocks/spacer/editor.css 307 B
build/block-library/blocks/spacer/style-rtl.css 48 B
build/block-library/blocks/spacer/style.css 48 B
build/block-library/blocks/table/editor-rtl.css 471 B
build/block-library/blocks/table/editor.css 472 B
build/block-library/blocks/table/style-rtl.css 481 B
build/block-library/blocks/table/style.css 481 B
build/block-library/blocks/table/theme-rtl.css 188 B
build/block-library/blocks/table/theme.css 188 B
build/block-library/blocks/tag-cloud/style-rtl.css 146 B
build/block-library/blocks/tag-cloud/style.css 146 B
build/block-library/blocks/template-part/editor-rtl.css 560 B
build/block-library/blocks/template-part/editor.css 559 B
build/block-library/blocks/template-part/theme-rtl.css 101 B
build/block-library/blocks/template-part/theme.css 101 B
build/block-library/blocks/text-columns/editor-rtl.css 95 B
build/block-library/blocks/text-columns/editor.css 95 B
build/block-library/blocks/text-columns/style-rtl.css 166 B
build/block-library/blocks/text-columns/style.css 166 B
build/block-library/blocks/verse/style-rtl.css 87 B
build/block-library/blocks/verse/style.css 87 B
build/block-library/blocks/video/editor-rtl.css 571 B
build/block-library/blocks/video/editor.css 572 B
build/block-library/blocks/video/style-rtl.css 173 B
build/block-library/blocks/video/style.css 173 B
build/block-library/blocks/video/theme-rtl.css 124 B
build/block-library/blocks/video/theme.css 124 B
build/block-library/common-rtl.css 815 B
build/block-library/common.css 812 B
build/block-library/editor-rtl.css 9.78 kB
build/block-library/editor.css 9.78 kB
build/block-library/reset-rtl.css 474 B
build/block-library/reset.css 474 B
build/block-library/style-rtl.css 10.5 kB
build/block-library/style.css 10.6 kB
build/block-library/theme-rtl.css 668 B
build/block-library/theme.css 673 B
build/block-serialization-default-parser/index.min.js 1.09 kB
build/block-serialization-spec-parser/index.min.js 2.79 kB
build/components/index.min.js 212 kB
build/components/style-rtl.css 15.4 kB
build/components/style.css 15.4 kB
build/compose/index.min.js 10.9 kB
build/core-data/index.min.js 12.6 kB
build/customize-widgets/index.min.js 11.2 kB
build/customize-widgets/style-rtl.css 1.5 kB
build/customize-widgets/style.css 1.49 kB
build/data-controls/index.min.js 614 B
build/data/index.min.js 7.1 kB
build/date/index.min.js 31.5 kB
build/deprecated/index.min.js 428 B
build/dom-ready/index.min.js 304 B
build/dom/index.min.js 4.44 kB
build/edit-navigation/index.min.js 15.8 kB
build/edit-navigation/style-rtl.css 3.76 kB
build/edit-navigation/style.css 3.76 kB
build/edit-post/classic-rtl.css 492 B
build/edit-post/classic.css 494 B
build/edit-post/index.min.js 29.4 kB
build/edit-post/style-rtl.css 7.12 kB
build/edit-post/style.css 7.12 kB
build/edit-site/index.min.js 30.7 kB
build/edit-site/style-rtl.css 5.79 kB
build/edit-site/style.css 5.79 kB
build/edit-widgets/index.min.js 16.3 kB
build/edit-widgets/style-rtl.css 4.17 kB
build/edit-widgets/style.css 4.18 kB
build/editor/index.min.js 37.7 kB
build/editor/style-rtl.css 3.78 kB
build/editor/style.css 3.77 kB
build/element/index.min.js 3.21 kB
build/escape-html/index.min.js 517 B
build/format-library/index.min.js 6.34 kB
build/format-library/style-rtl.css 571 B
build/format-library/style.css 571 B
build/hooks/index.min.js 1.55 kB
build/html-entities/index.min.js 424 B
build/i18n/index.min.js 3.6 kB
build/is-shallow-equal/index.min.js 501 B
build/keyboard-shortcuts/index.min.js 1.72 kB
build/keycodes/index.min.js 1.3 kB
build/list-reusable-blocks/index.min.js 1.85 kB
build/list-reusable-blocks/style-rtl.css 838 B
build/list-reusable-blocks/style.css 838 B
build/media-utils/index.min.js 2.92 kB
build/notices/index.min.js 845 B
build/nux/index.min.js 2.03 kB
build/nux/style-rtl.css 747 B
build/nux/style.css 743 B
build/plugins/index.min.js 1.83 kB
build/primitives/index.min.js 921 B
build/priority-queue/index.min.js 582 B
build/react-i18n/index.min.js 671 B
build/redux-routine/index.min.js 2.63 kB
build/reusable-blocks/index.min.js 2.19 kB
build/reusable-blocks/style-rtl.css 256 B
build/reusable-blocks/style.css 256 B
build/rich-text/index.min.js 10.7 kB
build/server-side-render/index.min.js 1.52 kB
build/shortcode/index.min.js 1.48 kB
build/token-list/index.min.js 562 B
build/url/index.min.js 1.74 kB
build/viewport/index.min.js 1.02 kB
build/warning/index.min.js 248 B
build/widgets/index.min.js 7.11 kB
build/widgets/style-rtl.css 1.16 kB
build/widgets/style.css 1.16 kB
build/wordcount/index.min.js 1.04 kB

compressed-size-action

Siobhan added 7 commits October 11, 2021 13:57
'providesNativeInnerBlockContext' and 'usesNativeInnerBlockContext' are intended to replace the default 'providesContext' and 'usesContext'. These will be used to indicate times where a context is only used with the apps and constant (usually in use with inner blocks).
Following this commit, native blocks will now be able to use 'providesNativeInnerBlockContext' in order to provide a context to an inner block.
Following this commit, native blocks will now be able to use 'usesNativeInnerBlockContext' in order to use a context provided by a parent block.
As the 'imageCrop' context is only used with the app, it's flagged with 'providesNativeInnerBlockContext' as part of this commit.
Following 65c3dd6, the 'imageCrop' context was flagged to indicate it's only used with the app. This commit builds on that work to reference it in the image block via 'usesNativeInnerBlockContext'.
We can only look up a block's context if 'blockType' is true. This commit therefore adds a check to ensure 'blockType' is true before creating a context object.
As with 5a7d414, we can only look up a block's context if 'blockType' is true. This commit addresses this issue in a second file.
@SiobhyB SiobhyB changed the title [RNMobile] Introduce "Context Constants" For Use with Inner Blocks [RNMobile] Indicate When Contexts Are Intended for Use with Inner Blocks and Native Oct 11, 2021
@SiobhyB SiobhyB marked this pull request as ready for review October 11, 2021 17:19
@SiobhyB
Copy link
Contributor Author

SiobhyB commented Oct 11, 2021

👋 @geriux, I've requested your review as this includes changes to the Gallery block's contexts, which I know you worked on and might have a sense of whether this approach seems sound. Let me know if there's anyone else who you think should review, too.

@glendaviesnz, I also wanted to get your thoughts around whether this seems similar to what you had in mind when suggesting context constants.

As well as gathering your general thoughts on the code changes here, I'm keen to hear whether the naming of usesNativeInnerBlockContext and providesNativeInnerBlockContext makes sense to you. I also pondered on the following, and am very open to changing if you have ideas/feelings around anything that would make their purpose as clear as possible:

  • usesNativeContext / providesNativeContext
  • usesNativeContextConstants / providesNativeContextConstants

Thanks in advance! And please do let me know if anything needs clarifying around these proposed changes. 🙇‍♀️

@glendaviesnz
Copy link
Contributor

glendaviesnz commented Oct 13, 2021

Thanks for the ping. A couple of thoughts after taking a look at this:

1/ I wonder if we need the native specific providesNativeInnerBlockContext namespace, and also further limiting it by tying it to innerBlocks. It seems to add complexity, and I think there is likely to be a need for other desktop parent blocks to handle this type of conflict as well - in which case we may end up having to duplicate context values between native and desktop namespaces. Maybe it is a case of tightening up the naming of the context values to avoid confusion, eg. imageCrop is meaningful in the context of the gallery block (although even there I don't think it is 100% clear what it means), but lacks meaning when passed down the context tree. Something like cropToEqualSizeGrid makes more sense at the gallery level, and it would make it clearer if the context that was passed down was something like parentIsEqualSizeGrid (still not perfect wording, just an example off the top of my head.

This would require a slightly different approach to context naming, which I haven't seen anywhere but may already be already be seen as a valid approach, and that is to use a different name for the context (name on left below) to the attribute (name on right), eg.

"providesContext": {
    "parentIsEqualSizeGrid": "cropToEqualSizeGrid"
}

To me this makes it easier to create names that are more meaningful when passed down than trying to come up with a single name that is accurate/meaningful in the parent and the children.

2/ The above naming discussion is separate to the idea of context constants. That discussion related to context values that were not being serialized to the block attributes, and imageCrop is currently serialized to the gallery block attributes, so it is very standard to treat this as a block context value.

The allowResize value in the gallery block however is not standard as it only ever uses the default value so is at no point serialized to the block attributes as context values were initially intended to be, hence the suggestion to be able to set a constant instead of adding an attribute that is never changed.

Sorry, that is probably a little long winded. I may be overlooking some native specific issues as I haven't been involved directly in any of the native dev, but it seems like looking at how we name the existing context values rather than creating a new namespace may be an alternative way forward. The addition of a contextConstants namespace for the likes of allowResize could be handled separately to that.

@SiobhyB
Copy link
Contributor Author

SiobhyB commented Oct 13, 2021

@glendaviesnz, I really appreciate your thoughtfulness here, not long-winded at all!

It seems there's a good case for keeping imageCrop as a "regular" context. A problem is, though, that we'll still need to add additional contexts on the native side. These are a few that are currently in mind for the image block to be able to consume from parent blocks:

  • fixedHeight: this will be provided by the core Gallery block and possibly other parent blocks in the future.
  • showSettings: this will be provided by parent blocks to indicate whether the image's cog/settings icon should be displayed. This is fairly specific to native as the settings icon is included within the block, unlike on the web, where the block's settings are separated out into the sidebar.

The above contexts would be set to true by default, in a similar way to allowResize. I didn't include them in this PR in an attempt to keep its scope as narrow as possible, but it's definitely relevant as the aim is for the image block to be able to consume more of these types of contexts.

To move forward, and with your feedback in mind, do the following seem reasonable as next steps for me to take?

  • Move imageCrop back to being a "regular" context and create a separate PR with the suggested tweaks to the naming.
  • Rename providesNativeInnerBlockContext/useNativeInnerBlockContext to providesContextConstants/usesContextConstants and add fixedHeight or showSettings as examples for now.

@glendaviesnz
Copy link
Contributor

  • Move imageCrop back to being a "regular" context and create a separate PR with the suggested tweaks to the naming.

Over to you if you want to worry about a new PR for the name change at this point, was using that as an example to highlight issues with with some of the current naming rather than indicating that it was a critical change we should make. I imagine it will take some wider discussion if we do go that way, as I think there might be some that would prefer to keep the attribute and context names the same for consistency ... but if you are open to starting a conversation on that then an example PR is probably a good place to start.

  • Rename providesNativeInnerBlockContext/useNativeInnerBlockContext to providesContextConstants/usesContextConstants and add fixedHeight or showSettings as examples for now.

Yeh, sounds good, will be interesting to see how this looks/works with a real example.

@illusaen
Copy link
Contributor

So I'm not as familiar with contexts as @glendaviesnz is, but one thought that I personally had was that there was a lot of repeat of

const blockUsesContext = blockType && [
		...blockType.usesContext,
		...blockType.usesNativeInnerBlockContext,
	];

which could be extracted into a function that's used throughout these files.

@SiobhyB SiobhyB removed the request for review from mkevins October 15, 2021 16:35
Siobhan added 5 commits October 17, 2021 17:30
Following the discussion here, a new 'fixedHeight' context is added to the Gallery block with this commit: #35510 (comment)

This context will be provided by the core Gallery block and possibly other parent blocks in the future. Its default will always be true. It's currently used to set a fixed height on the native Gallery block.
The new context types are renmaed from "*NativeInnerBlockContext" to "*ContextConstants" following feedback here: #35510 (comment)
It should be possible for all platforms to make use of context constants. As such, this commit removes the native-specific registration file, so the PR's changes are now visible in the main registration file and apply to all platforms.
},
"fixedHeight": {
"type": "boolean",
"default": true
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the idea with the constants was that they wouldn't be listed as block attributes because of the fact that they would be values that don't change and so aren't serialised in the block content. So if this is the case with fixed height this attribute wouldn't be here and below the constant would be defined as:

	"providesContextConstants": {
		"fixedHeight": true
	},

@talldan is this what you were imagining when you mentioned this idea here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@glendaviesnz, thank you for this, what you described makes perfect sense and I've refactored the code accordingly. It'd be great to hear if you (and @talldan) think this is on the right path or if there's anything else that needs to be changed here. 🙇‍♀️

Copy link
Contributor Author

@SiobhyB SiobhyB Oct 25, 2021

Choose a reason for hiding this comment

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

(Disclaimer: I'm aware of the error with the unit tests, which is directly related to the introduction of providesContextConstants. I plan to look into it after first gathering thoughts on the existing approach.) Edit: I've updated the tests to resolve the error in 8719c9e.

Siobhan added 3 commits October 25, 2021 22:31
As per the discussion here, part of the purpose of using context constant is to negate the need for serialisation, as the values won't change: #35510 (comment)

As such, with this commit, the function that was evaluating the value of the context constants based on a block's attributes is refactored.

With this change, 'providesContextConstants' are added to the block's context object in its entirety, with no need to grab anything from the block's attributes.
@SiobhyB SiobhyB changed the title [RNMobile] Indicate When Contexts Are Intended for Use with Inner Blocks and Native [RNMobile] Introduce Context Constants to Native Oct 25, 2021
@glendaviesnz
Copy link
Contributor

@SiobhyB - I am busy with some 5.9 release things, but should hopefully have time to look at this before the end of the week.

@SiobhyB
Copy link
Contributor Author

SiobhyB commented Nov 2, 2021

@glendaviesnz, just checking in, will you have time to look over this this week?

@glendaviesnz
Copy link
Contributor

@glendaviesnz, just checking in, will you have time to look over this this week?

Thanks for the reminder @SiobhyB. I had a quick look and for some reason on web the providesContextConstants doesn't get set on the blockType - just appears as empty object for the Gallery even though it is set in the block.json - I haven't managed to work out why, will take another look tomorrow - but does it work on the web side for you?

Also, I wonder if we want a separate useContextConstants or if at the child level it just becomes a context value? I guess one advantage of separating it out is that you then know that these are values that are not going to change so no need to run through a useEffect, etc. to deal with changes.

@talldan when you suggested this idea of contextConstants where you thinking that they would be defined separately at parent and child level or just merged in with the other usesContext values at the child level?

@SiobhyB
Copy link
Contributor Author

SiobhyB commented Nov 4, 2021

Thanks for this @glendaviesnz!

I had a quick look and for some reason on web the providesContextConstants doesn't get set on the blockType - just appears as empty object for the Gallery even though it is set in the block.json - I haven't managed to work out why, will take another look tomorrow - but does it work on the web side for you?

I confess some ignorance on the web side of things, and had focused my testing efforts on mobile, so thank you for bringing this up! I'm seeing the same as you on the web, and haven't been able to figure out why just yet. I'll keep looking into it and report back any findings.

Also, I wonder if we want a separate useContextConstants or if at the child level it just becomes a context value? I guess one advantage of separating it out is that you then know that these are values that are not going to change so no need to run through a useEffect, etc. to deal with changes.

Do you have a preference here? I can see benefits to both approaches and don't have a strong preference myself. Interested to hear @talldan's thoughts too.

@glendaviesnz
Copy link
Contributor

Do you have a preference here?

I don't have a strong preference, let's wait and see what @talldan has to say. I will hopefully have more time next week to take another look at why it isn't working on web side.

@SiobhyB
Copy link
Contributor Author

SiobhyB commented Nov 22, 2021

From the mobile-side, we were able to use block supports to achieve what we needed to in #36618 and #36371.

I'm going to go ahead to close this PR as I won't be working on it going forward and, following the discussion at #36582, it's unclear whether there's any other current need for context constants.

@SiobhyB SiobhyB closed this Nov 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Gallery Affects the Gallery Block - used to display groups of images [Block] Image Affects the Image Block [Feature] Nested / Inner Blocks Anything related to the experience of nested/inner blocks inside a larger container, like Group or P Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants