@carbon/react
Version:
React components for the Carbon Design System
72 lines (70 loc) • 2.81 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_usePrefix = require("../../internal/usePrefix.js");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(prop_types);
let react_jsx_runtime = require("react/jsx-runtime");
let _carbon_layout = require("@carbon/layout");
//#region src/components/Stack/Stack.tsx
/**
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* The steps in the spacing scale
*/
const SPACING_STEPS = Array.from({ length: _carbon_layout.spacing.length - 1 }, (_, step) => step + 1);
/**
* The Stack component is a useful layout utility in a component-based model.
* This allows components to not use margin and instead delegate the
* responsibility of positioning and layout to parent components.
*
* In the case of the Stack component, it uses the spacing scale from the
* Design Language in order to determine how much space there should be between
* items rendered by the Stack component. It also supports a custom `gap` prop
* which will allow a user to provide a custom value for the gap of the layout.
*
* This component supports both horizontal and vertical orientations.
*
* Inspiration for this component:
*
* - https://paste.twilio.design/layout/stack/
* - https://github.com/Workday/canvas-kit/blob/f2f599654876700f483a1d8c5de82a41315c76f1/modules/labs-react/layout/lib/Stack.tsx
*/
const Stack = (0, react.forwardRef)((props, ref) => {
const { as: BaseComponent = "div", children, className: customClassName, gap, orientation = "vertical", ...rest } = props;
const prefix = require_usePrefix.usePrefix();
const className = (0, classnames.default)(customClassName, {
[`${prefix}--stack-${orientation}`]: true,
[`${prefix}--stack-scale-${gap}`]: typeof gap === "number"
});
const style = { ...rest.style };
if (typeof gap === "string") style[`--${prefix}-stack-gap`] = gap;
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(BaseComponent, {
...rest,
ref,
className,
style,
children
});
});
Stack.propTypes = {
as: prop_types.default.elementType,
children: prop_types.default.node,
className: prop_types.default.string,
gap: prop_types.default.oneOfType([prop_types.default.string, prop_types.default.oneOf(SPACING_STEPS)]),
orientation: prop_types.default.oneOf(["horizontal", "vertical"])
};
//#endregion
exports.Stack = Stack;