UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

129 lines (128 loc) 5.26 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; const _excluded = ["show", "no_animation", "figure", "skeleton", "aria_busy", "aria_ready", "className", "children"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import { extendPropsWithContextInClassComponent, validateDOMAttributes, isTrue } from '../../shared/component-helper'; import { LOCALE } from '../../shared/defaults'; import Space from '../space/Space'; import { spacingPropTypes, createSpacingClasses } from '../space/SpacingHelper'; import Context from '../../shared/Context'; import Provider from '../../shared/Provider'; 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() { let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props; let context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 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 } = props, attributes = _objectWithoutProperties(props, _excluded); const { ariaLiveUpdate } = this.state; const showSkeleton = typeof show === 'boolean' || typeof show === 'string' ? isTrue(show) : skeleton; const params = _objectSpread({ 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 = _objectSpread(_objectSpread({ 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