@cfpb/cfpb-design-system 3.5.0 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@cfpb/cfpb-design-system",
3
- "version": "3.5.0",
3
+ "version": "3.6.0",
4
4
  "description": "CFPB's UI framework",
5
- "main": "src/index.js",
5
+ "exports": {
6
+ ".": "./src/index.js",
7
+ "./tooltips": "./src/components/cfpb-tooltips/index.js"
8
+ },
6
9
  "author": {
7
10
  "name": "Consumer Financial Protection Bureau",
8
11
  "email": "tech@cfpb.gov",
@@ -21,5 +24,8 @@
21
24
  "url": "https://github.com/cfpb/design-system/issues"
22
25
  },
23
26
  "gitHead": "d9b9862ef0a34a0ca6f4835347ac7f202ed50e3e",
24
- "type": "module"
27
+ "type": "module",
28
+ "dependencies": {
29
+ "tippy.js": "6.3.7"
30
+ }
25
31
  }
@@ -0,0 +1,79 @@
1
+ /* ==========================================================================
2
+ Tooltip Organism
3
+ ========================================================================== */
4
+
5
+ import tippy from 'tippy.js';
6
+ import { instantiateAll } from '@cfpb/cfpb-design-system';
7
+
8
+ import * as TooltipStyles from './tooltip.scss';
9
+
10
+ const BASE_ATTRIBUTE = 'data-tooltip';
11
+
12
+ /**
13
+ * Tooltip
14
+ * @class
15
+ * @classdesc Initializes a new Tooltip.
16
+ * @param {HTMLElement} element - The DOM element that should
17
+ * activate the tooltip.
18
+ * @returns {Tooltip} An instance.
19
+ */
20
+ function Tooltip(element) {
21
+ const tooltipContent = element.getAttribute(BASE_ATTRIBUTE);
22
+
23
+ /**
24
+ * Set up and create the tooltip.
25
+ * @returns {Tooltip} An instance.
26
+ */
27
+ function init() {
28
+ tippy(element, {
29
+ theme: 'cfpb',
30
+ maxWidth: 450,
31
+ content: function (reference) {
32
+ const template = reference.parentElement.querySelector(
33
+ `#${tooltipContent}`,
34
+ );
35
+ const container = document.createElement('div');
36
+ const node = document.importNode(template.content, true);
37
+ container.appendChild(node);
38
+ return container;
39
+ },
40
+ // See https://atomiks.github.io/tippyjs/v6/plugins/
41
+ plugins: [
42
+ {
43
+ name: 'hideOnEsc',
44
+ defaultValue: true,
45
+ fn({ hide }) {
46
+ /**
47
+ * Hide when the escape key is pressed.
48
+ * @param {KeyboardEvent} event - Key down event.
49
+ */
50
+ function onKeyDown(event) {
51
+ if (event.key === 'Escape') {
52
+ hide();
53
+ }
54
+ }
55
+ return {
56
+ onShow() {
57
+ document.body.addEventListener('keydown', onKeyDown);
58
+ },
59
+ onHide() {
60
+ document.body.removeEventListener('keydown', onKeyDown);
61
+ },
62
+ };
63
+ },
64
+ },
65
+ ],
66
+ });
67
+ }
68
+
69
+ // Attach public events.
70
+ this.init = init;
71
+
72
+ return this;
73
+ }
74
+
75
+ Tooltip.BASE_ATTRIBUTE = BASE_ATTRIBUTE;
76
+ Tooltip.init = (scope) =>
77
+ instantiateAll(`[${Tooltip.BASE_ATTRIBUTE}]`, Tooltip, scope);
78
+
79
+ export { Tooltip, TooltipStyles };
@@ -0,0 +1,36 @@
1
+ @use 'sass:math';
2
+ @use '@cfpb/cfpb-design-system/src/abstracts' as *;
3
+ @use 'tippy.js/dist/tippy.css';
4
+ @use 'tippy.js/dist/border.css';
5
+
6
+ // Custom theme, see https://atomiks.github.io/tippyjs/v6/themes/
7
+ .tippy-box[data-theme='cfpb'] {
8
+ background-color: var(--gray-5);
9
+ border: 1px solid var(--gray-40);
10
+ border-radius: 0;
11
+ color: var(--black);
12
+ padding: math.div(15px, $base-font-size-px) + rem;
13
+
14
+ .tippy-arrow {
15
+ color: var(--gray-5);
16
+ }
17
+
18
+ .tippy-heading {
19
+ font-weight: 500;
20
+ font-size: math.div(18px, $base-font-size-px) + rem;
21
+ }
22
+
23
+ .tippy-body {
24
+ font-size: math.div(16px, $base-font-size-px) + rem;
25
+ margin-top: math.div(15px, $base-font-size-px) + rem;
26
+ }
27
+ }
28
+
29
+ [data-tooltip] {
30
+ cursor: pointer;
31
+
32
+ // Hide tooltip trigger elements when JS isn't supported
33
+ .no-js & {
34
+ display: none;
35
+ }
36
+ }