Skip to content

Commit de2cf26

Browse files
committed
Adding support for tooltips on buttons
1 parent eaf7acb commit de2cf26

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

src/ReactMde.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,19 @@ const HeaderGroup = (props) => (
3535
</ul>
3636
);
3737

38-
const HeaderItem = ({icon, onClick}) => {
38+
const HeaderItem = ({icon, onClick, tooltip}) => {
3939
var x = React.isValidElement(icon) ? icon : <i className={`fa fa-${icon}`} aria-hidden="true"></i>
40+
41+
let buttonProps = {};
42+
if(tooltip) {
43+
buttonProps = {
44+
'aria-label': tooltip,
45+
className: 'tooltipped'
46+
}
47+
}
4048
return (
4149
<li className="mde-header-item">
42-
<button type="button" onClick={onClick}>
50+
<button type="button" {...buttonProps} onClick={onClick}>
4351
{x}
4452
</button>
4553
</li>
@@ -117,17 +125,17 @@ class ReactMde extends Component {
117125
<div className="react-mde">
118126
<div className="mde-header">
119127
<HeaderGroup>
120-
<HeaderItem icon="bold" onClick={this.getCommandHandler(ReactMdeCommands.makeBold).bind(this)} />
121-
<HeaderItem icon="italic" onClick={this.getCommandHandler(ReactMdeCommands.makeItalic).bind(this)} />
128+
<HeaderItem icon="bold" tooltip="Add bold text" onClick={this.getCommandHandler(ReactMdeCommands.makeBold).bind(this)} />
129+
<HeaderItem icon="italic" tooltip="Add italic text" onClick={this.getCommandHandler(ReactMdeCommands.makeItalic).bind(this)} />
122130
</HeaderGroup>
123131
<HeaderGroup>
124-
<HeaderItem icon="link" onClick={this.getCommandHandler(ReactMdeCommands.makeLink).bind(this)} />
125-
<HeaderItem icon="quote-right" onClick={this.getCommandHandler(ReactMdeCommands.makeQuote).bind(this)} />
126-
<HeaderItem icon="picture-o" onClick={this.getCommandHandler(ReactMdeCommands.makeImage).bind(this)} />
132+
<HeaderItem icon="link" tooltip="Insert a link" onClick={this.getCommandHandler(ReactMdeCommands.makeLink).bind(this)} />
133+
<HeaderItem icon="quote-right" tooltip="Insert a quote" onClick={this.getCommandHandler(ReactMdeCommands.makeQuote).bind(this)} />
134+
<HeaderItem icon="picture-o" tooltip="Insert a picture" onClick={this.getCommandHandler(ReactMdeCommands.makeImage).bind(this)} />
127135
</HeaderGroup>
128136
<HeaderGroup>
129-
<HeaderItem icon="list-ul" onClick={this.getCommandHandler(ReactMdeCommands.makeUnorderedList).bind(this)} />
130-
<HeaderItem icon="list-ol" onClick={this.getCommandHandler(ReactMdeCommands.makeOrderedList).bind(this)} />
137+
<HeaderItem icon="list-ul" tooltip="Add a bulleted list" onClick={this.getCommandHandler(ReactMdeCommands.makeUnorderedList).bind(this)} />
138+
<HeaderItem icon="list-ol" tooltip="Add a numbered list" onClick={this.getCommandHandler(ReactMdeCommands.makeOrderedList).bind(this)} />
131139
</HeaderGroup>
132140
</div>
133141
<div className="mde-text">

src/styles/react-mde.scss

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
list-style: none;
1212
li.mde-header-item {
1313
display: inline-block;
14+
position: relative;
1415
margin: 0 0.25rem;
1516
button {
1617
cursor: pointer;
@@ -21,6 +22,57 @@
2122
.fa {
2223
color: $mde-button-color;
2324
}
25+
@keyframes tooltip-appear {
26+
from {
27+
opacity: 0;
28+
}
29+
to {
30+
opacity: 1;
31+
}
32+
}
33+
@mixin tooltip-animation {
34+
animation-name: tooltip-appear;
35+
animation-duration: 0.2s;
36+
animation-delay: 0.5s;
37+
animation-fill-mode: forwards;
38+
}
39+
&.tooltipped {
40+
&:hover::before {
41+
@include tooltip-animation();
42+
opacity: 0;
43+
position: absolute;
44+
z-index: 1000001;
45+
width: 0;
46+
height: 0;
47+
color: rgba(0, 0, 0, 0.8);
48+
pointer-events: none;
49+
content: "";
50+
border: 5px solid transparent;
51+
top: -5px;
52+
right: 50%;
53+
bottom: auto;
54+
margin-right: -5px;
55+
border-top-color: rgba(0, 0, 0, 0.8);
56+
}
57+
&:hover::after {
58+
@include tooltip-animation();
59+
font-size: 11px;
60+
opacity: 0;
61+
position: absolute;
62+
z-index: 1000000;
63+
padding: 5px 8px;
64+
color: #fff;
65+
pointer-events: none;
66+
content: attr(aria-label);
67+
background: rgba(0, 0, 0, 0.8);
68+
border-radius: 3px;
69+
right: 50%;
70+
bottom: 100%;
71+
transform: translateX(50%);
72+
margin-bottom: 5px;
73+
white-space: nowrap;
74+
}
75+
}
2476
}
2577
}
2678
}

0 commit comments

Comments
 (0)