-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): adding truncate and line clamp option (#279)
Co-authored-by: Tristan Holaday <40547442+TristanHoladay@users.noreply.github.com>
- Loading branch information
1 parent
81bead7
commit de760fb
Showing
10 changed files
with
129 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<script lang="ts"> | ||
export let title = '' | ||
let isHovered = false | ||
let xOffset: number | ||
let yOffset: number | ||
let isWider: boolean = false | ||
type MouseEventType = MouseEvent & { | ||
currentTarget: EventTarget & HTMLDivElement | ||
} | ||
const mouseOver = (event: MouseEventType) => { | ||
const createElWidth = getElementWidth('div', event.currentTarget.children[0].textContent) | ||
// This offset is the padding for the table > tr > td element in app.postcss. The class of px-4 is 1em or 16px | ||
if (createElWidth - event.currentTarget.offsetLeft * 2 > event.currentTarget.clientWidth) { | ||
isWider = true | ||
} else { | ||
isWider = false | ||
} | ||
isHovered = true | ||
xOffset = event.pageX | ||
yOffset = event.pageY | ||
} | ||
const mouseMove = (event: MouseEventType) => { | ||
// calculate if the columns is a certain percent off right side and render on the left side | ||
const diff = ((window.innerWidth - event.pageX) / window.innerWidth) * 100 | ||
const tooltipOffset = diff < 15 ? 500 : 200 | ||
xOffset = event.pageX - tooltipOffset | ||
yOffset = event.pageY - 110 | ||
} | ||
const mouseLeave = () => (isHovered = false) | ||
const getElementWidth = (type: string, textContent: string | null): number => { | ||
if (textContent) { | ||
const newEl = document.createElement(type) | ||
const newContent = document.createTextNode(textContent) | ||
// add the text node to the newly created div | ||
newEl.appendChild(newContent) | ||
newEl.setAttribute('id', 'test-el') | ||
// Inline block so it does not take full width of screen | ||
newEl.style.display = 'inline-block' | ||
// Ensure the element is not visible | ||
newEl.style.position = 'fixed' | ||
newEl.style.zIndex = '0' | ||
// add the newly created element and its content into the DOM | ||
const bodyEl = document.querySelector('body')! | ||
bodyEl.appendChild(newEl) | ||
const createdEL = document.getElementById('test-el') | ||
const createElWidth = createdEL?.clientWidth | ||
bodyEl.removeChild(newEl) | ||
return createElWidth || 0 | ||
} | ||
return 0 | ||
} | ||
</script> | ||
|
||
<div | ||
id="container" | ||
role="button" | ||
tabindex={0} | ||
on:mouseover={mouseOver} | ||
on:mouseleave={mouseLeave} | ||
on:mousemove={mouseMove} | ||
on:focus | ||
> | ||
<slot /> | ||
</div> | ||
|
||
{#if isHovered && isWider} | ||
<div | ||
class="absolute bg-gray-900 text-white opacity-90 text-xs focus:border-gray-200 focus:outline-none focus:ring-0 dark:border-gray-700 p-4 rounded-[4px]" | ||
style="top: {yOffset}px; left: {xOffset}px; padding: 10px" | ||
> | ||
{title} | ||
</div> | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters