Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add button-with-tooltip and text-with-tooltip #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/components/button-with-tooltip/button-with-tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Button, ButtonType } from 'components/button';
import { h } from 'preact';
import { useState, useEffect } from 'preact/hooks';

interface ButtonWithTooltipProps {
type: ButtonType;
label: string;
onClick: () => void;
}

const ButtonWithTooltip = ({ type, label, onClick }: ButtonWithTooltipProps) => {
const [buttonRef, setButtonRef] = useState<HTMLButtonElement | null>();

const [isFinalized, setIsFinalized] = useState(false);
const [showTooltip, setShowTooltip] = useState(true);
const onClickWrapper = () => {
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur();
}

if (onClick) {
onClick();
}
};

useEffect(() => {
if (!isFinalized && buttonRef) {
setIsFinalized(true);

const textElement = buttonRef.children[0] as HTMLElement;
setShowTooltip(textElement.offsetWidth < textElement.scrollWidth);
}
});

if (!isFinalized) {
return (
<Button
type={type}
tooltip={{ label }}
onClick={onClickWrapper}
setRef={(ref: HTMLButtonElement) => setButtonRef(ref)}
disabled={false}
>
{label}
</Button>
);
}

return showTooltip ? (
<Button type={type} tooltip={{ label }} onClick={onClickWrapper} disabled={false}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use
<Button type={type} tooltip={showTooltip ? { label } : undefined} onClick={onClickWrapper} disabled={false}> {label} </Button>
or
const buttonProps = { type, onClick: onClickWrapper, }; if (showTooltip) { buttonProps['tooltip'] = label; } return <Button {...buttonProps}> {label} </Button>

{label}
</Button>
) : (
<Button type={type} onClick={onClickWrapper} disabled={false}>
{label}
</Button>
);
};

export { ButtonWithTooltip };
1 change: 1 addition & 0 deletions src/components/button-with-tooltip/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ButtonWithTooltip } from './button-with-tooltip';
1 change: 1 addition & 0 deletions src/components/text-with-tooltip/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { TextWithTooltip } from './text-with-tooltip';
9 changes: 9 additions & 0 deletions src/components/text-with-tooltip/text-with-tooltip.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.text-with-tooltip {
.text {
-webkit-box-orient: vertical;
display: -webkit-box;
overflow: hidden;
text-overflow: ellipsis;
word-break: break-word;
}
}
53 changes: 53 additions & 0 deletions src/components/text-with-tooltip/text-with-tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { h, Fragment } from 'preact';

import { useState, useRef, useLayoutEffect } from 'preact/hooks';
import { ui } from '@playkit-js/kaltura-player-js';
const { Tooltip } = ui.Components;

import * as styles from './text-with-tooltip.scss';

const TextWithTooltip = ({ text, numberOfLines }: { text: string; numberOfLines: number }) => {
const comparisonTextRef = useRef(null);
const textRef = useRef(null);

const [showTooltip, setShowTooltip] = useState(false);
const [isFinalized, setIsFinalized] = useState(false);

useLayoutEffect(() => {
if (!isFinalized && textRef?.current && comparisonTextRef?.current) {
setIsFinalized(true);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const textHeight = textRef?.current?.getBoundingClientRect().height;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const comparisonTextHeight = comparisonTextRef?.current?.getBoundingClientRect().height;
setShowTooltip(textHeight < comparisonTextHeight);
}
});

const textElement = (
<div ref={textRef} style={{ '-webkit-line-clamp': numberOfLines }} className={styles.text}>
{text}
</div>
);
const comparisonTextElement = (
<div ref={comparisonTextRef} style={{ '-webkit-line-clamp': numberOfLines + 1 }} className={styles.text}>
{text}
</div>
);
const content = !isFinalized ? (
<Fragment>
{textElement}
{comparisonTextElement}
</Fragment>
) : (
textElement
);

if (!text) return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can move if (!text) return null; to beginning of component, so if text not provided the component can safe few bytes of memory :)


return <div className={styles.textWithTooltip}>{showTooltip ? <Tooltip label={text}>{content}</Tooltip> : content}</div>;
};

export { TextWithTooltip };
Loading