├── .editorconfig
├── .gitignore
├── LICENSE
├── README.md
├── bower.json
├── demo
└── index.html
├── jquery-accessible-simple-tooltip-aria.js
└── package.json
/.editorconfig:
--------------------------------------------------------------------------------
1 | # EditorConfig is awesome: http://EditorConfig.org
2 |
3 | # top-most EditorConfig file
4 | root = true
5 |
6 | # Unix-style newlines with a newline ending every file
7 | [*]
8 | end_of_line = lf
9 | charset = utf-8
10 | trim_trailing_whitespace = true
11 | insert_final_newline = true
12 |
13 | [*.{js,scss,css,html,hbs,twig,json}]
14 | indent_style = space
15 | indent_size = 4
16 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 |
3 | # OS generated files #
4 | ######################
5 | .DS_Store
6 | .DS_Store?
7 | ._*
8 | .Spotlight-V100
9 | .Trashes
10 | ehthumbs.db
11 | Thumbs.db
12 | desktop.ini
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) Nicolas Hoffmann
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # jQuery accessible simple tooltip window, using ARIA
2 |
3 | This jQuery plugin will provide you an accessible and simple non-modal tooltip, using ARIA .
4 |
5 | ## Installation
6 |
7 | You can download and install the plugin via:
8 | - npm: `$ npm i jquery-accessible-simple-tooltip-aria`
9 | - bower: `$ bower install jquery-accessible-simple-tooltip-aria`
10 | ## Demo
11 |
12 | The demo is here: https://a11y.nicolas-hoffmann.net/simple-tooltip/
13 |
14 | ## Why it is accessible
15 |
16 | - It relies on ARIA Design pattern for Tooltips ;
17 | - Thanks to aria-describedby
and id
s generated on the fly, you never loose any information;
18 | - You can close it using Esc .
19 |
20 | ## How it works
21 |
22 | ### Default
23 |
24 | Basically, the scripts wraps each class="js-simple-tooltip"
into a span class="<your-prefix-class>-container"
and adds the content into a hidden content next to it. Once you focus or hover the element with class="js-simple-tooltip"
, it is displayed.
25 |
26 | You can use it on the tag you want (input
, button
, a
…)
27 |
28 | ### jQuery Plugin
29 |
30 | ```js
31 | $('div').accessibleSimpleTooltipAria({
32 | simpletooltipText: title
33 | });
34 | ```
35 |
36 | ## License
37 |
38 | No license problem, it uses MIT license, so it’s free, open-source and you can do whatever you want with it, including commercial use (permission notice )
39 |
40 | ## Options and attributes
41 |
42 | Use data-simpletooltip-text
or data-simpletooltip-content-id
attributes on an element to activate the tooltip.
43 |
44 | - Simply put class="js-simpletooltip"
on a button to activate the script.
45 | - Attribute data-simpletooltip-prefix-class
(non mandatory): the prefix to all style classes of the tooltip (useful to set up different styles).
46 | - Attribute data-simpletooltip-text
: the text of your tooltip.
47 | - Attribute data-simpletooltip-content-id
: the id
of (hidden) content in your page that will be put into your tooltip.
48 | - Attribute data-simpletooltip-wrapper-tag
: the tag used for wrapping your tooltip (span
by default).
49 | - Attribute data-simpletooltip-tag
: the tag used for your tooltip (span
by default).
50 |
51 | ## How to style it
52 |
53 | ```css
54 | /* it will work better with this box-sizing, you may adapt it to your needs */
55 | /*html { box-sizing: border-box; }
56 | *, *:before, *:after {
57 | box-sizing: inherit;
58 | }*/
59 |
60 | /* Tooltip hidden by default */
61 | .simpletooltip[aria-hidden="true"],
62 | .minimalist-simpletooltip[aria-hidden="true"],
63 | .minimalist-left-simpletooltip[aria-hidden="true"] {
64 | display: none;
65 | }
66 | /* position relative for containers */
67 | .simpletooltip_container,
68 | .minimalist-simpletooltip_container,
69 | .minimalist-left-simpletooltip_container {
70 | position: relative;
71 | display: inline;
72 | }
73 |
74 | /* tooltip styles */
75 | .simpletooltip,
76 | .minimalist-simpletooltip,
77 | .minimalist-left-simpletooltip {
78 | position: absolute;
79 | display: inline-block;
80 | z-index: 666;
81 | width: 10em;
82 | border-radius: .5em;
83 | background: rgba( 0, 0, 0, .9 );
84 | color: #eee;
85 | padding: .5em;
86 | text-align: left;
87 | line-height: 1.3;
88 | }
89 | .simpletooltip,
90 | .minimalist-simpletooltip {
91 | right: auto;
92 | left: 100%;
93 | margin-left: .5em;
94 | }
95 | .minimalist-left-simpletooltip {
96 | right: 100%;
97 | left: auto;
98 | margin-right: .5em;
99 | }
100 | /* used pseudo-element to make arrows */
101 | .simpletooltip::before,
102 | .minimalist-simpletooltip::before,
103 | .minimalist-left-simpletooltip::before {
104 | content: '';
105 | speak: none;
106 | position: absolute;
107 | z-index: 666;
108 | width: 10px;
109 | height: 10px;
110 | }
111 | .simpletooltip::before,
112 | .minimalist-simpletooltip::before {
113 | top: .5em;
114 | left: -10px;
115 | margin-left: -10px;
116 | border: 10px solid transparent;
117 | border-right: 10px solid rgba( 0, 0, 0, .9 );
118 | }
119 | .minimalist-left-simpletooltip::before {
120 | top: .5em;
121 | right: -10px;
122 | margin-right: -10px;
123 | border: 10px solid transparent;
124 | border-left: 10px solid rgba( 0, 0, 0, .9 )
125 | }
126 |
127 | /* it can be easily adapted in media-queries for tablets/mobile */
128 |
129 | /* for this example: mobile */
130 | @media (max-width: 44.375em) {
131 |
132 | .simpletooltip,
133 | .minimalist-simpletooltip,
134 | .minimalist-left-simpletooltip {
135 | top: 100%;
136 | left: 50%;
137 | right: 0;
138 | margin: 0;
139 | margin-top: .7em;
140 | margin-left: -5em;
141 | }
142 | .simpletooltip::before,
143 | .minimalist-simpletooltip::before,
144 | .minimalist-left-simpletooltip::before {
145 | top: -10px;
146 | right: auto;
147 | left: 50%;
148 | margin-left: -5px;
149 | margin-top: -10px;
150 | border: 10px solid transparent;
151 | border-bottom: 10px solid rgba( 0, 0, 0, .9 );
152 | }
153 |
154 | }
155 | ```
156 |
157 |
158 |
159 | ## examples
160 |
161 | ```html
162 |
164 | Hover or focus me to show the tooltip
165 |
166 |
167 |
169 | Show me another tooltip
170 |
171 |
Woot, you can take the content of a hidden block.
172 | ```
173 |
174 | Enjoy.
175 |
--------------------------------------------------------------------------------
/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "jquery-accessible-simple-tooltip-aria",
3 | "description": "A simple jQuery code to provide an accessible and simple non-modal tooltip, using ARIA.",
4 | "homepage": "https://a11y.nicolas-hoffmann.net/simple-tooltip/",
5 | "main": "jquery-accessible-simple-tooltip-aria.js",
6 | "version": "2.3.0",
7 | "keywords": [
8 | "Accessibility",
9 | "ARIA",
10 | "tooltip",
11 | "accessible",
12 | "a11y",
13 | "jQueryPlugin"
14 | ],
15 | "authors": [
16 | {
17 | "name": "Nicolas Hoffmann",
18 | "email": "dante3333@gmail.com",
19 | "web": "https://www.nicolas-hoffmann.net/"
20 | }
21 | ],
22 | "dependencies": {
23 | "jquery": ">=1.0.0"
24 | },
25 | "repositories": [
26 | {
27 | "type": "git",
28 | "url": "git://github.com/nico3333fr/jquery-accessible-simple-tooltip-aria.git"
29 | }
30 | ],
31 | "license": "MIT",
32 | "ignore": [
33 | "**/.*",
34 | "node_modules",
35 | "bower_components",
36 | "README.md"
37 | ]
38 | }
39 |
--------------------------------------------------------------------------------
/demo/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | jQuery accessible simple tooltip, using ARIA - By Nicolas Hoffmann
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
Made In La France !
49 |
50 |
51 |
52 |
jQuery accessible simple tooltip window, using ARIA
53 |
54 |
58 |
This jQuery plugin will provide you an accessible and simple non-modal tooltip, using ARIA .
59 |
60 |
61 |
62 |
63 |
First simple example
64 |
65 |
<button class="js-simple-tooltip"
66 | data-simpletooltip-text="Cool, it works!">
67 | Hover or focus me to show the tooltip
68 | </button>
69 |
70 |
71 |
Hover or focus me to show the tooltip
72 |
73 |
74 |
75 |
76 |
77 |
Second simple example
78 |
79 |
<button class="js-simple-tooltip"
80 | data-simpletooltip-content-id="tooltip-case_1">
81 | Show me another tooltip
82 | </button>
83 | <div id="tooltip-case_1" class="hidden">Woot, you can take the content of a hidden block.</div>
84 |
85 |
86 |
Show me another tooltip
87 |
88 |
Woot, you can take the content of a hidden block.
89 |
90 |
91 |
92 |
93 |
96 |
97 |
Some informations about this script
98 |
99 |
100 |
101 |
It’s accessible
102 |
103 |
104 |
105 |
It’s lightweight
106 |
107 |
110 |
111 |
112 |
It’s easy
113 |
114 |
115 |
116 |
It’s free
117 |
118 |
119 | No license problem, it uses MIT license, so it’s free, open-source and you can do whatever you want with it, including commercial use .
120 |
121 |
122 |
123 |
124 |
Why it is accessible
125 |
126 |
130 |
You can close it using Esc .
131 |
132 |
How it works
133 |
134 |
Basically, the scripts wraps each class="js-simple-tooltip"
into a span class="<your-prefix-class>-container"
and adds the content into a hidden content next to it. Once you focus or hover the element with class="js-simple-tooltip"
, it is displayed.
135 |
136 |
You can use it on the tag you want (input
, button
, a
…)
137 |
138 |
No license problem, it uses MIT license, so it’s free, open-source and you can do whatever you want with it, including commercial use (permission notice )
139 |
140 |
141 |
Last news
142 |
143 |
144 |
17th of July 2018: added data-simpletooltip-wrapper-tag
and data-simpletooltip-tag
attributes (to specify tags used for tooltip), based on as kindly suggested by @mh-nichts .
145 |
146 |
1st of December 2017: added a fix in case of an already existing attribute aria-describedby
, as kindly requested by @jum44 .
147 |
148 |
18th of April 2017: linted and reindented code properly.
149 |
150 |
15th of July 2016: thanks to dhoko’s fantastic work , this projet is a real jQuery plugin.
151 |
152 |
27th of June 2016: this plugin is available on bower, and you can install it with bower install jquery-accessible-simple-tooltip-aria
.
153 |
154 |
155 |
7th of February 2016: this plugin is available on NPMjs.com , and you can install it with npm i jquery-accessible-simple-tooltip-aria
.
156 |
Options and attributes
157 |
158 |
Use data-simpletooltip-text
or data-simpletooltip-content-id
attributes on an element to activate the tooltip.
159 |
160 |
161 | Simply put class="js-simpletooltip"
on a button to activate the script.
162 | Attribute data-simpletooltip-prefix-class
(non mandatory): the prefix to all style classes of the tooltip (useful to set up different styles).
163 | Attribute data-simpletooltip-text
: the text of your tooltip.
164 | Attribute data-simpletooltip-content-id
: the id
of (hidden) content in your page that will be put into your tooltip.
165 | Attribute data-simpletooltip-wrapper-tag
: the tag used for wrapping your tooltip (span
by default).
166 | Attribute data-simpletooltip-tag
: the tag used for your tooltip (span
by default).
167 |
168 |
169 |
How to style it
170 |
171 |
Here are the styles used for this page: (I’ve used no prefix class, data-simpletooltip-prefix-class="minimalist "
and data-simpletooltip-prefix-class="minimalist-left "
to position the tooltip on the left on some examples, you can set up your own
172 | styles)
173 |
174 |
/* Tooltip hidden by default */
175 | .simpletooltip[aria-hidden="true"],
176 | .minimalist-simpletooltip[aria-hidden="true"],
177 | .minimalist-left-simpletooltip[aria-hidden="true"] {
178 | display: none;
179 | }
180 | /* position relative for containers */
181 | .simpletooltip_container,
182 | .minimalist-simpletooltip_container,
183 | .minimalist-left-simpletooltip_container {
184 | position: relative;
185 | display: inline;
186 | }
187 |
188 | /* tooltip styles */
189 | .simpletooltip,
190 | .minimalist-simpletooltip,
191 | .minimalist-left-simpletooltip {
192 | position: absolute;
193 | display: inline-block;
194 | z-index: 666;
195 | width: 10em;
196 | border-radius: .5em;
197 | background: rgba( 0, 0, 0, .9 );
198 | color: #eee;
199 | padding: .5em;
200 | text-align: left;
201 | line-height: 1.3;
202 | }
203 | .simpletooltip,
204 | .minimalist-simpletooltip {
205 | right: auto;
206 | left: 100%;
207 | margin-left: .5em;
208 | }
209 | .minimalist-left-simpletooltip {
210 | right: 100%;
211 | left: auto;
212 | margin-right: .5em;
213 | }
214 | /* used pseudo-element to make arrows */
215 | .simpletooltip::before,
216 | .minimalist-simpletooltip::before,
217 | .minimalist-left-simpletooltip::before {
218 | content: '';
219 | speak: none;
220 | position: absolute;
221 | z-index: 666;
222 | width: 10px;
223 | height: 10px;
224 | }
225 | .simpletooltip::before,
226 | .minimalist-simpletooltip::before {
227 | top: .5em;
228 | left: -10px;
229 | margin-left: -10px;
230 | border: 10px solid transparent;
231 | border-right: 10px solid rgba( 0, 0, 0, .9 );
232 | }
233 | .minimalist-left-simpletooltip::before {
234 | top: .5em;
235 | right: -10px;
236 | margin-right: -10px;
237 | border: 10px solid transparent;
238 | border-left: 10px solid rgba( 0, 0, 0, .9 )
239 | }
240 |
241 | /* it can be easily adapted in media-queries for tablets/mobile */
242 |
243 | /* for this example: mobile */
244 | @media (max-width: 44.375em) {
245 |
246 | .simpletooltip,
247 | .minimalist-simpletooltip,
248 | .minimalist-left-simpletooltip {
249 | top: 100%;
250 | left: 50%;
251 | right: 0;
252 | margin: 0;
253 | margin-top: .7em;
254 | margin-left: -5em;
255 | }
256 | .simpletooltip::before,
257 | .minimalist-simpletooltip::before,
258 | .minimalist-left-simpletooltip::before {
259 | top: -10px;
260 | right: auto;
261 | left: 50%;
262 | margin-left: -5px;
263 | margin-top: -10px;
264 | border: 10px solid transparent;
265 | border-bottom: 10px solid rgba( 0, 0, 0, .9 );
266 | }
267 |
268 | }
269 |
270 |
271 |
272 |
273 |
As usual, it is not prohibited to tell me that you’ve used it, or send me a little “thank you”. ;)
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
--------------------------------------------------------------------------------
/jquery-accessible-simple-tooltip-aria.js:
--------------------------------------------------------------------------------
1 | (function() {
2 |
3 | 'use strict';
4 |
5 | /*
6 | * jQuery accessible simple (non-modal) tooltip window, using ARIA
7 | * @version v2.2.0
8 | * Website: https://a11y.nicolas-hoffmann.net/simple-tooltip/
9 | * License MIT: https://github.com/nico3333fr/jquery-accessible-simple-tooltip-aria/blob/master/LICENSE
10 | */
11 |
12 | function accessibleSimpleTooltipAria(options) {
13 | var element = $(this);
14 | options = options || element.data();
15 | var text = options.simpletooltipText || '';
16 | var prefix_class = typeof options.simpletooltipPrefixClass !== 'undefined' ? options.simpletooltipPrefixClass + '-' : '';
17 | var content_id = typeof options.simpletooltipContentId !== 'undefined' ? '#' + options.simpletooltipContentId : '';
18 |
19 | var index_lisible = Math.random().toString(32).slice(2, 12);
20 | var aria_describedby = element.attr('aria-describedby') || '';
21 |
22 | element.attr({
23 | 'aria-describedby': ('label_simpletooltip_' + index_lisible + ' ' + aria_describedby).trim()
24 | });
25 |
26 | element.wrap(' ');
27 |
28 | var html = '';
29 |
30 | if (text !== '') {
31 | html += '' + text + '';
32 | } else {
33 | var $contentId = $(content_id);
34 | if (content_id !== '' && $contentId.length) {
35 | html += $contentId.html();
36 | }
37 | }
38 | html += ' ';
39 |
40 | $(html).insertAfter(element);
41 | }
42 |
43 | // Bind as a jQuery plugin
44 | $.fn.accessibleSimpleTooltipAria = accessibleSimpleTooltipAria;
45 |
46 | $(document).ready(function() {
47 |
48 | $('.js-simple-tooltip')
49 | .each(function() {
50 | // Call the function with this as the current tooltip
51 | accessibleSimpleTooltipAria.apply(this);
52 | });
53 |
54 | // events ------------------
55 | $('body')
56 | .on('mouseenter focusin', '.js-simple-tooltip', function() {
57 | var $this = $(this);
58 | var aria_describedby = $this.attr('aria-describedby');
59 | var tooltip_to_show_id = aria_describedby.substr(0, aria_describedby.indexOf(" "));
60 | var $tooltip_to_show = $('#' + tooltip_to_show_id);
61 | $tooltip_to_show.attr('aria-hidden', 'false');
62 | })
63 | .on('mouseleave', '.js-simple-tooltip', function(event) {
64 | var $this = $(this);
65 | var aria_describedby = $this.attr('aria-describedby');
66 | var tooltip_to_show_id = aria_describedby.substr(0, aria_describedby.indexOf(" "));
67 | var $tooltip_to_show = $('#' + tooltip_to_show_id);
68 | var $is_target_hovered = $tooltip_to_show.is(':hover');
69 |
70 | //alert($target_hovered);
71 | //$target.addClass('redborder');
72 | if (!$is_target_hovered) {
73 | $tooltip_to_show.attr('aria-hidden', 'true');
74 | }
75 | })
76 | .on('focusout', '.js-simple-tooltip', function(event) {
77 | var $this = $(this);
78 | var aria_describedby = $this.attr('aria-describedby');
79 | var tooltip_to_show_id = aria_describedby.substr(0, aria_describedby.indexOf(" "));
80 | var $tooltip_to_show = $('#' + tooltip_to_show_id);
81 |
82 | $tooltip_to_show.attr('aria-hidden', 'true');
83 | })
84 | .on('mouseleave', '.js-simpletooltip', function() {
85 | var $this = $(this);
86 | $this.attr('aria-hidden', 'true');
87 | })
88 | .on('keydown', '.js-simple-tooltip', function(event) {
89 | // close esc key
90 |
91 | var $this = $(this);
92 | var aria_describedby = $this.attr('aria-describedby');
93 | var tooltip_to_show_id = aria_describedby.substr(0, aria_describedby.indexOf(" "));
94 | var $tooltip_to_show = $('#' + tooltip_to_show_id);
95 |
96 | if (event.keyCode == 27) { // esc
97 | $tooltip_to_show.attr('aria-hidden', 'true');
98 | }
99 | });
100 | });
101 |
102 | })();
103 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "title": "jQuery accessible simple tooltip using ARIA",
3 | "name": "jquery-accessible-simple-tooltip-aria",
4 | "description": "This jQuery plugin will provide you an accessible and simple non-modal tooltip, using ARIA.",
5 | "version": "2.3.0",
6 | "main": "jquery-accessible-simple-tooltip-aria.js",
7 | "keywords": [
8 | "Accessibility",
9 | "ARIA",
10 | "tooltip",
11 | "jQueryPlugin",
12 | "a11y"
13 | ],
14 | "author": [
15 | {
16 | "name": "Nicolas Hoffmann",
17 | "email": "dante3333@gmail.com",
18 | "web": "https://www.nicolas-hoffmann.net/"
19 | }
20 | ],
21 | "dependencies": {
22 | "jquery": ">=1.0.0"
23 | },
24 | "bugs": {
25 | "web": "https://github.com/nico3333fr/jquery-accessible-simple-tooltip-aria/issues"
26 | },
27 | "repositories": [
28 | {
29 | "type": "git",
30 | "url": "https://github.com/nico3333fr/jquery-accessible-simple-tooltip-aria.git"
31 | }
32 | ],
33 | "licenses": [
34 | {
35 | "name": "MIT",
36 | "url": "https://github.com/nico3333fr/jquery-accessible-simple-tooltip-aria/blob/master/LICENSE"
37 | }
38 | ],
39 | "homepage": "https://a11y.nicolas-hoffmann.net/simple-tooltip/"
40 | }
41 |
--------------------------------------------------------------------------------