@carbon/react
Version:
React components for the Carbon Design System
80 lines (78 loc) • 2.51 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_TextDirectionContext = require("./TextDirectionContext.js");
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");
//#region src/components/Text/Text.tsx
/**
* 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 Text = (0, react.forwardRef)(({ as, children, dir = "auto", ...rest }, ref) => {
const context = (0, react.useContext)(require_TextDirectionContext.TextDirectionContext);
const textProps = {};
const BaseComponent = as ?? "span";
const value = { ...context };
if (!context) {
textProps.dir = dir;
value.direction = dir;
} else {
const { direction: parentDirection, getTextDirection } = context;
if (getTextDirection && getTextDirection.current) {
const text = getTextFromChildren(children);
const override = getTextDirection.current(text);
if (parentDirection !== override) {
textProps.dir = override;
value.direction = override;
} else if (parentDirection === "auto") textProps.dir = override;
} else if (parentDirection !== dir) {
textProps.dir = dir;
value.direction = dir;
} else if (parentDirection === "auto") textProps.dir = dir;
}
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_TextDirectionContext.TextDirectionContext.Provider, {
value,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(BaseComponent, {
ref,
...rest,
...textProps,
children
})
});
});
Text.propTypes = {
as: prop_types.default.oneOfType([
prop_types.default.func,
prop_types.default.string,
prop_types.default.elementType
]),
children: prop_types.default.node,
dir: prop_types.default.oneOf([
"ltr",
"rtl",
"auto"
])
};
const getTextFromChildren = (children) => {
if (typeof children === "string") return children;
const text = react.Children.map(children, (child) => {
if (typeof child === "string") return child;
return null;
})?.filter((text) => {
return text !== null;
});
if (text?.length === 1) return text[0];
return text;
};
//#endregion
exports.Text = Text;