UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

124 lines (123 loc) 4.14 kB
"use client"; import _extends from "@babel/runtime-corejs3/helpers/esm/extends"; import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty"; import React from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import { extendPropsWithContextInClassComponent, validateDOMAttributes, isTrue } from "../../shared/component-helper.js"; import { LOCALE } from "../../shared/defaults.js"; import Space from "../space/Space.js"; import { spacingPropTypes, createSpacingClasses } from "../space/SpacingHelper.js"; import Context from "../../shared/Context.js"; import Provider from "../../shared/Provider.js"; export default class Skeleton extends React.PureComponent { constructor(props) { super(props); this.state = { ariaLiveUpdate: null }; } componentWillUnmount() { clearTimeout(this._ariaLiveUpdateTimeout); } componentDidUpdate(prevProps) { if (prevProps.show !== this.props.show) { this.setAriaLiveUpdate(); } } getProps(props = this.props, context = this.context) { return extendPropsWithContextInClassComponent(props, Skeleton.defaultProps, { skeleton: context.Skeleton || context.skeleton, no_animation: context.skeleton_no_animation }, context.getTranslation(props).Skeleton); } setAriaLiveUpdate() { clearTimeout(this._ariaLiveUpdateTimeout); this._ariaLiveUpdateTimeout = setTimeout(() => { const { aria_busy, aria_ready } = this.getProps(); let newString = null; if (isTrue(this.props.show)) { newString = aria_busy; } else { newString = aria_ready; } if (newString) { this.setState({ ariaLiveUpdate: newString }); this._ariaLiveUpdateTimeout = setTimeout(() => { this.setState({ ariaLiveUpdate: null }); }, 1e3); } }, 1e3); } render() { const props = this.getProps(); const { show, no_animation, figure, skeleton, aria_busy, aria_ready, className, children, ...attributes } = props; const { ariaLiveUpdate } = this.state; const showSkeleton = typeof show === 'boolean' || typeof show === 'string' ? isTrue(show) : skeleton; const params = { className: classnames(figure ? 'dnb-skeleton__figure' : 'dnb-skeleton__root', createSpacingClasses(props), className, isTrue(showSkeleton) && 'dnb-skeleton', isTrue(no_animation) && 'dnb-skeleton--no-animation'), 'aria-busy': showSkeleton, 'aria-label': showSkeleton ? aria_busy : undefined, lang: this.context.locale || LOCALE, ...attributes }; validateDOMAttributes(props, params); return React.createElement(Space, params, figure ? showSkeleton ? typeof figure === 'function' ? figure() : figure : children : React.createElement(Provider, { skeleton: showSkeleton, skeleton_no_animation: no_animation }, children), React.createElement("span", { className: "dnb-sr-only", "aria-live": "assertive" }, ariaLiveUpdate)); } } _defineProperty(Skeleton, "contextType", Context); _defineProperty(Skeleton, "defaultProps", { show: null, skeleton: null, no_animation: null, figure: null, aria_busy: null, aria_ready: null, element: null, className: null, children: null }); process.env.NODE_ENV !== "production" ? Skeleton.propTypes = { show: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), no_animation: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), figure: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.node]), aria_busy: PropTypes.string, aria_ready: PropTypes.string, element: PropTypes.node, ...spacingPropTypes, className: PropTypes.string, children: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.node]) } : void 0; function Exclude(props) { return React.createElement(Provider, _extends({}, props, { skeleton: false })); } Skeleton.Exclude = Exclude; Skeleton._supportsSpacingProps = true; //# sourceMappingURL=Skeleton.js.map