@faststore/ui 1.6.21 → 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/CHANGELOG.md CHANGED
@@ -3,6 +3,39 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## 1.6.24 (2022-03-29)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * fix dropdown click bug ([#1196](https://github.com/vtex/faststore/issues/1196)) ([55202ea](https://github.com/vtex/faststore/commit/55202ead64933b71688ac7399211cc2f086596f2))
12
+
13
+
14
+
15
+
16
+
17
+ ## 1.6.23 (2022-03-28)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * fix breadcrumb component bug ([#1193](https://github.com/vtex/faststore/issues/1193)) ([f36ba81](https://github.com/vtex/faststore/commit/f36ba814ed679523d8c1f030c31a086a9e5bb190))
23
+
24
+
25
+
26
+
27
+
28
+ ## 1.6.22 (2022-03-25)
29
+
30
+
31
+ ### Features
32
+
33
+ * **sdk:** Improve useStorage and Optimistic revalidate effect ([#1186](https://github.com/vtex/faststore/issues/1186)) ([08c616b](https://github.com/vtex/faststore/commit/08c616be4c6bc5a215da7ab0c37ff2731aa09f9a))
34
+
35
+
36
+
37
+
38
+
6
39
  ## 1.6.21 (2022-03-25)
7
40
 
8
41
  **Note:** Version bump only for package @faststore/ui
@@ -1,4 +1,4 @@
1
- import type { AriaAttributes, KeyboardEvent, MouseEvent, PropsWithChildren, ReactElement } from 'react';
1
+ import type { AriaAttributes, KeyboardEvent, PropsWithChildren, MouseEvent, ReactNode } from 'react';
2
2
  import React from 'react';
3
3
  import type { ModalContentProps } from '../Modal/ModalContent';
4
4
  export interface DropdownMenuProps extends ModalContentProps {
@@ -16,7 +16,7 @@ export interface DropdownMenuProps extends ModalContentProps {
16
16
  * the dialog.
17
17
  */
18
18
  onDismiss?: (event: MouseEvent | KeyboardEvent) => void;
19
- children: ReactElement[];
19
+ children: ReactNode[] | ReactNode;
20
20
  }
21
21
  declare const DropdownMenu: ({ children, testId, style, ...otherProps }: PropsWithChildren<DropdownMenuProps>) => React.ReactPortal | null;
22
22
  export default DropdownMenu;
@@ -1148,7 +1148,6 @@ const Breadcrumb = /*#__PURE__*/React.forwardRef(function Breadcrumb({
1148
1148
  testId = 'store-breadcrumb',
1149
1149
  ...otherProps
1150
1150
  }, ref) {
1151
- const lastIndex = React__default.Children.count(children);
1152
1151
  return React__default.createElement("nav", Object.assign({
1153
1152
  "aria-label": "Breadcrumb",
1154
1153
  role: "navigation",
@@ -1158,8 +1157,8 @@ const Breadcrumb = /*#__PURE__*/React.forwardRef(function Breadcrumb({
1158
1157
  }, otherProps), React__default.createElement(List, {
1159
1158
  "data-breadcrumb-list": true,
1160
1159
  variant: "ordered"
1161
- }, React__default.Children.map(children, (child, index) => {
1162
- const isLastItem = index === lastIndex - 1;
1160
+ }, React__default.Children.toArray(children).map((child, index, childrenArray) => {
1161
+ const isLastItem = index === childrenArray.length - 1;
1163
1162
  return React__default.createElement(ListItem, {
1164
1163
  isLastItem: isLastItem,
1165
1164
  divider: rawDivider,
@@ -1578,19 +1577,30 @@ const Dropdown = ({
1578
1577
  const dropdownItemsRef = React.useRef([]);
1579
1578
  const selectedDropdownItemIndexRef = React.useRef(0);
1580
1579
  const dropdownButtonRef = React.useRef(null);
1580
+ const close = React.useCallback(() => {
1581
+ var _dropdownButtonRef$cu;
1581
1582
 
1582
- const close = () => {
1583
1583
  setIsOpen(false);
1584
- };
1584
+ onDismiss == null ? void 0 : onDismiss();
1585
+ (_dropdownButtonRef$cu = dropdownButtonRef.current) == null ? void 0 : _dropdownButtonRef$cu.focus();
1586
+ }, [onDismiss]);
1585
1587
 
1586
1588
  const open = () => {
1587
1589
  setIsOpen(true);
1588
1590
  };
1589
1591
 
1590
- const toggle = () => {
1591
- setIsOpen(old => !old);
1592
- };
1592
+ const toggle = React.useCallback(() => {
1593
+ setIsOpen(old => {
1594
+ if (old) {
1595
+ var _dropdownButtonRef$cu2;
1596
+
1597
+ onDismiss == null ? void 0 : onDismiss();
1598
+ (_dropdownButtonRef$cu2 = dropdownButtonRef.current) == null ? void 0 : _dropdownButtonRef$cu2.focus();
1599
+ }
1593
1600
 
1601
+ return !old;
1602
+ });
1603
+ }, [onDismiss]);
1594
1604
  React.useEffect(() => {
1595
1605
  setIsOpen(isOpenDefault);
1596
1606
  }, [isOpenDefault]);
@@ -1599,6 +1609,30 @@ const Dropdown = ({
1599
1609
 
1600
1610
  isOpen && (dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur = dropdownItemsRef.current[0]) == null ? void 0 : _dropdownItemsRef$cur.focus());
1601
1611
  }, [isOpen]);
1612
+ React.useEffect(() => {
1613
+ let firstClick = true;
1614
+
1615
+ const event = e => {
1616
+ const someItemWasClicked = dropdownItemsRef == null ? void 0 : dropdownItemsRef.current.some(item => e.target === item);
1617
+
1618
+ if (firstClick) {
1619
+ firstClick = false;
1620
+ return;
1621
+ }
1622
+
1623
+ !someItemWasClicked && close();
1624
+ };
1625
+
1626
+ if (isOpen) {
1627
+ document.addEventListener('click', event);
1628
+ } else {
1629
+ document.removeEventListener('click', event);
1630
+ }
1631
+
1632
+ return () => {
1633
+ document.removeEventListener('click', event);
1634
+ };
1635
+ }, [close, isOpen]);
1602
1636
  const value = React.useMemo(() => {
1603
1637
  return {
1604
1638
  isOpen,
@@ -1611,7 +1645,7 @@ const Dropdown = ({
1611
1645
  dropdownItemsRef,
1612
1646
  id
1613
1647
  };
1614
- }, [id, isOpen, onDismiss]);
1648
+ }, [close, id, isOpen, onDismiss, toggle]);
1615
1649
  return React__default.createElement(DropdownContext.Provider, {
1616
1650
  value: value
1617
1651
  }, children);
@@ -1744,23 +1778,12 @@ const DropdownMenu = ({
1744
1778
  const {
1745
1779
  isOpen,
1746
1780
  close,
1747
- onDismiss,
1748
1781
  dropdownItemsRef,
1749
1782
  selectedDropdownItemIndexRef,
1750
1783
  id
1751
1784
  } = useDropdown();
1752
1785
  const dropdownPosition = useDropdownPosition();
1753
- const childrenLength = children.length;
1754
-
1755
- const handleBackdropClick = event => {
1756
- if (event.defaultPrevented) {
1757
- return;
1758
- }
1759
-
1760
- event.stopPropagation();
1761
- onDismiss == null ? void 0 : onDismiss();
1762
- close == null ? void 0 : close();
1763
- };
1786
+ const childrenLength = React__default.Children.toArray(children).length;
1764
1787
 
1765
1788
  const handleDownPress = () => {
1766
1789
  var _dropdownItemsRef$cur;
@@ -1801,7 +1824,6 @@ const DropdownMenu = ({
1801
1824
  };
1802
1825
 
1803
1826
  const handleEscapePress = () => {
1804
- onDismiss == null ? void 0 : onDismiss();
1805
1827
  close == null ? void 0 : close();
1806
1828
  };
1807
1829
 
@@ -1824,10 +1846,11 @@ const DropdownMenu = ({
1824
1846
  return null;
1825
1847
  };
1826
1848
 
1827
- return isOpen ? reactDom.createPortal(React__default.createElement(Overlay, {
1849
+ return isOpen ? reactDom.createPortal(React__default.createElement("div", {
1850
+ role: "presentation",
1828
1851
  "data-store-dropdown-overlay": true,
1829
- onClick: handleBackdropClick,
1830
- onKeyDown: handleBackdropKeyDown
1852
+ onKeyDown: handleBackdropKeyDown,
1853
+ "data-testid": `${testId}-overlay`
1831
1854
  }, React__default.createElement("div", Object.assign({
1832
1855
  role: "menu",
1833
1856
  "aria-orientation": "vertical",