woocommerce/plugins/woocommerce-blocks/packages/checkout/slot
Mike Jolley dbbb99d55e Move Block Type Settings into Block Type Classes (https://github.com/woocommerce/woocommerce-blocks/pull/4059)
* BLOCK SETTINGS: Remove unused constants/settings

* AssetDataRegistry: Helpers to check for settings that exist, and registering page ID/permalinks

* Move checkout and cart block settings to checkout and cart blocktypes

* Move isShippingCalculatorEnabled to cart block

* Remove HAS_DARK_EDITOR_STYLE_SUPPORT and IS_SHIPPING_CALCULATOR_ENABLED in favour of getSetting

* Move displayCartPricesIncludingTax to blocktypes, and implement getSetting

* Move block settings to core settings and blocktypes

* Fix namespace usage

* Move review settings

* move tag settings

* Keep productCount in core data

* Move min and default height

* Improve storePages code

* Move attributes to attribute filter block type

* Move $word_count_type outside of settings array

* Remove unneeded setting in preview data (shippingCostRequiresAddress)

* Move min/max settings dependency from GridLayoutControl to Blocks themselves and use getSettings

* DEFAULT_COLUMNS and ROWS to settings

* Move product columns/rows to block types

* Add grid settings to AllProducts block

* Correct default rows

* correct min rows default

* Move hasDarkEditorStyleSupport

* Move hideOutOfStockItems to block type settings

* Move build settings to inline script dependency

* Pass data through asset api and move restApiRoutes

* Export all core settings as constants

* Remove WORD_COUNT_TYPE from core settings

* Move some other core settings to assets

* Update constants

* Make settings use TypeScript

* Update CURRENT_USER_IS_ADMIN usage

* WORD_COUNT_TYPE

* REST_API_ROUTES

* REVIEW_RATINGS_ENABLED and SHOW_AVATARS

* Remove REVIEW_RATINGS_ENABLED and SHOW_AVATARS constants

* Remove MIN_HEIGHT

* Remove DEFAULT_HEIGHT

* PLACEHOLDER_IMG_SRC

* LIMIT_TAGS

* HAS_PRODUCTS

* HOME_URL

* HAS_TAGS

* COUPONS_ENABLED

* SHIPPING_ENABLED

* TAXES_ENABLED

* DISPLAY_ITEMIZED_TAXES

* SHIPPING_COST_REQUIRES_ADDRESS

* SHIPPING_STATES and SHIPPING_COUNTRIES

* STORE_PAGES

* ALLOWED_COUNTRIES

* ALLOWED_STATES

* SHIPPING_METHODS_EXIST

* PAYMENT_GATEWAY_SORT_ORDER

* CHECKOUT_SHOW_LOGIN_REMINDER

* CHECKOUT_ALLOWS_GUEST and CHECKOUT_ALLOWS_SIGNUP

* ATTRIBUTES

* DISPLAY_CART_PRICES_INCLUDING_TAX

* DISPLAY_CART_PRICES_INCLUDING_TAX

* update build for TS files

* fix build dir

* Move blocks build config params

* Move placeholderImgSrc to core settings

* Move rest api hydration hoc to shared hocs and provide it restApiRoutes directly to avoid asset data registration

* Move wordCountType to abstract block

* Remove WORD_COUNT_TYPE in favour of getSetting

* Move IS_LARGE_CATALOG and PRODUCT_COUNT to abstract block type and use getSetting inline

* Add wcBlocksConfig

* fix tests

* Remove unused $asset_data_registry

* remove console.log

* Move build settings to abstract block

* Trigger build again

* Move hydration back to regular hocs for compatibility with trunk (merge conflict)

* Removed wcSharedHocsConfig

* esc home url

* Update search fixture

* Update search snap

* 40000 timeout

* hasProducts -> productCount

* Product Count is part of blocks config

* update mocks

* Use version comparison to determine if batching is enabled

* Change isWpVersion

* scrollTo button
2021-04-22 12:37:27 +01:00
..
README.md Add guards to useSlot and move shared code to a new file. (https://github.com/woocommerce/woocommerce-blocks/pull/3772) 2021-02-08 12:37:55 +01:00
index.js Move Block Type Settings into Block Type Classes (https://github.com/woocommerce/woocommerce-blocks/pull/4059) 2021-04-22 12:37:27 +01:00

README.md

Slot Fill

Slot and Fill are a pair of components which enable developers to render elsewhere in a React element tree, a pattern often referred to as "portal" rendering. It is a pattern for component extensibility, where a single Slot may be occupied by an indeterminate number of Fills elsewhere in the application.

Read more about Slot Fill in @wordpress/components documentation.

This file is an abstraction above Gutenberg's implementation and is meant to be used internally, therefor, the documentation only touches the abstraction part.

Usage

Calling createSlotFill with a slotName would give you a couple of components: Slot and Fill. 3PD would use Fill, and you will use Slot inside your code. A Slot must be called in a tree that has SlotFillProvider in it.

Always prefix your slotName with __experimental and your Fill with Experimental until you decide to publicly announce them.

Assign your Slot to your Fill ExperimentalOrderMeta.Slot = Slot.

If you need to pass extra data from the Slot to the Fill, use fillProps.

import { createSlotFill } from '@woocommerce/blocks-checkout';

const slotName = '__experimentalOrderMeta';

const { Fill: ExperimentalOrderMeta, Slot: OrderMetaSlot } = createSlotFill(
	slotName
);

const Slot = ( { className } ) => {
	const { extensions, cartData } = useStoreCart();
	return (
		<OrderMetaSlot
			className={ classnames(
				className,
				'wc-block-components-order-meta'
			) }
			fillProps={ { extensions, cartData } }
		/>
	);
};

ExperimentalOrderMeta.Slot = Slot;

export default ExperimentalOrderMeta;

Fill renders an errorBoundary inside of it, this is meant to catch broken fills and preventing them from breaking code or other fills. If the current user is an admin, the error would be shown instead of the components. Otherwise, nothing would be shown and the fill would be removed. You can customize the error shown to admins by passing onError to createSlotFill.

import { createSlotFill } from '@woocommerce/blocks-checkout';

const slotName = '__experimentalOrderMeta';

const onError = ( errorMessage ) => {
	return (
		<div className="my-custom-error">
		You got an error! <br />
		{errorMessage}
		Contact support at <a href="mailto:help@example.com">help@example.com</a>
		</div>
	)
}
const { Fill: ExperimentalOrderMeta, Slot: OrderMetaSlot } = createSlotFill(
	slotName, onError
);

You can pass props to the fills to be used.

import { createSlotFill } from '@woocommerce/blocks-checkout';

const slotName = '__experimentalOrderMeta';

const { Fill: ExperimentalOrderMeta, Slot: OrderMetaSlot } = createSlotFill( slotName );

const Slot = () => {
	const { extensions, cartData } = useStoreCart();
	return <OrderMetaSlot fillProps={ { extensions, cartData } } />
}

ExperimentalOrderMeta.Slot = Slot;

export default ExperimentalOrderMeta;
import { ExperimentalOrderMeta } from '@woocommerce/blocks-checkout';
import { registerPlugin } from '@wordpress/plugins';

const MyComponent = ( { extensions, cartData } ) => {
	const { myPlugin } = extensions;
	return <Meta data={myPlugin} />
}

const render = () => {
	return <ExperimentalOrderMeta><MyComponent /></ExperimentalOrderMeta>
}

registerPlugin( 'my-plugin', { render } );

Props

Slot accepts several props to customize it.

as

By default, Slot would render a div inside your DOM, you can customize what gets rendered instead.

  • Type: String|Element
  • Required: No

className

The rendered element can accept a className.

  • Type: String
  • Required: No

fillProps

Props passed to each fill implementation.

  • Type: Object
  • Required: No

createSlotFill accepts a couple of props.

slotName

The name of slot to be created.

  • Type: String
  • Required: Yes

onError

A function returns an element to be rendered if the current is an admin and an error is caught, accepts errorMessage as a param that is the formatted error.

  • Type: Function
  • Required: No