@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/dist/ui.esm.js CHANGED
@@ -1551,6 +1551,286 @@ const QuantitySelector = /*#__PURE__*/forwardRef(function QuantitySelector({
1551
1551
  }, rightButtonProps)));
1552
1552
  });
1553
1553
 
1554
+ const defaultState = {
1555
+ isOpen: false,
1556
+ dropdownButtonRef: null,
1557
+ selectedDropdownItemIndexRef: null,
1558
+ dropdownItemsRef: null,
1559
+ id: 'store-dropdown'
1560
+ };
1561
+ const DropdownContext = /*#__PURE__*/createContext(defaultState);
1562
+
1563
+ const Dropdown = ({
1564
+ children,
1565
+ isOpen: isOpenDefault = false,
1566
+ onDismiss,
1567
+ id = 'store-dropdown'
1568
+ }) => {
1569
+ const [isOpen, setIsOpen] = useState(isOpenDefault);
1570
+ const dropdownItemsRef = useRef([]);
1571
+ const selectedDropdownItemIndexRef = useRef(0);
1572
+ const dropdownButtonRef = useRef(null);
1573
+
1574
+ const close = () => {
1575
+ setIsOpen(false);
1576
+ };
1577
+
1578
+ const open = () => {
1579
+ setIsOpen(true);
1580
+ };
1581
+
1582
+ const toggle = () => {
1583
+ setIsOpen(old => !old);
1584
+ };
1585
+
1586
+ useEffect(() => {
1587
+ setIsOpen(isOpenDefault);
1588
+ }, [isOpenDefault]);
1589
+ useEffect(() => {
1590
+ var _dropdownItemsRef$cur;
1591
+
1592
+ isOpen && (dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur = dropdownItemsRef.current[0]) == null ? void 0 : _dropdownItemsRef$cur.focus());
1593
+ }, [isOpen]);
1594
+ const value = useMemo(() => {
1595
+ return {
1596
+ isOpen,
1597
+ close,
1598
+ open,
1599
+ toggle,
1600
+ dropdownButtonRef,
1601
+ onDismiss,
1602
+ selectedDropdownItemIndexRef,
1603
+ dropdownItemsRef,
1604
+ id
1605
+ };
1606
+ }, [id, isOpen, onDismiss]);
1607
+ return React.createElement(DropdownContext.Provider, {
1608
+ value: value
1609
+ }, children);
1610
+ };
1611
+
1612
+ /**
1613
+ * Hook to use the Dropdown context.
1614
+ * @returns Dropdown context.
1615
+ */
1616
+
1617
+ const useDropdown = () => {
1618
+ const context = useContext(DropdownContext);
1619
+
1620
+ if (context === undefined) {
1621
+ throw new Error('Do not use useDropdown hook outside the Dropdown context.');
1622
+ }
1623
+
1624
+ return context;
1625
+ };
1626
+
1627
+ const DropdownButton = /*#__PURE__*/forwardRef(function Button({
1628
+ children,
1629
+ testId = 'store-dropdown-button',
1630
+ ...otherProps
1631
+ }, ref) {
1632
+ const {
1633
+ toggle,
1634
+ dropdownButtonRef,
1635
+ isOpen,
1636
+ id
1637
+ } = useDropdown();
1638
+ useImperativeHandle(ref, () => dropdownButtonRef.current, [dropdownButtonRef]);
1639
+ return React.createElement("button", Object.assign({
1640
+ "data-store-dropdown-button": true,
1641
+ onClick: toggle,
1642
+ "data-testid": testId,
1643
+ ref: dropdownButtonRef,
1644
+ "aria-expanded": isOpen,
1645
+ "aria-haspopup": "menu",
1646
+ "aria-controls": id
1647
+ }, otherProps), children);
1648
+ });
1649
+
1650
+ const DropdownItem = /*#__PURE__*/forwardRef(function Button({
1651
+ children,
1652
+ onClick,
1653
+ testId = 'store-dropdown-item',
1654
+ ...otherProps
1655
+ }, ref) {
1656
+ const {
1657
+ dropdownItemsRef,
1658
+ selectedDropdownItemIndexRef,
1659
+ close
1660
+ } = useDropdown();
1661
+ const [dropdownItemIndex, setDropdownItemIndex] = useState(0);
1662
+ const dropdownItemRef = useRef();
1663
+
1664
+ const addToRefs = el => {
1665
+ if (el && !(dropdownItemsRef != null && dropdownItemsRef.current.includes(el))) {
1666
+ var _dropdownItemsRef$cur;
1667
+
1668
+ dropdownItemsRef == null ? void 0 : dropdownItemsRef.current.push(el);
1669
+ setDropdownItemIndex((_dropdownItemsRef$cur = dropdownItemsRef == null ? void 0 : dropdownItemsRef.current.findIndex(element => element === el)) != null ? _dropdownItemsRef$cur : 0);
1670
+ }
1671
+
1672
+ dropdownItemRef.current = el;
1673
+ };
1674
+
1675
+ const onFocusItem = () => {
1676
+ var _dropdownItemsRef$cur2;
1677
+
1678
+ selectedDropdownItemIndexRef.current = dropdownItemIndex;
1679
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur2 = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur2.focus();
1680
+ };
1681
+
1682
+ const handleOnClickItem = event => {
1683
+ onClick == null ? void 0 : onClick(event);
1684
+ close == null ? void 0 : close();
1685
+ };
1686
+
1687
+ useImperativeHandle(ref, () => dropdownItemRef.current, []);
1688
+ return React.createElement("button", Object.assign({
1689
+ "data-store-dropdown-item": true,
1690
+ "data-testid": testId,
1691
+ ref: addToRefs,
1692
+ onFocus: onFocusItem,
1693
+ onMouseEnter: onFocusItem,
1694
+ onClick: handleOnClickItem,
1695
+ role: "menuitem",
1696
+ tabIndex: -1,
1697
+ "data-index": dropdownItemIndex
1698
+ }, otherProps), children);
1699
+ });
1700
+
1701
+ /**
1702
+ * Hook used to find the DropdownMenu position in relation to DropdownButton
1703
+ * @returns Style with positions.
1704
+ */
1705
+
1706
+ const useDropdownPosition = () => {
1707
+ var _dropdownButtonRef$cu, _dropdownButtonRef$cu2, _dropdownButtonRef$cu3, _dropdownButtonRef$cu4, _dropdownButtonRef$cu5, _dropdownButtonRef$cu6;
1708
+
1709
+ const {
1710
+ dropdownButtonRef
1711
+ } = useDropdown();
1712
+ const topLevel = (_dropdownButtonRef$cu = dropdownButtonRef == null ? void 0 : (_dropdownButtonRef$cu2 = dropdownButtonRef.current) == null ? void 0 : _dropdownButtonRef$cu2.offsetTop) != null ? _dropdownButtonRef$cu : 0;
1713
+ const topOffset = (_dropdownButtonRef$cu3 = dropdownButtonRef == null ? void 0 : (_dropdownButtonRef$cu4 = dropdownButtonRef.current) == null ? void 0 : _dropdownButtonRef$cu4.offsetHeight) != null ? _dropdownButtonRef$cu3 : 0;
1714
+ const topPosition = topLevel + topOffset;
1715
+ const leftPosition = (_dropdownButtonRef$cu5 = dropdownButtonRef == null ? void 0 : (_dropdownButtonRef$cu6 = dropdownButtonRef.current) == null ? void 0 : _dropdownButtonRef$cu6.offsetLeft) != null ? _dropdownButtonRef$cu5 : 0;
1716
+ return {
1717
+ position: 'absolute',
1718
+ top: topPosition,
1719
+ left: leftPosition
1720
+ };
1721
+ };
1722
+
1723
+ /*
1724
+ * This component is based on @reach/dialog.
1725
+ * https://github.com/reach/reach-ui/blob/main/packages/dialog/src/index.tsx
1726
+ * https://reach.tech/dialog
1727
+ */
1728
+
1729
+ const DropdownMenu = ({
1730
+ children,
1731
+ testId = 'store-dropdown-menu',
1732
+ style,
1733
+ ...otherProps
1734
+ }) => {
1735
+ const {
1736
+ isOpen,
1737
+ close,
1738
+ onDismiss,
1739
+ dropdownItemsRef,
1740
+ selectedDropdownItemIndexRef,
1741
+ id
1742
+ } = useDropdown();
1743
+ 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
+ };
1755
+
1756
+ const handleDownPress = () => {
1757
+ var _dropdownItemsRef$cur;
1758
+
1759
+ if (selectedDropdownItemIndexRef.current < childrenLength - 1) {
1760
+ selectedDropdownItemIndexRef.current++;
1761
+ } else {
1762
+ selectedDropdownItemIndexRef.current = 0;
1763
+ }
1764
+
1765
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur.focus();
1766
+ };
1767
+
1768
+ const handleUpPress = () => {
1769
+ var _dropdownItemsRef$cur2;
1770
+
1771
+ if (selectedDropdownItemIndexRef.current > 0) {
1772
+ selectedDropdownItemIndexRef.current--;
1773
+ } else {
1774
+ selectedDropdownItemIndexRef.current = childrenLength - 1;
1775
+ }
1776
+
1777
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur2 = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur2.focus();
1778
+ };
1779
+
1780
+ const handleHomePress = () => {
1781
+ var _dropdownItemsRef$cur3;
1782
+
1783
+ selectedDropdownItemIndexRef.current = 0;
1784
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur3 = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur3.focus();
1785
+ };
1786
+
1787
+ const handleEndPress = () => {
1788
+ var _dropdownItemsRef$cur4;
1789
+
1790
+ selectedDropdownItemIndexRef.current = childrenLength - 1;
1791
+ dropdownItemsRef == null ? void 0 : (_dropdownItemsRef$cur4 = dropdownItemsRef.current[selectedDropdownItemIndexRef.current]) == null ? void 0 : _dropdownItemsRef$cur4.focus();
1792
+ };
1793
+
1794
+ const handleEscapePress = () => {
1795
+ onDismiss == null ? void 0 : onDismiss();
1796
+ close == null ? void 0 : close();
1797
+ };
1798
+
1799
+ const handleBackdropKeyDown = event => {
1800
+ if (event.defaultPrevented) {
1801
+ return;
1802
+ }
1803
+
1804
+ event.key === 'Escape' && handleEscapePress();
1805
+ event.key === 'ArrowDown' && handleDownPress();
1806
+ event.key === 'ArrowUp' && handleUpPress();
1807
+ event.key === 'Home' && handleHomePress();
1808
+ event.key === 'End' && handleEndPress();
1809
+ event.key === 'Tab' && event.preventDefault();
1810
+ event.stopPropagation();
1811
+ };
1812
+
1813
+ const clearChildrenReferences = () => {
1814
+ dropdownItemsRef.current = [];
1815
+ return null;
1816
+ };
1817
+
1818
+ return isOpen ? createPortal(React.createElement(Overlay, {
1819
+ "data-store-dropdown-overlay": true,
1820
+ onClick: handleBackdropClick,
1821
+ onKeyDown: handleBackdropKeyDown
1822
+ }, React.createElement("div", Object.assign({
1823
+ role: "menu",
1824
+ "aria-orientation": "vertical",
1825
+ "data-store-dropdown-menu": true,
1826
+ "data-testid": testId,
1827
+ style: { ...dropdownPosition,
1828
+ ...style
1829
+ },
1830
+ id: id
1831
+ }, otherProps), children)), document.body) : clearChildrenReferences();
1832
+ };
1833
+
1554
1834
  const Card = /*#__PURE__*/forwardRef(function Card({
1555
1835
  testId = 'store-card',
1556
1836
  children,
@@ -1599,5 +1879,5 @@ const CardActions = /*#__PURE__*/forwardRef(function CardActions({
1599
1879
  }, otherProps), children);
1600
1880
  });
1601
1881
 
1602
- export { Accordion, AccordionButton, AccordionItem, AccordionPanel, Alert, Badge, Banner, BannerContent, BannerImage, BannerLink, Breadcrumb, Bullets, Button, Card, CardActions, CardContent, CardImage, Carousel, Checkbox, Form, Icon, IconButton, Incentive, Input, Label, Link, List, LoadingButton, Modal, Overlay, PaymentMethods, Popover, Price, PriceRange, QuantitySelector, Radio, RadioGroup, RadioOption, SearchInput, Select, Skeleton, Slider, Spinner, Table, TableBody, TableCell, TableFooter, TableHead, TableRow, TextArea, useSlider };
1882
+ export { Accordion, AccordionButton, AccordionItem, AccordionPanel, Alert, Badge, Banner, BannerContent, BannerImage, BannerLink, Breadcrumb, Bullets, Button, Card, CardActions, CardContent, CardImage, Carousel, Checkbox, Dropdown, DropdownButton, DropdownItem, DropdownMenu, Form, Icon, IconButton, Incentive, Input, Label, Link, List, LoadingButton, Modal, Overlay, PaymentMethods, Popover, Price, PriceRange, QuantitySelector, Radio, RadioGroup, RadioOption, SearchInput, Select, Skeleton, Slider, Spinner, Table, TableBody, TableCell, TableFooter, TableHead, TableRow, TextArea, useSlider };
1603
1883
  //# sourceMappingURL=ui.esm.js.map