UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

80 lines (79 loc) 2.99 kB
"use client"; import _extends from "@babel/runtime-corejs3/helpers/esm/extends"; var _b; import React, { useCallback, useMemo, useRef, useState } from 'react'; import classnames from 'classnames'; import { Space } from "../../../../components/index.js"; import { omitSpacingProps, pickSpacingProps } from "../../../../components/flex/utils.js"; import useTranslation from "../../hooks/useTranslation.js"; import { convertJsxToString } from "../../../../shared/component-helper.js"; import { useIsomorphicLayoutEffect as useLayoutEffect } from "../../../../shared/helpers/useIsomorphicLayoutEffect.js"; function SubmitIndicator(props) { const translation = useTranslation(); const { className, children, state, showLabel, label = translation.SubmitIndicator.label, ...rest } = props; const childrenRef = useRef(null); const [willWrap, setWillWrap] = useState(false); const key = useMemo(() => convertJsxToString(children), [children]); const recalculate = useCallback(() => { setWillWrap(willWordWrap(childrenRef.current, '. . . ')); }, [childrenRef]); useLayoutEffect(() => { if (key) { recalculate(); window.addEventListener('resize', recalculate); return () => { window.removeEventListener('resize', recalculate); }; } }, [key, recalculate]); const params = { className: classnames('dnb-forms-submit-indicator', className, state && `dnb-forms-submit-indicator--state-${state}`, willWrap && 'dnb-forms-submit-indicator--inline-wrap'), ...pickSpacingProps(rest) }; const ariaAttributes = state === 'pending' ? { role: 'status', 'aria-busy': true, 'aria-label': convertJsxToString(label) } : { 'aria-hidden': true }; const dot = _b || (_b = React.createElement("b", null, ".")); const indicator = React.createElement("span", _extends({ className: "dnb-forms-submit-indicator__content" }, ariaAttributes, omitSpacingProps(rest)), showLabel && React.createElement("span", { className: "dnb-forms-submit-indicator__label" }, label), state && state !== 'success' && state !== 'abort' && React.createElement(React.Fragment, null, dot, dot, dot)); return React.createElement(Space, _extends({}, params, { element: "span" }), children && React.createElement("span", { ref: childrenRef, key: key }, children), indicator); } function willWordWrap(element, word) { var _element$parentElemen; if (!element) { return; } const { offsetHeight, innerHTML } = element; const clone = element.cloneNode(true); (_element$parentElemen = element.parentElement) === null || _element$parentElemen === void 0 || _element$parentElemen.insertBefore(clone, element); clone.innerHTML += word; const height = clone.offsetHeight; clone.innerHTML = innerHTML; clone.remove(); return height > offsetHeight; } SubmitIndicator._supportsSpacingProps = true; export default SubmitIndicator; //# sourceMappingURL=SubmitIndicator.js.map