Skip to content

Commit

Permalink
Merge pull request #7681 from VKCOM/e.muhamethanov/7636/add-text-elli…
Browse files Browse the repository at this point in the history
…psis-title

feat(EllipsisText): add title attribute
  • Loading branch information
EldarMuhamethanov authored Oct 1, 2024
2 parents 4bcec20 + 308fdda commit dcd391a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
max-inline-size: 100%;
}

/* Хак для того, чтобы убрать системный тултип в Safari */
.host.disableNativeTitle::after,
.host.disableNativeTitle .content::after {
content: '';
display: block;
}

.content {
min-inline-size: 1em;
overflow: hidden;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useRef } from 'react';
import { classNames } from '@vkontakte/vkjs';
import { getTextFromChildren } from '../../../lib/children';
import { useIsomorphicLayoutEffect } from '../../../lib/useIsomorphicLayoutEffect';
import type { HasRootRef } from '../../../types';
import type { RootComponentProps } from '../../RootComponent/RootComponent';
import styles from './EllipsisText.module.css';

export interface EllipsisTextProps
extends RootComponentProps<HTMLElement>,
extends Omit<RootComponentProps<HTMLElement>, 'title'>,
HasRootRef<HTMLElement> {
/**
* Пользовательская маскимальная ширина.
Expand All @@ -22,6 +23,10 @@ export interface EllipsisTextProps
* > @see [line-clamp](https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-line-clamp)
*/
maxLines?: number;
/**
* Отключает отображение нативного тултипа с полным текстом
*/
disableNativeTitle?: boolean;
}

/** Компонент ограничивает текстовый контент убирая его в многоточие.
Expand All @@ -35,6 +40,7 @@ const EllipsisText = ({
children,
maxWidth,
maxLines = 1,
disableNativeTitle = false,
...restProps
}: EllipsisTextProps): React.ReactNode => {
const contentRef = useRef<HTMLSpanElement | null>(null);
Expand All @@ -46,7 +52,16 @@ const EllipsisText = ({
}, [contentRef, maxLines]);

return (
<span ref={getRootRef} className={classNames(styles.host, className)} {...restProps}>
<span
ref={getRootRef}
className={classNames(
styles.host,
disableNativeTitle && styles.disableNativeTitle,
className,
)}
title={disableNativeTitle ? undefined : getTextFromChildren(children)}
{...restProps}
>
<span
style={{ maxWidth }}
ref={contentRef}
Expand Down

0 comments on commit dcd391a

Please sign in to comment.