UNPKG

@atlaskit/flag

Version:

A flag is used for confirmations, alerts, and acknowledgments that require minimal user interaction, often displayed using a flag group.

214 lines (212 loc) 9.93 kB
/* flag.tsx generated by @compiled/babel-plugin v3.0.2 */ import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import "./flag.compiled.css"; import * as React from 'react'; import { ax, ix } from "@compiled/react/runtime"; 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 { useCallback, useEffect, useState } from 'react'; import { usePlatformLeafEventHandler } from '@atlaskit/analytics-next/usePlatformLeafEventHandler'; import noop from '@atlaskit/ds-lib/noop'; import Heading from '@atlaskit/heading/heading'; import { fg } from '@atlaskit/platform-feature-flags/fg'; import { Box, Inline, Stack } from '@atlaskit/primitives/compiled'; import VisuallyHidden from '@atlaskit/visually-hidden/visually-hidden'; import { DEFAULT_APPEARANCE } from './constants'; import Actions from './flag-actions'; import { flagBackgroundColor } from './flag-background-color'; import { flagIconColor } from './flag-icon-color'; import { flagIconGlyph } from './flag-icon-glyph'; import { flagTextColor } from './flag-text-color'; import { flagTextColorToken } from './flag-text-color-token'; import { default as DismissButton } from './internal/dismiss-button'; import { default as Expander } from './internal/expander'; import { useFlagGroup } from './internal/use-flag-group'; var styles = { overflowWrap: "_1i4qfg65", flag: "_2rko1mok _1reo15vq _18m915vq _v564z97a _16qs130s _1pbyni7l _1bsb1osq", contentStack: "_k8m01vax _1oec1mzy _156yru3m" }; var CSS_VAR_ICON_COLOR = '--flag-icon-color'; var descriptionStyles = null; var flagWrapperStyles = null; var iconWrapperStyles = null; var transitionStyles = null; var analyticsAttributes = { componentName: 'flag', packageName: "@atlaskit/flag", packageVersion: "18.5.5" }; /** * __Flag__ * * A flag is used for confirmations, alerts, and acknowledgments that require minimal user interaction, * often displayed using a flag group. * * - [Examples](https://atlassian.design/components/flag/examples) * - [Code](https://atlassian.design/components/flag/code) * - [Usage](https://atlassian.design/components/flag/usage) */ var Flag = function Flag(props) { var _props$actions = props.actions, actions = _props$actions === void 0 ? [] : _props$actions, _props$appearance = props.appearance, appearance = _props$appearance === void 0 ? DEFAULT_APPEARANCE : _props$appearance, icon = props.icon, title = props.title, description = props.description, linkComponent = props.linkComponent, onMouseOver = props.onMouseOver, _props$onFocus = props.onFocus, onFocus = _props$onFocus === void 0 ? noop : _props$onFocus, onMouseOut = props.onMouseOut, _props$onBlur = props.onBlur, onBlur = _props$onBlur === void 0 ? noop : _props$onBlur, _props$onDismissed = props.onDismissed, onDismissedProp = _props$onDismissed === void 0 ? noop : _props$onDismissed, testId = props.testId, id = props.id, analyticsContext = props.analyticsContext, delayAnnouncement = props.delayAnnouncement, _props$headingLevel = props.headingLevel, headingLevel = _props$headingLevel === void 0 ? 2 : _props$headingLevel; var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), isDelayToAnnounce = _useState2[0], setIsDelayToAnnounce = _useState2[1]; var _useFlagGroup = useFlagGroup(), onDismissedFromFlagGroup = _useFlagGroup.onDismissed, isDismissAllowed = _useFlagGroup.isDismissAllowed; var onDismissed = useCallback(function (id, analyticsEvent) { onDismissedProp(id, analyticsEvent); onDismissedFromFlagGroup(id, analyticsEvent); }, [onDismissedProp, onDismissedFromFlagGroup]); var _useState3 = useState(false), _useState4 = _slicedToArray(_useState3, 2), isExpanded = _useState4[0], setIsExpanded = _useState4[1]; var onDismissedAnalytics = usePlatformLeafEventHandler(_objectSpread({ fn: onDismissed, action: 'dismissed', analyticsData: analyticsContext }, analyticsAttributes)); var isBold = appearance !== DEFAULT_APPEARANCE; var toggleExpand = useCallback(function () { setIsExpanded(function (previous) { return !previous; }); }, []); var buttonActionCallback = useCallback(function () { if (isDismissAllowed) { onDismissedAnalytics(id); } }, [onDismissedAnalytics, id, isDismissAllowed]); useEffect(function () { // If buttons are removed as a prop, update isExpanded to be false if (isBold && isExpanded && !description && !actions.length) { setIsExpanded(false); } }, [actions.length, description, isBold, isExpanded]); useEffect(function () { if (!delayAnnouncement) { return; } setTimeout(function () { setIsDelayToAnnounce(true); }, delayAnnouncement); }, [delayAnnouncement]); var onFocusAnalytics = usePlatformLeafEventHandler(_objectSpread({ fn: onFocus, action: 'focused', analyticsData: analyticsContext }, analyticsAttributes)); var onBlurAnalytics = usePlatformLeafEventHandler(_objectSpread({ fn: onBlur, action: 'blurred', analyticsData: analyticsContext }, analyticsAttributes)); var autoDismissProps = { onMouseOver: onMouseOver, onFocus: onFocusAnalytics, onMouseOut: onMouseOut, onBlur: onBlurAnalytics }; var textColor = flagTextColor[appearance]; var iconColor = flagIconColor[appearance]; var iconGlyph = flagIconGlyph[appearance]; var isDismissable = isBold || isDismissAllowed; var shouldRenderGap = !isBold && (description || actions.length) || isExpanded; var isCollapseAnimationEnabled = fg('platform-dst-flag-collapse-animation-fix'); // when delayAnnouncement is available we will use a hidden content for announcement var delayedAnnouncement = delayAnnouncement ? /*#__PURE__*/React.createElement(VisuallyHidden, null, title, description) : undefined; // A11y improvement opportunity: all flags currently use role="alert" (assertive), // but non-critical flags (e.g. info, success) should use role="status" (polite) // to avoid interrupting screen reader users. See WCAG 4.1.3. return /*#__PURE__*/React.createElement("div", _extends({ role: "alert", "data-testid": testId }, autoDismissProps, { className: ax(["_1bsb1osq"]) }), /*#__PURE__*/React.createElement(Box, { padding: "space.200", backgroundColor: flagBackgroundColor[appearance], xcss: styles.flag }, /*#__PURE__*/React.createElement(Inline, { alignBlock: "start", space: "space.200" }, /*#__PURE__*/React.createElement("div", { "data-testid": testId && "".concat(testId, "-icon-container"), className: ax(["_1e0c1txw _1ul91tcg _1tke1tcg _4cvr1h6o _1bah1h6o _1o9zidpf _syaz16r9"]), style: _objectSpread(_objectSpread({}, _defineProperty({}, CSS_VAR_ICON_COLOR, iconColor)), {}, { "--_11v68d2": ix("var(".concat(CSS_VAR_ICON_COLOR, ")")) }) }, icon || iconGlyph), /*#__PURE__*/React.createElement("span", { className: ax(["_v5641hdc _1ul9idpf _16jlkb7n"]) }, /*#__PURE__*/React.createElement(Stack, { space: shouldRenderGap ? 'space.100' : 'space.0' // Gap exists even when not expanded due to Expander internals always being in the DOM , xcss: isCollapseAnimationEnabled ? styles.contentStack : undefined, testId: testId && "".concat(testId, "-content-stack") }, isDelayToAnnounce && delayedAnnouncement, /*#__PURE__*/React.createElement(Inline, { alignBlock: "stretch", space: "space.100", spread: "space-between" }, /*#__PURE__*/React.createElement(Box, { paddingBlockStart: "space.050", paddingBlockEnd: "space.025", xcss: styles.overflowWrap }, /*#__PURE__*/React.createElement(Heading, { as: "h".concat(headingLevel), size: "xsmall", color: textColor }, /*#__PURE__*/React.createElement("span", { /* if isDelayToAnnounce is true, we will hide the content form screen reader to avoid duplicate announcement */ "aria-hidden": isDelayToAnnounce }, title))), isDismissable ? !(isBold && !description && !actions.length) && /*#__PURE__*/React.createElement(DismissButton, { testId: testId, appearance: appearance, isBold: isBold, isExpanded: isExpanded, onClick: isBold ? toggleExpand : buttonActionCallback }) : null), /*#__PURE__*/React.createElement(Expander, { isExpanded: !isBold || isExpanded, testId: testId }, description && /*#__PURE__*/React.createElement("div", { // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop, @atlaskit/ui-styling-standard/no-imported-style-values -- Ignored via go/DSP-18766 style: { color: flagTextColorToken[appearance] }, "data-testid": testId && "".concat(testId, "-description") /* if isDelayToAnnounce is true, we will hide the content form screen reader to avoid duplicate announcement */, "aria-hidden": isDelayToAnnounce, className: ax(["_11c8fhey _1reo1wug _18m91wug _c71l53f4 _1i4qfg65"]) }, description), /*#__PURE__*/React.createElement(Actions, { actions: actions, appearance: appearance, linkComponent: linkComponent, testId: testId }))))))); }; export default Flag;