UNPKG

@wordpress/components

Version:
97 lines (78 loc) 3.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useControlGroup = useControlGroup; var _element = require("@wordpress/element"); var _getValidChildren = require("../utils/get-valid-children"); var _context = require("../context"); var _context2 = require("./context"); var styles = _interopRequireWildcard(require("./styles")); var _useCx = require("../../utils/hooks/use-cx"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /** * Internal dependencies */ /** * @param {import('../context').WordPressComponentProps<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 cx = (0, _useCx.useCx)(); const classes = 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 {import('@emotion/react').SerializedStyles | undefined} */ let first; if (isFirst) { if (isVertical) { first = styles.firstRow; } else { first = styles.first; } } /** @type {import('@emotion/react').SerializedStyles | undefined} */ let last; if (isLast) { if (isVertical) { last = styles.lastRow; } else { last = styles.last; } } const contextStyles = 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