0.1.15 • Published 1 year ago

solid-tooltip v0.1.15

Weekly downloads
-
License
GPL
Repository
github
Last release
1 year ago

Solid Tooltip

Directive for displaying simple tooltips on components given a title attribute.

A Solid component. See https://www.solidjs.com/

Simple Usage

import tooltip from 'solid-tooltip'

export default function YourComponent() {
	return (
		<div use:tooltip="top" title="the title!">
			hover me!
		</div>
	)
}

Reactive Usage

import tooltip from 'solid-tooltip'

export default function YourComponent() {
	return <div use:tooltip={[() => signal()]}>hover me!</div>
}

Reactive Usage With Position

import tooltip from 'solid-tooltip'

export default function YourComponent() {
	return <div use:tooltip={['bottom', () => signal()]}>hover me!</div>
}

Install

npm install solid-tooltip

How it works?

Appends one div container to the body. Whenever your mouse moves over the component that has the title attribute, it updates the content of the attached div with the title, show it, and position it in the desired place. When the mouse moves out, click, or the tab lose focus, the tooltip is hidden.

Custom Tooltip

Customizing the tooltip is as simple as writting a wrapper. Lets say you want a tooltip that displays multiple lines.

//fancy-tooltip.js

import { createSignal } from 'solid-js'
import tooltip from 'solid-tooltip'

// create a signal to read/set the tooltip content
const [content, setContent] = createSignal()

// create the custom div that we will reuse on every tooltip
const container = (
	<div
		style={`
			background:orange;
			color:black;
			border:1px solid white;
			white-space: pre-wrap;
			font-size:.8rem;
			padding:6px;
			margin:3px;
		`}
	>
		{content()}
	</div>
)

export default function myMultipleLinesTooltip(related, at) {
	return tooltip(related, at, (tooltip, position) => {
		setContent(
			tooltip
				.split('\n')
				.map(s => s.trim())
				.join('\n')
				.trim(),
		)
		return container
	})
}

// other-file.js

import myMultipleLinesTooltip from './fancy-tooltip.js'

export default function YourComponent() {
	return (
		<div use:myMultipleLinesTooltip="bottom" title="fancy\ntitle!">
			hover me!
		</div>
	)
}

Positions

The argument of the directive its just the position.

value
bottom
bottom-left
bottom-left-overlap
bottom-right
bottom-right-overlap
top (the default)
top-left
top-left-overlap
top-right
top-right-overlap
left
right

Alternatives

Author

URL

0.1.15

1 year ago

0.1.11

2 years ago

0.1.12

2 years ago

0.1.13

2 years ago

0.1.14

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago