UNPKG

@shopify/polaris

Version:

Shopify’s product component library

171 lines (158 loc) • 6.18 kB
import React$1, { createRef } from 'react'; import { useFeatures } from '../../utilities/features/hooks.js'; import { useI18n } from '../../utilities/i18n/hooks.js'; import { classNames } from '../../utilities/css.js'; import { ChevronLeftMinor, ChevronRightMinor, ArrowLeftMinor, ArrowRightMinor } from '@shopify/polaris-icons'; import { handleMouseUpByBlurring } from '../../utilities/focus.js'; import { Icon as Icon$1 } from '../Icon/Icon.js'; import { ConditionalWrapper } from '../../utilities/components.js'; import { KeypressListener as KeypressListener$1 } from '../KeypressListener/KeypressListener.js'; import { UnstyledLink as UnstyledLink$1 } from '../UnstyledLink/UnstyledLink.js'; import { TextStyle as TextStyle$1 } from '../TextStyle/TextStyle.js'; import { Button as Button$1 } from '../Button/Button.js'; import { ButtonGroup as ButtonGroup$1 } from '../ButtonGroup/ButtonGroup.js'; import { isInputFocused as isInputFocused$1 } from '../../utilities/is-input-focused.js'; import { Tooltip as Tooltip$1 } from '../Tooltip/Tooltip.js'; import styles from './Pagination.scss.js'; var _ref = /*#__PURE__*/React$1.createElement(Icon$1, { source: ArrowLeftMinor }); var _ref2 = /*#__PURE__*/React$1.createElement(Icon$1, { source: ArrowLeftMinor }); var _ref3 = /*#__PURE__*/React$1.createElement(Icon$1, { source: ArrowRightMinor }); var _ref4 = /*#__PURE__*/React$1.createElement(Icon$1, { source: ArrowRightMinor }); function Pagination({ hasNext, hasPrevious, nextURL, previousURL, onNext, onPrevious, nextTooltip, previousTooltip, nextKeys, previousKeys, plain, accessibilityLabel, accessibilityLabels, label }) { const i18n = useI18n(); const { newDesignLanguage } = useFeatures(); const node = /*#__PURE__*/createRef(); const navLabel = accessibilityLabel || i18n.translate('Polaris.Pagination.pagination'); const previousLabel = (accessibilityLabels == null ? void 0 : accessibilityLabels.previous) || i18n.translate('Polaris.Pagination.previous'); const nextLabel = (accessibilityLabels == null ? void 0 : accessibilityLabels.next) || i18n.translate('Polaris.Pagination.next'); const className = classNames(styles.Pagination, plain && styles.plain); const previousClassName = classNames(styles.Button, !label && styles.PreviousButton); const nextClassName = classNames(styles.Button, !label && styles.NextButton); const previousButton = previousURL ? /*#__PURE__*/React$1.createElement(UnstyledLink$1, { className: previousClassName, url: previousURL, onMouseUp: handleMouseUpByBlurring, "aria-label": previousLabel, id: "previousURL" }, _ref) : /*#__PURE__*/React$1.createElement("button", { onClick: onPrevious, type: "button", onMouseUp: handleMouseUpByBlurring, className: previousClassName, "aria-label": previousLabel, disabled: !hasPrevious }, _ref2); const nextButton = nextURL ? /*#__PURE__*/React$1.createElement(UnstyledLink$1, { className: nextClassName, url: nextURL, onMouseUp: handleMouseUpByBlurring, "aria-label": nextLabel, id: "nextURL" }, _ref3) : /*#__PURE__*/React$1.createElement("button", { onClick: onNext, type: "button", onMouseUp: handleMouseUpByBlurring, className: nextClassName, "aria-label": nextLabel, disabled: !hasNext }, _ref4); const prev = newDesignLanguage ? /*#__PURE__*/React$1.createElement(Button$1, { outline: true, icon: ChevronLeftMinor, accessibilityLabel: previousLabel, url: previousURL, onClick: onPrevious, disabled: !hasPrevious }) : previousButton; const constructedPrevious = previousTooltip && hasPrevious ? /*#__PURE__*/React$1.createElement(Tooltip$1, { activatorWrapper: "span", content: previousTooltip }, prev) : prev; const next = newDesignLanguage ? /*#__PURE__*/React$1.createElement(Button$1, { outline: true, icon: ChevronRightMinor, accessibilityLabel: nextLabel, url: nextURL, onClick: onNext, disabled: !hasNext }) : nextButton; const constructedNext = nextTooltip && hasNext ? /*#__PURE__*/React$1.createElement(Tooltip$1, { activatorWrapper: "span", content: nextTooltip }, next) : next; const previousHandler = onPrevious || noop; const previousButtonEvents = previousKeys && (previousURL || onPrevious) && hasPrevious && previousKeys.map(key => /*#__PURE__*/React$1.createElement(KeypressListener$1, { key: key, keyCode: key, handler: previousURL ? handleCallback(clickPaginationLink('previousURL', node)) : handleCallback(previousHandler) })); const nextHandler = onNext || noop; const nextButtonEvents = nextKeys && (nextURL || onNext) && hasNext && nextKeys.map(key => /*#__PURE__*/React$1.createElement(KeypressListener$1, { key: key, keyCode: key, handler: nextURL ? handleCallback(clickPaginationLink('nextURL', node)) : handleCallback(nextHandler) })); const labelTextMarkup = hasNext && hasPrevious ? /*#__PURE__*/React$1.createElement(TextStyle$1, null, label) : /*#__PURE__*/React$1.createElement(TextStyle$1, { variation: "subdued" }, label); const labelMarkup = label ? /*#__PURE__*/React$1.createElement("div", { className: newDesignLanguage ? undefined : styles.Label, "aria-live": "polite" }, labelTextMarkup) : null; return /*#__PURE__*/React$1.createElement("nav", { className: newDesignLanguage ? undefined : className, "aria-label": navLabel, ref: node }, previousButtonEvents, nextButtonEvents, /*#__PURE__*/React$1.createElement(ConditionalWrapper, { condition: Boolean(newDesignLanguage), wrapper: children => /*#__PURE__*/React$1.createElement(ButtonGroup$1, { segmented: !label }, children) }, constructedPrevious, labelMarkup, constructedNext)); } function clickPaginationLink(id, node) { return () => { if (node.current == null) { return; } const link = node.current.querySelector(`#${id}`); if (link) { link.click(); } }; } function handleCallback(fn) { return () => { if (isInputFocused$1()) { return; } fn(); }; } function noop() {} export { Pagination };