diff --git a/src/components/button-with-tooltip/button-with-tooltip.tsx b/src/components/button-with-tooltip/button-with-tooltip.tsx new file mode 100644 index 0000000..91fc3c5 --- /dev/null +++ b/src/components/button-with-tooltip/button-with-tooltip.tsx @@ -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(); + + 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 ( + + ); + } + + return showTooltip ? ( + + ) : ( + + ); +}; + +export { ButtonWithTooltip }; diff --git a/src/components/button-with-tooltip/index.ts b/src/components/button-with-tooltip/index.ts new file mode 100644 index 0000000..04ed78c --- /dev/null +++ b/src/components/button-with-tooltip/index.ts @@ -0,0 +1 @@ +export { ButtonWithTooltip } from './button-with-tooltip'; diff --git a/src/components/text-with-tooltip/index.ts b/src/components/text-with-tooltip/index.ts new file mode 100644 index 0000000..5cb91d3 --- /dev/null +++ b/src/components/text-with-tooltip/index.ts @@ -0,0 +1 @@ +export { TextWithTooltip } from './text-with-tooltip'; diff --git a/src/components/text-with-tooltip/text-with-tooltip.scss b/src/components/text-with-tooltip/text-with-tooltip.scss new file mode 100644 index 0000000..d91f733 --- /dev/null +++ b/src/components/text-with-tooltip/text-with-tooltip.scss @@ -0,0 +1,9 @@ +.text-with-tooltip { + .text { + -webkit-box-orient: vertical; + display: -webkit-box; + overflow: hidden; + text-overflow: ellipsis; + word-break: break-word; + } +} \ No newline at end of file diff --git a/src/components/text-with-tooltip/text-with-tooltip.tsx b/src/components/text-with-tooltip/text-with-tooltip.tsx new file mode 100644 index 0000000..de82246 --- /dev/null +++ b/src/components/text-with-tooltip/text-with-tooltip.tsx @@ -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 = ( +
+ {text} +
+ ); + const comparisonTextElement = ( +
+ {text} +
+ ); + const content = !isFinalized ? ( + + {textElement} + {comparisonTextElement} + + ) : ( + textElement + ); + + if (!text) return null; + + return
{showTooltip ? {content} : content}
; +}; + +export { TextWithTooltip };