@faststore/ui 1.6.16 → 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.
Files changed (33) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/dist/index.d.ts +2 -0
  3. package/dist/molecules/Dropdown/Dropdown.d.ts +9 -0
  4. package/dist/molecules/Dropdown/DropdownButton.d.ts +10 -0
  5. package/dist/molecules/Dropdown/DropdownItem.d.ts +10 -0
  6. package/dist/molecules/Dropdown/DropdownMenu.d.ts +22 -0
  7. package/dist/molecules/Dropdown/contexts/DropdownContext.d.ts +41 -0
  8. package/dist/molecules/Dropdown/hooks/useDropdown.d.ts +6 -0
  9. package/dist/molecules/Dropdown/hooks/useDropdownPosition.d.ts +11 -0
  10. package/dist/molecules/Dropdown/index.d.ts +8 -0
  11. package/dist/molecules/Dropdown/stories/Dropdown.stories.d.ts +7 -0
  12. package/dist/molecules/Dropdown/stories/assets/Caret.d.ts +2 -0
  13. package/dist/ui.cjs.development.js +286 -3
  14. package/dist/ui.cjs.development.js.map +1 -1
  15. package/dist/ui.cjs.production.min.js +1 -1
  16. package/dist/ui.cjs.production.min.js.map +1 -1
  17. package/dist/ui.esm.js +283 -4
  18. package/dist/ui.esm.js.map +1 -1
  19. package/package.json +3 -3
  20. package/src/index.ts +13 -0
  21. package/src/molecules/Dropdown/Dropdown.test.tsx +159 -0
  22. package/src/molecules/Dropdown/Dropdown.tsx +65 -0
  23. package/src/molecules/Dropdown/DropdownButton.tsx +42 -0
  24. package/src/molecules/Dropdown/DropdownItem.tsx +72 -0
  25. package/src/molecules/Dropdown/DropdownMenu.tsx +156 -0
  26. package/src/molecules/Dropdown/contexts/DropdownContext.ts +53 -0
  27. package/src/molecules/Dropdown/hooks/useDropdown.ts +18 -0
  28. package/src/molecules/Dropdown/hooks/useDropdownPosition.ts +26 -0
  29. package/src/molecules/Dropdown/index.ts +11 -0
  30. package/src/molecules/Dropdown/stories/Dropdown.mdx +51 -0
  31. package/src/molecules/Dropdown/stories/Dropdown.stories.tsx +94 -0
  32. package/src/molecules/Dropdown/stories/assets/Caret.tsx +19 -0
  33. package/src/molecules/Modal/ModalContent.tsx +1 -1
package/dist/ui.esm.js CHANGED
@@ -948,14 +948,13 @@ const ModalContentPure = ({
948
948
  tabIndex: 0,
949
949
  "aria-hidden": "true"
950
950
  }), React.createElement("div", Object.assign({
951
- "data-store-modal-content": true
952
- }, otherProps, {
951
+ "data-store-modal-content": true,
953
952
  "data-testid": testId,
954
953
  ref: trapFocusRef,
955
954
  "aria-modal": "true",
956
955
  role: "dialog",
957
956
  tabIndex: -1
958
- }), children), React.createElement("div", {
957
+ }, otherProps), children), React.createElement("div", {
959
958
  ref: afterElementRef,
960
959
  "data-testid": "afterElement",
961
960
  tabIndex: 0,
@@ -1552,6 +1551,286 @@ const QuantitySelector = /*#__PURE__*/forwardRef(function QuantitySelector({
1552
1551
  }, rightButtonProps)));
1553
1552
  });
1554
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
+
1555
1834
  const Card = /*#__PURE__*/forwardRef(function Card({
1556
1835
  testId = 'store-card',
1557
1836
  children,
@@ -1600,5 +1879,5 @@ const CardActions = /*#__PURE__*/forwardRef(function CardActions({
1600
1879
  }, otherProps), children);
1601
1880
  });
1602
1881
 
1603
- 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 };
1604
1883
  //# sourceMappingURL=ui.esm.js.map