baby_in_magento

vcrvrbhj

Apr 12th, 2024
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. import React, { Fragment, useMemo } from 'react';
  2. import { FormattedMessage } from 'react-intl';
  3. import { string } from 'prop-types';
  4. import { Link } from 'react-router-dom';
  5.  
  6. import { useBreadcrumbs } from '@magento/peregrine/lib/talons/Breadcrumbs/useBreadcrumbs';
  7. import resourceUrl from '@magento/peregrine/lib/util/makeUrl';
  8. import { useStyle } from '@magento/venia-ui/lib/classify';
  9. import Shimmer from './breadcrumbs.shimmer';
  10. import defaultClasses from './breadcrumbs.module.css';
  11.  
  12. const DELIMITER = '.'
  13. /**
  14. * Breadcrumbs! Generates a sorted display of category links.
  15. *
  16. * @param {String} props.categoryId the uid of the category for which to generate breadcrumbs
  17. * @param {String} props.currentProduct the name of the product we're currently on, if any.
  18. */
  19. const Breadcrumbs = props => {
  20. const classes = useStyle(defaultClasses, props.classes);
  21.  
  22. const { categoryId, currentProduct } = props;
  23.  
  24. const talonProps = useBreadcrumbs({ categoryId });
  25.  
  26. const {
  27. currentCategory,
  28. currentCategoryPath,
  29. hasError,
  30. isLoading,
  31. normalizedData,
  32. handleClick
  33. } = talonProps;
  34.  
  35. // For all links generate a fragment like "/ Text"
  36. const links = useMemo(() => {
  37. return normalizedData.map(({ text, path }) => {
  38. return (
  39. <Fragment key={text}>
  40. <span className={classes.divider}>{DELIMITER}</span>
  41.  
  42. <Link
  43. className='text-[#3D4A51]'
  44. to={resourceUrl(path)}
  45. onClick={handleClick}
  46. >
  47. {text}
  48. </Link>
  49. </Fragment>
  50. );
  51. });
  52. }, [classes.divider, classes.link, handleClick, normalizedData]);
  53.  
  54. if (isLoading) {
  55. return <Shimmer />;
  56. }
  57.  
  58. // Don't display anything but the empty, static height div when there's an error.
  59. if (hasError) {
  60. return (
  61. <div
  62. className={classes.root}
  63. aria-live="polite"
  64. aria-busy="false"
  65. />
  66. );
  67. }
  68.  
  69.  
  70. const currentCategoryLink = currentProduct ? (
  71. <Link
  72. className={'text-[#3D4A51]'}
  73. to={resourceUrl(currentCategoryPath)}
  74. onClick={handleClick}
  75. >
  76. {currentCategory}
  77. </Link>
  78. ) : (
  79. <span className={'text-[#3D4A51] font-semibold'}>{currentCategory}</span>
  80. );
  81.  
  82. const currentProductNode = currentProduct ? (
  83. <Fragment>
  84. <span className={classes.divider}>{DELIMITER}</span>
  85. <span className={classes.text + ' text-[#3D4A51] font-semibold'}>{currentProduct}</span>
  86. </Fragment>
  87. ) : null;
  88.  
  89. return (
  90. <div className={classes.root} aria-live="polite" aria-busy="false">
  91. <Link to="/" className="text-[#3D4A51] after:content-[' _↗']">
  92. <FormattedMessage id={'global.home'} defaultMessage={'Home'} />
  93. </Link>
  94. {links}
  95. <span className={classes.divider}>{DELIMITER}</span>
  96. {currentCategoryLink}
  97. {currentProductNode}
  98. </div >
  99. );
  100. };
  101.  
  102. export default Breadcrumbs;
  103.  
  104. Breadcrumbs.propTypes = {
  105. categoryId: string.isRequired,
  106. currentProduct: string
  107. };
  108.  
Advertisement
Add Comment
Please, Sign In to add comment