Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { Fragment, useMemo } from 'react';
- import { FormattedMessage } from 'react-intl';
- import { string } from 'prop-types';
- import { Link } from 'react-router-dom';
- import { useBreadcrumbs } from '@magento/peregrine/lib/talons/Breadcrumbs/useBreadcrumbs';
- import resourceUrl from '@magento/peregrine/lib/util/makeUrl';
- import { useStyle } from '@magento/venia-ui/lib/classify';
- import Shimmer from './breadcrumbs.shimmer';
- import defaultClasses from './breadcrumbs.module.css';
- const DELIMITER = '.'
- /**
- * Breadcrumbs! Generates a sorted display of category links.
- *
- * @param {String} props.categoryId the uid of the category for which to generate breadcrumbs
- * @param {String} props.currentProduct the name of the product we're currently on, if any.
- */
- const Breadcrumbs = props => {
- const classes = useStyle(defaultClasses, props.classes);
- const { categoryId, currentProduct } = props;
- const talonProps = useBreadcrumbs({ categoryId });
- const {
- currentCategory,
- currentCategoryPath,
- hasError,
- isLoading,
- normalizedData,
- handleClick
- } = talonProps;
- // For all links generate a fragment like "/ Text"
- const links = useMemo(() => {
- return normalizedData.map(({ text, path }) => {
- return (
- <Fragment key={text}>
- <span className={classes.divider}>{DELIMITER}</span>
- <Link
- className='text-[#3D4A51]'
- to={resourceUrl(path)}
- onClick={handleClick}
- >
- {text}
- </Link>
- </Fragment>
- );
- });
- }, [classes.divider, classes.link, handleClick, normalizedData]);
- if (isLoading) {
- return <Shimmer />;
- }
- // Don't display anything but the empty, static height div when there's an error.
- if (hasError) {
- return (
- <div
- className={classes.root}
- aria-live="polite"
- aria-busy="false"
- />
- );
- }
- const currentCategoryLink = currentProduct ? (
- <Link
- className={'text-[#3D4A51]'}
- to={resourceUrl(currentCategoryPath)}
- onClick={handleClick}
- >
- {currentCategory}
- </Link>
- ) : (
- <span className={'text-[#3D4A51] font-semibold'}>{currentCategory}</span>
- );
- const currentProductNode = currentProduct ? (
- <Fragment>
- <span className={classes.divider}>{DELIMITER}</span>
- <span className={classes.text + ' text-[#3D4A51] font-semibold'}>{currentProduct}</span>
- </Fragment>
- ) : null;
- return (
- <div className={classes.root} aria-live="polite" aria-busy="false">
- <Link to="/" className="text-[#3D4A51] after:content-[' _↗']">
- <FormattedMessage id={'global.home'} defaultMessage={'Home'} />
- </Link>
- {links}
- <span className={classes.divider}>{DELIMITER}</span>
- {currentCategoryLink}
- {currentProductNode}
- </div >
- );
- };
- export default Breadcrumbs;
- Breadcrumbs.propTypes = {
- categoryId: string.isRequired,
- currentProduct: string
- };
Advertisement
Add Comment
Please, Sign In to add comment