@atlaskit/flag
Version:
A flag is used for confirmations, alerts, and acknowledgments that require minimal user interaction, often displayed using a flag group.
203 lines (201 loc) • 8.04 kB
JavaScript
/* flag.tsx generated by @compiled/babel-plugin v3.0.2 */
import _extends from "@babel/runtime/helpers/extends";
import "./flag.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
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';
const styles = {
overflowWrap: "_1i4qfg65",
flag: "_2rko1mok _1reo15vq _18m915vq _v564z97a _16qs130s _1pbyni7l _1bsb1osq",
contentStack: "_k8m01vax _1oec1mzy _156yru3m"
};
const CSS_VAR_ICON_COLOR = '--flag-icon-color';
const descriptionStyles = null;
const flagWrapperStyles = null;
const iconWrapperStyles = null;
const transitionStyles = null;
const 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)
*/
const Flag = props => {
const {
actions = [],
appearance = DEFAULT_APPEARANCE,
icon,
title,
description,
linkComponent,
onMouseOver,
onFocus = noop,
onMouseOut,
onBlur = noop,
onDismissed: onDismissedProp = noop,
testId,
id,
analyticsContext,
delayAnnouncement,
headingLevel = 2
} = props;
const [isDelayToAnnounce, setIsDelayToAnnounce] = useState(false);
const {
onDismissed: onDismissedFromFlagGroup,
isDismissAllowed
} = useFlagGroup();
const onDismissed = useCallback((id, analyticsEvent) => {
onDismissedProp(id, analyticsEvent);
onDismissedFromFlagGroup(id, analyticsEvent);
}, [onDismissedProp, onDismissedFromFlagGroup]);
const [isExpanded, setIsExpanded] = useState(false);
const onDismissedAnalytics = usePlatformLeafEventHandler({
fn: onDismissed,
action: 'dismissed',
analyticsData: analyticsContext,
...analyticsAttributes
});
const isBold = appearance !== DEFAULT_APPEARANCE;
const toggleExpand = useCallback(() => {
setIsExpanded(previous => !previous);
}, []);
const buttonActionCallback = useCallback(() => {
if (isDismissAllowed) {
onDismissedAnalytics(id);
}
}, [onDismissedAnalytics, id, isDismissAllowed]);
useEffect(() => {
// 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(() => {
if (!delayAnnouncement) {
return;
}
setTimeout(() => {
setIsDelayToAnnounce(true);
}, delayAnnouncement);
}, [delayAnnouncement]);
const onFocusAnalytics = usePlatformLeafEventHandler({
fn: onFocus,
action: 'focused',
analyticsData: analyticsContext,
...analyticsAttributes
});
const onBlurAnalytics = usePlatformLeafEventHandler({
fn: onBlur,
action: 'blurred',
analyticsData: analyticsContext,
...analyticsAttributes
});
const autoDismissProps = {
onMouseOver,
onFocus: onFocusAnalytics,
onMouseOut,
onBlur: onBlurAnalytics
};
const textColor = flagTextColor[appearance];
const iconColor = flagIconColor[appearance];
const iconGlyph = flagIconGlyph[appearance];
const isDismissable = isBold || isDismissAllowed;
const shouldRenderGap = !isBold && (description || actions.length) || isExpanded;
const isCollapseAnimationEnabled = fg('platform-dst-flag-collapse-animation-fix');
// when delayAnnouncement is available we will use a hidden content for announcement
const 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", {
// eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
style: {
[CSS_VAR_ICON_COLOR]: iconColor
},
"data-testid": testId && `${testId}-icon-container`,
className: ax(["_1e0c1txw _1ul91tcg _1tke1tcg _4cvr1h6o _1bah1h6o _1o9zidpf _syaz1jn0"])
}, 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 && `${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${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 && `${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;