outline-item.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { useState, useEffect, useRef, memo } from 'react'
  2. import PropTypes from 'prop-types'
  3. import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed'
  4. import classNames from 'classnames'
  5. import { useTranslation } from 'react-i18next'
  6. import OutlineList from './outline-list'
  7. import Icon from '../../../shared/components/icon'
  8. const OutlineItem = memo(function OutlineItem({
  9. outlineItem,
  10. jumpToLine,
  11. highlightedLine,
  12. matchesHighlightedLine,
  13. containsHighlightedLine,
  14. }) {
  15. const { t } = useTranslation()
  16. const [expanded, setExpanded] = useState(true)
  17. const titleElementRef = useRef()
  18. const isHighlightedRef = useRef(false)
  19. const mainItemClasses = classNames('outline-item', {
  20. 'outline-item-no-children': !outlineItem.children,
  21. })
  22. const hasHighlightedChild = !expanded && containsHighlightedLine
  23. const isHighlighted = matchesHighlightedLine || hasHighlightedChild
  24. const itemLinkClasses = classNames('outline-item-link', {
  25. 'outline-item-link-highlight': isHighlighted,
  26. })
  27. function handleExpandCollapseClick() {
  28. setExpanded(!expanded)
  29. }
  30. function handleOutlineItemLinkClick(event) {
  31. const syncToPdf = event.detail === 2 // double-click = sync to PDF
  32. jumpToLine(outlineItem.line, syncToPdf)
  33. }
  34. useEffect(() => {
  35. const wasHighlighted = isHighlightedRef.current
  36. isHighlightedRef.current = isHighlighted
  37. if (!wasHighlighted && isHighlighted) {
  38. scrollIntoViewIfNeeded(titleElementRef.current, {
  39. scrollMode: 'if-needed',
  40. block: 'center',
  41. })
  42. }
  43. }, [isHighlighted, titleElementRef, isHighlightedRef])
  44. // don't set the aria-expanded attribute when there are no children
  45. const ariaExpandedValue = outlineItem.children ? expanded : undefined
  46. return (
  47. <li
  48. className={mainItemClasses}
  49. aria-expanded={ariaExpandedValue}
  50. role="treeitem"
  51. aria-current={isHighlighted}
  52. aria-label={outlineItem.title}
  53. >
  54. <div className="outline-item-row">
  55. {outlineItem.children ? (
  56. <button
  57. className="outline-item-expand-collapse-btn"
  58. onClick={handleExpandCollapseClick}
  59. aria-label={expanded ? t('collapse') : t('expand')}
  60. >
  61. <Icon
  62. type={expanded ? 'angle-down' : 'angle-right'}
  63. className="outline-caret-icon"
  64. />
  65. </button>
  66. ) : null}
  67. <button
  68. className={itemLinkClasses}
  69. onClick={handleOutlineItemLinkClick}
  70. ref={titleElementRef}
  71. >
  72. {outlineItem.title}
  73. </button>
  74. </div>
  75. {expanded && outlineItem.children ? (
  76. // highlightedLine is only provided to this list if the list contains
  77. // the highlighted line. This means that whenever the list does not
  78. // contain the highlighted line, the props provided to it are the same
  79. // and the component can be memoized.
  80. <OutlineList
  81. outline={outlineItem.children}
  82. jumpToLine={jumpToLine}
  83. isRoot={false}
  84. highlightedLine={containsHighlightedLine ? highlightedLine : null}
  85. containsHighlightedLine={containsHighlightedLine}
  86. />
  87. ) : null}
  88. </li>
  89. )
  90. })
  91. OutlineItem.propTypes = {
  92. outlineItem: PropTypes.exact({
  93. line: PropTypes.number.isRequired,
  94. title: PropTypes.string.isRequired,
  95. level: PropTypes.number,
  96. children: PropTypes.array,
  97. // Used for caching in CM6
  98. from: PropTypes.number,
  99. to: PropTypes.number,
  100. }).isRequired,
  101. jumpToLine: PropTypes.func.isRequired,
  102. highlightedLine: PropTypes.number,
  103. matchesHighlightedLine: PropTypes.bool,
  104. containsHighlightedLine: PropTypes.bool,
  105. }
  106. export default OutlineItem