[英]What is best way to implement multi line bread crumb links in react?
I am trying to implement a multiline breadcrumb links for mobile/tablet devices.我正在尝试为移动/平板设备实现多行面包屑链接。 User will be navigating through multiple folders and if it exceeds the max lines configured of breadcrumb link container I am dropping out links from beginning (trying to show maximum path from current path).用户将浏览多个文件夹,如果它超过面包屑链接容器配置的最大行数,我将从头开始删除链接(试图显示当前路径的最大路径)。 Couldn't find a css method for this.找不到为此的 css 方法。 Please find an example below with max line = 2
请在下面找到max line = 2
的示例
current path =
path1 / path2
/ path3 / path4
/path5
exceeds at path5 (went to 3rd line should update the link to超过 path5(去到第 3 行应该更新链接到
updated path =
.../ path2
/ path3 / path4
if user navigates back should updates without the ellipse.如果用户向后导航,则应该在没有省略号的情况下进行更新。 I tried the below methods, didn't work out as expected -我尝试了以下方法,没有达到预期效果 -
white-space: no wrap
and text-overflow: ellipse
, I can't use it since it wont allow multiline使用white-space: no wrap
和text-overflow: ellipse
,我不能使用它,因为它不允许多行Width(in pixel) * Font Constant / font Size (in pixels)
尝试使用表达式找到适合单行 div 的最大字符数 - Width(in pixel) * Font Constant / font Size (in pixels)
Figured out width and font size dynamically.动态计算出宽度和字体大小。 but couldn't figure out how to get the font constant of an element.但无法弄清楚如何获取元素的字体常量。 Any help, greatly appreciated.任何帮助,不胜感激。
I created a demo component here.我在这里创建了一个演示组件。 Hope it will be helpful.希望它会有所帮助。
The strategy I'm using here is:我在这里使用的策略是:
Breadcrumbs.js面包屑.js
import { createRef, useEffect, useState } from "react";
import "./Breadcrumbs.css";
function Breadcrumbs() {
const wrapper = createRef();
const ellipsis = createRef();
const [items, setItems] = useState([]);
const [dropped, setDropped] = useState(false);
const countLines = (element, lineHeight) => {
let height = element.offsetHeight;
let lines = height / lineHeight;
return Math.round(lines);
}
useEffect(() => {
setItems(window.location.pathname.split("/").filter((e) => e.length > 0))
}, [window.location.pathname])
useEffect(() => {
const lines = countLines(wrapper.current, ellipsis.current.clientHeight);
const maxLines = 2;
if (lines > maxLines) {
setItems(items.slice(1));
setDropped(true);
}
}, [items])
return <nav className="breadcrumbs-wrapper" ref={wrapper}>
<span ref={ellipsis}>
{dropped ? "..." : ""}
</span>
{items.map((item, index) => {
return <span key={index.toString()}>
<span> / </span>
<a href="#">{item}</a>
</span>
})}
</nav>
}
export default Breadcrumbs;
Breadcrumbs.css面包屑.css
.breadcrumbs-wrapper {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
Here are screenshots of the working example这是工作示例的屏幕截图
import React from 'react';从“反应”导入反应; import { Link, useLocation } from 'react-router-dom';从'react-router-dom'导入{Link,useLocation};
const Breadcrumb = ({ handleDrawerOpen, open }) => {
const { pathname } = useLocation();
const pathName = (paths, index) => {
return paths.filter((path, pathIndex) => pathIndex !== 0 && pathIndex <= index).join('/');
};
const func = () => {
const paths = pathname.split('/');
return paths.map(
(path, index) =>
index !== 0 && (
<Link
style={{
textDecoration: 'none',
color: index === paths.length - 1 ? '#49C5B6' : 'white',
}}
to={`/${pathName(paths, index)}`}>{`${
path?.charAt(0)?.toUpperCase() + path.slice(1).toLowerCase()
} ${index !== paths.length - 1 ? '>' : ''} `}</Link>
)
);
};
return (
<Box>
{!open && (
<IconButton onClick={handleDrawerOpen}>
<MenuIcon />
</IconButton>
)}
<Profile />
</Box>
);
};
export default Breadcrumb;
You can slice last 5 items and map through them to render the links:您可以切片最后 5 个项目并映射它们以呈现链接:
breadcrumbs.slice(-5).map((breadcrumb, index) => (
<React.Fragment key={index}>
{index !== 0 && breadcrumbs.length > 1 ? ' / ' : ''}
{index === 0 && breadcrumb.path !== breadcrumbs[0].path ? (
'...'
) : (
<a href={breadcrumb.link}>{breadcrumb.path}</a>
)}
{index % 2 ? <br /> : ''}
</React.Fragment>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.