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

[Tooltip]: Validates arrow positioning / adds timeout props #464

Merged
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
9 changes: 9 additions & 0 deletions src/components/floating/floating.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,22 @@
return result
}

// We need this check because the middleware sometimes generates an
// x or y value of undefined, 1, or less. This causes the arrow on
// the floating element to be positioned in the corner incorrectly.
function hasInvalidArrowPosition({ x, y }: { x?: number; y?: number }) {
return (!x && !y) || x <= 1 || y <= 1
fallaciousreasoning marked this conversation as resolved.
Show resolved Hide resolved
}

function updatePosition(...args: any[]) {
if (!floating || !target) return

computePosition(target, floating, {
placement: placement,
middleware: getMiddlewares(flip, shift, offset, middleware)
}).then(({ x, y, placement, middlewareData }) => {
if (hasInvalidArrowPosition(middlewareData.arrow)) return

if (floating) {
Object.assign(floating.style, {
left: `${x}px`,
Expand Down
12 changes: 10 additions & 2 deletions src/components/tooltip/tooltip.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@
export let offset: number = 8
export let mode: Mode = 'mini'
/** The length of time the tooltip is open in ms after mouse leave of
* the trigger or tooltip */
export let mouseleaveTimeout: number = 150
/* Whether the tooltip is currently visible */
export let visible: boolean | undefined = undefined
/** The length of time in ms the tooltip element takes to fade
* after the users mouse leaves the trigger or tooltip*/
export let fadeDuration: number = 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice!

// Note: This is separate from the |visible| flag because we want to handle
// controlled and uncontrolled states for this component.
$: visibleInternal = visible ?? false
Expand Down Expand Up @@ -70,7 +78,7 @@
if (!triggerHovered && !tooltipHovered) {
setVisible(false)
}
}, 150)
}, mouseleaveTimeout)
}
})()
Expand Down Expand Up @@ -116,8 +124,8 @@
class:hero={mode === 'hero'}
class:info={mode === 'info'}
class:mini={mode === 'mini'}
transition:fade={{ duration: fadeDuration }}
class:default={mode === 'default' || !mode}
transition:fade={{ duration: 60 }}
hidden={!visibleInternal}
bind:this={tooltip}
>
Expand Down