woocommerce/packages/js/block-templates
Vladimir Reznichenko 21d14759b3
[dev] Monorepo: minor build speed improvement (tsconfig consolidation) (#52191)
In this PR, we consolidate TypeScript config files (using the 'include' setting) and JS test directory naming. The goal is to reduce the number of processed files during different build steps.
2024-10-23 13:48:36 +02:00
..
changelog [dev] Monorepo: minor build speed improvement (tsconfig consolidation) (#52191) 2024-10-23 13:48:36 +02:00
src Product Editor: Fix crash and issues with variations (that will cause problems with React 18) (#49248) 2024-07-09 10:44:41 -04:00
.eslintrc.js CI: update linting jobs to skip build step (#49193) 2024-07-08 08:39:39 +02:00
.npmrc Template API: Expose template block id and order to client (#40263) 2023-09-27 13:38:56 -07:00
README.md Implement useLayoutTemplate hook (#43347) 2024-01-09 15:42:20 -05: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 Prepare @woocommerce/admin-layout and block-templates for release (#46538) 2024-04-12 13:52:36 +00:00
composer.json bump php version in packages/js/*/composer.json (#42020) 2024-01-04 10:18:34 -04: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 Fix Jest Preset (#42707) 2023-12-12 09:58:13 -08:00
package.json [dev] CI: liverage composer packages cache in pr-lint-monorepo workflow (#52054) 2024-10-16 12:02:31 +02:00
tsconfig-cjs.json [dev] Monorepo: minor build speed improvement (tsconfig consolidation) (#52191) 2024-10-23 13:48:36 +02:00
tsconfig.json [dev] Monorepo: minor build speed improvement (tsconfig consolidation) (#52191) 2024-10-23 13:48:36 +02: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.

useLayoutTemplate

This hook is used to retrieve a layout template from the server.

Usage

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

export function Example() {
	const { layoutTemplate, isResolving } =
		useLayoutTemplate( 'my-layout-template' );

	return (
		<div>
			{ isResolving && <p>Loading layout template...</p> }
			{ layoutTemplate && (
				<p>{ JSON.stringify( layoutTemplate, null, 4 ) }</p>
			) }
			{ ! layoutTemplate && ! isResolving && (
				<p>'Layout template does not exist!'</p>
			) }
		</div>
	);
}

Parameters

  • layoutTemplateId string: The id of the layout template to retrieve.

Returns

  • Object
    • layoutTemplate Object | undefined: The layout template if it was found, otherwise null.
    • isResolving boolean: Whether or not the layout template is resolving.

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.