@faststore/ui 1.6.23 → 1.6.24

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/ui.esm.js CHANGED
@@ -1569,19 +1569,30 @@ const Dropdown = ({
1569
1569
  const dropdownItemsRef = useRef([]);
1570
1570
  const selectedDropdownItemIndexRef = useRef(0);
1571
1571
  const dropdownButtonRef = useRef(null);
1572
+ const close = useCallback(() => {
1573
+ var _dropdownButtonRef$cu;
1572
1574
 
1573
- const close = () => {
1574
1575
  setIsOpen(false);
1575
- };
1576
+ onDismiss == null ? void 0 : onDismiss();
1577
+ (_dropdownButtonRef$cu = dropdownButtonRef.current) == null ? void 0 : _dropdownButtonRef$cu.focus();
1578
+ }, [onDismiss]);
1576
1579
 
1577
1580
  const open = () => {
1578
1581
  setIsOpen(true);
1579
1582
  };
1580
1583
 
1581
- const toggle = () => {
1582
- setIsOpen(old => !old);
1583
- };
1584
+ const toggle = useCallback(() => {
1585
+ setIsOpen(old => {
1586
+ if (old) {
1587
+ var _dropdownButtonRef$cu2;
1588
+
1589
+ onDismiss == null ? void 0 : onDismiss();
1590
+ (_dropdownButtonRef$cu2 = dropdownButtonRef.current) == null ? void 0 : _dropdownButtonRef$cu2.focus();
1591
+ }
1584
1592
 
1593
+ return !old;
1594
+ });
1595
+ }, [onDismiss]);
1585
1596
  useEffect(() => {
1586
1597
  setIsOpen(isOpenDefault);
1587
1598
  }, [isOpenDefault]);
@@ -1590,6 +1601,30 @@ const Dropdown = ({
1590
1601
 
1591
1602
  isOpen && (dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur = dropdownItemsRef.current[0]) == null ? void 0 : _dropdownItemsRef$cur.focus());
1592
1603
  }, [isOpen]);
1604
+ useEffect(() => {
1605
+ let firstClick = true;
1606
+
1607
+ const event = e => {
1608
+ const someItemWasClicked = dropdownItemsRef == null ? void 0 : dropdownItemsRef.current.some(item => e.target === item);
1609
+
1610
+ if (firstClick) {
1611
+ firstClick = false;
1612
+ return;
1613
+ }
1614
+
1615
+ !someItemWasClicked && close();
1616
+ };
1617
+
1618
+ if (isOpen) {
1619
+ document.addEventListener('click', event);
1620
+ } else {
1621
+ document.removeEventListener('click', event);
1622
+ }
1623
+
1624
+ return () => {
1625
+ document.removeEventListener('click', event);
1626
+ };
1627
+ }, [close, isOpen]);
1593
1628
  const value = useMemo(() => {
1594
1629
  return {
1595
1630
  isOpen,
@@ -1602,7 +1637,7 @@ const Dropdown = ({
1602
1637
  dropdownItemsRef,
1603
1638
  id
1604
1639
  };
1605
- }, [id, isOpen, onDismiss]);
1640
+ }, [close, id, isOpen, onDismiss, toggle]);
1606
1641
  return React.createElement(DropdownContext.Provider, {
1607
1642
  value: value
1608
1643
  }, children);
@@ -1735,23 +1770,12 @@ const DropdownMenu = ({
1735
1770
  const {
1736
1771
  isOpen,
1737
1772
  close,
1738
- onDismiss,
1739
1773
  dropdownItemsRef,
1740
1774
  selectedDropdownItemIndexRef,
1741
1775
  id
1742
1776
  } = useDropdown();
1743
1777
  const dropdownPosition = useDropdownPosition();
1744
- const childrenLength = children.length;
1745
-
1746
- const handleBackdropClick = event => {
1747
- if (event.defaultPrevented) {
1748
- return;
1749
- }
1750
-
1751
- event.stopPropagation();
1752
- onDismiss == null ? void 0 : onDismiss();
1753
- close == null ? void 0 : close();
1754
- };
1778
+ const childrenLength = React.Children.toArray(children).length;
1755
1779
 
1756
1780
  const handleDownPress = () => {
1757
1781
  var _dropdownItemsRef$cur;
@@ -1792,7 +1816,6 @@ const DropdownMenu = ({
1792
1816
  };
1793
1817
 
1794
1818
  const handleEscapePress = () => {
1795
- onDismiss == null ? void 0 : onDismiss();
1796
1819
  close == null ? void 0 : close();
1797
1820
  };
1798
1821
 
@@ -1815,10 +1838,11 @@ const DropdownMenu = ({
1815
1838
  return null;
1816
1839
  };
1817
1840
 
1818
- return isOpen ? createPortal(React.createElement(Overlay, {
1841
+ return isOpen ? createPortal(React.createElement("div", {
1842
+ role: "presentation",
1819
1843
  "data-store-dropdown-overlay": true,
1820
- onClick: handleBackdropClick,
1821
- onKeyDown: handleBackdropKeyDown
1844
+ onKeyDown: handleBackdropKeyDown,
1845
+ "data-testid": `${testId}-overlay`
1822
1846
  }, React.createElement("div", Object.assign({
1823
1847
  role: "menu",
1824
1848
  "aria-orientation": "vertical",