@faststore/ui 1.6.17 → 1.6.20

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.20 (2022-03-24)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * fix dropdown component bug ([#1192](https://github.com/vtex/faststore/issues/1192)) ([f188ac0](https://github.com/vtex/faststore/commit/f188ac0c59db5a0ef7a95eb2412d0bbcd024ae36))
12
+
13
+
14
+
15
+
16
+
17
+ ## 1.6.19 (2022-03-24)
18
+
19
+
20
+ ### Features
21
+
22
+ * Add exports to dropdown ([#1188](https://github.com/vtex/faststore/issues/1188)) ([f24359a](https://github.com/vtex/faststore/commit/f24359a34439e0ec8d3069483c31a6f62fa6535d))
23
+
24
+
25
+
26
+
27
+
28
+ ## 1.6.18 (2022-03-24)
29
+
30
+
31
+ ### Features
32
+
33
+ * **api:** Add session mutation ([#1176](https://github.com/vtex/faststore/issues/1176)) ([0306167](https://github.com/vtex/faststore/commit/030616739cc62d9b5aa55cdf532ee6dea0bd793c))
34
+
35
+
36
+
37
+
38
+
6
39
  ## 1.6.17 (2022-03-23)
7
40
 
8
41
 
package/dist/index.d.ts CHANGED
@@ -66,6 +66,8 @@ export { default as Alert } from './molecules/Alert';
66
66
  export type { AlertProps } from './molecules/Alert';
67
67
  export { default as QuantitySelector } from './molecules/QuantitySelector';
68
68
  export type { QuantitySelectorProps } from './molecules/QuantitySelector';
69
+ export { default as Dropdown, DropdownButton, DropdownItem, DropdownMenu, } from './molecules/Dropdown';
70
+ export type { DropdownProps, DropdownButtonProps, DropdownItemProps, DropdownMenuProps, } from './molecules/Dropdown';
69
71
  export { default as Card, CardImage, CardContent, CardActions, } from './organisms/Card';
70
72
  export type { CardProps, CardImageProps, CardContentProps, CardActionsProps, } from './organisms/Card';
71
73
  export { default as useSlider } from './hooks/useSlider';
@@ -1559,6 +1559,287 @@ const QuantitySelector = /*#__PURE__*/React.forwardRef(function QuantitySelector
1559
1559
  }, rightButtonProps)));
1560
1560
  });
1561
1561
 
1562
+ const defaultState = {
1563
+ isOpen: false,
1564
+ dropdownButtonRef: null,
1565
+ selectedDropdownItemIndexRef: null,
1566
+ dropdownItemsRef: null,
1567
+ id: 'store-dropdown'
1568
+ };
1569
+ const DropdownContext = /*#__PURE__*/React.createContext(defaultState);
1570
+
1571
+ const Dropdown = ({
1572
+ children,
1573
+ isOpen: isOpenDefault = false,
1574
+ onDismiss,
1575
+ id = 'store-dropdown'
1576
+ }) => {
1577
+ const [isOpen, setIsOpen] = React.useState(isOpenDefault);
1578
+ const dropdownItemsRef = React.useRef([]);
1579
+ const selectedDropdownItemIndexRef = React.useRef(0);
1580
+ const dropdownButtonRef = React.useRef(null);
1581
+
1582
+ const close = () => {
1583
+ setIsOpen(false);
1584
+ };
1585
+
1586
+ const open = () => {
1587
+ setIsOpen(true);
1588
+ };
1589
+
1590
+ const toggle = () => {
1591
+ setIsOpen(old => !old);
1592
+ };
1593
+
1594
+ React.useEffect(() => {
1595
+ setIsOpen(isOpenDefault);
1596
+ }, [isOpenDefault]);
1597
+ React.useEffect(() => {
1598
+ var _dropdownItemsRef$cur;
1599
+
1600
+ isOpen && (dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur = dropdownItemsRef.current[0]) == null ? void 0 : _dropdownItemsRef$cur.focus());
1601
+ }, [isOpen]);
1602
+ const value = React.useMemo(() => {
1603
+ return {
1604
+ isOpen,
1605
+ close,
1606
+ open,
1607
+ toggle,
1608
+ dropdownButtonRef,
1609
+ onDismiss,
1610
+ selectedDropdownItemIndexRef,
1611
+ dropdownItemsRef,
1612
+ id
1613
+ };
1614
+ }, [id, isOpen, onDismiss]);
1615
+ return React__default.createElement(DropdownContext.Provider, {
1616
+ value: value
1617
+ }, children);
1618
+ };
1619
+
1620
+ /**
1621
+ * Hook to use the Dropdown context.
1622
+ * @returns Dropdown context.
1623
+ */
1624
+
1625
+ const useDropdown = () => {
1626
+ const context = React.useContext(DropdownContext);
1627
+
1628
+ if (context === undefined) {
1629
+ throw new Error('Do not use useDropdown hook outside the Dropdown context.');
1630
+ }
1631
+
1632
+ return context;
1633
+ };
1634
+
1635
+ const DropdownButton = /*#__PURE__*/React.forwardRef(function Button({
1636
+ children,
1637
+ testId = 'store-dropdown-button',
1638
+ ...otherProps
1639
+ }, ref) {
1640
+ const {
1641
+ toggle,
1642
+ dropdownButtonRef,
1643
+ isOpen,
1644
+ id
1645
+ } = useDropdown();
1646
+ React.useImperativeHandle(ref, () => dropdownButtonRef.current, [dropdownButtonRef]);
1647
+ return React__default.createElement("button", Object.assign({
1648
+ "data-store-dropdown-button": true,
1649
+ onClick: toggle,
1650
+ "data-testid": testId,
1651
+ ref: dropdownButtonRef,
1652
+ "aria-expanded": isOpen,
1653
+ "aria-haspopup": "menu",
1654
+ "aria-controls": id
1655
+ }, otherProps), children);
1656
+ });
1657
+
1658
+ const DropdownItem = /*#__PURE__*/React.forwardRef(function Button({
1659
+ children,
1660
+ onClick,
1661
+ testId = 'store-dropdown-item',
1662
+ ...otherProps
1663
+ }, ref) {
1664
+ const {
1665
+ dropdownItemsRef,
1666
+ selectedDropdownItemIndexRef,
1667
+ close
1668
+ } = useDropdown();
1669
+ const [dropdownItemIndex, setDropdownItemIndex] = React.useState(0);
1670
+ const dropdownItemRef = React.useRef();
1671
+
1672
+ const addToRefs = el => {
1673
+ if (el && !(dropdownItemsRef != null && dropdownItemsRef.current.includes(el))) {
1674
+ var _dropdownItemsRef$cur;
1675
+
1676
+ dropdownItemsRef == null ? void 0 : dropdownItemsRef.current.push(el);
1677
+ setDropdownItemIndex((_dropdownItemsRef$cur = dropdownItemsRef == null ? void 0 : dropdownItemsRef.current.findIndex(element => element === el)) != null ? _dropdownItemsRef$cur : 0);
1678
+ }
1679
+
1680
+ dropdownItemRef.current = el;
1681
+ };
1682
+
1683
+ const onFocusItem = () => {
1684
+ var _dropdownItemsRef$cur2;
1685
+
1686
+ selectedDropdownItemIndexRef.current = dropdownItemIndex;
1687
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur2 = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur2.focus();
1688
+ };
1689
+
1690
+ const handleOnClickItem = event => {
1691
+ onClick == null ? void 0 : onClick(event);
1692
+ close == null ? void 0 : close();
1693
+ };
1694
+
1695
+ React.useImperativeHandle(ref, () => dropdownItemRef.current, []);
1696
+ return React__default.createElement("button", Object.assign({
1697
+ "data-store-dropdown-item": true,
1698
+ "data-testid": testId,
1699
+ ref: addToRefs,
1700
+ onFocus: onFocusItem,
1701
+ onMouseEnter: onFocusItem,
1702
+ onClick: handleOnClickItem,
1703
+ role: "menuitem",
1704
+ tabIndex: -1,
1705
+ "data-index": dropdownItemIndex
1706
+ }, otherProps), children);
1707
+ });
1708
+
1709
+ /**
1710
+ * Hook used to find the DropdownMenu position in relation to DropdownButton
1711
+ * @returns Style with positions.
1712
+ */
1713
+
1714
+ const useDropdownPosition = () => {
1715
+ var _dropdownButtonRef$cu, _buttonRect$top, _buttonRect$height, _buttonRect$left;
1716
+
1717
+ const {
1718
+ dropdownButtonRef
1719
+ } = useDropdown();
1720
+ const buttonRect = dropdownButtonRef == null ? void 0 : (_dropdownButtonRef$cu = dropdownButtonRef.current) == null ? void 0 : _dropdownButtonRef$cu.getBoundingClientRect();
1721
+ const topLevel = (_buttonRect$top = buttonRect == null ? void 0 : buttonRect.top) != null ? _buttonRect$top : 0;
1722
+ const topOffset = (_buttonRect$height = buttonRect == null ? void 0 : buttonRect.height) != null ? _buttonRect$height : 0;
1723
+ const topPosition = topLevel + topOffset;
1724
+ const leftPosition = (_buttonRect$left = buttonRect == null ? void 0 : buttonRect.left) != null ? _buttonRect$left : 0;
1725
+ return {
1726
+ position: 'absolute',
1727
+ top: topPosition,
1728
+ left: leftPosition
1729
+ };
1730
+ };
1731
+
1732
+ /*
1733
+ * This component is based on @reach/dialog.
1734
+ * https://github.com/reach/reach-ui/blob/main/packages/dialog/src/index.tsx
1735
+ * https://reach.tech/dialog
1736
+ */
1737
+
1738
+ const DropdownMenu = ({
1739
+ children,
1740
+ testId = 'store-dropdown-menu',
1741
+ style,
1742
+ ...otherProps
1743
+ }) => {
1744
+ const {
1745
+ isOpen,
1746
+ close,
1747
+ onDismiss,
1748
+ dropdownItemsRef,
1749
+ selectedDropdownItemIndexRef,
1750
+ id
1751
+ } = useDropdown();
1752
+ 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
+ };
1764
+
1765
+ const handleDownPress = () => {
1766
+ var _dropdownItemsRef$cur;
1767
+
1768
+ if (selectedDropdownItemIndexRef.current < childrenLength - 1) {
1769
+ selectedDropdownItemIndexRef.current++;
1770
+ } else {
1771
+ selectedDropdownItemIndexRef.current = 0;
1772
+ }
1773
+
1774
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur.focus();
1775
+ };
1776
+
1777
+ const handleUpPress = () => {
1778
+ var _dropdownItemsRef$cur2;
1779
+
1780
+ if (selectedDropdownItemIndexRef.current > 0) {
1781
+ selectedDropdownItemIndexRef.current--;
1782
+ } else {
1783
+ selectedDropdownItemIndexRef.current = childrenLength - 1;
1784
+ }
1785
+
1786
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur2 = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur2.focus();
1787
+ };
1788
+
1789
+ const handleHomePress = () => {
1790
+ var _dropdownItemsRef$cur3;
1791
+
1792
+ selectedDropdownItemIndexRef.current = 0;
1793
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur3 = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur3.focus();
1794
+ };
1795
+
1796
+ const handleEndPress = () => {
1797
+ var _dropdownItemsRef$cur4;
1798
+
1799
+ selectedDropdownItemIndexRef.current = childrenLength - 1;
1800
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur4 = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur4.focus();
1801
+ };
1802
+
1803
+ const handleEscapePress = () => {
1804
+ onDismiss == null ? void 0 : onDismiss();
1805
+ close == null ? void 0 : close();
1806
+ };
1807
+
1808
+ const handleBackdropKeyDown = event => {
1809
+ if (event.defaultPrevented) {
1810
+ return;
1811
+ }
1812
+
1813
+ event.key === 'Escape' && handleEscapePress();
1814
+ event.key === 'ArrowDown' && handleDownPress();
1815
+ event.key === 'ArrowUp' && handleUpPress();
1816
+ event.key === 'Home' && handleHomePress();
1817
+ event.key === 'End' && handleEndPress();
1818
+ event.key === 'Tab' && event.preventDefault();
1819
+ event.stopPropagation();
1820
+ };
1821
+
1822
+ const clearChildrenReferences = () => {
1823
+ dropdownItemsRef.current = [];
1824
+ return null;
1825
+ };
1826
+
1827
+ return isOpen ? reactDom.createPortal(React__default.createElement(Overlay, {
1828
+ "data-store-dropdown-overlay": true,
1829
+ onClick: handleBackdropClick,
1830
+ onKeyDown: handleBackdropKeyDown
1831
+ }, React__default.createElement("div", Object.assign({
1832
+ role: "menu",
1833
+ "aria-orientation": "vertical",
1834
+ "data-store-dropdown-menu": true,
1835
+ "data-testid": testId,
1836
+ style: { ...dropdownPosition,
1837
+ ...style
1838
+ },
1839
+ id: id
1840
+ }, otherProps), children)), document.body) : clearChildrenReferences();
1841
+ };
1842
+
1562
1843
  const Card = /*#__PURE__*/React.forwardRef(function Card({
1563
1844
  testId = 'store-card',
1564
1845
  children,
@@ -1626,6 +1907,10 @@ exports.CardContent = CardContent;
1626
1907
  exports.CardImage = CardImage;
1627
1908
  exports.Carousel = Carousel;
1628
1909
  exports.Checkbox = Checkbox;
1910
+ exports.Dropdown = Dropdown;
1911
+ exports.DropdownButton = DropdownButton;
1912
+ exports.DropdownItem = DropdownItem;
1913
+ exports.DropdownMenu = DropdownMenu;
1629
1914
  exports.Form = Form;
1630
1915
  exports.Icon = Icon;
1631
1916
  exports.IconButton = IconButton;