woocommerce/plugins/woocommerce-blocks
Manish Menaria 9f5f93cba6
Product Collection: Implement Preview Mode (#46369)
* POC: Preview Mode using HOC

* Add explanation as comments

POC: Implement preview mode for Product Collection block in editor
- Added extensive commentary to clarify the mechanism and usage of the `handlePreviewState` function
- Implemented an internal state within `ProductCollectionContent` to manage preview status and messages, serving as a foundational example of how preview mode can enrich block functionality.
- Showcased the application of `handlePreviewState` by incorporating it as a prop in `BlockEdit`, illustrating the potential for extending the block's capabilities for dynamic and interactive previews.

This POC demonstrates a flexible approach to managing preview states within the editor, paving the way for further development and integration based on feedback and use-case analysis.

* Refactor preview state handling and collection registration

This commit introduces a centralized approach for registering product collection variations and managing their preview states. It abstracts the registration logic into a dedicated function and enhances the flexibility of preview state handling across different collection types.

* Rename file

* Minor improvements

* Don't pass previewState to handlePreviewState

I don't see any good use of it in handlePreviewState. Also, We will be going to call handlePreviewState only once
therefore, it will always have the same value as the initial value of the previewState. If in future, we decide to run it
multiple times then we can pass the previewState as an argument to handlePreviewState.

* Add comment

* Use JS closure to inject handlePreviewState

This commit introduces a refined approach for injecting the `handlePreviewState` function into product collection blocks, utilizing JavaScript closures to streamline the process. This method replaces the previous global registry mechanism, offering a more direct and efficient way to manage preview states.

Advantages of This Approach:
- Utilizing JavaScript closures for injecting `handlePreviewState` simplifies the overall architecture by directly modifying block edit components without relying on an external registry. This method enhances code clarity and reduces the cognitive load for developers.
- The conditional application of `withHandlePreviewState` ensures that the preview state handling logic is only added to blocks that require it, optimizing performance and maintainability.

* Refactor preview state management into custom hook

This commit enhances the organization and readability of the product collection content component by abstracting the preview state management into a custom hook named `usePreviewState`. This change streamlines the component's structure and aligns with React best practices for managing state and side effects.

Key Changes:
- Introduced `usePreviewState`, a custom hook responsible for initializing and managing the preview state (`isPreview` and `previewMessage`) of the product collection block. This hook encapsulates the state logic and its side effects, including the conditional invocation of `handlePreviewState`.
- Modified `ProductCollectionContent` to utilize `usePreviewState` for handling its preview state. This adjustment makes the component cleaner and focuses it more on presentation and behavior rather than state management details.

* Replace useEffect with useLayoutEffect

* Add cleanup function in handlePreviewState function

Based on [this discussion](https://github.com/woocommerce/woocommerce/pull/45703#discussion_r1535323883), I added a cleanup function support for handlePreviewState. `handlePreviewState` can return a function which will be called on cleanup in `useLayoutEffect` hook.

* Fetching random products in Preview mode

* Allow collection to set initial preview state

* Pass location & all attributes to handlePreviewState function

* Handling collection specific query for preview mode

- Consolidated `handlePreviewState` and `initialPreviewState` into a single `preview` prop in `register-product-collection.tsx` and `product-collection-content.tsx` to streamline prop passing and improve the component interface.
- Updated the `queryContextIncludes` in `constants.ts` to include 'previewState'
- Enhanced the `ProductCollection` PHP class to handle preview-specific queries more effectively, introducing a new method `get_preview_query_args` that adjusts query parameters based on the collection being previewed, thereby improving the relevance and accuracy of products displayed in preview mode.

* Always set initialPreviewState on load

* Refine preview state handling

- Renamed `HandlePreviewStateArgs` to `SetPreviewStateArgs` in `featured.tsx` to better reflect its purpose, which is now more focused on setting rather than handling states. The implementation details within `featured.tsx` have also been refined to include async operations and cleanup functions, demonstrating a more sophisticated approach to managing state.

Overall, these updates make the preview state logic more understandable and maintainable.

* Rename "initialState" to "initialPreviewState"

* Fix: Correct merging of newPreviewState into previewState attribute

This commit addresses an issue in the product-collection-content.tsx where the newPreviewState was not properly merged into the existing previewState attribute. Previously, the spread operator was incorrectly applied, leading to potential loss of existing state attributes. By changing the order of operations and correctly spreading the existing attributes before merging the newPreviewState, we ensure that all state attributes are preserved and updated correctly.

* Initial refactor POC code to productionize it

* Move `useSetPreviewState` to Utils

* Implement preview mode for Generic archive templates

Implemented a new useLayoutEffect in `utils.tsx` to dynamically set a preview message in the editor for product collection blocks located in generic archive templates (like Products by Category, Products by Tag, or Products by Attribute).

* Remove preview mode from Featured and On sale collection

* Remove preview query modfication for On Sale collection

* Add changefile(s) from automation for the following project(s): woocommerce-blocks, woocommerce

* Fix: hide/show preview label based on value of "inherit"

If user change the toggle "Sync with current query", then it should reflect for the preview label as well.
- If the toggle is on, then the preview label should be shown.
- If the toggle is off, then the preview label should be hidden.

* Minor improvements

* Add changefile(s) from automation for the following project(s): woocommerce-blocks, woocommerce

* Add changefile(s) from automation for the following project(s): woocommerce-blocks, woocommerce

* Refactor: Simplify SetPreviewState type definition in types.ts

This commit refines the SetPreviewState type definition by eliminating the previously used intermediate interface, SetPreviewStateArgs. The change streamlines the type definition directly within the SetPreviewState type, enhancing readability and reducing redundancy.

* Update import syntax for ElementType in register-product-collection.tsx

This commit updates the import statement for `ElementType` from `@wordpress/element` to use the more modern and concise `import type` syntax. This change does not affect functionality but aligns with TypeScript best practices for importing types, ensuring that type imports are distinguished from regular imports. This helps in clarity and in optimizing the build process by explicitly indicating that `ElementType` is used solely for type checking and not included in the JavaScript runtime.

* Refactor: Update TypeScript usage in Product Collection

This commit introduces several TypeScript refinements across product-collection components:

1. **DEFAULT_ATTRIBUTES** in `constants.ts` now uses `Pick` to explicitly define its shape, ensuring only relevant attributes are included and typed accurately.
2. **ProductCollectionAdvancedInspectorControls** and **ToolbarControls** in the `edit` subdirectory now use `Omit` to exclude the 'preview' property from props, clarifying the intended prop usage and improving type safety.

These changes collectively tighten the type definitions and improve the codebase's adherence to best practices in TypeScript.

* Refactor: Update dependencies of useSetPreviewState hook in utils.tsx

This change enhances the stability and predictability of the hook's behavior, ensuring it updates its internal state accurately when its dependencies change, thus aligning with best practices in React development.

* Refactor preview button CSS and conditional rendering

1. **CSS Refactoring:** Moved the positioning styles of the `.wc-block-product-collection__preview-button` from inline styles in the JSX to the `editor.scss` file. This separation of concerns improves maintainability and readability, aligning the styling responsibilities solely within the CSS file.
2. **Conditional Rendering Logic:** Updated the rendering condition for the preview button. Now, it not only checks if `isPreview` is true but also if the block is currently selected (`props.isSelected`). This prevents the preview button from appearing when the block is not actively selected, reducing visual clutter and enhancing the user experience in the editor.

* Enhance: Update preview button visibility logic in ProductCollectionContent

This commit enhances the visibility logic of the preview button in the `ProductCollectionContent` component:

1. **Added `isSelectedOrInnerBlockSelected`:** Introduced a new `useSelect` hook to determine if the current block or any of its inner blocks are selected. This ensures that the preview button is visible when either the product collection block or any of its inner blocks are selected.
2. **Updated Conditional Rendering:** Adjusted the conditional rendering of the preview button to use the new `isSelectedOrInnerBlockSelected` value, providing a more intuitive user experience by ensuring the preview button remains visible when any relevant block is selected.

* use __private prefix with attribute name

* Add E2E tests for Preview Mode

1. **Template-Specific Tests:** Each template (tag, category, attribute) undergoes a test to ensure the preview button behaves as expected when replacing products with product collections in these contexts.
2. **Visibility Checks:** The tests verify that the preview button is visible when the block or its inner blocks are selected and hidden when the block is not selected. This helps confirm the correct implementation of the preview button visibility logic across different use cases.
3. **Interaction with Inner Blocks:** Additional checks are included to ensure the preview button's visibility toggles appropriately when interacting with inner blocks, reinforcing the dynamic nature of block selection and its effect on UI elements within the editor.

* Add setPreviewState to dependencies

* Add data-test-id to Preview button and update e2e locator

Modifications:
- Added `data-test-id="product-collection-preview-button"` to the Preview button in `product-collection-content.tsx`.
- Updated the corresponding e2e test locator in `product-collection.block_theme.side_effects.spec.ts` to use the new `data-test-id` instead of the class name.

By using `data-test-id`, we ensure that the e2e tests are not affected by changes in the styling or restructuring of the DOM that might alter CSS classes but do not affect functionality.

* Enhance: Localize preview message in useSetPreviewState hook

* Don't show shadow & outline on focus

* Make preview button font same as Admin

* Fix SCSS lint errors

* Add missing await keyword

---------

Co-authored-by: github-actions <github-actions@github.com>
2024-05-15 13:18:43 +05:30
..
.github clear out unneeded github files from block folder (#44895) 2024-02-29 13:30:56 -04:00
.sources
.vscode Fix ESLint errors (https://github.com/woocommerce/woocommerce-blocks/pull/7556) 2022-11-02 17:46:14 +01:00
.wordpress-org Update WooCommerce Blocks brand assets (https://github.com/woocommerce/woocommerce-blocks/pull/10622) 2023-08-16 14:15:20 +01:00
assets Product Collection: Implement Preview Mode (#46369) 2024-05-15 13:18:43 +05:30
bin Product Filters: add individual clear button (#47101) 2024-05-13 15:19:56 -03:00
changelog clear out unneeded github files from block folder (#44895) 2024-02-29 13:30:56 -04:00
docs Update docs about blocks styling to clearly state global styles are the recommended approach (#47269) 2024-05-13 18:26:50 +02:00
packages [Enhancement]: Block-based checkout - Pickup section updates (#47173) 2024-05-08 17:58:09 +07:00
patches Revert "Remove Combobox patch (https://github.com/woocommerce/woocommerce-blocks/pull/6465)" (https://github.com/woocommerce/woocommerce-blocks/pull/6531) 2022-06-07 14:09:50 +02:00
storybook Update Woo.com references to WooCommerce.com (#46259) 2024-04-09 09:50:15 +01:00
tests Product Collection: Implement Preview Mode (#46369) 2024-05-15 13:18:43 +05:30
.distignore Rename commands and directories for Playwright and JEST e2e tests (https://github.com/woocommerce/woocommerce-blocks/pull/10493) 2023-08-08 17:58:12 +01:00
.editorconfig Update MD editor config (https://github.com/woocommerce/woocommerce-blocks/pull/7968) 2022-12-19 12:06:48 +00:00
.env Update relevant files due to L2->L0 policy change for minimum required versions. (https://github.com/woocommerce/woocommerce-blocks/pull/4484) 2021-07-29 10:52:34 -04:00
.eslintignore Rename commands and directories for Playwright and JEST e2e tests (https://github.com/woocommerce/woocommerce-blocks/pull/10493) 2023-08-08 17:58:12 +01:00
.eslintrc.js Blocks E2E: Align ESLint and TS configs with Gutenberg (#47228) 2024-05-14 10:23:17 +02:00
.gitattributes Revert "Remove .gitattributes file (https://github.com/woocommerce/woocommerce-blocks/pull/5242)" (https://github.com/woocommerce/woocommerce-blocks/pull/6049) 2022-03-14 12:42:34 +00:00
.gitignore More block package cleanup (#45256) 2024-03-04 10:27:58 -08:00
.markdownlint.json Upgrade wp-prettier to 2.6.2 and reformat the codebase (https://github.com/woocommerce/woocommerce-blocks/pull/6566) 2022-06-15 11:56:52 +02:00
.markdownlintignore Improve the structure of the WooCommerce Blocks Handbook (https://github.com/woocommerce/woocommerce-blocks/pull/6429) 2022-06-09 17:52:19 +02:00
.prettierignore Create 2e2 tests for Cross-Sells block (https://github.com/woocommerce/woocommerce-blocks/pull/6983) 2022-09-21 13:29:16 +07:00
.prettierrc.js Update @wordpress/scripts and eslint and stylint configurations (https://github.com/woocommerce/woocommerce-blocks/pull/3749) 2021-01-27 11:49:33 -05:00
.stylelintrc.json Enable declaration-block-no-duplicate-properties stylelint rule (https://github.com/woocommerce/woocommerce-blocks/pull/10163) 2023-07-11 13:03:05 +00:00
.wp-env.json Relocate helper (child) themes (#47080) 2024-05-02 21:23:52 +07:00
LICENSE Create LICENSE (https://github.com/woocommerce/woocommerce-blocks/pull/4235) 2021-05-20 15:07:12 +01:00
README.md Update Woo.com references to WooCommerce.com (#46259) 2024-04-09 09:50:15 +01:00
babel.config.js Migrate Storybook to 6.4.0 and enable auto-detection of controls (https://github.com/woocommerce/woocommerce-blocks/pull/5249) (https://github.com/woocommerce/woocommerce-blocks/pull/5264) 2021-12-01 12:28:27 +01:00
checkstyle.xml Migrate Block Title component to TypeScript (https://github.com/woocommerce/woocommerce-blocks/pull/9712) 2023-06-13 16:30:57 +05:30
composer.json Update Woo.com references to WooCommerce.com (#46259) 2024-04-09 09:50:15 +01:00
composer.lock Fix Component Project Changelog Checking (#42794) 2023-12-13 15:41:11 -08:00
docker-compose.yml Add PHP 8.1/8.2 to unit testing matrix (https://github.com/woocommerce/woocommerce-blocks/pull/8757) 2023-04-21 12:07:37 +01:00
global.d.ts Add camelcase rule to eslint config (https://github.com/woocommerce/woocommerce-blocks/pull/4382) 2021-07-09 17:53:45 +01:00
package-lock.json Bump @wordpress/wordcount from 3.13.0 to 3.47.0 (https://github.com/woocommerce/woocommerce-blocks/pull/12094) 2023-12-08 14:28:16 +00:00
package.json Update to pnpm 9.1 (#47385) 2024-05-13 10:57:39 -03:00
phpcs.xml Introduce Additional Fields extensibility API (#42695) 2023-12-15 13:45:38 +01:00
phpunit.xml.dist Add PHP 8.1/8.2 to unit testing matrix (https://github.com/woocommerce/woocommerce-blocks/pull/8757) 2023-04-21 12:07:37 +01:00
postcss.config.js
readme.txt Update Woo.com references to WooCommerce.com (#46259) 2024-04-09 09:50:15 +01:00
tsconfig.base.json Upgrade TypeScript to 5.3.2 (https://github.com/woocommerce/woocommerce-blocks/pull/11926) 2023-12-05 18:17:04 +13:00
tsconfig.json Add a stock filter block powered by the interactivity API (https://github.com/woocommerce/woocommerce-blocks/pull/11663) 2023-11-15 16:50:05 +13:00
webpack.config.js Fix: load blocks style separately for classic themes (https://github.com/woocommerce/woocommerce-blocks/pull/10758) 2023-09-08 11:15:48 +07:00
wp-cli.yml Improve the E2E testing process with Playwright (https://github.com/woocommerce/woocommerce-blocks/pull/9148) 2023-05-05 11:03:36 +00:00

README.md

WooCommerce Blocks

This is the client for WooCommerce + Gutenberg. This package serves as a space to iterate and explore new Blocks and updates to existing blocks for WooCommerce, and how WooCommerce might work with the Block Editor.

Table of Contents

Documentation

To find out more about the blocks and how to use them, check out the documentation on WooCommerce.com.

If you want to see what we're working on for future versions, or want to help out, read on.

Code Documentation

  • Blocks - Documentation for specific Blocks.
  • Editor Components - Shared components used in WooCommerce blocks for the editor (Gutenberg) UI.
  • WooCommerce Blocks Handbook - Documentation for designers and developers on how to extend or contribute to blocks, and how internal developers should handle new releases.
  • WooCommerce Blocks Storybook - Contains a list and demo of components used in the plugin.

Getting started with block development

Run through the "Writing Your First Block Type" tutorial for a quick course in block-building.

For deeper dive, try looking at the core blocks code, or see what components are available.

Other useful docs to explore:

Long-term vision

WooCommerce Blocks are the easiest, most flexible way to build your store's user interface and showcase your products.