@faststore/ui 1.6.18 → 1.6.19

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,17 @@
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.19 (2022-03-24)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add exports to dropdown ([#1188](https://github.com/vtex/faststore/issues/1188)) ([f24359a](https://github.com/vtex/faststore/commit/f24359a34439e0ec8d3069483c31a6f62fa6535d))
12
+
13
+
14
+
15
+
16
+
6
17
  ## 1.6.18 (2022-03-24)
7
18
 
8
19
 
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,286 @@ 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, _dropdownButtonRef$cu2, _dropdownButtonRef$cu3, _dropdownButtonRef$cu4, _dropdownButtonRef$cu5, _dropdownButtonRef$cu6;
1716
+
1717
+ const {
1718
+ dropdownButtonRef
1719
+ } = useDropdown();
1720
+ const topLevel = (_dropdownButtonRef$cu = dropdownButtonRef == null ? void 0 : (_dropdownButtonRef$cu2 = dropdownButtonRef.current) == null ? void 0 : _dropdownButtonRef$cu2.offsetTop) != null ? _dropdownButtonRef$cu : 0;
1721
+ const topOffset = (_dropdownButtonRef$cu3 = dropdownButtonRef == null ? void 0 : (_dropdownButtonRef$cu4 = dropdownButtonRef.current) == null ? void 0 : _dropdownButtonRef$cu4.offsetHeight) != null ? _dropdownButtonRef$cu3 : 0;
1722
+ const topPosition = topLevel + topOffset;
1723
+ const leftPosition = (_dropdownButtonRef$cu5 = dropdownButtonRef == null ? void 0 : (_dropdownButtonRef$cu6 = dropdownButtonRef.current) == null ? void 0 : _dropdownButtonRef$cu6.offsetLeft) != null ? _dropdownButtonRef$cu5 : 0;
1724
+ return {
1725
+ position: 'absolute',
1726
+ top: topPosition,
1727
+ left: leftPosition
1728
+ };
1729
+ };
1730
+
1731
+ /*
1732
+ * This component is based on @reach/dialog.
1733
+ * https://github.com/reach/reach-ui/blob/main/packages/dialog/src/index.tsx
1734
+ * https://reach.tech/dialog
1735
+ */
1736
+
1737
+ const DropdownMenu = ({
1738
+ children,
1739
+ testId = 'store-dropdown-menu',
1740
+ style,
1741
+ ...otherProps
1742
+ }) => {
1743
+ const {
1744
+ isOpen,
1745
+ close,
1746
+ onDismiss,
1747
+ dropdownItemsRef,
1748
+ selectedDropdownItemIndexRef,
1749
+ id
1750
+ } = useDropdown();
1751
+ const dropdownPosition = useDropdownPosition();
1752
+ const childrenLength = children.length;
1753
+
1754
+ const handleBackdropClick = event => {
1755
+ if (event.defaultPrevented) {
1756
+ return;
1757
+ }
1758
+
1759
+ event.stopPropagation();
1760
+ onDismiss == null ? void 0 : onDismiss();
1761
+ close == null ? void 0 : close();
1762
+ };
1763
+
1764
+ const handleDownPress = () => {
1765
+ var _dropdownItemsRef$cur;
1766
+
1767
+ if (selectedDropdownItemIndexRef.current < childrenLength - 1) {
1768
+ selectedDropdownItemIndexRef.current++;
1769
+ } else {
1770
+ selectedDropdownItemIndexRef.current = 0;
1771
+ }
1772
+
1773
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur.focus();
1774
+ };
1775
+
1776
+ const handleUpPress = () => {
1777
+ var _dropdownItemsRef$cur2;
1778
+
1779
+ if (selectedDropdownItemIndexRef.current > 0) {
1780
+ selectedDropdownItemIndexRef.current--;
1781
+ } else {
1782
+ selectedDropdownItemIndexRef.current = childrenLength - 1;
1783
+ }
1784
+
1785
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur2 = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur2.focus();
1786
+ };
1787
+
1788
+ const handleHomePress = () => {
1789
+ var _dropdownItemsRef$cur3;
1790
+
1791
+ selectedDropdownItemIndexRef.current = 0;
1792
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur3 = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur3.focus();
1793
+ };
1794
+
1795
+ const handleEndPress = () => {
1796
+ var _dropdownItemsRef$cur4;
1797
+
1798
+ selectedDropdownItemIndexRef.current = childrenLength - 1;
1799
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur4 = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur4.focus();
1800
+ };
1801
+
1802
+ const handleEscapePress = () => {
1803
+ onDismiss == null ? void 0 : onDismiss();
1804
+ close == null ? void 0 : close();
1805
+ };
1806
+
1807
+ const handleBackdropKeyDown = event => {
1808
+ if (event.defaultPrevented) {
1809
+ return;
1810
+ }
1811
+
1812
+ event.key === 'Escape' && handleEscapePress();
1813
+ event.key === 'ArrowDown' && handleDownPress();
1814
+ event.key === 'ArrowUp' && handleUpPress();
1815
+ event.key === 'Home' && handleHomePress();
1816
+ event.key === 'End' && handleEndPress();
1817
+ event.key === 'Tab' && event.preventDefault();
1818
+ event.stopPropagation();
1819
+ };
1820
+
1821
+ const clearChildrenReferences = () => {
1822
+ dropdownItemsRef.current = [];
1823
+ return null;
1824
+ };
1825
+
1826
+ return isOpen ? reactDom.createPortal(React__default.createElement(Overlay, {
1827
+ "data-store-dropdown-overlay": true,
1828
+ onClick: handleBackdropClick,
1829
+ onKeyDown: handleBackdropKeyDown
1830
+ }, React__default.createElement("div", Object.assign({
1831
+ role: "menu",
1832
+ "aria-orientation": "vertical",
1833
+ "data-store-dropdown-menu": true,
1834
+ "data-testid": testId,
1835
+ style: { ...dropdownPosition,
1836
+ ...style
1837
+ },
1838
+ id: id
1839
+ }, otherProps), children)), document.body) : clearChildrenReferences();
1840
+ };
1841
+
1562
1842
  const Card = /*#__PURE__*/React.forwardRef(function Card({
1563
1843
  testId = 'store-card',
1564
1844
  children,
@@ -1626,6 +1906,10 @@ exports.CardContent = CardContent;
1626
1906
  exports.CardImage = CardImage;
1627
1907
  exports.Carousel = Carousel;
1628
1908
  exports.Checkbox = Checkbox;
1909
+ exports.Dropdown = Dropdown;
1910
+ exports.DropdownButton = DropdownButton;
1911
+ exports.DropdownItem = DropdownItem;
1912
+ exports.DropdownMenu = DropdownMenu;
1629
1913
  exports.Form = Form;
1630
1914
  exports.Icon = Icon;
1631
1915
  exports.IconButton = IconButton;