Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Svelte action to display a tooltip

License

NotificationsYou must be signed in to change notification settings

untemps/svelte-use-tooltip

Repository files navigation

svelte-use-tooltip

Svelte action to display a tooltip

npmGitHub Workflow StatusCodecov

Demo

LIVE DEMO

Installation

npm i @untemps/svelte-use-tooltip

Usage

Basic usage

<script>import {useTooltip }from'@untemps/svelte-use-tooltip';const_onTooltipClick= (arg,event)=> {console.log(arg,event);};</script><divuse:useTooltip={{position:'right',contentSelector:'#tooltip-template',contentActions: {'*': {eventType:'click',callback:_onTooltipClick,callbackParams: ['ok'],closeOnCallback:true}},containerClassName:`tooltip tooltip-right`,animated:true,animationEnterClassName:'tooltip-enter',animationLeaveClassName:null,enterDelay:100,leaveDelay:100,offset:20}}class="target">Hover me</div><templateid="tooltip-template"><spanclass="tooltip__content">Hi! I'm a <i>fancy</i> <strong>tooltip</strong>!</span></template><style>.target {width:10rem;height:3rem;background-color:white;color:black;display:flex;align-items:center;justify-content:center;box-shadow:005px0rgba(0,0,0,0.5);}.target:hover {cursor:pointer;background-color:black;color:white;}.tooltip__content {background-color:yellow;color:black;}:global(.tooltip) {position:absolute;z-index:9999;max-width:120px;background-color:#ee7008;color:#fff;text-align:center;border-radius:6px;padding:0.5rem;}:global(.tooltip::after) {content:'';position:absolute;margin-left:-5px;border-width:5px;border-style:solid;}:global(.tooltip-right::after) {top:calc(50%-5px);left:-5px;border-color:transparent#ee7008transparenttransparent;}:global(.tooltip-enter) {animation: fadeIn0.2slinearforwards;}@keyframesfadeIn {from {opacity:0;transform:translateX(50px);}to {opacity:1;transform:translateX(0);}}@keyframesfadeOut {to {opacity:0;transform:translateX(-50px);}}</style>

API

PropsTypeDefaultDescription
contentstringnullText content to display in the tooltip.
contentSelectorstringnullSelector of the content to display in the tooltip.
contentActionsobjectnullConfiguration of the tooltip actions (seeContent Actions).
containerClassNamestring'__tooltip'Class name to apply to the tooltip container.
positionstring'top'Position of the tooltip. Available values: 'top', 'bottom', 'left', 'right'
animatedbooleanfalseFlag to animate tooltip transitions.
animationEnterClassNamestring'__tooltip-enter'Class name to apply to the tooltip enter transition.
animationLeaveClassNamestring'__tooltip-leave'Class name to apply to the tooltip leave transition.
enterDelaynumber0Delay before showing the tooltip in milliseconds.
leaveDelaynumber0Delay before hiding the tooltip in milliseconds.
onEnterfuncnullCallback triggered when the tooltip appears.
onLeavefuncnullCallback triggered when the tooltip disappears.
offsetnumber10Distance between the tooltip and the target in pixels.
disabledbooleanfalseFlag to disable the tooltip content.

Content and Content Selector

The tooltip content can be specified either by thecontent prop or thecontentSelector prop.

content must be a text string that will be displayed as is in the tooltip.

It's useful for most of the use cases of a tooltip however sometimes you need to display some more complex content, with interactive elements or formatted text.

To do so, you may use thecontentSelector prop that allows to specify the selector of an element from the DOM.

The best option is to use atemplate HTML element although you may also use a plain element. In this case,it will remain in the DOM and will be clones in the tooltip.

Content Actions

ThecontentActions prop allows to handle interactions within the tooltip content.

Each element inside the content parent may configure its own action since it can be queried using the key-selector.

One event by element is possible so far as elements are referenced by selector. The last one declared in thecontentActions object has precedence over the previous ones.

<script>import {useTooltip }from'@untemps/svelte-use-tooltip';</script><divuse:useTooltip={{contentSelector:'#content',contentActions: {'#button1': {eventType:'mouseenter',callback: (arg)=>console.log(arg),callbackParams: ["Haha you're hovering the button 1"],closeOnCallback:false},'#button2': {eventType:'click',callback: (arg1,arg2)=>console.log(arg1,arg2),callbackParams: ["Haha you've clicked the",'button 2'],closeOnCallback:true}}}}>Hover me</div><spanid="content"><buttonid="button1">Action 1</button><buttonid="button2">Action 2</button></span>
PropsTypeDefaultDescription
eventTypestringnullType of the event. All availableevents can be used.
callbackfunctionnullFunction to be used as event handler.
callbackParamsarraynullList of arguments to pass to the event handler in.
closeOnCallbackbooleanfalseFlag to automatically close the tooltip when the event handler is triggered.

* selector

If you need the whole tooltip content to be interactive, you can use the special* key:

<script>import {useTooltip }from'@untemps/svelte-use-tooltip';</script><divuse:useTooltip={{content:'Foo',contentActions: {'*': {eventType:'click',callback: (arg)=>console.log(arg),callbackParams: ['Haha you clicked the tooltip'],closeOnCallback:true}}}}>Hover me</div>

If you combine the* selector with other events, its callback will be triggered along with the other ones.

Development

The component can be served for development purpose onhttp://localhost:5173/ running:

npm run dev

Contributing

Contributions are warmly welcomed:

  • Fork the repository
  • Create a feature branch
  • Develop the feature AND write the tests (or write the tests AND develop the feature)
  • Commit your changesusingAngular Git Commit Guidelines
  • Submit a Pull Request

[8]ページ先頭

©2009-2025 Movatter.jp