All Projects → slmgc → React Hint

slmgc / React Hint

Licence: mit
Tooltip component for React, Preact, Inferno

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Hint

Nwb
A toolkit for React, Preact, Inferno & vanilla JS apps, React libraries and other npm modules for the web, with no configuration (until you need it)
Stars: ✭ 5,429 (+1506.21%)
Mutual labels:  react-component, preact, inferno
Reactjs Popup
React Popup Component - Modals,Tooltips and Menus —  All in one
Stars: ✭ 1,211 (+258.28%)
Mutual labels:  component, react-component, tooltip
React Splitters
React splitter component, written in TypeScript.
Stars: ✭ 127 (-62.43%)
Mutual labels:  component, react-component
Redux Tooltip
A tooltip React component for Redux.
Stars: ✭ 134 (-60.36%)
Mutual labels:  component, tooltip
React Reorder
Drag & drop, touch enabled, reorderable / sortable list, React component
Stars: ✭ 209 (-38.17%)
Mutual labels:  component, react-component
Uskin
A front-end framework aims at developing web projects based on CSS3 and provides common components.
Stars: ✭ 74 (-78.11%)
Mutual labels:  component, react-component
React Multi Select
A Multi Select component built with and for React
Stars: ✭ 111 (-67.16%)
Mutual labels:  component, react-component
React Component Echarts
React component echarts. 组件式百度图表。
Stars: ✭ 175 (-48.22%)
Mutual labels:  component, react-component
Ui Box
Blazing Fast React UI Primitive
Stars: ✭ 847 (+150.59%)
Mutual labels:  component, react-component
react-layer-stack
Layering system for React. Useful for popover/modals/tooltip/dnd application
Stars: ✭ 158 (-53.25%)
Mutual labels:  react-component, tooltip
Yoshino
A themable React component library!Flexible Lightweight PC UI Components built on React! Anyone can generate easily all kinds of themes by it!
Stars: ✭ 216 (-36.09%)
Mutual labels:  component, react-component
react-hotkey-tooltip
A global Hotkey provider with built in tooltip for React
Stars: ✭ 34 (-89.94%)
Mutual labels:  react-component, tooltip
Shudan
A highly customizable, low-level Preact Goban component.
Stars: ✭ 43 (-87.28%)
Mutual labels:  component, preact
React Magnifier
🔍 React image zoom component
Stars: ✭ 116 (-65.68%)
Mutual labels:  component, react-component
React Planner
✏️ A React Component for plans design. Draw a 2D floorplan and navigate it in 3D mode.
Stars: ✭ 846 (+150.3%)
Mutual labels:  component, react-component
Reactopt
A CLI React performance optimization tool that identifies potential unnecessary re-rendering
Stars: ✭ 1,975 (+484.32%)
Mutual labels:  component, react-component
React Tags
⚛️ A fantastically simple tagging component for your React projects
Stars: ✭ 321 (-5.03%)
Mutual labels:  react-component, preact
Reactprimer
React component prototyping tool that generates fully connected class component code.
Stars: ✭ 743 (+119.82%)
Mutual labels:  component, react-component
Sparkline
Lightweight React sparklines ✨ 📈
Stars: ✭ 19 (-94.38%)
Mutual labels:  component, react-component
React Intl Tel Input
Rewrite International Telephone Input in React.js. (Looking for maintainers, and PRs & contributors are also welcomed!)
Stars: ✭ 212 (-37.28%)
Mutual labels:  component, react-component

React-hint

npm package npm package

React-hint is a small tooltip component for React which is developed with simplicity and performance in mind. It also plays nicely with Preact and Inferno.

react-hint tooltip custom tooltip

How to install

npm i -S react-hint

How to import

// React
import React from 'react'
import ReactHintFactory from 'react-hint'
const ReactHint = ReactHintFactory(React)

// Preact
import {h, Component} from 'preact'
import ReactHintFactory from 'react-hint'
const ReactHint = ReactHintFactory({createElement: h, Component})

// Inferno
import Inferno from 'inferno-compat'
import ReactHintFactory from 'react-hint'
const ReactHint = ReactHintFactory(Inferno)

// UMD
const ReactHint = window.ReactHintFactory(window.React)

You don't need to include ReactHint in every component which uses tooltips, just include it once in the topmost container component.

Use

ReactHint is (in 99% of cases) a singleton-component which is used to render tooltips which appear on multiple elements :

<ReactHint autoPosition events />
<button data-rh="tooltip 1">Hover me 1 !</button>
<button data-rh="tooltip 2">Hover me 2 !</button>
<button data-rh="tooltip 3">Hover me 3 !</button>
<button data-rh="tooltip 4">Hover me 4 !</button>

The text content which appears inside the tooltip is set by data-rh attribute.

Tooltip will appear on hover on every element with data-rh attribute.

Note : tooltip can also be toggled by calling toggleHint() on the ref of a the component:

<ReactHint autoPosition events ref={(ref) => { this.tooltip = ref; }} />
<div data-rh="tooltip">Element with tooltip</div>
<button onClick={() => { this.tooltip.toggleHint(); }}>Click me to toggle React Hint on element !</button>

custom content

In case you need to define custom content (HTML), you must use the onRenderContent prop of ReactHint:

<ReactHint
	autoPosition
	events
	onRenderContent={(target) => (<div><p>`tooltip ${target.number}`</p></div>)}
/>
<button data-rh data-number="1">Hover me 1 !</button>
<button data-rh data-number="2">Hover me 2 !</button>
<button data-rh data-number="3">Hover me 3 !</button>
<button data-rh data-number="4">Hover me 4 !</button>

Use data-abcdef attribute on component which uses tooltip to pass data which can be accessed via target.abcdef in onRenderContent().

ReactHint is not your regular wrapping tooltip component library, e.g. this is incorrect:

<ReactHint>
	<div>Content of the tooltip</div>
</ReactHint>

multiple instances

In case you need to define multiple instances of ReactHint (ex to show tooltips with different content layout), you can customize the attribute name per instance. ReactHint also supports custom tooltip content with attached event handlers by overriding the content renderer and returning a react node.

// default tooltip
<ReactHint autoPosition events />

// custom tooltip 1
<ReactHint
	persist
	attribute="data-custom-1"
	events={{click: true}}
	onRenderContent={(target) => (<div><p>`tooltip ${target.number}`</p></div>)}
/>

// custom tooltip 2
<ReactHint
	persist
	attribute="data-custom-2"
	events={{click: true}}
	onRenderContent={(target) => (<h1>`tooltip ${target.title}`</h1>)}
/>

<button data-rh="default tooltip 1">Hover me 1 to show default tooltip !</button>
<button data-rh="default tooltip 2">Hover me 2 to show default tooltip !</button>

<button data-custom-1	data-custom-1-number="123">Hover me to show custom tooltip 1 !</button>
<button data-custom-1	data-custom-1-number="456">Hover me to show custom tooltip 1 !</button>

<button data-custom-2	data-custom-2-title="Hello">Hover me to show custom tooltip 2 !</button>
<button data-custom-2	data-custom-2-title="World">Hover me to show custom tooltip 2 !</button>

Note : when using custom attribute name, data should be passed prefixed with attribute name as shown above.

Options

ReactHint Property Type Default Value Description
attribute String "data-rh" Allows setting a custom tooltip attribute instead of the default one.
autoPosition Boolean false Autopositions tooltips based on closeness to window borders.
className String "react-hint" You can override the tooltip style by passing the className property.
delay Number or {show: Number, hide: Number} 0 The default delay (in milliseconds) before showing/hiding the tooltip.
events Boolean or {click: Boolean, focus: Boolean, hover: Boolean} false Enables/disables all events or a subset of events.
onRenderContent Function Allows rendering of custom HTML content (with attached event handlers). Pass a function which returns a react node.
persist Boolean false Hide the tooltip only on outside click, hover, etc.
position "top", "left", "right", "bottom" "top" Allows setting the default tooltip placement.
ref Function You can pass a function which will get a reference to the tooltip instance.
DOM Element Attribute Type Default Value Description
data-rh String Sets the tooltip's text content (if onRenderContent is not used to set custom HTML content).
data-rh-at "top", "left", "right", "bottom" "top" Allows overriding the default tooltip placement.

Full Example

import React from 'react'
import {render} from 'react-dom'
import ReactHintFactory from 'react-hint'
import 'react-hint/css/index.css'

const ReactHint = ReactHintFactory(React)
class App extends React.Component {
	onRenderContent = (target, content) => {
		const {catId} = target.dataset
		const width = 240
		const url = `https://images.pexels.com/photos/${catId}/pexels-photo-${catId}.jpeg?w=${width}`

		return <div className="custom-hint__content">
			<img src={url} width={width} />
			<button ref={(ref) => ref && ref.focus()}
				onClick={() => this.instance.toggleHint()}>Ok</button>
		</div>
	}

	render() {
		return <div>
			<ReactHint autoPosition events delay={{show: 100, hide: 1000}} />
			<ReactHint persist
				attribute="data-custom"
				className="custom-hint"
				events={{click: true}}
				onRenderContent={this.onRenderContent}
				ref={(ref) => this.instance = ref} />

			<button data-rh="Default">Default</button>
			<button data-rh="Top" data-rh-at="top">Top</button>
			<button data-rh="Right" data-rh-at="right">Right</button>
			<button data-rh="Bottom" data-rh-at="bottom">Bottom</button>
			<button data-rh="Left" data-rh-at="left">Left</button>

			<button data-custom
				data-custom-at="bottom"
				data-cat-id="10913">Click Me</button>

			<button data-custom
				data-custom-at="bottom"
				data-cat-id="416088">Click Me</button>
		</div>
	}
}

render(<App />, demo)

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].