@wordpress/block-editor
Version:
135 lines (130 loc) • 4.58 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _clsx = _interopRequireDefault(require("clsx"));
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
var _i18n = require("@wordpress/i18n");
var _icons = require("@wordpress/icons");
var _lockUnlock = require("../../lock-unlock");
var _store = require("../../store");
var _blockIcon = _interopRequireDefault(require("../block-icon"));
var _jsxRuntime = require("react/jsx-runtime");
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const {
Badge
} = (0, _lockUnlock.unlock)(_components.privateApis);
/**
* A card component that displays block information including title, icon, and description.
* Can be used to show block metadata and navigation controls for parent blocks.
*
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/block-card/README.md
*
* @example
* ```jsx
* function Example() {
* return (
* <BlockCard
* title="My Block"
* icon="smiley"
* description="A simple block example"
* name="Custom Block"
* />
* );
* }
* ```
*
* @param {Object} props Component props.
* @param {string} props.title The title of the block.
* @param {string|Object} props.icon The icon of the block. This can be any of [WordPress' Dashicons](https://developer.wordpress.org/resource/dashicons/), or a custom `svg` element.
* @param {string} props.description The description of the block.
* @param {Object} [props.blockType] Deprecated: Object containing block type data.
* @param {string} [props.className] Additional classes to apply to the card.
* @param {string} [props.name] Custom block name to display before the title.
* @return {Element} Block card component.
*/
function BlockCard({
title,
icon,
description,
blockType,
className,
name
}) {
if (blockType) {
(0, _deprecated.default)('`blockType` property in `BlockCard component`', {
since: '5.7',
alternative: '`title, icon and description` properties'
});
({
title,
icon,
description
} = blockType);
}
const {
parentNavBlockClientId
} = (0, _data.useSelect)(select => {
const {
getSelectedBlockClientId,
getBlockParentsByBlockName
} = select(_store.store);
const _selectedBlockClientId = getSelectedBlockClientId();
return {
parentNavBlockClientId: getBlockParentsByBlockName(_selectedBlockClientId, 'core/navigation', true)[0]
};
}, []);
const {
selectBlock
} = (0, _data.useDispatch)(_store.store);
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
className: (0, _clsx.default)('block-editor-block-card', className),
children: [parentNavBlockClientId &&
/*#__PURE__*/
// This is only used by the Navigation block for now. It's not ideal having Navigation block specific code here.
(0, _jsxRuntime.jsx)(_components.Button, {
onClick: () => selectBlock(parentNavBlockClientId),
label: (0, _i18n.__)('Go to parent Navigation block'),
style:
// TODO: This style override is also used in ToolsPanelHeader.
// It should be supported out-of-the-box by Button.
{
minWidth: 24,
padding: 0
},
icon: (0, _i18n.isRTL)() ? _icons.chevronRight : _icons.chevronLeft,
size: "small"
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockIcon.default, {
icon: icon,
showColors: true
}), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
spacing: 1,
children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("h2", {
className: "block-editor-block-card__title",
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
className: "block-editor-block-card__name",
children: !!name?.length ? name : title
}), !!name?.length && /*#__PURE__*/(0, _jsxRuntime.jsx)(Badge, {
children: title
})]
}), description && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalText, {
className: "block-editor-block-card__description",
children: description
})]
})]
});
}
var _default = exports.default = BlockCard;
//# sourceMappingURL=index.js.map
;