@thecb/components 7.0.2-beta.4 → 7.0.2-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -22756,6 +22756,22 @@ var fallbackValues$i = {
22756
22756
  popoverTriggerColor: popoverTriggerColor
22757
22757
  };
22758
22758
 
22759
+ var arrowBorder = function arrowBorder(direction) {
22760
+ var width = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "8px";
22761
+ var angle = "".concat(width, " solid transparent");
22762
+ var straight = "".concat(width, " solid rgba(255, 255, 255, 0.85)");
22763
+
22764
+ if (direction == "down") {
22765
+ return "border-left: ".concat(angle, "; border-right: ").concat(angle, "; border-top: ").concat(straight);
22766
+ } else if (direction == "up") {
22767
+ return "border-left: ".concat(angle, "; border-right: ").concat(angle, "; border-bottom: ").concat(straight);
22768
+ } else if (direction == "left") {
22769
+ return "border-top: ".concat(angle, "; border-bottom: ").concat(angle, "; border-right: ").concat(straight);
22770
+ } else if (direction == "right") {
22771
+ return "border-top: ".concat(angle, "; border-bottom: ").concat(angle, "; border-left: ").concat(straight);
22772
+ }
22773
+ };
22774
+
22759
22775
  var Popover = function Popover(_ref) {
22760
22776
  var themeValues = _ref.themeValues,
22761
22777
  _ref$triggerText = _ref.triggerText,
@@ -22780,7 +22796,11 @@ var Popover = function Popover(_ref) {
22780
22796
  _ref$height = _ref.height,
22781
22797
  height = _ref$height === void 0 ? "auto" : _ref$height,
22782
22798
  position = _ref.position,
22783
- arrowPosition = _ref.arrowPosition;
22799
+ arrowPosition = _ref.arrowPosition,
22800
+ _ref$arrowDirection = _ref.arrowDirection,
22801
+ arrowDirection = _ref$arrowDirection === void 0 ? "down" : _ref$arrowDirection,
22802
+ transform = _ref.transform,
22803
+ buttonExtraStyles = _ref.buttonExtraStyles;
22784
22804
  var hoverColor = themeValues.hoverColor,
22785
22805
  activeColor = themeValues.activeColor,
22786
22806
  popoverTriggerColor = themeValues.popoverTriggerColor;
@@ -22855,7 +22875,8 @@ var Popover = function Popover(_ref) {
22855
22875
  "aria-labelledby": "btnPopover".concat(popoverID, "_info Disclosure").concat(popoverID),
22856
22876
  "aria-describedby": "Disclosure".concat(popoverID),
22857
22877
  "aria-controls": "Disclosed".concat(popoverID),
22858
- ref: triggerRef
22878
+ ref: triggerRef,
22879
+ extraStyles: buttonExtraStyles
22859
22880
  }, hasIcon && /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(Icon, null), /*#__PURE__*/React__default.createElement(Box, {
22860
22881
  padding: "0",
22861
22882
  srOnly: true
@@ -22874,10 +22895,10 @@ var Popover = function Popover(_ref) {
22874
22895
  tabIndex: popoverFocus && popoverOpen ? "0" : "-1",
22875
22896
  minWidth: minWidth,
22876
22897
  maxWidth: maxWidth,
22877
- extraStyles: "\n display: ".concat(popoverOpen ? "block" : "none", "; \n position: absolute; \n top: ").concat(top, "; \n right: ").concat(right, "; \n bottom: ").concat(bottom, "; \n left: ").concat(left, ";\n height: ").concat(height, ";\n ")
22898
+ extraStyles: "\n display: ".concat(popoverOpen ? "block" : "none", "; \n position: absolute; \n top: ").concat(top, "; \n right: ").concat(right, "; \n bottom: ").concat(bottom, "; \n left: ").concat(left, ";\n height: ").concat(height, ";\n transform: ").concat(transform ? transform : "none", ";\n ")
22878
22899
  }, /*#__PURE__*/React__default.createElement(Paragraph$1, null, content), /*#__PURE__*/React__default.createElement(Box, {
22879
22900
  padding: "0",
22880
- extraStyles: "position: absolute; content: \"\"; width: 0; height: 0; border-left: 8px solid transparent; border-right: 8px solid transparent; border-top: 8px solid rgba(255, 255, 255, 0.85); filter: drop-shadow(2px 8px 14px black); bottom: ".concat(arrowBottom, "; right: ").concat(arrowRight, "; top: ").concat(arrowTop, "; left: ").concat(arrowLeft, ";")
22901
+ extraStyles: "\n position: absolute;\n content: \"\";\n width: 0;\n height: 0;\n ".concat(arrowBorder(arrowDirection, "8px"), ";\n filter: drop-shadow(2px 8px 14px black);\n bottom: ").concat(arrowBottom, ";\n right: ").concat(arrowRight, ";\n top: ").concat(arrowTop, ";\n left: ").concat(arrowLeft, ";\n ")
22881
22902
  })));
22882
22903
  };
22883
22904
 
@@ -37574,13 +37595,23 @@ var ClipboardIcon = function ClipboardIcon(_ref) {
37574
37595
  var ClipboardIcon$1 = themeComponent(ClipboardIcon, "Icons", fallbackValues$2, "primary");
37575
37596
 
37576
37597
  /*
37577
- This component will render `text` and a clipboard icon. When hovered, a popover with content `initialPopoverContent` will be displayed.
37578
- When clicked, `text` will be copied to the clipboard, the popover content will change to `copiedPopoverContent` for `copiedPopoverContentDuration` milliseconds,
37579
- and the `onCopy` callback will be executed.
37598
+ This component will render `content` and a clipboard icon.
37599
+ When hovered, a popover with content `initialPopoverContent` will be displayed.
37600
+ to the left of the content.
37601
+
37602
+ When clicked, `content` will be copied to the clipboard, the popover content
37603
+ will change to `copySuccessPopoverContent` for `copiedPopoverContentDuration`
37604
+ milliseconds, and the `onCopy` callback will be executed.
37605
+
37606
+ If the content was unable to be copied to the clipboard, the popover will instead
37607
+ display `copyErrorPopoverContent`
37608
+
37609
+ If you only want the copy to clipboard behaviour without a popover, pass the prop
37610
+ `hasPopover={false}`
37580
37611
  */
37581
37612
 
37582
- var CopyableText = function CopyableText(_ref) {
37583
- var text = _ref.text,
37613
+ var CopyableContent = function CopyableContent(_ref) {
37614
+ var content = _ref.content,
37584
37615
  onClick = _ref.onClick;
37585
37616
  return /*#__PURE__*/React__default.createElement(ButtonWithAction, {
37586
37617
  "data-testid": "copyable-trigger",
@@ -37591,79 +37622,123 @@ var CopyableText = function CopyableText(_ref) {
37591
37622
  }, /*#__PURE__*/React__default.createElement(Stack, {
37592
37623
  direction: "row",
37593
37624
  childGap: "0.25rem"
37594
- }, /*#__PURE__*/React__default.createElement(Text$1, null, text), /*#__PURE__*/React__default.createElement(ClipboardIcon$1, null)));
37625
+ }, /*#__PURE__*/React__default.createElement(Text$1, null, content), /*#__PURE__*/React__default.createElement(ClipboardIcon$1, null)));
37595
37626
  };
37596
37627
 
37597
37628
  var Copyable = function Copyable(_ref2) {
37598
- var text = _ref2.text,
37629
+ var content = _ref2.content,
37599
37630
  onCopy = _ref2.onCopy,
37600
37631
  _ref2$initialPopoverC = _ref2.initialPopoverContent,
37601
37632
  initialPopoverContent = _ref2$initialPopoverC === void 0 ? "Click to copy to clipboard" : _ref2$initialPopoverC,
37602
- _ref2$copiedPopoverCo = _ref2.copiedPopoverContent,
37603
- copiedPopoverContent = _ref2$copiedPopoverCo === void 0 ? "Copied!" : _ref2$copiedPopoverCo,
37604
- _ref2$withPopover = _ref2.withPopover,
37605
- withPopover = _ref2$withPopover === void 0 ? true : _ref2$withPopover,
37633
+ _ref2$copySuccessPopo = _ref2.copySuccessPopoverContent,
37634
+ copySuccessPopoverContent = _ref2$copySuccessPopo === void 0 ? "Copied!" : _ref2$copySuccessPopo,
37635
+ _ref2$copyErrorPopove = _ref2.copyErrorPopoverContent,
37636
+ copyErrorPopoverContent = _ref2$copyErrorPopove === void 0 ? "Unable to copy value" : _ref2$copyErrorPopove,
37637
+ _ref2$copiedPopoverCo = _ref2.copiedPopoverContentDuration,
37638
+ copiedPopoverContentDuration = _ref2$copiedPopoverCo === void 0 ? 1000 : _ref2$copiedPopoverCo,
37639
+ _ref2$hasPopover = _ref2.hasPopover,
37640
+ hasPopover = _ref2$hasPopover === void 0 ? true : _ref2$hasPopover,
37606
37641
  _ref2$popoverMinWidth = _ref2.popoverMinWidth,
37607
37642
  popoverMinWidth = _ref2$popoverMinWidth === void 0 ? "210px" : _ref2$popoverMinWidth,
37608
- _ref2$copiedPopoverCo2 = _ref2.copiedPopoverContentDuration,
37609
- copiedPopoverContentDuration = _ref2$copiedPopoverCo2 === void 0 ? 1000 : _ref2$copiedPopoverCo2,
37610
37643
  _ref2$popoverID = _ref2.popoverID,
37611
37644
  popoverID = _ref2$popoverID === void 0 ? 0 : _ref2$popoverID,
37612
- extraStyles = _ref2.extraStyles;
37613
- var popoverPosition = {
37614
- top: "-65px",
37615
- right: "auto",
37616
- bottom: "auto",
37617
- left: "-50%"
37618
- };
37619
- var popoverArrowPosition = {
37620
- arrowTop: "auto",
37621
- arrowRight: "auto",
37622
- arrowBottom: "-8px",
37623
- arrowLeft: "calc(50% - 4px)" // Popover arrow is 8px wide. This places the middle of the arrow in the middle of the popover.
37624
-
37625
- };
37645
+ popoverExtraStyles = _ref2.popoverExtraStyles;
37626
37646
 
37627
37647
  var _useState = React.useState(initialPopoverContent),
37628
37648
  _useState2 = _slicedToArray(_useState, 2),
37629
37649
  popoverContent = _useState2[0],
37630
37650
  setPopoverContent = _useState2[1];
37631
37651
 
37632
- var _useState3 = React.useState(undefined),
37652
+ var _useState3 = React.useState(false),
37633
37653
  _useState4 = _slicedToArray(_useState3, 2),
37634
- timer = _useState4[0],
37635
- setTimer = _useState4[1];
37654
+ startTimer = _useState4[0],
37655
+ setStartTimer = _useState4[1];
37636
37656
 
37637
- var onClick = function onClick() {
37638
- setPopoverContent(copiedPopoverContent);
37639
- navigator.clipboard.writeText(text)["catch"](function (e) {
37640
- return console.error(e);
37641
- });
37642
- onCopy === null || onCopy === void 0 ? void 0 : onCopy();
37657
+ var _useState5 = React.useState(false),
37658
+ _useState6 = _slicedToArray(_useState5, 2),
37659
+ attemptCopy = _useState6[0],
37660
+ setAttemptCopy = _useState6[1];
37643
37661
 
37644
- if (timer) {
37645
- clearTimeout(timer);
37662
+ var _useState7 = React.useState(null),
37663
+ _useState8 = _slicedToArray(_useState7, 2),
37664
+ timeoutId = _useState8[0],
37665
+ setTimeoutId = _useState8[1];
37666
+
37667
+ var cleanup = function cleanup() {
37668
+ if (timeoutId) {
37669
+ // console.log(`Clearing timeout ${timeoutId}`);
37670
+ clearTimeout(timeoutId);
37671
+ }
37672
+ };
37673
+
37674
+ React.useEffect(function () {
37675
+ if (startTimer) {
37676
+ if (timeoutId) {
37677
+ // If there's an active timeout, clean it up.
37678
+ cleanup();
37679
+ } // Start a timeout to restore popover content to the initial value.
37680
+ // Record the ID of the timeout so it can be cleaned up later.
37681
+ // console.log("Starting timeout");
37682
+
37683
+
37684
+ setTimeoutId(setTimeout(function () {
37685
+ // console.log(`Timeout ${timeoutId} finished`);
37686
+ setPopoverContent(initialPopoverContent);
37687
+ }, copiedPopoverContentDuration)); // Set startTimer to false immediately after starting the timer,
37688
+ // so subsequent clicks will start a new timer, refreshing the duration.
37689
+
37690
+ setStartTimer(false);
37691
+ } // Return cleanup function so timeout will be cleared when component unmoumnts.
37692
+
37693
+
37694
+ return cleanup;
37695
+ }, [startTimer]);
37696
+ React.useEffect(function () {
37697
+ if (attemptCopy) {
37698
+ navigator.clipboard.writeText(content).then(function () {
37699
+ setPopoverContent(copySuccessPopoverContent);
37700
+ onCopy === null || onCopy === void 0 ? void 0 : onCopy();
37701
+ setAttemptCopy(false);
37702
+ })["catch"](function (error) {
37703
+ console.error(error);
37704
+ setPopoverContent(copyErrorPopoverContent);
37705
+ setAttemptCopy(false);
37706
+ });
37646
37707
  }
37708
+ }, [attemptCopy]);
37647
37709
 
37648
- setTimer(setTimeout(function () {
37649
- setPopoverContent(initialPopoverContent);
37650
- setTimer(undefined);
37651
- }, copiedPopoverContentDuration));
37710
+ var onClick = function onClick() {
37711
+ setStartTimer(true);
37712
+ setAttemptCopy(true);
37652
37713
  };
37653
37714
 
37654
- return withPopover ? /*#__PURE__*/React__default.createElement(Popover$1, {
37715
+ var popoverArrowWidth = "8px";
37716
+ return hasPopover ? /*#__PURE__*/React__default.createElement(Popover$1, {
37655
37717
  minWidth: popoverMinWidth,
37656
- position: popoverPosition,
37657
- arrowPosition: popoverArrowPosition,
37718
+ position: {
37719
+ top: "auto",
37720
+ right: "auto",
37721
+ bottom: "auto",
37722
+ left: "calc(-".concat(popoverArrowWidth, " - 8px)")
37723
+ },
37724
+ arrowPosition: {
37725
+ arrowTop: "auto",
37726
+ arrowRight: "-8px",
37727
+ arrowBottom: "calc(50% - ".concat(popoverArrowWidth, ")"),
37728
+ arrowLeft: "auto"
37729
+ },
37730
+ transform: "translate(-100%, -75%)",
37731
+ arrowDirection: "right",
37658
37732
  popoverID: popoverID,
37659
- extraStyles: "> button#btnPopover".concat(popoverID, " { margin: 0 }; ").concat(extraStyles),
37660
- triggerText: CopyableText({
37661
- text: text,
37733
+ buttonExtraStyles: "margin: 0",
37734
+ extraStyles: popoverExtraStyles,
37735
+ triggerText: CopyableContent({
37736
+ content: content,
37662
37737
  onClick: onClick
37663
37738
  }),
37664
37739
  content: popoverContent
37665
- }) : CopyableText({
37666
- text: text,
37740
+ }) : CopyableContent({
37741
+ content: content,
37667
37742
  onClick: onClick
37668
37743
  });
37669
37744
  };