UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

93 lines (92 loc) 3.02 kB
"use client"; var _b; import React, { useCallback, useMemo, useRef, useState } from 'react'; import clsx from 'clsx'; 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"; import withComponentMarkers from "../../../../shared/helpers/withComponentMarkers.js"; import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; function SubmitIndicator(props) { const translation = useTranslation(); const { id, 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); }; } return undefined; }, [key, recalculate]); const params = { id, className: clsx('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 = _jsx("b", { children: "." })); const indicator = _jsxs("span", { className: "dnb-forms-submit-indicator__content", ...ariaAttributes, ...omitSpacingProps(rest), children: [showLabel && _jsx("span", { className: "dnb-forms-submit-indicator__label", children: label }), state && state !== 'success' && state !== 'abort' && _jsxs(_Fragment, { children: [dot, dot, dot] })] }); return _jsxs(Space, { ...params, element: "span", children: [children && _jsx("span", { ref: childrenRef, children: children }, key), indicator] }); } function willWordWrap(element, word) { if (!element) { return undefined; } const { offsetHeight } = element; const clone = element.cloneNode(true); element.parentElement?.insertBefore(clone, element); clone.appendChild(document.createTextNode(word)); const height = clone.offsetHeight; clone.remove(); return height > offsetHeight; } withComponentMarkers(SubmitIndicator, { _supportsSpacingProps: true }); export default SubmitIndicator; //# sourceMappingURL=SubmitIndicator.js.map