aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/components/ui/AutoEllipsis.tsx
blob: 57183ec8bbb9f008cc5a4e84195050d25f65392a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * SonarQube
 * Copyright (C) 2009-2023 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import classNames from 'classnames';
import * as React from 'react';

type EllipsisPredicate = (
  node: HTMLElement,
  props: Omit<AutoEllipsisProps, 'customShouldEllipsis'>
) => boolean;

interface AutoEllipsisProps {
  customShouldEllipsis?: EllipsisPredicate;
  maxHeight?: number;
  maxWidth?: number;
  useParent?: boolean;
}

interface Props extends AutoEllipsisProps {
  children: React.ReactElement;
}

/*
 * This component allows to automatically add the .text-ellipsis class on it's children if this one
 * might overflow the max width/height passed as props.
 * If one of maxHeight or maxWidth is not specified, they will be ignored in the conditions to add the ellipsis class.
 * If useParent is true, then the parent size will be used instead of the undefined maxHeight/maxWidth
 */
export default function AutoEllipsis(props: Props) {
  const { children, ...autoEllipsisProps } = props;
  const [autoEllispis, ref] = useAutoEllipsis(autoEllipsisProps);

  return React.cloneElement(children, {
    className: classNames(children.props.className, { 'text-ellipsis': autoEllispis }),
    ref,
  });
}

export function useAutoEllipsis(props: AutoEllipsisProps): [boolean, (node: HTMLElement) => void] {
  const [autoEllipsis, setAutoEllipsis] = React.useState(false);

  // useCallback instead of useRef to be able to compute if the flag is needed as soon as the ref is attached
  // useRef doesn't accept a callback to notify us that the current ref value was attached,
  // see https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node for more info on this.
  const ref = React.useCallback(
    (node: HTMLElement) => {
      if (!autoEllipsis && node) {
        const shouldEllipsis = props.customShouldEllipsis ?? defaultShouldEllipsis;
        setAutoEllipsis(shouldEllipsis(node, props));
      }
    },
    // We don't want to apply this effect when ellipsis state change, only this effect can change it
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [props.customShouldEllipsis, props.maxHeight, props.maxWidth, props.useParent]
  );

  return [autoEllipsis, ref];
}

export const defaultShouldEllipsis: EllipsisPredicate = (
  node,
  { useParent = true, maxWidth, maxHeight }
) => {
  if (node.parentElement && useParent) {
    maxWidth = maxWidth ?? node.parentElement.clientWidth;
    maxHeight = maxHeight ?? node.parentElement.clientHeight;
  }
  return (
    (maxWidth !== undefined && node.clientWidth > maxWidth) ||
    (maxHeight !== undefined && node.clientHeight > maxHeight)
  );
};