woocommerce/packages/js/block-templates
Matt Sherman 1e35d45c99
Template API: Expose template block id and order to client (#40263)
* Update unit tests to handle _templateBlockId and _templateBlockOrder

* Refactor get_formatted_template

* Initial @woocommerce/block-templates package

* Add block-templates to admin webpack

* Add block-templates to dependency-extraction-webpack-plugin

* Add block-templates to admin assets

* Add block-templates dependency

* Update name block

* Update syncpack

* Update regular price block

* Update tab block

* Update section block

* Add @wordpress/deprecated to package

* Deprecated initBlock

* Update attributes block

* Update catalog visibility block

* Update checkbox block

* Update conditional block

* Update collapsible block

* Allow additional props to be passed to useWooBlockProps

* Update inventory sku block

* Update inventory quantity block

* Update inventory email block

* Update images block

* Update description block

* Update radio block

* Update pricing block

* Update password block

* Update notice block

* Update shipping dimensions block

* Update shipping class block

* Update schedule sale block

* Update sale price block

* Update toggle block

* Update taxonomy block

* Update tag block

* Update summary block

* Update variations block

* Update variations options block

* Update variation items blocks

* Changelog

* Changelog

* Changelog

* Changelog

* Changelog

* Add test for registerWooBlockType

* Add @testing-library/react-hooks to devDependencies

* Add test for useWooBlockProps

* Document API

* Fix linting issues in README.md

* Fix tabs tests by mocking useWooBlockProps

* Allow header duplication under different nesting

* Remove unused import (fixes lint error)

* Update lock file
2023-09-27 13:38:56 -07:00
..
changelog Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
src Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
.eslintrc.js Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
.npmrc Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
README.md Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
babel.config.js Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
changelog.md Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
composer.json Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
composer.lock Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
jest.config.json Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
package.json Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
tsconfig-cjs.json Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
tsconfig.json Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
webpack.config.js Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00

README.md

@woocommerce/block-templates

A collection of utility functions for use with WooCommerce admin block templates.

API

registerWooBlockType

Registers a WooCommerce block type.

Usage

import { registerWooBlockType } from '@woocommerce/block-templates';

import metadata from './block.json';
import { Edit } from './edit';

registerWooBlockType( {
  name: metadata.name,
  metadata: metadata,
  settings: {
    edit: Edit,
  }
} );

Parameters

  • blockMetadata Object: Block metadata.

Returns

  • WPBlockType | undefined: The block type if it was registered successfully, otherwise undefined.

useWooBlockProps

This hook is used to lightly mark an element as a WooCommerce block template block. The block's attributes must be passed to this hook and the return result passed to the outermost element of the block in order for the block to properly function in WooCommerce block template contexts.

If you define a ref for the element, it is important to pass the ref to this hook, which the hook in turn will pass to the component through the props it returns. Optionally, you can also pass any other props through this hook, and they will be merged and returned.

Usage

import { useWooBlockProps } from '@woocommerce/block-templates';

export function Edit( { attributes } ) {
  const { blockProps } = useWooBlockProps( 
    attributes,
    {
      className: 'my-block',
    }
  );

  return (
    <div { ...blockProps }>
      Block content
    </div>
  );
}

Parameters

  • attributes: Object: Block attributes.
  • props: Object: Optional. Props to pass to the element.

Returns

  • Object: Props to pass to the element to mark as a WooCommerce block.