HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div id="react-root"></div>
#react-root {
max-width: 1200px;
margin: 0 auto;
padding: 0 10px;
}
section {
margin-bottom: 50px;
}
a {
display: inline-block;
}
.target {
text-decoration: underline;
cursor: pointer;
}
.target .react-tooltip-lite {
cursor: default;
}
.flex-spread {
display: flex;
justify-content: space-between;
}
.tip-heading {
margin: 0 0 10px;
}
.tip-list {
margin: 0;
padding: 0 0 0 15px;
}
.tip-list li {
margin: 5px 0;
padding: 0;
}
/* tooltip styles */
.react-tooltip-lite {
background: #333;
color: white;
}
.react-tooltip-lite a {
color: #86b0f4;
text-decoration: none;
}
.react-tooltip-lite a:hover {
color: #4286f4;
}
.react-tooltip-lite-arrow {
border-color: #333;
}
/* overrides with a custom class */
.customTip .react-tooltip-lite {
border: 1px solid #888;
background: #ccc;
color: black;
}
.customTip .react-tooltip-lite-arrow {
border-color: #444;
position: relative;
}
.customTip .react-tooltip-lite-arrow::before {
content: '';
position: absolute;
width: 0;
height: 0;
z-index: 99;
display: block;
}
.customTip .react-tooltip-lite-up-arrow::before {
border-top: 10px solid #ccc;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
left: -10px;
top: -11px;
}
.customTip .react-tooltip-lite-down-arrow::before {
border-bottom: 10px solid #ccc;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
left: -10px;
bottom: -11px;
}
.customTip .react-tooltip-lite-right-arrow::before {
border-right: 10px solid #ccc;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
right: -11px;
top: -10px;
}
.customTip .react-tooltip-lite-left-arrow::before {
border-left: 10px solid #ccc;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
left: -11px;
top: -10px;
}
.imageWrapper {
margin: 50px 0 0;
position: relative;
}
.imageWrapper img {
width: 500px;
height: 500px;
}
.controlled-example {
max-width: 250px;
}
.controlled-example_header {
display: flex;
justify-content: space-between;
margin-bottom: 15px;
padding-bottom: 5px;
border-bottom: 1px solid #fff;
}
.controlled-example_close-button {
cursor: pointer;
background: none;
border: none;
color: white;
font-size: 16px;
padding: 0;
}
.controlled-example_close-button:hover {
color: grey;
}
.internal-scroll-container {
height: 200px;
overflow: auto;
}
.internal-scroll-container > div {
padding-top: 100px;
height: 400px;
}
.arrow-content-tooltip .react-tooltip-lite {
box-sizing: border-box;
border: 1px solid gray;
border-radius: 8px;
box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.2);
}
.arrow-content-tooltip .react-tooltip-lite-down-arrow svg {
transform: translateY(1px);
}
.arrow-content-tooltip .react-tooltip-lite-right-arrow svg {
transform: rotate(270deg) translateY(-4px) translateX(-4px);
}
.arrow-content-tooltip .react-tooltip-lite-up-arrow svg {
transform: rotate(180deg) translateY(1px);
}
.arrow-content-tooltip .react-tooltip-lite-left-arrow svg {
transform: rotate(90deg) translateY(5px) translateX(4px);
}
const Tooltip = ReactToolTipLite.default;
class App extends React.Component {
constructor(props) {
super(props);
this.state = { tipOpen: false };
this.toggleTip = this.toggleTip.bind(this);
this.bodyClick = this.bodyClick.bind(this);
}
componentDidMount() {
document.addEventListener('mousedown', this.bodyClick);
}
componentWillUnmount() {
document.removeEventListener('mousedown', this.bodyClick);
}
tipContentRef;
buttonRef;
toggleTip() {
this.setState(prevState => ({ tipOpen: !prevState.tipOpen }));
}
bodyClick(e) {
if ((this.tipContentRef && this.tipContentRef.contains(e.target)) || this.buttonRef.contains(e.target)) {
return;
}
this.setState({ tipOpen: false });
}
render() {
const { tipOpen } = this.state;
return (
<div className="wrapper">
<h1>React tooltip-lite examples</h1>
<section>
<h3>Basic:</h3>
<div className="flex-spread">
<Tooltip padding={20} content="By default the text is above the element" className="target" tipContentClassName="foo">
Target
</Tooltip>
<Tooltip content="It'll center if it has room" className="target" tipContentClassName="">
Target
</Tooltip>
<Tooltip content="you can specify 'direction' (up, down, left, right) too" direction="down" className="target" tipContentClassName="">
t
</Tooltip>
<Tooltip onToggle={(isOpen) => { console.log(`Is tooltip open ? \n Answer : ${isOpen ? 'Yes' : 'No'}`); }} content="alert shown" className="target" tipContentClassName="">
Hover Me
</Tooltip>
</div>
</section>
<section>
<h3>In a paragraph</h3>
<p>
For
<Tooltip content="Go to google" direction="right" tagName="span">
<a href="http://google.com" target="_blank" rel="noopener noreferrer">inline text</a>
</Tooltip>
, a right or left tip works nicely. The tip will try to go the desired way and flip if there is not
enough
<Tooltip content="Go to google" direction="right" tagName="span" distance={20}>
<a href="http://google.com" target="_blank" rel="noopener noreferrer">space</a>
</Tooltip>
. Shrink the window and see how the tip behaves when close to the
<Tooltip content="Go to google" direction="right" tagName="span">
<a href="http://google.com" target="_blank" rel="noopener noreferrer">edge</a>
</Tooltip>
. You can also force the direction of the tip and it will allow itself
<Tooltip className="target" tipContentClassName="" content="this direction is forced" direction="right" tagName="span" forceDirection>to go off screen</Tooltip>
.
</p>
</section>
<section>
<h3>Html Contents</h3>
<p>
You can also have a tooltip with
<Tooltip
content={(
<div>
<h4 className="tip-heading">An unordered list to demo some html content</h4>
<ul className="tip-list">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</div>
)}
direction="down"
tagName="span"
className="target"
tipContentClassName=""
>
Html content
</Tooltip>
.
</p>
<p>
By specifying the prop "tipContentHover" as true, you can persist hover state when cursor is over the tip. This allows for links
in your tip, copying contents and other behaviors. Here's an
<Tooltip
content={(
<div>
You can copy this text, or click this
<a href="https://www.npmjs.com/package/react-tooltip-lite" target="_blank" rel="noopener noreferrer">link</a>
</div>
)}
tagName="span"
direction="right"
className="target"
tipContentClassName=""
tipContentHover
>
example
</Tooltip>
.
</p>
</section>
<section>
<h3>Colors</h3>
You can pass
<Tooltip
tagName="span"
className="target"
tipContentClassName=""
color="blue"
background="red"
content="The color for this is defined by props"
>
color options as props
</Tooltip>
or use a
<Tooltip
tagName="span"
className="target customTip"
direction="right"
content="The color for this tip is defined by examples/index.css"
>
css stylesheet.
</Tooltip>
With the arrowContent prop you have
<Tooltip
content="Event more customizations possible via the arrowContent prop"
className="target"
background="white"
color="black"
tipContentClassName="arrow-content-tooltip"
spacing={0}
tagName="span"
arrowContent={(
<svg style={{ display: 'block' }} viewBox="0 0 21 11" width="20px" height="10px">
<path
d="M0,11 L9.43630703,1.0733987 L9.43630703,1.0733987 C10.1266203,0.3284971 11.2459708,0 11.936284,1.0733987 L20,11"
style={{ stroke: 'gray', fill: 'white' }}
/>
</svg>
)}
>
even more control.
</Tooltip>
</section>
<section>
<h3>Wrap anything as a target</h3>
<Tooltip content="this is lorem ipsum">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut tincidunt egestas sapien quis lacinia. Praesent ut sem leo.
Curabitur vel dolor eu nulla ultrices efficitur a ut mauris. Nulla non odio non nibh venenatis commodo non vitae magna.
Nunc porttitor, dolor nec sodales commodo, velit elit auctor arcu, sed dapibus nibh lacus sit amet nunc.
Phasellus enim dui, blandit sed faucibus sit amet, feugiat vel urna. Vivamus ut lacus sollicitudin, dignissim risus vel,
iaculis leo. Donec lobortis, turpis nec pulvinar venenatis, orci nunc semper sem, nec ornare nisl nisi ut ligula. Integer
ut tempus elit. Cras luctus, tellus id vestibulum accumsan, purus velit mattis erat, euismod tempor mauris elit eget metus.
Vivamus interdum ex sed egestas tincidunt.
</Tooltip>
</section>
<section>
<h3>Custom events</h3>
<p>
<Tooltip content="this uses hover but also closes on click" className="target" tipContentClassName="" tagName="span" eventOff="onClick">
Close on click
</Tooltip>
</p>
<p>
<Tooltip
content="opens on a click and closes on mouse out"
className="target"
tipContentClassName=""
tagName="span"
eventOn="onClick"
eventOff="onMouseOut"
useHover={false}
>
Open on click
</Tooltip>
</p>
<p>
<Tooltip content="this uses hover but also closes on click" className="target" tipContentClassName="" tagName="span" eventToggle="onClick">
Toggle on click
</Tooltip>
</p>
</section>
<section>
<h3>Default styles</h3>
<p>
pass the
{'"defaultStyles"'}
prop as true to get up and running quick and easy
</p>
<p>
<Tooltip content="styled with defaults" className="target" tipContentClassName="" useDefaultStyles tagName="span">
See default styles
</Tooltip>
</p>
</section>
<section>
<h3>Controlled by props</h3>
<button
type="button"
ref={(el) => { this.buttonRef = el; }}
onClick={this.toggleTip}
>
{tipOpen ? 'close' : 'open'}
</button>
<br />
<br />
<Tooltip
content={(
<div ref={(el) => { this.tipContentRef = el; }} className="controlled-example">
<div className="controlled-example_header">
Hello
<button type="button" className="controlled-example_close-button" onClick={this.toggleTip}>×</button>
</div>
This tip is controlled by the button, you can also click outside the tip or on the
{'"x"'}
to close it
</div>
)}
isOpen={tipOpen}
tagName="span"
direction="down"
forceDirection
>
click the button
</Tooltip>
</section>
<section>
<h3>Distance and arrow size</h3>
<div className="flex-spread">
<Tooltip content="This has an arrowSize of 20, where the default is 10" className="target" tipContentClassName="" arrowSize={20}>Larger arrowSize</Tooltip>
<Tooltip content="This has an arrowSize of 5, where the default is 10" className="target" tipContentClassName="" arrowSize={5}>Smaller arrowSize</Tooltip>
<Tooltip content="This has a distance prop of 20, where the default is arrowSize" className="target" tipContentClassName="" distance={20}>Increase distance</Tooltip>
<Tooltip content="This has a distance prop of 0, where the default is the arrowSize" className="target" tipContentClassName="" distance={0}>Decrease distance</Tooltip>
</div>
</section>
<section>
<h3>Compound Alignment</h3>
<div className="flex-spread">
<Tooltip content="you can have compound alignments" direction="right-start" className="target" tipContentClassName="" arrow={false}>
right-start
</Tooltip>
<Tooltip content="you can have compound alignments" direction="right-end" className="target" tipContentClassName="" arrow={false}>
right-end
</Tooltip>
<Tooltip content="you can have compound alignments" direction="left-start" className="target" tipContentClassName="" arrow={false}>
left-start
</Tooltip>
<Tooltip content="you can have compound alignments" direction="left-end" className="target" tipContentClassName="" arrow={false}>
left-end
</Tooltip>
<Tooltip content="you can have compound alignments" direction="up-start" className="target" tipContentClassName="" arrow={false}>
top-start
</Tooltip>
<Tooltip content="you can have compound alignments" direction="up-end" className="target" tipContentClassName="" arrow={false}>
top-end
</Tooltip>
<Tooltip content="you can have compound alignments" direction="down-start" className="target" tipContentClassName="" arrow={false}>
down-start
</Tooltip>
<Tooltip content="you can have compound alignments" direction="down-end" className="target" tipContentClassName="" arrow={false}>
down-end
</Tooltip>
</div>
<br />
<br />
<div className="flex-spread">
<Tooltip content="you can have compound alignments" direction="right-start" className="target" tipContentClassName="">
right-start with arrow
</Tooltip>
<Tooltip content="you can have compound alignments" direction="right-end" className="target" tipContentClassName="">
right-end with arrow
</Tooltip>
<Tooltip content="you can have compound alignments" direction="left-start" className="target" tipContentClassName="">
left-start with arrow
</Tooltip>
<Tooltip content="you can have compound alignments" direction="left-end" className="target" tipContentClassName="">
left-end with arrow
</Tooltip>
<Tooltip content="you can have compound alignments" direction="down-start" className="target" tipContentClassName="">
down-start with arrow
</Tooltip>
<Tooltip content="you can have compound alignments" direction="down-end" className="target" tipContentClassName="">
down-end with arrow
</Tooltip>
<Tooltip content="you can have compound alignments" direction="up-start" className="target" tipContentClassName="">
up-start with arrow
</Tooltip>
<Tooltip content="you can have compound alignments" direction="up-end" className="target" tipContentClassName="">
up-end with arrow
</Tooltip>
</div>
</section>
<section>
<Tooltip tagName="span" content="check the console log" onToggle={isVisible => console.log(`is visible: ${isVisible}`)}>
On toggle example
</Tooltip>
<Tooltip tagName="span" content="This has a z-index of 5000" zIndex={5000} eventToggle="onClick">
z-index example
</Tooltip>
</section>
<section>
<h3>Internal scrollbars</h3>
<div className="internal-scroll-container">
<div>
<Tooltip content="hello" tagName="span">
Scroll on mobile tapping here
</Tooltip>
</div>
</div>
</section>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('react-root'));
Also see: Tab Triggers