@razorpay/blade
Version:
The Design System that powers Razorpay
181 lines (178 loc) • 7.75 kB
JavaScript
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import React__default from 'react';
import styled from 'styled-components';
import { FloatingOverlay, useFloating, useDismiss, useInteractions, FloatingPortal, FloatingFocusManager } from '@floating-ui/react';
import usePresence from 'use-presence';
import { LightBoxProvider } from './LightBoxContext.js';
import '../../utils/assignWithoutSideEffects/index.js';
import { componentZIndices } from '../../utils/componentZIndices.js';
import { useControllableState } from '../../utils/useControllable.js';
import '../../utils/makeMotionTime/index.web.js';
import { castWebType } from '../../utils/platform/castUtils.js';
import '../Button/IconButton/index.js';
import '../BladeProvider/index.js';
import '../Box/index.js';
import '../Box/BaseBox/index.js';
import '../Icons/index.js';
import { jsx, jsxs } from 'react/jsx-runtime';
import { makeMotionTime } from '../../utils/makeMotionTime/makeMotionTime.web.js';
import { BaseBox } from '../Box/BaseBox/BaseBox.web.js';
import useTheme from '../BladeProvider/useTheme.js';
import { Box } from '../Box/Box.js';
import { IconButton } from '../Button/IconButton/IconButton.js';
import CloseIcon from '../Icons/CloseIcon/CloseIcon.js';
import { assignWithoutSideEffects } from '../../utils/assignWithoutSideEffects/assignWithoutSideEffects.js';
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; }
var StyledBackdrop = /*#__PURE__*/styled(FloatingOverlay).withConfig({
displayName: "LightBoxweb__StyledBackdrop",
componentId: "sc-1g6cr2-0"
})(function (_ref) {
var theme = _ref.theme,
$isVisible = _ref.$isVisible;
return {
backgroundColor: theme.colors.overlay.background.subtle,
transitionProperty: 'opacity',
backdropFilter: "blur(".concat(theme.backdropBlur.high, "px)"),
transitionDuration: "".concat(makeMotionTime(theme.motion.duration.moderate)),
transitionTimingFunction: $isVisible ? castWebType(theme.motion.easing.entrance) : castWebType(theme.motion.easing.exit),
opacity: $isVisible ? 1 : 0
};
});
var StyledLightBoxContent = /*#__PURE__*/styled(BaseBox).withConfig({
displayName: "LightBoxweb__StyledLightBoxContent",
componentId: "sc-1g6cr2-1"
})(function (_ref2) {
var theme = _ref2.theme,
$isVisible = _ref2.$isVisible;
return {
opacity: $isVisible ? 1 : 0,
transitionProperty: 'opacity',
transitionDuration: "".concat(makeMotionTime(theme.motion.duration.moderate)),
transitionTimingFunction: $isVisible ? castWebType(theme.motion.easing.entrance) : castWebType(theme.motion.easing.exit)
};
});
var LightBoxBackdrop = function LightBoxBackdrop(_ref3) {
var isVisible = _ref3.isVisible;
return /*#__PURE__*/jsx(StyledBackdrop, {
$isVisible: isVisible,
lockScroll: true
});
};
var _LightBox = function _LightBox(_ref4) {
var isOpen = _ref4.isOpen,
onDismiss = _ref4.onDismiss,
activeIndexProp = _ref4.activeIndex,
defaultActiveIndex = _ref4.defaultActiveIndex,
onIndexChange = _ref4.onIndexChange,
_ref4$accessibilityLa = _ref4.accessibilityLabel,
accessibilityLabel = _ref4$accessibilityLa === void 0 ? 'Media viewer' : _ref4$accessibilityLa,
children = _ref4.children;
var _useTheme = useTheme(),
theme = _useTheme.theme;
var _usePresence = usePresence(isOpen, {
transitionDuration: theme.motion.duration.moderate,
initialEnter: true
}),
isMounted = _usePresence.isMounted,
isVisible = _usePresence.isVisible;
var _useControllableState = useControllableState({
value: activeIndexProp,
defaultValue: defaultActiveIndex !== null && defaultActiveIndex !== void 0 ? defaultActiveIndex : 0,
onChange: function onChange(index) {
onIndexChange === null || onIndexChange === void 0 || onIndexChange({
index: index
});
}
}),
_useControllableState2 = _slicedToArray(_useControllableState, 2),
activeIndex = _useControllableState2[0],
setActiveIndex = _useControllableState2[1];
var handleIndexChange = function handleIndexChange(index) {
setActiveIndex(function () {
return index;
});
};
var _useFloating = useFloating({
open: isOpen,
onOpenChange: function onOpenChange(open) {
if (!open) onDismiss === null || onDismiss === void 0 || onDismiss();
}
}),
refs = _useFloating.refs,
context = _useFloating.context;
var dismiss = useDismiss(context);
var _useInteractions = useInteractions([dismiss]),
getFloatingProps = _useInteractions.getFloatingProps;
var defaultInitialFocusRef = React__default.useRef(null);
return /*#__PURE__*/jsx(LightBoxProvider, {
value: {
activeIndex: activeIndex,
handleIndexChange: handleIndexChange
},
children: /*#__PURE__*/jsx(FloatingPortal, {
children: isMounted ? /*#__PURE__*/jsxs(Box, {
position: "fixed",
zIndex: componentZIndices.modal,
children: [/*#__PURE__*/jsx(LightBoxBackdrop, {
isVisible: isVisible
}), /*#__PURE__*/jsx(FloatingFocusManager, {
returnFocus: true,
initialFocus: defaultInitialFocusRef,
context: context,
modal: true,
children: /*#__PURE__*/jsxs(StyledLightBoxContent, _objectSpread(_objectSpread({
ref: refs.setFloating
}, getFloatingProps()), {}, {
$isVisible: isVisible,
role: "dialog",
"aria-label": accessibilityLabel,
"aria-modal": true,
position: "fixed",
top: "spacing.0",
right: "spacing.0",
bottom: "spacing.0",
left: "spacing.0",
display: "flex",
height: "100%",
width: "100%",
alignItems: "center",
justifyContent: "center",
padding: "spacing.6",
onClick: function onClick() {
onDismiss === null || onDismiss === void 0 || onDismiss();
},
children: [/*#__PURE__*/jsx(Box, {
position: "absolute",
top: "spacing.6",
right: "spacing.6",
zIndex: 1,
children: /*#__PURE__*/jsx(IconButton, {
ref: defaultInitialFocusRef,
icon: CloseIcon
// eslint-disable-next-line @typescript-eslint/no-empty-function
,
onClick: function onClick() {} // onClick of parent already handles dismiss
,
accessibilityLabel: "Close lightbox",
emphasis: "subtle",
size: "large"
})
}), /*#__PURE__*/jsx(BaseBox, {
position: "relative",
height: "100%",
width: "100%",
children: children
})]
}))
})]
}) : null
})
});
};
var LightBox = /*#__PURE__*/assignWithoutSideEffects(_LightBox, {
displayName: 'LightBox'
});
export { LightBox };
//# sourceMappingURL=LightBox.web.js.map