UNPKG

@atlaskit/onboarding

Version:

An onboarding spotlight introduces new features to users through focused messages or multi-step tours.

152 lines (150 loc) 7.04 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; 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; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } import React, { createContext, PureComponent, startTransition } from 'react'; import memoizeOne from 'memoize-one'; import noop from '@atlaskit/ds-lib/noop'; import ExitingPersistence from '@atlaskit/motion/exiting-persistence'; import FadeIn from '@atlaskit/motion/fade-in'; import Portal from '@atlaskit/portal/portal'; import Blanket from '../styled/blanket'; var dest = /*#__PURE__*/createContext(undefined); var TargetConsumer = dest.Consumer; var TargetProvider = dest.Provider; var SpotlightContext = /*#__PURE__*/createContext({ opened: noop, closed: noop, targets: {} }); var SpotlightStateConsumer = SpotlightContext.Consumer; var SpotlightStateProvider = SpotlightContext.Provider; export { TargetConsumer }; // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports export { SpotlightContext, SpotlightStateConsumer as SpotlightConsumer }; var Container = function Container(_ref) { var Wrapper = _ref.component, children = _ref.children; return /*#__PURE__*/React.createElement(Wrapper, null, children); }; /** * __Spotlight manager__ * * A spotlight manager manages the visibility of spotlights used to introduce new features to users through focused messages or multi-step tours. * * - [Examples](https://atlassian.design/components/onboarding/examples) * - [Code](https://atlassian.design/components/onboarding/code) * - [Usage](https://atlassian.design/components/onboarding/usage) * * @deprecated Use `@atlaskit/spotlight` instead. */ // eslint-disable-next-line @repo/internal/react/no-class-components, @atlaskit/volt-strict-mode/no-multiple-exports var SpotlightManager = /*#__PURE__*/function (_PureComponent) { function SpotlightManager() { var _this; _classCallCheck(this, SpotlightManager); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, SpotlightManager, [].concat(args)); _defineProperty(_this, "state", { spotlightCount: 0, targets: {} }); /* * When enabling React Streaming in Confluence, a * "This Suspense boundary received an update before it finished hydrating. This caused the boundary to switch to client rendering" * error happens. * This is to fix this error by wrapping the state update in startTransition as suggested by React: https://react.dev/errors/421?invariant=421 */ _defineProperty(_this, "getTargetRef", function (name) { return function (element) { startTransition(function () { _this.setState(function (state) { return { targets: _objectSpread(_objectSpread({}, state.targets), {}, _defineProperty({}, name, element || undefined)) }; }); }); }; }); _defineProperty(_this, "spotlightOpen", function () { _this.setState(function (state) { return { spotlightCount: state.spotlightCount + 1 }; }); }); _defineProperty(_this, "spotlightClose", function () { _this.setState(function (state) { return { spotlightCount: state.spotlightCount - 1 }; }); }); _defineProperty(_this, "getStateProviderValue", memoizeOne(function (targets) { return { opened: _this.spotlightOpen, closed: _this.spotlightClose, targets: targets }; })); return _this; } _inherits(SpotlightManager, _PureComponent); return _createClass(SpotlightManager, [{ key: "componentDidMount", value: function componentDidMount() { if (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production' && !process.env.CI) { if (this.props.component) { // eslint-disable-next-line no-console console.warn("Atlaskit: The SpotlightManager 'component' prop is deprecated. Please wrap the SpotlightManager in the component instead."); } } } }, { key: "render", value: function render() { var _this$props = this.props, blanketIsTinted = _this$props.blanketIsTinted, children = _this$props.children, Tag = _this$props.component, onBlanketClicked = _this$props.onBlanketClicked; var isActive = this.state.spotlightCount > 0; return /*#__PURE__*/React.createElement(SpotlightStateProvider, { value: this.getStateProviderValue(this.state.targets) }, /*#__PURE__*/React.createElement(TargetProvider, { value: this.getTargetRef }, /*#__PURE__*/React.createElement(Container, { component: Tag || React.Fragment }, /*#__PURE__*/React.createElement(ExitingPersistence, null, isActive && /*#__PURE__*/React.createElement(FadeIn, { duration: "medium" }, function (_ref2) { var ref = _ref2.ref, className = _ref2.className, style = _ref2.style; return /*#__PURE__*/React.createElement(Portal, { zIndex: 700 }, /*#__PURE__*/React.createElement(Blanket, { ref: ref // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop , className: className, style: style, isTinted: blanketIsTinted, onBlanketClicked: onBlanketClicked })); })), children))); } }]); }(PureComponent); _defineProperty(SpotlightManager, "defaultProps", { blanketIsTinted: true }); export { SpotlightManager as default };