forked from runme-io/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tooltip): to use an action instead of a component (runme-io#126
- Loading branch information
Showing
8 changed files
with
145 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
$background-color: #eee | ||
|
||
.tippy-box | ||
user-select: text | ||
cursor: default | ||
font-size: 1.1rem | ||
|
||
&[role='popover'], | ||
&[role='popover'] .tippy-content | ||
padding: 0 | ||
|
||
&[role='popover'][data-placement^=bottom] > .tippy-arrow:before | ||
border-bottom-color: $background-color !important | ||
|
||
.popover-title | ||
border-bottom: .1rem solid #e2e2e2 | ||
padding: .7rem 1rem | ||
font-size: 1.2rem | ||
background: $background-color | ||
|
||
.popover-content | ||
padding: 1rem |
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 |
---|---|---|
|
@@ -53,3 +53,4 @@ body | |
padding-right: $spacing | ||
|
||
@import "form" | ||
@import "tippy" |
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,77 @@ | ||
import 'tippy.js/dist/tippy.css' | ||
import 'tippy.js/themes/light-border.css' | ||
|
||
export async function tooltip (elem, { | ||
enabled = true, | ||
content, | ||
componentProps, | ||
asPopover = false, | ||
popoverTooltip, | ||
title = '', | ||
...opts | ||
}) { | ||
if (!enabled) { return } | ||
|
||
let component, componentObject | ||
|
||
// defaults | ||
opts = { | ||
placement: asPopover ? 'bottom' : 'auto', | ||
allowHTML: asPopover, | ||
trigger: asPopover ? 'click' : 'mouseenter focus', | ||
theme: asPopover ? 'light-border' : '', | ||
animation: 'fade', | ||
interactive: asPopover, | ||
role: asPopover ? 'popover' : 'tooltip', | ||
delay: 100, | ||
...opts, | ||
} | ||
|
||
// in case we want also a tooltip when having a popover | ||
if (asPopover && popoverTooltip) { | ||
tooltip(elem, popoverTooltip) | ||
} | ||
|
||
// if we are dealing with a Svelte component, parse the content with an empty body | ||
// then onCreate we are binding the new Svelte component to the tippy/popover selector with the component props | ||
if (typeof content === 'function') { | ||
componentObject = content | ||
content = '' | ||
|
||
opts.onCreate = instance => { | ||
const selector = asPopover ? '.popover-content' : '.tippy-content' | ||
// eslint-disable-next-line new-cap | ||
component = new componentObject({ | ||
target: instance.popper.querySelector(selector), | ||
props: componentProps, | ||
}) | ||
} | ||
} | ||
|
||
opts.content = formatContent(content, asPopover, title) | ||
|
||
const { default: tippy } = await import('tippy.js') | ||
const tippyInstance = tippy(elem, opts) | ||
|
||
return { | ||
update ({ componentProps, content, asPopover, title }) { | ||
if (component) { | ||
component.$set(componentProps) | ||
} else { | ||
const updatedContent = formatContent(content, asPopover, title) | ||
tippyInstance.setContent(updatedContent) | ||
} | ||
}, | ||
destroy () { | ||
tippyInstance.destroy() | ||
if (component) { | ||
component.$destroy() | ||
} | ||
}, | ||
} | ||
} | ||
|
||
function formatContent (content, asPopover, title) { | ||
const popoverTitle = asPopover && title ? `<div class="popover-title">${title}</div>` : '' | ||
return asPopover ? `${popoverTitle}<div class="popover-content">${content}</div>` : content | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<script> | ||
import { faCopy } from '@fortawesome/free-solid-svg-icons' | ||
import ButtonIcon from '../ButtonIcon.svelte' | ||
export let content = '' | ||
async function copy () { | ||
try { | ||
await navigator.clipboard.writeText(content) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} | ||
</script> | ||
|
||
{content} | ||
<ButtonIcon | ||
aria-label="copy" | ||
icon={faCopy} | ||
on:click={copy} | ||
tooltipOptions={{ content: 'Copy' }} | ||
/> |
This file was deleted.
Oops, something went wrong.