@atlaskit/form
Version:
A form allows people to input information.
138 lines (133 loc) • 4.71 kB
JavaScript
/* message.tsx generated by @compiled/babel-plugin v3.0.2 */
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "./message.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { useContext, useEffect, useRef, useState } from 'react';
import ErrorIcon from '@atlaskit/icon/core/status-error';
import SuccessIcon from '@atlaskit/icon/core/status-success';
import { useMotion } from '@atlaskit/motion/entering/use-motion';
import { fg } from '@atlaskit/platform-feature-flags/fg';
import { MessageWrapperContext } from './message-context';
import MessageTrack from './message-track';
/**
* API for the internal `<Message />` component. This is not public API.
*/
/**
* Public API of the various message components.
*/
var messageStyles = null;
var messageAppearanceStyles = {
default: "_syaz1rpy",
error: "_syaz1tmw",
valid: "_syazgsak"
};
var iconWrapperStyles = {
root: "_1e0c1txw _4t3i7vkz _4cvr1h6o"
};
var IconWrapper = function IconWrapper(_ref) {
var children = _ref.children;
return /*#__PURE__*/React.createElement("span", {
className: ax([iconWrapperStyles.root])
}, children);
};
var messageIcons = {
error: /*#__PURE__*/React.createElement(ErrorIcon, {
color: "currentColor",
label: "error",
size: "small"
}),
valid: /*#__PURE__*/React.createElement(SuccessIcon, {
color: "currentColor",
label: "success",
size: "small"
})
};
var MessageRow = function MessageRow(_ref2) {
var children = _ref2.children,
_ref2$appearance = _ref2.appearance,
appearance = _ref2$appearance === void 0 ? 'default' : _ref2$appearance,
fieldId = _ref2.fieldId,
_ref2$isExiting = _ref2.isExiting,
isExiting = _ref2$isExiting === void 0 ? false : _ref2$isExiting,
testId = _ref2.testId;
var icon = messageIcons[appearance];
var messageRef = useRef(null);
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
hasMessageWrapper = _useState2[0],
setHasMessageWrapper = _useState2[1];
var _useContext = useContext(MessageWrapperContext),
isWrapper = _useContext.isWrapper;
useEffect(function () {
if (messageRef.current) {
setHasMessageWrapper(isWrapper);
}
}, [isWrapper]);
/**
* The wrapping span is necessary to preserve spaces between children.
* Otherwise the flex layout of the message will remove any whitespace
* between children.
*
* If the child is just a string, this is not required and we can use one
* less DOM element.
*/
var content = typeof children === 'string' ? children : /*#__PURE__*/React.createElement("span", null, children);
return /*#__PURE__*/React.createElement("div", {
"data-testid": testId,
id: isExiting ? undefined : fieldId,
ref: messageRef
// For backwards compatability, if there is a wrapper, aria-live is not needed
,
"aria-live": !hasMessageWrapper ? 'polite' : undefined,
"aria-hidden": isExiting ? true : undefined,
className: ax(["_zulp12x7 _11c8wadc _1e0c1txw _1bah1q9y _1pfh1b66", messageAppearanceStyles[appearance]])
}, icon && /*#__PURE__*/React.createElement(IconWrapper, null, icon), content);
};
var MotionMessage = function MotionMessage(props) {
var isErrorMessage = props.appearance === 'error';
var _useContext2 = useContext(MessageWrapperContext),
hasMotionBoundary = _useContext2.hasMotionBoundary,
transition = _useContext2.transition;
var shouldAnimate = isErrorMessage || hasMotionBoundary;
var _useMotion = useMotion({
initialState: shouldAnimate && transition !== 'instant' ? undefined : 'visible'
}),
ref = _useMotion.ref,
state = _useMotion.state;
var row = /*#__PURE__*/React.createElement(MessageRow, {
appearance: props.appearance,
fieldId: props.fieldId,
isExiting: state === 'exiting',
testId: props.testId
}, props.children);
if (!shouldAnimate) {
return row;
}
return /*#__PURE__*/React.createElement(MessageTrack, {
state: state,
transition: transition,
motionRef: ref,
testId: props.testId && "".concat(props.testId, "-motion")
}, row);
};
/**
* __Message__
*
* A message component for displaying messages in a form.
*/
var Message = function Message(props) {
if (!fg('platform-dst-motion-uplift-input')) {
return /*#__PURE__*/React.createElement(MessageRow, {
appearance: props.appearance,
fieldId: props.fieldId,
testId: props.testId
}, props.children);
}
return /*#__PURE__*/React.createElement(MotionMessage, {
appearance: props.appearance,
fieldId: props.fieldId,
testId: props.testId
}, props.children);
};
export default Message;