|
| 1 | +/** |
| 2 | + * Copyright IBM Corp. 2016, 2018 |
| 3 | + * |
| 4 | + * This source code is licensed under the Apache-2.0 license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import { spacing } from '@carbon/layout'; |
| 9 | +import cx from 'classnames'; |
| 10 | +import PropTypes from 'prop-types'; |
| 11 | +import React from 'react'; |
| 12 | +import { usePrefix } from '../../internal/usePrefix'; |
| 13 | + |
| 14 | +/** |
| 15 | + * The steps in the spacing scale |
| 16 | + * @type {Array<number>} |
| 17 | + */ |
| 18 | +const SPACING_STEPS = Array.from({ length: spacing.length - 1 }).map( |
| 19 | + (_, step) => { |
| 20 | + return step + 1; |
| 21 | + } |
| 22 | +); |
| 23 | + |
| 24 | +/** |
| 25 | + * The Stack component is a useful layout utility in a component-based model. |
| 26 | + * This allows components to not use margin and instead delegate the |
| 27 | + * responsibility of positioning and layout to parent components. |
| 28 | + * |
| 29 | + * In the case of the Stack component, it uses the spacing scale from the |
| 30 | + * Design Language in order to determine how much space there should be between |
| 31 | + * items rendered by the Stack component. It also supports a custom `gap` prop |
| 32 | + * which will allow a user to provide a custom value for the gap of the layout. |
| 33 | + * |
| 34 | + * This component supports both horizontal and vertical orientations. |
| 35 | + * |
| 36 | + * Inspiration for this component: |
| 37 | + * |
| 38 | + * - https://paste.twilio.design/layout/stack/ |
| 39 | + * - https://github.com/Workday/canvas-kit/blob/f2f599654876700f483a1d8c5de82a41315c76f1/modules/labs-react/layout/lib/Stack.tsx |
| 40 | + */ |
| 41 | +const Stack = React.forwardRef(function Stack(props, ref) { |
| 42 | + const { |
| 43 | + as: BaseComponent = 'div', |
| 44 | + children, |
| 45 | + className: customClassName, |
| 46 | + gap, |
| 47 | + orientation = 'vertical', |
| 48 | + ...rest |
| 49 | + } = props; |
| 50 | + const prefix = usePrefix(); |
| 51 | + const className = cx(customClassName, { |
| 52 | + [`${prefix}--stack-${orientation}`]: true, |
| 53 | + [`${prefix}--stack-scale-${gap}`]: typeof gap === 'number', |
| 54 | + }); |
| 55 | + const style = {}; |
| 56 | + |
| 57 | + if (typeof gap === 'string') { |
| 58 | + style[`--${prefix}-stack-gap`] = gap; |
| 59 | + } |
| 60 | + |
| 61 | + return ( |
| 62 | + <BaseComponent {...rest} ref={ref} className={className} style={style}> |
| 63 | + {children} |
| 64 | + </BaseComponent> |
| 65 | + ); |
| 66 | +}); |
| 67 | + |
| 68 | +Stack.propTypes = { |
| 69 | + /** |
| 70 | + * Provide a custom element type to render as the outermost element in |
| 71 | + * the Stack component. By default, this component will render a `div`. |
| 72 | + */ |
| 73 | + as: PropTypes.elementType, |
| 74 | + |
| 75 | + /** |
| 76 | + * Provide the elements that will be rendered as children inside of the Stack |
| 77 | + * component. These elements will have having spacing between them according |
| 78 | + * to the `step` and `orientation` prop |
| 79 | + */ |
| 80 | + children: PropTypes.node, |
| 81 | + |
| 82 | + /** |
| 83 | + * Provide a custom class name to be used by the outermost element rendered by |
| 84 | + * Stack |
| 85 | + */ |
| 86 | + className: PropTypes.string, |
| 87 | + |
| 88 | + /** |
| 89 | + * Provide either a custom value or a step from the spacing scale to be used |
| 90 | + * as the gap in the layout |
| 91 | + */ |
| 92 | + gap: PropTypes.oneOfType([PropTypes.string, PropTypes.oneOf(SPACING_STEPS)]), |
| 93 | + |
| 94 | + /** |
| 95 | + * Specify the orientation of them items in the Stack |
| 96 | + */ |
| 97 | + orientation: PropTypes.oneOf(['horizontal', 'vertical']), |
| 98 | +}; |
| 99 | + |
| 100 | +export { Stack }; |
0 commit comments