UNPKG

@engie-group/fluid-design-system-react

Version:

Fluid Design System React

33 lines (30 loc) 1.3 kB
import { jsx } from 'react/jsx-runtime'; import React__default from 'react'; import { Utils } from '../../utils/util.js'; import { NJButton } from '../button/NJButton.js'; import { useNJModalContext } from '../modal/NJModal.context.js'; const isAsChild = (props) => { return 'asChild' in props && !!props.asChild; }; const NJModalTrigger = React__default.forwardRef((props, forwardedRef) => { const context = useNJModalContext(); // `asChild` allows the user to pass any element as the anchor if (isAsChild(props)) { const { children } = props; if (React__default.isValidElement(children)) { const childrenRef = children.ref; const ref = Utils.mergeRefs([context.refs.setReference, forwardedRef, childrenRef]); return React__default.cloneElement(children, context.getReferenceProps({ ref, ...children.props })); } } else { const ref = Utils.mergeRefs([context.refs.setReference, forwardedRef]); const { children, ...otherProps } = props; return (jsx(NJButton, { ...otherProps, ref: ref, label: children?.toString(), ...context.getReferenceProps(otherProps) })); } }); NJModalTrigger.displayName = 'NJModalTrigger'; export { NJModalTrigger };