UNPKG

@wordpress/components

Version:
94 lines (76 loc) 2.32 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); Object.defineProperty(exports, "__esModule", { value: true }); exports.useControlGroup = useControlGroup; var _element = require("@wordpress/element"); var _emotion = require("emotion"); var _getValidChildren = require("../utils/get-valid-children"); var _context = require("../context"); var _context2 = require("./context"); var styles = _interopRequireWildcard(require("./styles")); /** * Internal dependencies */ /** * @param {import('../context').ViewOwnProps<import('./types').Props, 'div'>} props */ function useControlGroup(props) { const { children, className, direction = 'row', templateColumns, ...otherProps } = (0, _context.useContextSystem)(props, 'ControlGroup'); const validChildren = (0, _getValidChildren.getValidChildren)(children); const isVertical = direction === 'column'; const isGrid = !!templateColumns; const classes = (0, _emotion.cx)(styles.itemFocus, isGrid && styles.itemGrid, className); const clonedChildren = validChildren && validChildren.map((child, index) => { const isFirst = index === 0; const isLast = index + 1 === validChildren.length; const isOnly = isFirst && isLast; const isMiddle = !isFirst && !isLast; // @ts-ignore const _key = (child === null || child === void 0 ? void 0 : child.key) || index; /** @type {string | undefined} */ let first; if (isFirst) { if (isVertical) { first = styles.firstRow; } else { first = styles.first; } } /** @type {string | undefined} */ let last; if (isLast) { if (isVertical) { last = styles.lastRow; } else { last = styles.last; } } const contextStyles = (0, _emotion.cx)(first, isMiddle && styles.middle, last); const contextProps = { isFirst, isLast, isMiddle, isOnly, isVertical, styles: contextStyles }; return (0, _element.createElement)(_context2.ControlGroupContext.Provider, { key: _key, value: contextProps }, child); }); return { ...otherProps, children: clonedChildren, className: classes, direction, templateColumns }; } //# sourceMappingURL=hook.js.map