@carbon/react
Version:
React components for the Carbon Design System
51 lines (46 loc) • 1.39 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
;
var PropTypes = require('prop-types');
var React = require('react');
var TextDirectionContext = require('./TextDirectionContext.js');
const TextDirection = ({
children,
dir = 'auto',
getTextDirection
}) => {
const savedCallback = React.useRef(getTextDirection);
const value = React.useMemo(() => {
return {
direction: dir,
getTextDirection: savedCallback
};
}, [dir]);
React.useEffect(() => {
savedCallback.current = getTextDirection;
}, [getTextDirection]);
return /*#__PURE__*/React.createElement(TextDirectionContext.TextDirectionContext.Provider, {
value: value
}, children);
};
TextDirection.propTypes = {
/**
* Provide children to be rendered inside of this component
*/
children: PropTypes.node,
/**
* Specify the text direction for rendered children
*/
dir: PropTypes.oneOf(['ltr', 'rtl', 'auto']),
/**
* Optionally provide a custom function to get the text direction for a piece
* of text. Whatever is returned will become the value of the `dir` attribute
* on a node of text. Should return one of: 'ltr', 'rtl', or 'auto'
*/
getTextDirection: PropTypes.func
};
exports.TextDirection = TextDirection;