@entur/tooltip 2.5.23 → 2.5.24
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.
|
@@ -19,68 +19,77 @@ function _extends() {
|
|
|
19
19
|
_extends = Object.assign || function (target) {
|
|
20
20
|
for (var i = 1; i < arguments.length; i++) {
|
|
21
21
|
var source = arguments[i];
|
|
22
|
+
|
|
22
23
|
for (var key in source) {
|
|
23
24
|
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
24
25
|
target[key] = source[key];
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
}
|
|
29
|
+
|
|
28
30
|
return target;
|
|
29
31
|
};
|
|
32
|
+
|
|
30
33
|
return _extends.apply(this, arguments);
|
|
31
34
|
}
|
|
35
|
+
|
|
32
36
|
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
33
37
|
if (source == null) return {};
|
|
34
38
|
var target = {};
|
|
35
39
|
var sourceKeys = Object.keys(source);
|
|
36
40
|
var key, i;
|
|
41
|
+
|
|
37
42
|
for (i = 0; i < sourceKeys.length; i++) {
|
|
38
43
|
key = sourceKeys[i];
|
|
39
44
|
if (excluded.indexOf(key) >= 0) continue;
|
|
40
45
|
target[key] = source[key];
|
|
41
46
|
}
|
|
47
|
+
|
|
42
48
|
return target;
|
|
43
49
|
}
|
|
44
50
|
|
|
45
51
|
var _excluded$1 = ["placement", "content", "children", "className", "isOpen", "disableHoverListener", "disableFocusListener", "showCloseButton", "variant", "popperModifiers"];
|
|
46
52
|
var Tooltip = function Tooltip(_ref) {
|
|
47
53
|
var placement = _ref.placement,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
54
|
+
content = _ref.content,
|
|
55
|
+
children = _ref.children,
|
|
56
|
+
className = _ref.className,
|
|
57
|
+
_ref$isOpen = _ref.isOpen,
|
|
58
|
+
isOpen = _ref$isOpen === void 0 ? false : _ref$isOpen,
|
|
59
|
+
_ref$disableHoverList = _ref.disableHoverListener,
|
|
60
|
+
disableHoverListener = _ref$disableHoverList === void 0 ? false : _ref$disableHoverList,
|
|
61
|
+
_ref$disableFocusList = _ref.disableFocusListener,
|
|
62
|
+
disableFocusListener = _ref$disableFocusList === void 0 ? false : _ref$disableFocusList,
|
|
63
|
+
_ref$showCloseButton = _ref.showCloseButton,
|
|
64
|
+
showCloseButton = _ref$showCloseButton === void 0 ? true : _ref$showCloseButton,
|
|
65
|
+
variant = _ref.variant,
|
|
66
|
+
_ref$popperModifiers = _ref.popperModifiers,
|
|
67
|
+
popperModifiers = _ref$popperModifiers === void 0 ? [{
|
|
68
|
+
name: 'offset',
|
|
69
|
+
options: {
|
|
70
|
+
offset: [0, 10]
|
|
71
|
+
}
|
|
72
|
+
}] : _ref$popperModifiers,
|
|
73
|
+
rest = _objectWithoutPropertiesLoose(_ref, _excluded$1);
|
|
74
|
+
|
|
68
75
|
var _useState = React.useState(isOpen || false),
|
|
69
|
-
|
|
70
|
-
|
|
76
|
+
showTooltip = _useState[0],
|
|
77
|
+
setShowTooltip = _useState[1];
|
|
78
|
+
|
|
71
79
|
var hoverTimer;
|
|
80
|
+
|
|
72
81
|
var handleOpen = function handleOpen(event) {
|
|
73
82
|
event.persist();
|
|
74
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment -- Typescript interprets this differently depending on whether it's run in Jest or not
|
|
75
|
-
// @ts-ignore -- for some reason, the type checker thinks that this setTimeout is run in node; it is not, because it is run in the browser
|
|
76
83
|
hoverTimer = setTimeout(function () {
|
|
77
84
|
setShowTooltip(true);
|
|
78
85
|
}, 150);
|
|
79
86
|
};
|
|
87
|
+
|
|
80
88
|
var onMouseExit = function onMouseExit() {
|
|
81
89
|
setShowTooltip(false);
|
|
82
90
|
clearTimeout(hoverTimer);
|
|
83
91
|
};
|
|
92
|
+
|
|
84
93
|
React__default["default"].useEffect(function () {
|
|
85
94
|
return function () {
|
|
86
95
|
clearTimeout(hoverTimer);
|
|
@@ -96,32 +105,40 @@ var Tooltip = function Tooltip(_ref) {
|
|
|
96
105
|
}
|
|
97
106
|
}, [content]);
|
|
98
107
|
var popperPlacement = placement;
|
|
108
|
+
|
|
99
109
|
if (placement.includes('-')) {
|
|
100
110
|
if (placement.includes('right')) {
|
|
101
111
|
popperPlacement = placement.replace('right', 'end');
|
|
102
112
|
}
|
|
113
|
+
|
|
103
114
|
if (placement.includes('left')) {
|
|
104
115
|
popperPlacement = placement.replace('left', 'start');
|
|
105
116
|
}
|
|
106
117
|
}
|
|
118
|
+
|
|
107
119
|
var childProps = {};
|
|
108
120
|
childProps['aria-describedby'] = tooltipId;
|
|
121
|
+
|
|
109
122
|
if (!disableFocusListener) {
|
|
110
123
|
childProps.onFocus = function () {
|
|
111
124
|
return setShowTooltip(true);
|
|
112
125
|
};
|
|
126
|
+
|
|
113
127
|
childProps.onBlur = function () {
|
|
114
128
|
return setShowTooltip(false);
|
|
115
129
|
};
|
|
116
130
|
}
|
|
131
|
+
|
|
117
132
|
if (!disableHoverListener) {
|
|
118
133
|
childProps.onMouseEnter = function (e) {
|
|
119
134
|
return handleOpen(e);
|
|
120
135
|
};
|
|
136
|
+
|
|
121
137
|
childProps.onMouseLeave = function () {
|
|
122
138
|
return onMouseExit();
|
|
123
139
|
};
|
|
124
140
|
}
|
|
141
|
+
|
|
125
142
|
return React__default["default"].createElement(reactPopper.Manager, null, React__default["default"].createElement(reactPopper.Reference, null, function (_ref2) {
|
|
126
143
|
var ref = _ref2.ref;
|
|
127
144
|
return React.cloneElement(children, _extends({
|
|
@@ -135,8 +152,8 @@ var Tooltip = function Tooltip(_ref) {
|
|
|
135
152
|
placement: popperPlacement
|
|
136
153
|
}, function (_ref3) {
|
|
137
154
|
var ref = _ref3.ref,
|
|
138
|
-
|
|
139
|
-
|
|
155
|
+
style = _ref3.style,
|
|
156
|
+
popperPlacement = _ref3.placement;
|
|
140
157
|
return React__default["default"].createElement("div", _extends({
|
|
141
158
|
className: classNames__default["default"]('eds-tooltip', className, "eds-tooltip--" + popperPlacement, {
|
|
142
159
|
'eds-tooltip--error': variant === 'error'
|
|
@@ -157,37 +174,45 @@ var Tooltip = function Tooltip(_ref) {
|
|
|
157
174
|
|
|
158
175
|
var _excluded = ["children"];
|
|
159
176
|
var PopoverContext = /*#__PURE__*/React.createContext(undefined);
|
|
177
|
+
|
|
160
178
|
var usePopoverContext = function usePopoverContext() {
|
|
161
179
|
var context = React.useContext(PopoverContext);
|
|
180
|
+
|
|
162
181
|
if (context == null) {
|
|
163
182
|
throw Error('usePopoverContext must be used within <Popover/>');
|
|
164
183
|
}
|
|
184
|
+
|
|
165
185
|
return context;
|
|
166
186
|
};
|
|
187
|
+
|
|
167
188
|
var Popover = function Popover(_ref) {
|
|
168
189
|
var children = _ref.children,
|
|
169
|
-
|
|
170
|
-
|
|
190
|
+
_ref$placement = _ref.placement,
|
|
191
|
+
placement = _ref$placement === void 0 ? 'bottom-start' : _ref$placement;
|
|
192
|
+
|
|
171
193
|
var _React$useState = React__default["default"].useState(false),
|
|
172
|
-
|
|
173
|
-
|
|
194
|
+
showPopover = _React$useState[0],
|
|
195
|
+
setShowPopover = _React$useState[1];
|
|
196
|
+
|
|
174
197
|
var triggerElement = React__default["default"].useRef(null);
|
|
175
198
|
var contentElement = React__default["default"].useRef(null);
|
|
199
|
+
|
|
176
200
|
var _usePopper = reactPopper.usePopper(triggerElement.current, contentElement.current, {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
201
|
+
modifiers: [{
|
|
202
|
+
name: 'arrow',
|
|
203
|
+
enabled: false
|
|
204
|
+
}, {
|
|
205
|
+
name: 'offset',
|
|
206
|
+
options: {
|
|
207
|
+
offset: [0, 8]
|
|
208
|
+
}
|
|
209
|
+
}],
|
|
210
|
+
placement: placement
|
|
211
|
+
}),
|
|
212
|
+
styles = _usePopper.styles,
|
|
213
|
+
attributes = _usePopper.attributes,
|
|
214
|
+
forceUpdate = _usePopper.forceUpdate;
|
|
215
|
+
|
|
191
216
|
React__default["default"].useEffect(function () {
|
|
192
217
|
if (forceUpdate) {
|
|
193
218
|
forceUpdate();
|
|
@@ -227,8 +252,8 @@ var Popover = function Popover(_ref) {
|
|
|
227
252
|
}
|
|
228
253
|
},
|
|
229
254
|
onBlur: function onBlur(event) {
|
|
230
|
-
var elementReceivingFocus = event.relatedTarget;
|
|
231
|
-
|
|
255
|
+
var elementReceivingFocus = event.relatedTarget; // The check for 'tabindex=-1' is a special case for focus handling in Docz
|
|
256
|
+
|
|
232
257
|
if (!elementReceivingFocus || elementReceivingFocus.getAttribute('tabindex') === '-1') return;
|
|
233
258
|
var focusElementIsPopover = elementContainsElement(contentElement.current, elementReceivingFocus);
|
|
234
259
|
var focusElementIsTrigger = elementContainsElement(triggerElement.current, elementReceivingFocus);
|
|
@@ -254,25 +279,31 @@ var Popover = function Popover(_ref) {
|
|
|
254
279
|
};
|
|
255
280
|
var PopoverTrigger = function PopoverTrigger(_ref2) {
|
|
256
281
|
var children = _ref2.children;
|
|
282
|
+
|
|
257
283
|
var _usePopoverContext = usePopoverContext(),
|
|
258
|
-
|
|
284
|
+
triggerProps = _usePopoverContext.triggerProps;
|
|
285
|
+
|
|
259
286
|
var child = React__default["default"].Children.only(children);
|
|
260
287
|
return React.cloneElement(child, triggerProps());
|
|
261
288
|
};
|
|
262
289
|
var PopoverCloseButton = function PopoverCloseButton(_ref3) {
|
|
263
290
|
var children = _ref3.children,
|
|
264
|
-
|
|
291
|
+
rest = _objectWithoutPropertiesLoose(_ref3, _excluded);
|
|
292
|
+
|
|
265
293
|
var _usePopoverContext2 = usePopoverContext(),
|
|
266
|
-
|
|
294
|
+
closeButtonProps = _usePopoverContext2.closeButtonProps;
|
|
295
|
+
|
|
267
296
|
return React.cloneElement(children, _extends({}, closeButtonProps, rest));
|
|
268
297
|
};
|
|
269
298
|
var PopoverContent = /*#__PURE__*/React__default["default"].forwardRef(function (_ref4, ref) {
|
|
270
299
|
var children = _ref4.children;
|
|
300
|
+
|
|
271
301
|
var _usePopoverContext3 = usePopoverContext(),
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
302
|
+
showPopover = _usePopoverContext3.showPopover,
|
|
303
|
+
attributes = _usePopoverContext3.attributes,
|
|
304
|
+
styles = _usePopoverContext3.styles,
|
|
305
|
+
popoverContentProps = _usePopoverContext3.popoverContentProps;
|
|
306
|
+
|
|
276
307
|
var props = popoverContentProps({
|
|
277
308
|
ref: ref
|
|
278
309
|
});
|
|
@@ -284,37 +315,46 @@ var PopoverContent = /*#__PURE__*/React__default["default"].forwardRef(function
|
|
|
284
315
|
"aria-hidden": !showPopover
|
|
285
316
|
}, attributes.styles, props), children);
|
|
286
317
|
});
|
|
318
|
+
|
|
287
319
|
function elementContainsEventTarget(element, event) {
|
|
288
320
|
if (!element) {
|
|
289
321
|
return false;
|
|
290
322
|
}
|
|
323
|
+
|
|
291
324
|
if (element.contains(event.target)) {
|
|
292
325
|
return true;
|
|
293
|
-
}
|
|
294
|
-
|
|
326
|
+
} // For elements inside a Shadow DOM we need to check the composedPath
|
|
327
|
+
|
|
328
|
+
|
|
295
329
|
if (event.composed && event.composedPath) {
|
|
296
330
|
var contains = event.composedPath().find(function (target) {
|
|
297
331
|
if (target === window) {
|
|
298
332
|
return false;
|
|
299
333
|
}
|
|
334
|
+
|
|
300
335
|
return element.contains(target);
|
|
301
336
|
});
|
|
302
337
|
return contains ? true : false;
|
|
303
338
|
}
|
|
339
|
+
|
|
304
340
|
return false;
|
|
305
341
|
}
|
|
342
|
+
|
|
306
343
|
function elementContainsElement(parent, child) {
|
|
307
344
|
if (!parent) return false;
|
|
308
345
|
return parent === child || parent.contains(child);
|
|
309
346
|
}
|
|
347
|
+
|
|
310
348
|
function useOnClickOutside(ref, buttonRef, handler) {
|
|
311
349
|
React__default["default"].useEffect(function () {
|
|
312
350
|
var listener = function listener(event) {
|
|
313
351
|
if (elementContainsEventTarget(ref.current, event) || elementContainsEventTarget(buttonRef.current, event)) {
|
|
314
352
|
return;
|
|
315
353
|
}
|
|
354
|
+
|
|
316
355
|
handler();
|
|
317
356
|
};
|
|
357
|
+
|
|
318
358
|
document.addEventListener('mousedown', listener);
|
|
319
359
|
document.addEventListener('touchstart', listener);
|
|
320
360
|
return function () {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tooltip.cjs.development.js","sources":["../src/Tooltip.tsx","../src/Popover.tsx","../src/index.tsx"],"sourcesContent":["import React, { cloneElement, useState } from 'react';\nimport { Manager, Reference, Popper } from 'react-popper';\nimport classNames from 'classnames';\nimport { useRandomId } from '@entur/utils';\nimport { Placement as PopperPlacementProps } from '@popperjs/core';\nimport { CloseIcon } from '@entur/icons';\nimport { IconButton } from '@entur/button';\nimport './Tooltip.scss';\n\ntype Modifier = {\n name: string;\n enabled?: boolean;\n requires?: Array<string>;\n requiresIfExists?: Array<string>;\n options?: Record<string, unknown>;\n data?: Record<string, unknown>;\n [key: string]: any;\n};\n\nexport type TooltipProps = {\n /** Plassering av tooltip-en */\n placement:\n | 'top'\n | 'top-left'\n | 'top-right'\n | 'left'\n | 'right'\n | 'bottom-left' // bottom-start\n | 'bottom'\n | 'bottom-right'; // bottom-end\n /** Innholdet i tooltip-boksen */\n content: React.ReactNode;\n /** Elementet som skal ha tooltip-funksjonalitet */\n children: React.ReactElement;\n /** Om tooltipen skal vises */\n isOpen?: boolean;\n /** Ekstra klassenavn for tooltip */\n className?: string;\n /** Åpner ikke tooltip ved hover-events\n * @default false\n */\n disableHoverListener?: boolean;\n /** Åpner ikke tooltip ved focus-events\n * @default false\n */\n disableFocusListener?: boolean;\n /** Viser en lukkeknapp om man kontrollerer åpningen av Tooltip vha `isOpen`\n * @default true\n */\n showCloseButton?: boolean;\n /** Valideringsvariant for Tooltip */\n variant?: 'error';\n /** En array av modifiers som sendes til Popper, rammeverket som brukes til plassering av Tooltip\n * @default [{ name: 'offset', options: { offset: [0, 10]} }]\n */\n popperModifiers?: Modifier[];\n [key: string]: any;\n};\n\nexport const Tooltip: React.FC<TooltipProps> = ({\n placement,\n content,\n children,\n className,\n isOpen = false,\n disableHoverListener = false,\n disableFocusListener = false,\n showCloseButton = true,\n variant,\n popperModifiers = [\n {\n name: 'offset',\n options: { offset: [0, 10] },\n },\n ],\n ...rest\n}) => {\n const [showTooltip, setShowTooltip] = useState(isOpen || false);\n let hoverTimer: number;\n\n const handleOpen: (event: React.MouseEvent) => void = event => {\n event.persist();\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment -- Typescript interprets this differently depending on whether it's run in Jest or not\n // @ts-ignore -- for some reason, the type checker thinks that this setTimeout is run in node; it is not, because it is run in the browser\n hoverTimer = setTimeout(() => {\n setShowTooltip(true);\n }, 150);\n };\n const onMouseExit: () => void = () => {\n setShowTooltip(false);\n clearTimeout(hoverTimer);\n };\n React.useEffect(() => {\n return () => {\n clearTimeout(hoverTimer);\n };\n });\n\n const tooltipId = useRandomId('eds-tooltip');\n React.useEffect(() => {\n setShowTooltip(isOpen);\n }, [isOpen]);\n\n React.useEffect(() => {\n if (!content) {\n setShowTooltip(false);\n }\n }, [content]);\n\n let popperPlacement = placement as PopperPlacementProps;\n if (placement.includes('-')) {\n if (placement.includes('right')) {\n popperPlacement = placement.replace(\n 'right',\n 'end',\n ) as PopperPlacementProps;\n }\n if (placement.includes('left')) {\n popperPlacement = placement.replace(\n 'left',\n 'start',\n ) as PopperPlacementProps;\n }\n }\n\n const childProps: {\n 'aria-describedby'?: string;\n onFocus?: () => void;\n onBlur?: () => void;\n onMouseEnter?: (e: React.MouseEvent) => void;\n onMouseLeave?: () => void;\n } = {};\n childProps['aria-describedby'] = tooltipId;\n if (!disableFocusListener) {\n childProps.onFocus = () => setShowTooltip(true);\n childProps.onBlur = () => setShowTooltip(false);\n }\n if (!disableHoverListener) {\n childProps.onMouseEnter = e => handleOpen(e);\n childProps.onMouseLeave = () => onMouseExit();\n }\n\n return (\n <Manager>\n <Reference>\n {({ ref }) =>\n cloneElement(children, {\n ref: ref,\n ...childProps,\n })\n }\n </Reference>\n {showTooltip && (\n <Popper\n modifiers={[{ name: 'arrow', enabled: false }, ...popperModifiers]}\n placement={popperPlacement}\n >\n {({ ref, style, placement: popperPlacement }) => (\n <div\n className={classNames(\n 'eds-tooltip',\n className,\n `eds-tooltip--${popperPlacement}`,\n { 'eds-tooltip--error': variant === 'error' },\n )}\n ref={ref}\n style={style}\n role=\"tooltip\"\n id={tooltipId}\n data-placement={popperPlacement}\n {...rest}\n >\n {content}\n {isOpen && showCloseButton && (\n <IconButton\n className=\"eds-tooltip__close-button\"\n onClick={() => setShowTooltip(false)}\n >\n <CloseIcon />\n </IconButton>\n )}\n </div>\n )}\n </Popper>\n )}\n </Manager>\n );\n};\n","import React, { cloneElement, createContext, useContext } from 'react';\nimport { usePopper } from 'react-popper';\nimport { Placement } from '@popperjs/core';\nimport classNames from 'classnames';\nimport { Contrast } from '@entur/layout';\nimport './Popover.scss';\n\ntype PopoverContentCallbackProps = {\n ref: React.Ref<HTMLDivElement>;\n};\n\ntype PopoverContextProps = {\n showPopover: boolean;\n triggerElement?: React.RefObject<HTMLButtonElement>;\n contentElement: React.RefObject<HTMLDivElement>;\n styles: { [key: string]: React.CSSProperties };\n attributes: { [key: string]: { [key: string]: string } | undefined };\n closeButtonProps: Record<string, unknown>;\n popoverContentProps: (\n e: PopoverContentCallbackProps,\n ) => Record<string, unknown>;\n triggerProps: () => Record<string, unknown>;\n [key: string]: any;\n};\n\nconst PopoverContext = createContext<PopoverContextProps | undefined>(\n undefined,\n);\nconst usePopoverContext = () => {\n const context = useContext(PopoverContext);\n if (context == null) {\n throw Error('usePopoverContext must be used within <Popover/>');\n }\n return context;\n};\n\nexport type PopoverProps = {\n /** Innholdet i Popover */\n children: React.ReactNode;\n /** Plasseringen av Popover\n * @default \"bottom-start\"\n */\n placement?: Placement;\n};\n\nexport const Popover: React.FC<PopoverProps> = ({\n children,\n placement = 'bottom-start',\n}) => {\n const [showPopover, setShowPopover] = React.useState(false);\n const triggerElement = React.useRef(null);\n const contentElement = React.useRef(null);\n\n const { styles, attributes, forceUpdate } = usePopper(\n triggerElement.current,\n contentElement.current,\n {\n modifiers: [\n { name: 'arrow', enabled: false },\n {\n name: 'offset',\n options: {\n offset: [0, 8],\n },\n },\n ],\n placement: placement,\n },\n );\n\n React.useEffect(() => {\n if (forceUpdate) {\n forceUpdate();\n }\n }, [showPopover, forceUpdate]);\n\n const triggerProps = React.useCallback(() => {\n const buttonProps = {\n onClick: (e: React.MouseEvent) => {\n e.preventDefault();\n setShowPopover(prev => !prev);\n },\n 'aria-haspopup': 'dialog',\n 'aria-expanded': showPopover,\n ref: triggerElement,\n };\n return buttonProps;\n }, [triggerElement, showPopover]);\n\n useOnClickOutside(contentElement, triggerElement, () =>\n setShowPopover(false),\n );\n const closeButtonProps = {\n onClick: (e: React.MouseEvent) => {\n e.preventDefault();\n setShowPopover(false);\n },\n };\n const popoverContentProps = React.useCallback(() => {\n const contentProps = {\n role: 'dialog',\n 'aria-modal': 'false',\n ref: contentElement,\n onKeyDown: (event: React.KeyboardEvent) => {\n if (event.key === 'Escape') {\n showPopover && setShowPopover(false);\n }\n },\n onBlur: (event: React.FocusEvent) => {\n const elementReceivingFocus = event.relatedTarget as HTMLElement;\n // The check for 'tabindex=-1' is a special case for focus handling in Docz\n if (\n !elementReceivingFocus ||\n elementReceivingFocus.getAttribute('tabindex') === '-1'\n )\n return;\n const focusElementIsPopover = elementContainsElement(\n contentElement.current,\n elementReceivingFocus,\n );\n const focusElementIsTrigger = elementContainsElement(\n triggerElement.current,\n elementReceivingFocus,\n );\n const isValidBlur = !focusElementIsPopover && !focusElementIsTrigger;\n if (showPopover && isValidBlur) setShowPopover(false);\n },\n };\n return contentProps;\n }, [contentElement, showPopover]);\n const contextValue: PopoverContextProps = {\n showPopover,\n triggerElement,\n contentElement,\n styles,\n attributes,\n popoverContentProps,\n closeButtonProps,\n triggerProps,\n };\n return (\n <PopoverContext.Provider value={contextValue}>\n {children}\n </PopoverContext.Provider>\n );\n};\n\nexport type PopoverTriggerProps = {\n /** Knapp som skal brukes for å åpne Popover */\n children: React.ReactElement;\n};\n\nexport const PopoverTrigger: React.FC<PopoverTriggerProps> = ({ children }) => {\n const { triggerProps } = usePopoverContext();\n const child = React.Children.only(children) as React.ReactElement<any>;\n return cloneElement(child, triggerProps());\n};\n\nexport type PopoverCloseButtonProps = {\n /** En valgfri knapp som kan legges inn for å lukke Popover */\n children: React.ReactElement;\n};\n\nexport const PopoverCloseButton: React.FC<PopoverCloseButtonProps> = ({\n children,\n ...rest\n}) => {\n const { closeButtonProps } = usePopoverContext();\n return cloneElement(children, { ...closeButtonProps, ...rest });\n};\n\nexport type PopoverContentProps = {\n /**Innholdet til Popover */\n children: React.ReactNode;\n};\n\nexport const PopoverContent = React.forwardRef<\n HTMLDivElement,\n PopoverContentProps\n>(({ children }, ref: React.Ref<HTMLDivElement>) => {\n const { showPopover, attributes, styles, popoverContentProps } =\n usePopoverContext();\n const props = popoverContentProps({ ref });\n return (\n <Contrast\n className={classNames(\n 'eds-popover',\n {\n 'eds-popover--hidden': !showPopover,\n },\n 'eds-contrast',\n )}\n style={styles.popper}\n aria-hidden={!showPopover}\n {...attributes.styles}\n {...props}\n >\n {children}\n </Contrast>\n );\n});\n\nfunction elementContainsEventTarget(element: HTMLElement | null, event: Event) {\n if (!element) {\n return false;\n }\n\n if (element.contains(event.target as Node)) {\n return true;\n }\n\n // For elements inside a Shadow DOM we need to check the composedPath\n if (event.composed && event.composedPath) {\n const contains = event.composedPath().find(target => {\n if (target === window) {\n return false;\n }\n return element.contains(target as Node);\n });\n return contains ? true : false;\n }\n\n return false;\n}\n\nfunction elementContainsElement(\n parent: HTMLElement | null,\n child: HTMLElement,\n) {\n if (!parent) return false;\n return parent === child || parent.contains(child);\n}\n\nfunction useOnClickOutside(\n ref: React.RefObject<HTMLDivElement>,\n buttonRef: React.RefObject<HTMLButtonElement>,\n handler: () => void,\n) {\n React.useEffect(() => {\n const listener = (event: Event) => {\n if (\n elementContainsEventTarget(ref.current, event) ||\n elementContainsEventTarget(buttonRef.current, event)\n ) {\n return;\n }\n\n handler();\n };\n\n document.addEventListener('mousedown', listener);\n document.addEventListener('touchstart', listener);\n\n return () => {\n document.removeEventListener('mousedown', listener);\n document.removeEventListener('touchstart', listener);\n };\n }, [ref, buttonRef, handler]);\n}\n","import { warnAboutMissingStyles } from '@entur/utils';\nimport './index.scss';\n\nwarnAboutMissingStyles('tooltip');\n\nexport * from './Tooltip';\nexport * from './Popover';\n"],"names":["Tooltip","placement","content","children","className","isOpen","disableHoverListener","disableFocusListener","showCloseButton","variant","popperModifiers","name","options","offset","rest","useState","showTooltip","setShowTooltip","hoverTimer","handleOpen","event","persist","setTimeout","onMouseExit","clearTimeout","React","useEffect","tooltipId","useRandomId","popperPlacement","includes","replace","childProps","onFocus","onBlur","onMouseEnter","e","onMouseLeave","Manager","Reference","ref","cloneElement","Popper","modifiers","enabled","style","classNames","role","id","IconButton","onClick","CloseIcon","PopoverContext","createContext","undefined","usePopoverContext","context","useContext","Error","Popover","showPopover","setShowPopover","triggerElement","useRef","contentElement","usePopper","current","styles","attributes","forceUpdate","triggerProps","useCallback","buttonProps","preventDefault","prev","useOnClickOutside","closeButtonProps","popoverContentProps","contentProps","onKeyDown","key","elementReceivingFocus","relatedTarget","getAttribute","focusElementIsPopover","elementContainsElement","focusElementIsTrigger","isValidBlur","contextValue","Provider","value","PopoverTrigger","child","Children","only","PopoverCloseButton","PopoverContent","forwardRef","props","Contrast","popper","elementContainsEventTarget","element","contains","target","composed","composedPath","find","window","parent","buttonRef","handler","listener","document","addEventListener","removeEventListener","warnAboutMissingStyles"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IA2DaA,OAAO,GAA2B,SAAlCA,OAAO;MAClBC,SAAS,QAATA,SAAS;IACTC,OAAO,QAAPA,OAAO;IACPC,QAAQ,QAARA,QAAQ;IACRC,SAAS,QAATA,SAAS;IAAA,mBACTC,MAAM;IAANA,MAAM,4BAAG,KAAK;IAAA,6BACdC,oBAAoB;IAApBA,oBAAoB,sCAAG,KAAK;IAAA,6BAC5BC,oBAAoB;IAApBA,oBAAoB,sCAAG,KAAK;IAAA,4BAC5BC,eAAe;IAAfA,eAAe,qCAAG,IAAI;IACtBC,OAAO,QAAPA,OAAO;IAAA,4BACPC,eAAe;IAAfA,eAAe,qCAAG,CAChB;MACEC,IAAI,EAAE,QAAQ;MACdC,OAAO,EAAE;QAAEC,MAAM,EAAE,CAAC,CAAC,EAAE,EAAE;;KAC1B,CACF;IACEC,IAAI;EAEP,gBAAsCC,cAAQ,CAACV,MAAM,IAAI,KAAK,CAAC;IAAxDW,WAAW;IAAEC,cAAc;EAClC,IAAIC,UAAkB;EAEtB,IAAMC,UAAU,GAAsC,SAAhDA,UAAU,CAAsCC,KAAK;IACzDA,KAAK,CAACC,OAAO,EAAE;;;IAGfH,UAAU,GAAGI,UAAU,CAAC;MACtBL,cAAc,CAAC,IAAI,CAAC;KACrB,EAAE,GAAG,CAAC;GACR;EACD,IAAMM,WAAW,GAAe,SAA1BA,WAAW;IACfN,cAAc,CAAC,KAAK,CAAC;IACrBO,YAAY,CAACN,UAAU,CAAC;GACzB;EACDO,yBAAK,CAACC,SAAS,CAAC;IACd,OAAO;MACLF,YAAY,CAACN,UAAU,CAAC;KACzB;GACF,CAAC;EAEF,IAAMS,SAAS,GAAGC,iBAAW,CAAC,aAAa,CAAC;EAC5CH,yBAAK,CAACC,SAAS,CAAC;IACdT,cAAc,C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AC/C,IAAIA,MAAM,KAAKI,MAAM,EAAE;QACrB,OAAO,KAAK;;MAEd,OAAON,OAAO,CAACC,QAAQ,CAACC,MAAc,CAAC;KACxC,CAAC;IACF,OAAOD,QAAQ,GAAG,IAAI,GAAG,KAAK;;EAGhC,OAAO,KAAK;AACd;AAEA,SAASlB,sBAAsB,CAC7BwB,MAA0B,EAC1BjB,KAAkB;EAElB,IAAI,CAACiB,MAAM,EAAE,OAAO,KAAK;EACzB,OAAOA,MAAM,KAAKjB,KAAK,IAAIiB,MAAM,CAACN,QAAQ,CAACX,KAAK,CAAC;AACnD;AAEA,SAASjB,iBAAiB,CACxBnC,GAAoC,EACpCsE,SAA6C,EAC7CC,OAAmB;EAEnBtF,yBAAK,CAACC,SAAS,CAAC;IACd,IAAMsF,QAAQ,GAAG,SAAXA,QAAQ,CAAI5F,KAAY;MAC5B,IACEiF,0BAA0B,CAAC7D,GAAG,CAAC0B,OAAO,EAAE9C,KAAK,CAAC,IAC9CiF,0BAA0B,CAACS,SAAS,CAAC5C,OAAO,EAAE9C,KAAK,CAAC,EACpD;QACA;;MAGF2F,OAAO,EAAE;KACV;IAEDE,QAAQ,CAACC,gBAAgB,CAAC,WAAW,EAAEF,QAAQ,CAAC;IAChDC,QAAQ,CAACC,gBAAgB,CAAC,YAAY,EAAEF,QAAQ,CAAC;IAEjD,OAAO;MACLC,QAAQ,CAACE,mBAAmB,CAAC,WAAW,EAAEH,QAAQ,CAAC;MACnDC,QAAQ,CAACE,mBAAmB,CAAC,YAAY,EAAEH,QAAQ,CAAC;KACrD;GACF,EAAE,CAACxE,GAAG,EAAEsE,SAAS,EAAEC,OAAO,CAAC,CAAC;AAC/B;;AC/PAK,4BAAsB,CAAC,SAAS,CAAC;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"tooltip.cjs.development.js","sources":["../src/Tooltip.tsx","../src/Popover.tsx","../src/index.tsx"],"sourcesContent":["import React, { cloneElement, useState } from 'react';\nimport { Manager, Reference, Popper } from 'react-popper';\nimport classNames from 'classnames';\nimport { useRandomId } from '@entur/utils';\nimport { Placement as PopperPlacementProps } from '@popperjs/core';\nimport { CloseIcon } from '@entur/icons';\nimport { IconButton } from '@entur/button';\nimport './Tooltip.scss';\n\ntype Modifier = {\n name: string;\n enabled?: boolean;\n requires?: Array<string>;\n requiresIfExists?: Array<string>;\n options?: Record<string, unknown>;\n data?: Record<string, unknown>;\n [key: string]: any;\n};\n\nexport type TooltipProps = {\n /** Plassering av tooltip-en */\n placement:\n | 'top'\n | 'top-left'\n | 'top-right'\n | 'left'\n | 'right'\n | 'bottom-left' // bottom-start\n | 'bottom'\n | 'bottom-right'; // bottom-end\n /** Innholdet i tooltip-boksen */\n content: React.ReactNode;\n /** Elementet som skal ha tooltip-funksjonalitet */\n children: React.ReactElement;\n /** Om tooltipen skal vises */\n isOpen?: boolean;\n /** Ekstra klassenavn for tooltip */\n className?: string;\n /** Åpner ikke tooltip ved hover-events\n * @default false\n */\n disableHoverListener?: boolean;\n /** Åpner ikke tooltip ved focus-events\n * @default false\n */\n disableFocusListener?: boolean;\n /** Viser en lukkeknapp om man kontrollerer åpningen av Tooltip vha `isOpen`\n * @default true\n */\n showCloseButton?: boolean;\n /** Valideringsvariant for Tooltip */\n variant?: 'error';\n /** En array av modifiers som sendes til Popper, rammeverket som brukes til plassering av Tooltip\n * @default [{ name: 'offset', options: { offset: [0, 10]} }]\n */\n popperModifiers?: Modifier[];\n [key: string]: any;\n};\n\nexport const Tooltip: React.FC<TooltipProps> = ({\n placement,\n content,\n children,\n className,\n isOpen = false,\n disableHoverListener = false,\n disableFocusListener = false,\n showCloseButton = true,\n variant,\n popperModifiers = [\n {\n name: 'offset',\n options: { offset: [0, 10] },\n },\n ],\n ...rest\n}) => {\n const [showTooltip, setShowTooltip] = useState(isOpen || false);\n let hoverTimer: NodeJS.Timeout;\n\n const handleOpen: (event: React.MouseEvent) => void = event => {\n event.persist();\n hoverTimer = setTimeout(() => {\n setShowTooltip(true);\n }, 150);\n };\n const onMouseExit: () => void = () => {\n setShowTooltip(false);\n clearTimeout(hoverTimer);\n };\n React.useEffect(() => {\n return () => {\n clearTimeout(hoverTimer);\n };\n });\n\n const tooltipId = useRandomId('eds-tooltip');\n React.useEffect(() => {\n setShowTooltip(isOpen);\n }, [isOpen]);\n\n React.useEffect(() => {\n if (!content) {\n setShowTooltip(false);\n }\n }, [content]);\n\n let popperPlacement = placement as PopperPlacementProps;\n if (placement.includes('-')) {\n if (placement.includes('right')) {\n popperPlacement = placement.replace(\n 'right',\n 'end',\n ) as PopperPlacementProps;\n }\n if (placement.includes('left')) {\n popperPlacement = placement.replace(\n 'left',\n 'start',\n ) as PopperPlacementProps;\n }\n }\n\n const childProps: {\n 'aria-describedby'?: string;\n onFocus?: () => void;\n onBlur?: () => void;\n onMouseEnter?: (e: React.MouseEvent) => void;\n onMouseLeave?: () => void;\n } = {};\n childProps['aria-describedby'] = tooltipId;\n if (!disableFocusListener) {\n childProps.onFocus = () => setShowTooltip(true);\n childProps.onBlur = () => setShowTooltip(false);\n }\n if (!disableHoverListener) {\n childProps.onMouseEnter = e => handleOpen(e);\n childProps.onMouseLeave = () => onMouseExit();\n }\n\n return (\n <Manager>\n <Reference>\n {({ ref }) =>\n cloneElement(children, {\n ref: ref,\n ...childProps,\n })\n }\n </Reference>\n {showTooltip && (\n <Popper\n modifiers={[{ name: 'arrow', enabled: false }, ...popperModifiers]}\n placement={popperPlacement}\n >\n {({ ref, style, placement: popperPlacement }) => (\n <div\n className={classNames(\n 'eds-tooltip',\n className,\n `eds-tooltip--${popperPlacement}`,\n { 'eds-tooltip--error': variant === 'error' },\n )}\n ref={ref}\n style={style}\n role=\"tooltip\"\n id={tooltipId}\n data-placement={popperPlacement}\n {...rest}\n >\n {content}\n {isOpen && showCloseButton && (\n <IconButton\n className=\"eds-tooltip__close-button\"\n onClick={() => setShowTooltip(false)}\n >\n <CloseIcon />\n </IconButton>\n )}\n </div>\n )}\n </Popper>\n )}\n </Manager>\n );\n};\n","import React, { cloneElement, createContext, useContext } from 'react';\nimport { usePopper } from 'react-popper';\nimport { Placement } from '@popperjs/core';\nimport classNames from 'classnames';\nimport { Contrast } from '@entur/layout';\nimport './Popover.scss';\n\ntype PopoverContentCallbackProps = {\n ref: React.Ref<HTMLDivElement>;\n};\n\ntype PopoverContextProps = {\n showPopover: boolean;\n triggerElement?: React.RefObject<HTMLButtonElement>;\n contentElement: React.RefObject<HTMLDivElement>;\n styles: { [key: string]: React.CSSProperties };\n attributes: { [key: string]: { [key: string]: string } | undefined };\n closeButtonProps: Record<string, unknown>;\n popoverContentProps: (\n e: PopoverContentCallbackProps,\n ) => Record<string, unknown>;\n triggerProps: () => Record<string, unknown>;\n [key: string]: any;\n};\n\nconst PopoverContext = createContext<PopoverContextProps | undefined>(\n undefined,\n);\nconst usePopoverContext = () => {\n const context = useContext(PopoverContext);\n if (context == null) {\n throw Error('usePopoverContext must be used within <Popover/>');\n }\n return context;\n};\n\nexport type PopoverProps = {\n /** Innholdet i Popover */\n children: React.ReactNode;\n /** Plasseringen av Popover\n * @default \"bottom-start\"\n */\n placement?: Placement;\n};\n\nexport const Popover: React.FC<PopoverProps> = ({\n children,\n placement = 'bottom-start',\n}) => {\n const [showPopover, setShowPopover] = React.useState(false);\n const triggerElement = React.useRef(null);\n const contentElement = React.useRef(null);\n\n const { styles, attributes, forceUpdate } = usePopper(\n triggerElement.current,\n contentElement.current,\n {\n modifiers: [\n { name: 'arrow', enabled: false },\n {\n name: 'offset',\n options: {\n offset: [0, 8],\n },\n },\n ],\n placement: placement,\n },\n );\n\n React.useEffect(() => {\n if (forceUpdate) {\n forceUpdate();\n }\n }, [showPopover, forceUpdate]);\n\n const triggerProps = React.useCallback(() => {\n const buttonProps = {\n onClick: (e: React.MouseEvent) => {\n e.preventDefault();\n setShowPopover(prev => !prev);\n },\n 'aria-haspopup': 'dialog',\n 'aria-expanded': showPopover,\n ref: triggerElement,\n };\n return buttonProps;\n }, [triggerElement, showPopover]);\n\n useOnClickOutside(contentElement, triggerElement, () =>\n setShowPopover(false),\n );\n const closeButtonProps = {\n onClick: (e: React.MouseEvent) => {\n e.preventDefault();\n setShowPopover(false);\n },\n };\n const popoverContentProps = React.useCallback(() => {\n const contentProps = {\n role: 'dialog',\n 'aria-modal': 'false',\n ref: contentElement,\n onKeyDown: (event: React.KeyboardEvent) => {\n if (event.key === 'Escape') {\n showPopover && setShowPopover(false);\n }\n },\n onBlur: (event: React.FocusEvent) => {\n const elementReceivingFocus = event.relatedTarget as HTMLElement;\n // The check for 'tabindex=-1' is a special case for focus handling in Docz\n if (\n !elementReceivingFocus ||\n elementReceivingFocus.getAttribute('tabindex') === '-1'\n )\n return;\n const focusElementIsPopover = elementContainsElement(\n contentElement.current,\n elementReceivingFocus,\n );\n const focusElementIsTrigger = elementContainsElement(\n triggerElement.current,\n elementReceivingFocus,\n );\n const isValidBlur = !focusElementIsPopover && !focusElementIsTrigger;\n if (showPopover && isValidBlur) setShowPopover(false);\n },\n };\n return contentProps;\n }, [contentElement, showPopover]);\n const contextValue: PopoverContextProps = {\n showPopover,\n triggerElement,\n contentElement,\n styles,\n attributes,\n popoverContentProps,\n closeButtonProps,\n triggerProps,\n };\n return (\n <PopoverContext.Provider value={contextValue}>\n {children}\n </PopoverContext.Provider>\n );\n};\n\nexport type PopoverTriggerProps = {\n /** Knapp som skal brukes for å åpne Popover */\n children: React.ReactElement;\n};\n\nexport const PopoverTrigger: React.FC<PopoverTriggerProps> = ({ children }) => {\n const { triggerProps } = usePopoverContext();\n const child = React.Children.only(children) as React.ReactElement<any>;\n return cloneElement(child, triggerProps());\n};\n\nexport type PopoverCloseButtonProps = {\n /** En valgfri knapp som kan legges inn for å lukke Popover */\n children: React.ReactElement;\n};\n\nexport const PopoverCloseButton: React.FC<PopoverCloseButtonProps> = ({\n children,\n ...rest\n}) => {\n const { closeButtonProps } = usePopoverContext();\n return cloneElement(children, { ...closeButtonProps, ...rest });\n};\n\nexport type PopoverContentProps = {\n /**Innholdet til Popover */\n children: React.ReactNode;\n};\n\nexport const PopoverContent = React.forwardRef<\n HTMLDivElement,\n PopoverContentProps\n>(({ children }, ref: React.Ref<HTMLDivElement>) => {\n const { showPopover, attributes, styles, popoverContentProps } =\n usePopoverContext();\n const props = popoverContentProps({ ref });\n return (\n <Contrast\n className={classNames(\n 'eds-popover',\n {\n 'eds-popover--hidden': !showPopover,\n },\n 'eds-contrast',\n )}\n style={styles.popper}\n aria-hidden={!showPopover}\n {...attributes.styles}\n {...props}\n >\n {children}\n </Contrast>\n );\n});\n\nfunction elementContainsEventTarget(element: HTMLElement | null, event: Event) {\n if (!element) {\n return false;\n }\n\n if (element.contains(event.target as Node)) {\n return true;\n }\n\n // For elements inside a Shadow DOM we need to check the composedPath\n if (event.composed && event.composedPath) {\n const contains = event.composedPath().find(target => {\n if (target === window) {\n return false;\n }\n return element.contains(target as Node);\n });\n return contains ? true : false;\n }\n\n return false;\n}\n\nfunction elementContainsElement(\n parent: HTMLElement | null,\n child: HTMLElement,\n) {\n if (!parent) return false;\n return parent === child || parent.contains(child);\n}\n\nfunction useOnClickOutside(\n ref: React.RefObject<HTMLDivElement>,\n buttonRef: React.RefObject<HTMLButtonElement>,\n handler: () => void,\n) {\n React.useEffect(() => {\n const listener = (event: Event) => {\n if (\n elementContainsEventTarget(ref.current, event) ||\n elementContainsEventTarget(buttonRef.current, event)\n ) {\n return;\n }\n\n handler();\n };\n\n document.addEventListener('mousedown', listener);\n document.addEventListener('touchstart', listener);\n\n return () => {\n document.removeEventListener('mousedown', listener);\n document.removeEventListener('touchstart', listener);\n };\n }, [ref, buttonRef, handler]);\n}\n","import { warnAboutMissingStyles } from '@entur/utils';\nimport './index.scss';\n\nwarnAboutMissingStyles('tooltip');\n\nexport * from './Tooltip';\nexport * from './Popover';\n"],"names":["Tooltip","placement","content","children","className","isOpen","disableHoverListener","disableFocusListener","showCloseButton","variant","popperModifiers","name","options","offset","rest","useState","showTooltip","setShowTooltip","hoverTimer","handleOpen","event","persist","setTimeout","onMouseExit","clearTimeout","React","useEffect","tooltipId","useRandomId","popperPlacement","includes","replace","childProps","onFocus","onBlur","onMouseEnter","e","onMouseLeave","Manager","Reference","ref","cloneElement","Popper","modifiers","enabled","style","classNames","role","id","IconButton","onClick","CloseIcon","PopoverContext","createContext","undefined","usePopoverContext","context","useContext","Error","Popover","showPopover","setShowPopover","triggerElement","useRef","contentElement","usePopper","current","styles","attributes","forceUpdate","triggerProps","useCallback","buttonProps","preventDefault","prev","useOnClickOutside","closeButtonProps","popoverContentProps","contentProps","onKeyDown","key","elementReceivingFocus","relatedTarget","getAttribute","focusElementIsPopover","elementContainsElement","focusElementIsTrigger","isValidBlur","contextValue","Provider","value","PopoverTrigger","child","Children","only","PopoverCloseButton","PopoverContent","forwardRef","props","Contrast","popper","elementContainsEventTarget","element","contains","target","composed","composedPath","find","window","parent","buttonRef","handler","listener","document","addEventListener","removeEventListener","warnAboutMissingStyles"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IA2DaA,OAAO,GAA2B,SAAlCA,OAAkC;MAC7CC,iBAAAA;MACAC,eAAAA;MACAC,gBAAAA;MACAC,iBAAAA;yBACAC;MAAAA,kCAAS;mCACTC;MAAAA,0DAAuB;mCACvBC;MAAAA,0DAAuB;kCACvBC;MAAAA,oDAAkB;MAClBC,eAAAA;kCACAC;MAAAA,oDAAkB,CAChB;AACEC,IAAAA,IAAI,EAAE,QADR;AAEEC,IAAAA,OAAO,EAAE;AAAEC,MAAAA,MAAM,EAAE,CAAC,CAAD,EAAI,EAAJ;AAAV;AAFX,GADgB;MAMfC;;AAEH,kBAAsCC,cAAQ,CAACV,MAAM,IAAI,KAAX,CAA9C;AAAA,MAAOW,WAAP;AAAA,MAAoBC,cAApB;;AACA,MAAIC,UAAJ;;AAEA,MAAMC,UAAU,GAAsC,SAAhDA,UAAgD,CAAAC,KAAK;AACzDA,IAAAA,KAAK,CAACC,OAAN;AACAH,IAAAA,UAAU,GAAGI,U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src/Popover.tsx","../src/index.tsx","../src/Tooltip.tsx"],"sourcesContent":["import React, { cloneElement, createContext, useContext } from 'react';\nimport { usePopper } from 'react-popper';\nimport { Placement } from '@popperjs/core';\nimport classNames from 'classnames';\nimport { Contrast } from '@entur/layout';\nimport './Popover.scss';\n\ntype PopoverContentCallbackProps = {\n ref: React.Ref<HTMLDivElement>;\n};\n\ntype PopoverContextProps = {\n showPopover: boolean;\n triggerElement?: React.RefObject<HTMLButtonElement>;\n contentElement: React.RefObject<HTMLDivElement>;\n styles: { [key: string]: React.CSSProperties };\n attributes: { [key: string]: { [key: string]: string } | undefined };\n closeButtonProps: Record<string, unknown>;\n popoverContentProps: (\n e: PopoverContentCallbackProps,\n ) => Record<string, unknown>;\n triggerProps: () => Record<string, unknown>;\n [key: string]: any;\n};\n\nconst PopoverContext = createContext<PopoverContextProps | undefined>(\n undefined,\n);\nconst usePopoverContext = () => {\n const context = useContext(PopoverContext);\n if (context == null) {\n throw Error('usePopoverContext must be used within <Popover/>');\n }\n return context;\n};\n\nexport type PopoverProps = {\n /** Innholdet i Popover */\n children: React.ReactNode;\n /** Plasseringen av Popover\n * @default \"bottom-start\"\n */\n placement?: Placement;\n};\n\nexport const Popover: React.FC<PopoverProps> = ({\n children,\n placement = 'bottom-start',\n}) => {\n const [showPopover, setShowPopover] = React.useState(false);\n const triggerElement = React.useRef(null);\n const contentElement = React.useRef(null);\n\n const { styles, attributes, forceUpdate } = usePopper(\n triggerElement.current,\n contentElement.current,\n {\n modifiers: [\n { name: 'arrow', enabled: false },\n {\n name: 'offset',\n options: {\n offset: [0, 8],\n },\n },\n ],\n placement: placement,\n },\n );\n\n React.useEffect(() => {\n if (forceUpdate) {\n forceUpdate();\n }\n }, [showPopover, forceUpdate]);\n\n const triggerProps = React.useCallback(() => {\n const buttonProps = {\n onClick: (e: React.MouseEvent) => {\n e.preventDefault();\n setShowPopover(prev => !prev);\n },\n 'aria-haspopup': 'dialog',\n 'aria-expanded': showPopover,\n ref: triggerElement,\n };\n return buttonProps;\n }, [triggerElement, showPopover]);\n\n useOnClickOutside(contentElement, triggerElement, () =>\n setShowPopover(false),\n );\n const closeButtonProps = {\n onClick: (e: React.MouseEvent) => {\n e.preventDefault();\n setShowPopover(false);\n },\n };\n const popoverContentProps = React.useCallback(() => {\n const contentProps = {\n role: 'dialog',\n 'aria-modal': 'false',\n ref: contentElement,\n onKeyDown: (event: React.KeyboardEvent) => {\n if (event.key === 'Escape') {\n showPopover && setShowPopover(false);\n }\n },\n onBlur: (event: React.FocusEvent) => {\n const elementReceivingFocus = event.relatedTarget as HTMLElement;\n // The check for 'tabindex=-1' is a special case for focus handling in Docz\n if (\n !elementReceivingFocus ||\n elementReceivingFocus.getAttribute('tabindex') === '-1'\n )\n return;\n const focusElementIsPopover = elementContainsElement(\n contentElement.current,\n elementReceivingFocus,\n );\n const focusElementIsTrigger = elementContainsElement(\n triggerElement.current,\n elementReceivingFocus,\n );\n const isValidBlur = !focusElementIsPopover && !focusElementIsTrigger;\n if (showPopover && isValidBlur) setShowPopover(false);\n },\n };\n return contentProps;\n }, [contentElement, showPopover]);\n const contextValue: PopoverContextProps = {\n showPopover,\n triggerElement,\n contentElement,\n styles,\n attributes,\n popoverContentProps,\n closeButtonProps,\n triggerProps,\n };\n return (\n <PopoverContext.Provider value={contextValue}>\n {children}\n </PopoverContext.Provider>\n );\n};\n\nexport type PopoverTriggerProps = {\n /** Knapp som skal brukes for å åpne Popover */\n children: React.ReactElement;\n};\n\nexport const PopoverTrigger: React.FC<PopoverTriggerProps> = ({ children }) => {\n const { triggerProps } = usePopoverContext();\n const child = React.Children.only(children) as React.ReactElement<any>;\n return cloneElement(child, triggerProps());\n};\n\nexport type PopoverCloseButtonProps = {\n /** En valgfri knapp som kan legges inn for å lukke Popover */\n children: React.ReactElement;\n};\n\nexport const PopoverCloseButton: React.FC<PopoverCloseButtonProps> = ({\n children,\n ...rest\n}) => {\n const { closeButtonProps } = usePopoverContext();\n return cloneElement(children, { ...closeButtonProps, ...rest });\n};\n\nexport type PopoverContentProps = {\n /**Innholdet til Popover */\n children: React.ReactNode;\n};\n\nexport const PopoverContent = React.forwardRef<\n HTMLDivElement,\n PopoverContentProps\n>(({ children }, ref: React.Ref<HTMLDivElement>) => {\n const { showPopover, attributes, styles, popoverContentProps } =\n usePopoverContext();\n const props = popoverContentProps({ ref });\n return (\n <Contrast\n className={classNames(\n 'eds-popover',\n {\n 'eds-popover--hidden': !showPopover,\n },\n 'eds-contrast',\n )}\n style={styles.popper}\n aria-hidden={!showPopover}\n {...attributes.styles}\n {...props}\n >\n {children}\n </Contrast>\n );\n});\n\nfunction elementContainsEventTarget(element: HTMLElement | null, event: Event) {\n if (!element) {\n return false;\n }\n\n if (element.contains(event.target as Node)) {\n return true;\n }\n\n // For elements inside a Shadow DOM we need to check the composedPath\n if (event.composed && event.composedPath) {\n const contains = event.composedPath().find(target => {\n if (target === window) {\n return false;\n }\n return element.contains(target as Node);\n });\n return contains ? true : false;\n }\n\n return false;\n}\n\nfunction elementContainsElement(\n parent: HTMLElement | null,\n child: HTMLElement,\n) {\n if (!parent) return false;\n return parent === child || parent.contains(child);\n}\n\nfunction useOnClickOutside(\n ref: React.RefObject<HTMLDivElement>,\n buttonRef: React.RefObject<HTMLButtonElement>,\n handler: () => void,\n) {\n React.useEffect(() => {\n const listener = (event: Event) => {\n if (\n elementContainsEventTarget(ref.current, event) ||\n elementContainsEventTarget(buttonRef.current, event)\n ) {\n return;\n }\n\n handler();\n };\n\n document.addEventListener('mousedown', listener);\n document.addEventListener('touchstart', listener);\n\n return () => {\n document.removeEventListener('mousedown', listener);\n document.removeEventListener('touchstart', listener);\n };\n }, [ref, buttonRef, handler]);\n}\n","import { warnAboutMissingStyles } from '@entur/utils';\nimport './index.scss';\n\nwarnAboutMissingStyles('tooltip');\n\nexport * from './Tooltip';\nexport * from './Popover';\n","import React, { cloneElement, useState } from 'react';\nimport { Manager, Reference, Popper } from 'react-popper';\nimport classNames from 'classnames';\nimport { useRandomId } from '@entur/utils';\nimport { Placement as PopperPlacementProps } from '@popperjs/core';\nimport { CloseIcon } from '@entur/icons';\nimport { IconButton } from '@entur/button';\nimport './Tooltip.scss';\n\ntype Modifier = {\n name: string;\n enabled?: boolean;\n requires?: Array<string>;\n requiresIfExists?: Array<string>;\n options?: Record<string, unknown>;\n data?: Record<string, unknown>;\n [key: string]: any;\n};\n\nexport type TooltipProps = {\n /** Plassering av tooltip-en */\n placement:\n | 'top'\n | 'top-left'\n | 'top-right'\n | 'left'\n | 'right'\n | 'bottom-left' // bottom-start\n | 'bottom'\n | 'bottom-right'; // bottom-end\n /** Innholdet i tooltip-boksen */\n content: React.ReactNode;\n /** Elementet som skal ha tooltip-funksjonalitet */\n children: React.ReactElement;\n /** Om tooltipen skal vises */\n isOpen?: boolean;\n /** Ekstra klassenavn for tooltip */\n className?: string;\n /** Åpner ikke tooltip ved hover-events\n * @default false\n */\n disableHoverListener?: boolean;\n /** Åpner ikke tooltip ved focus-events\n * @default false\n */\n disableFocusListener?: boolean;\n /** Viser en lukkeknapp om man kontrollerer åpningen av Tooltip vha `isOpen`\n * @default true\n */\n showCloseButton?: boolean;\n /** Valideringsvariant for Tooltip */\n variant?: 'error';\n /** En array av modifiers som sendes til Popper, rammeverket som brukes til plassering av Tooltip\n * @default [{ name: 'offset', options: { offset: [0, 10]} }]\n */\n popperModifiers?: Modifier[];\n [key: string]: any;\n};\n\nexport const Tooltip: React.FC<TooltipProps> = ({\n placement,\n content,\n children,\n className,\n isOpen = false,\n disableHoverListener = false,\n disableFocusListener = false,\n showCloseButton = true,\n variant,\n popperModifiers = [\n {\n name: 'offset',\n options: { offset: [0, 10] },\n },\n ],\n ...rest\n}) => {\n const [showTooltip, setShowTooltip] = useState(isOpen || false);\n let hoverTimer: number;\n\n const handleOpen: (event: React.MouseEvent) => void = event => {\n event.persist();\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment -- Typescript interprets this differently depending on whether it's run in Jest or not\n // @ts-ignore -- for some reason, the type checker thinks that this setTimeout is run in node; it is not, because it is run in the browser\n hoverTimer = setTimeout(() => {\n setShowTooltip(true);\n }, 150);\n };\n const onMouseExit: () => void = () => {\n setShowTooltip(false);\n clearTimeout(hoverTimer);\n };\n React.useEffect(() => {\n return () => {\n clearTimeout(hoverTimer);\n };\n });\n\n const tooltipId = useRandomId('eds-tooltip');\n React.useEffect(() => {\n setShowTooltip(isOpen);\n }, [isOpen]);\n\n React.useEffect(() => {\n if (!content) {\n setShowTooltip(false);\n }\n }, [content]);\n\n let popperPlacement = placement as PopperPlacementProps;\n if (placement.includes('-')) {\n if (placement.includes('right')) {\n popperPlacement = placement.replace(\n 'right',\n 'end',\n ) as PopperPlacementProps;\n }\n if (placement.includes('left')) {\n popperPlacement = placement.replace(\n 'left',\n 'start',\n ) as PopperPlacementProps;\n }\n }\n\n const childProps: {\n 'aria-describedby'?: string;\n onFocus?: () => void;\n onBlur?: () => void;\n onMouseEnter?: (e: React.MouseEvent) => void;\n onMouseLeave?: () => void;\n } = {};\n childProps['aria-describedby'] = tooltipId;\n if (!disableFocusListener) {\n childProps.onFocus = () => setShowTooltip(true);\n childProps.onBlur = () => setShowTooltip(false);\n }\n if (!disableHoverListener) {\n childProps.onMouseEnter = e => handleOpen(e);\n childProps.onMouseLeave = () => onMouseExit();\n }\n\n return (\n <Manager>\n <Reference>\n {({ ref }) =>\n cloneElement(children, {\n ref: ref,\n ...childProps,\n })\n }\n </Reference>\n {showTooltip && (\n <Popper\n modifiers={[{ name: 'arrow', enabled: false }, ...popperModifiers]}\n placement={popperPlacement}\n >\n {({ ref, style, placement: popperPlacement }) => (\n <div\n className={classNames(\n 'eds-tooltip',\n className,\n `eds-tooltip--${popperPlacement}`,\n { 'eds-tooltip--error': variant === 'error' },\n )}\n ref={ref}\n style={style}\n role=\"tooltip\"\n id={tooltipId}\n data-placement={popperPlacement}\n {...rest}\n >\n {content}\n {isOpen && showCloseButton && (\n <IconButton\n className=\"eds-tooltip__close-button\"\n onClick={() => setShowTooltip(false)}\n >\n <CloseIcon />\n </IconButton>\n )}\n </div>\n )}\n </Popper>\n )}\n </Manager>\n );\n};\n"],"names":["PopoverContext","createContext","undefined","usePopoverContext","context","useContext","Error","PopoverContent","React","forwardRef","ref","children","showPopover","attributes","styles","props","popoverContentProps","Contrast","className","classNames","style","popper","elementContainsEventTarget","element","event","contains","target","composed","composedPath","find","window","elementContainsElement","parent","child","warnAboutMissingStyles","_ref$placement","placement","useState","setShowPopover","triggerElement","useRef","contentElement","usePopper","current","modifiers","name","enabled","options","offset","forceUpdate","useEffect","buttonRef","handler","triggerProps","useCallback","onClick","e","preventDefault","prev","listener","document","addEventListener","removeEventListener","closeButtonProps","role","onKeyDown","key","onBlur","elementReceivingFocus","relatedTarget","getAttribute","focusElementIsPopover","focusElementIsTrigger","Provider","value","rest","cloneElement","Children","only","hoverTimer","content","_ref$isOpen","isOpen","_ref$disableHoverList","disableHoverListener","_ref$disableFocusList","disableFocusListener","_ref$showCloseButton","showCloseButton","variant","_ref$popperModifiers","popperModifiers","showTooltip","setShowTooltip","clearTimeout","tooltipId","useRandomId","popperPlacement","includes","replace","childProps","onFocus","onMouseEnter","persist","setTimeout","onMouseLeave","Manager","Reference","Popper","id","IconButton","CloseIcon"],"mappings":"80BAyBMA,EAAiBC,qBACrBC,GAEIC,EAAoB,WACxB,IAAMC,EAAUC,aAAWL,GAC3B,GAAe,MAAXI,EACF,MAAME,MAAM,oDAEd,OAAOF,GA+IIG,EAAiBC,UAAMC,YAGlC,WAAeC,OAAZC,IAAAA,WAEDR,IADMS,IAAAA,YAAaC,IAAAA,WAAYC,IAAAA,OAE3BC,GAAQC,IAF2BA,qBAEP,CAAEN,IAAAA,IACpC,OACEF,wBAACS,cACCC,UAAWC,UACT,cACA,CACE,uBAAwBP,GAE1B,gBAEFQ,MAAON,EAAOO,sBACAT,GACVC,EAAWC,OACXC,GAEHJ,MAKP,SAASW,EAA2BC,EAA6BC,GAC/D,SAAKD,KAIDA,EAAQE,SAASD,EAAME,SA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EJ1E,UAAM0C,WAAU,WACT8B,GACHa,GAAe,KAEhB,CAACb,IAEJ,IAAIiB,EAAkB7D,EAClBA,EAAU8D,SAAS,OACjB9D,EAAU8D,SAAS,WACrBD,EAAkB7D,EAAU+D,QAC1B,QACA,QAGA/D,EAAU8D,SAAS,UACrBD,EAAkB7D,EAAU+D,QAC1B,OACA,WAKN,IAAMC,EAMF,GAWJ,OAVAA,EAAW,oBAAsBL,EAC5BT,IACHc,EAAWC,QAAU,WAAA,OAAMR,GAAe,IAC1CO,EAAWjC,OAAS,WAAA,OAAM0B,GAAe,KAEtCT,IACHgB,EAAWE,aAAe,SAAA9C,GAAC,OAAeA,EAzDpC+C,eAGNxB,EAAayB,YAAW,WACtBX,GAAe,KACd,OAqDHO,EAAWK,aAAe,WAAA,OAlD1BZ,GAAe,QACfC,aAAaf,KAqDbvE,wBAACkG,eACClG,wBAACmG,kBACE,YAAM,OACL/B,eAAajE,KACXD,MAFAA,KAGG0F,OAIRR,GACCpF,wBAACoG,UACChE,WAAY,CAAEC,KAAM,QAASC,SAAS,WAAY6C,GAClDvD,UAAW6D,IAEV,YAAA,IAAGvF,IAAAA,IAAKU,IAAAA,MAAkB6E,IAAX7D,UAAS,OACvB5B,iCACEU,UAAWC,UACT,cACAD,kBACgB+E,EAChB,CAAE,qBAAkC,UAAZR,IAE1B/E,IAAKA,EACLU,MAAOA,EACP4C,KAAK,UACL6C,GAAId,mBACYE,GACZtB,GAEHK,EACAE,GAAUM,GACThF,wBAACsG,cACC5F,UAAU,4BACVqC,QAAS,WAAA,OAAMsC,GAAe,KAE9BrF,wBAACuG"}
|
|
1
|
+
{"version":3,"file":"tooltip.cjs.production.min.js","sources":["../src/Popover.tsx","../src/index.tsx","../src/Tooltip.tsx"],"sourcesContent":["import React, { cloneElement, createContext, useContext } from 'react';\nimport { usePopper } from 'react-popper';\nimport { Placement } from '@popperjs/core';\nimport classNames from 'classnames';\nimport { Contrast } from '@entur/layout';\nimport './Popover.scss';\n\ntype PopoverContentCallbackProps = {\n ref: React.Ref<HTMLDivElement>;\n};\n\ntype PopoverContextProps = {\n showPopover: boolean;\n triggerElement?: React.RefObject<HTMLButtonElement>;\n contentElement: React.RefObject<HTMLDivElement>;\n styles: { [key: string]: React.CSSProperties };\n attributes: { [key: string]: { [key: string]: string } | undefined };\n closeButtonProps: Record<string, unknown>;\n popoverContentProps: (\n e: PopoverContentCallbackProps,\n ) => Record<string, unknown>;\n triggerProps: () => Record<string, unknown>;\n [key: string]: any;\n};\n\nconst PopoverContext = createContext<PopoverContextProps | undefined>(\n undefined,\n);\nconst usePopoverContext = () => {\n const context = useContext(PopoverContext);\n if (context == null) {\n throw Error('usePopoverContext must be used within <Popover/>');\n }\n return context;\n};\n\nexport type PopoverProps = {\n /** Innholdet i Popover */\n children: React.ReactNode;\n /** Plasseringen av Popover\n * @default \"bottom-start\"\n */\n placement?: Placement;\n};\n\nexport const Popover: React.FC<PopoverProps> = ({\n children,\n placement = 'bottom-start',\n}) => {\n const [showPopover, setShowPopover] = React.useState(false);\n const triggerElement = React.useRef(null);\n const contentElement = React.useRef(null);\n\n const { styles, attributes, forceUpdate } = usePopper(\n triggerElement.current,\n contentElement.current,\n {\n modifiers: [\n { name: 'arrow', enabled: false },\n {\n name: 'offset',\n options: {\n offset: [0, 8],\n },\n },\n ],\n placement: placement,\n },\n );\n\n React.useEffect(() => {\n if (forceUpdate) {\n forceUpdate();\n }\n }, [showPopover, forceUpdate]);\n\n const triggerProps = React.useCallback(() => {\n const buttonProps = {\n onClick: (e: React.MouseEvent) => {\n e.preventDefault();\n setShowPopover(prev => !prev);\n },\n 'aria-haspopup': 'dialog',\n 'aria-expanded': showPopover,\n ref: triggerElement,\n };\n return buttonProps;\n }, [triggerElement, showPopover]);\n\n useOnClickOutside(contentElement, triggerElement, () =>\n setShowPopover(false),\n );\n const closeButtonProps = {\n onClick: (e: React.MouseEvent) => {\n e.preventDefault();\n setShowPopover(false);\n },\n };\n const popoverContentProps = React.useCallback(() => {\n const contentProps = {\n role: 'dialog',\n 'aria-modal': 'false',\n ref: contentElement,\n onKeyDown: (event: React.KeyboardEvent) => {\n if (event.key === 'Escape') {\n showPopover && setShowPopover(false);\n }\n },\n onBlur: (event: React.FocusEvent) => {\n const elementReceivingFocus = event.relatedTarget as HTMLElement;\n // The check for 'tabindex=-1' is a special case for focus handling in Docz\n if (\n !elementReceivingFocus ||\n elementReceivingFocus.getAttribute('tabindex') === '-1'\n )\n return;\n const focusElementIsPopover = elementContainsElement(\n contentElement.current,\n elementReceivingFocus,\n );\n const focusElementIsTrigger = elementContainsElement(\n triggerElement.current,\n elementReceivingFocus,\n );\n const isValidBlur = !focusElementIsPopover && !focusElementIsTrigger;\n if (showPopover && isValidBlur) setShowPopover(false);\n },\n };\n return contentProps;\n }, [contentElement, showPopover]);\n const contextValue: PopoverContextProps = {\n showPopover,\n triggerElement,\n contentElement,\n styles,\n attributes,\n popoverContentProps,\n closeButtonProps,\n triggerProps,\n };\n return (\n <PopoverContext.Provider value={contextValue}>\n {children}\n </PopoverContext.Provider>\n );\n};\n\nexport type PopoverTriggerProps = {\n /** Knapp som skal brukes for å åpne Popover */\n children: React.ReactElement;\n};\n\nexport const PopoverTrigger: React.FC<PopoverTriggerProps> = ({ children }) => {\n const { triggerProps } = usePopoverContext();\n const child = React.Children.only(children) as React.ReactElement<any>;\n return cloneElement(child, triggerProps());\n};\n\nexport type PopoverCloseButtonProps = {\n /** En valgfri knapp som kan legges inn for å lukke Popover */\n children: React.ReactElement;\n};\n\nexport const PopoverCloseButton: React.FC<PopoverCloseButtonProps> = ({\n children,\n ...rest\n}) => {\n const { closeButtonProps } = usePopoverContext();\n return cloneElement(children, { ...closeButtonProps, ...rest });\n};\n\nexport type PopoverContentProps = {\n /**Innholdet til Popover */\n children: React.ReactNode;\n};\n\nexport const PopoverContent = React.forwardRef<\n HTMLDivElement,\n PopoverContentProps\n>(({ children }, ref: React.Ref<HTMLDivElement>) => {\n const { showPopover, attributes, styles, popoverContentProps } =\n usePopoverContext();\n const props = popoverContentProps({ ref });\n return (\n <Contrast\n className={classNames(\n 'eds-popover',\n {\n 'eds-popover--hidden': !showPopover,\n },\n 'eds-contrast',\n )}\n style={styles.popper}\n aria-hidden={!showPopover}\n {...attributes.styles}\n {...props}\n >\n {children}\n </Contrast>\n );\n});\n\nfunction elementContainsEventTarget(element: HTMLElement | null, event: Event) {\n if (!element) {\n return false;\n }\n\n if (element.contains(event.target as Node)) {\n return true;\n }\n\n // For elements inside a Shadow DOM we need to check the composedPath\n if (event.composed && event.composedPath) {\n const contains = event.composedPath().find(target => {\n if (target === window) {\n return false;\n }\n return element.contains(target as Node);\n });\n return contains ? true : false;\n }\n\n return false;\n}\n\nfunction elementContainsElement(\n parent: HTMLElement | null,\n child: HTMLElement,\n) {\n if (!parent) return false;\n return parent === child || parent.contains(child);\n}\n\nfunction useOnClickOutside(\n ref: React.RefObject<HTMLDivElement>,\n buttonRef: React.RefObject<HTMLButtonElement>,\n handler: () => void,\n) {\n React.useEffect(() => {\n const listener = (event: Event) => {\n if (\n elementContainsEventTarget(ref.current, event) ||\n elementContainsEventTarget(buttonRef.current, event)\n ) {\n return;\n }\n\n handler();\n };\n\n document.addEventListener('mousedown', listener);\n document.addEventListener('touchstart', listener);\n\n return () => {\n document.removeEventListener('mousedown', listener);\n document.removeEventListener('touchstart', listener);\n };\n }, [ref, buttonRef, handler]);\n}\n","import { warnAboutMissingStyles } from '@entur/utils';\nimport './index.scss';\n\nwarnAboutMissingStyles('tooltip');\n\nexport * from './Tooltip';\nexport * from './Popover';\n","import React, { cloneElement, useState } from 'react';\nimport { Manager, Reference, Popper } from 'react-popper';\nimport classNames from 'classnames';\nimport { useRandomId } from '@entur/utils';\nimport { Placement as PopperPlacementProps } from '@popperjs/core';\nimport { CloseIcon } from '@entur/icons';\nimport { IconButton } from '@entur/button';\nimport './Tooltip.scss';\n\ntype Modifier = {\n name: string;\n enabled?: boolean;\n requires?: Array<string>;\n requiresIfExists?: Array<string>;\n options?: Record<string, unknown>;\n data?: Record<string, unknown>;\n [key: string]: any;\n};\n\nexport type TooltipProps = {\n /** Plassering av tooltip-en */\n placement:\n | 'top'\n | 'top-left'\n | 'top-right'\n | 'left'\n | 'right'\n | 'bottom-left' // bottom-start\n | 'bottom'\n | 'bottom-right'; // bottom-end\n /** Innholdet i tooltip-boksen */\n content: React.ReactNode;\n /** Elementet som skal ha tooltip-funksjonalitet */\n children: React.ReactElement;\n /** Om tooltipen skal vises */\n isOpen?: boolean;\n /** Ekstra klassenavn for tooltip */\n className?: string;\n /** Åpner ikke tooltip ved hover-events\n * @default false\n */\n disableHoverListener?: boolean;\n /** Åpner ikke tooltip ved focus-events\n * @default false\n */\n disableFocusListener?: boolean;\n /** Viser en lukkeknapp om man kontrollerer åpningen av Tooltip vha `isOpen`\n * @default true\n */\n showCloseButton?: boolean;\n /** Valideringsvariant for Tooltip */\n variant?: 'error';\n /** En array av modifiers som sendes til Popper, rammeverket som brukes til plassering av Tooltip\n * @default [{ name: 'offset', options: { offset: [0, 10]} }]\n */\n popperModifiers?: Modifier[];\n [key: string]: any;\n};\n\nexport const Tooltip: React.FC<TooltipProps> = ({\n placement,\n content,\n children,\n className,\n isOpen = false,\n disableHoverListener = false,\n disableFocusListener = false,\n showCloseButton = true,\n variant,\n popperModifiers = [\n {\n name: 'offset',\n options: { offset: [0, 10] },\n },\n ],\n ...rest\n}) => {\n const [showTooltip, setShowTooltip] = useState(isOpen || false);\n let hoverTimer: NodeJS.Timeout;\n\n const handleOpen: (event: React.MouseEvent) => void = event => {\n event.persist();\n hoverTimer = setTimeout(() => {\n setShowTooltip(true);\n }, 150);\n };\n const onMouseExit: () => void = () => {\n setShowTooltip(false);\n clearTimeout(hoverTimer);\n };\n React.useEffect(() => {\n return () => {\n clearTimeout(hoverTimer);\n };\n });\n\n const tooltipId = useRandomId('eds-tooltip');\n React.useEffect(() => {\n setShowTooltip(isOpen);\n }, [isOpen]);\n\n React.useEffect(() => {\n if (!content) {\n setShowTooltip(false);\n }\n }, [content]);\n\n let popperPlacement = placement as PopperPlacementProps;\n if (placement.includes('-')) {\n if (placement.includes('right')) {\n popperPlacement = placement.replace(\n 'right',\n 'end',\n ) as PopperPlacementProps;\n }\n if (placement.includes('left')) {\n popperPlacement = placement.replace(\n 'left',\n 'start',\n ) as PopperPlacementProps;\n }\n }\n\n const childProps: {\n 'aria-describedby'?: string;\n onFocus?: () => void;\n onBlur?: () => void;\n onMouseEnter?: (e: React.MouseEvent) => void;\n onMouseLeave?: () => void;\n } = {};\n childProps['aria-describedby'] = tooltipId;\n if (!disableFocusListener) {\n childProps.onFocus = () => setShowTooltip(true);\n childProps.onBlur = () => setShowTooltip(false);\n }\n if (!disableHoverListener) {\n childProps.onMouseEnter = e => handleOpen(e);\n childProps.onMouseLeave = () => onMouseExit();\n }\n\n return (\n <Manager>\n <Reference>\n {({ ref }) =>\n cloneElement(children, {\n ref: ref,\n ...childProps,\n })\n }\n </Reference>\n {showTooltip && (\n <Popper\n modifiers={[{ name: 'arrow', enabled: false }, ...popperModifiers]}\n placement={popperPlacement}\n >\n {({ ref, style, placement: popperPlacement }) => (\n <div\n className={classNames(\n 'eds-tooltip',\n className,\n `eds-tooltip--${popperPlacement}`,\n { 'eds-tooltip--error': variant === 'error' },\n )}\n ref={ref}\n style={style}\n role=\"tooltip\"\n id={tooltipId}\n data-placement={popperPlacement}\n {...rest}\n >\n {content}\n {isOpen && showCloseButton && (\n <IconButton\n className=\"eds-tooltip__close-button\"\n onClick={() => setShowTooltip(false)}\n >\n <CloseIcon />\n </IconButton>\n )}\n </div>\n )}\n </Popper>\n )}\n </Manager>\n );\n};\n"],"names":["PopoverContext","createContext","undefined","usePopoverContext","context","useContext","Error","PopoverContent","React","forwardRef","ref","children","showPopover","attributes","styles","props","popoverContentProps","Contrast","className","classNames","style","popper","elementContainsEventTarget","element","event","contains","target","composed","composedPath","find","window","elementContainsElement","parent","child","warnAboutMissingStyles","placement","useState","setShowPopover","triggerElement","useRef","contentElement","usePopper","current","modifiers","name","enabled","options","offset","forceUpdate","useEffect","buttonRef","handler","triggerProps","useCallback","onClick","e","preventDefault","prev","listener","document","addEventListener","removeEventListener","closeButtonProps","role","onKeyDown","key","onBlur","elementReceivingFocus","relatedTarget","getAttribute","focusElementIsPopover","focusElementIsTrigger","Provider","value","rest","cloneElement","Children","only","hoverTimer","content","isOpen","disableHoverListener","disableFocusListener","showCloseButton","variant","popperModifiers","showTooltip","setShowTooltip","clearTimeout","tooltipId","useRandomId","popperPlacement","includes","replace","childProps","onFocus","onMouseEnter","persist","setTimeout","onMouseLeave","Manager","Reference","Popper","id","IconButton","CloseIcon"],"mappings":"80BAyBMA,EAAiBC,qBACrBC,GAEIC,EAAoB,eAClBC,EAAUC,aAAWL,MACZ,MAAXI,QACIE,MAAM,2DAEPF,GA+IIG,EAAiBC,UAAMC,YAGlC,WAAeC,OAAZC,IAAAA,WAEDR,IADMS,IAAAA,YAAaC,IAAAA,WAAYC,IAAAA,OAE3BC,GAAQC,IAF2BA,qBAEP,CAAEN,IAAAA,WAElCF,wBAACS,cACCC,UAAWC,UACT,cACA,wBAC0BP,GAE1B,gBAEFQ,MAAON,EAAOO,sBACAT,GACVC,EAAWC,OACXC,GAEHJ,MAKP,SAASW,EAA2BC,EAA6BC,YAC1DD,KAIDA,EAAQE,SAASD,EAAME,SAKvBF,EAAMG,UAAYH,EAAMI,cACTJ,EAAMI,eAAeC,MAAK,SAAAH,UACrCA,IAAWI,QAGRP,EAAQE,SAASC,QAQ9B,SAASK,EACPC,EACAC,WAEKD,IACEA,IAAWC,GAASD,EAAOP,SAASQ,ICnO7CC,yBAAuB,2BD0CwB,gBAC7CvB,IAAAA,aACAwB,UAAAA,aAAY,mBAE0B3B,UAAM4B,UAAS,GAA9CxB,OAAayB,OACdC,EAAiB9B,UAAM+B,OAAO,MAC9BC,EAAiBhC,UAAM+B,OAAO,QAEQE,YAC1CH,EAAeI,QACfF,EAAeE,QACf,CACEC,UAAW,CACT,CAAEC,KAAM,QAASC,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