@vingy/vueltip 1.1.0 → 1.2.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/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { n as __reExport, t as __exportAll } from "./chunk-CtajNgzt.mjs";
2
2
  import * as Vue from "vue";
3
- import { App, Component, Directive, ShallowRef, StyleValue } from "vue";
3
+ import { App, Component, Directive, DirectiveBinding, ShallowRef, StyleValue } from "vue";
4
4
 
5
5
  //#region ../../node_modules/.pnpm/@floating-ui+utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.d.mts
6
6
  declare type AlignedPlacement = `${Side}-${Alignment}`;
@@ -253,7 +253,7 @@ type Binding = string | {
253
253
  placement: Placement;
254
254
  };
255
255
  type Modifier = 'x' | 'y' | 'none' | 'both';
256
- type TooltipDirective = Directive<HTMLElement, Binding, Modifier>;
256
+ type TooltipDirective = Directive<HTMLElement, Binding, Modifier, Placement>;
257
257
  type Options = {
258
258
  /** @default 'vueltip-placement' */placementAttribute: string; /** @default 'vueltip-key' */
259
259
  keyAttribute: string; /** @default 'vueltip-truncate' */
@@ -261,7 +261,8 @@ type Options = {
261
261
  showDelay: number; /** @default 200 */
262
262
  hideDelay: number; /** @default false */
263
263
  handleDialogModals: boolean; /** @default 'both' */
264
- defaultTruncateDetection: Modifier;
264
+ defaultTruncateDetection: Modifier; /** @default 'top' */
265
+ defaultPlacement: Placement;
265
266
  };
266
267
  type UseTooltipOptions = {
267
268
  tooltipElement: Readonly<ShallowRef<HTMLElement | null>>;
@@ -310,8 +311,8 @@ declare const useVueltip: ({
310
311
  //#endregion
311
312
  //#region src/directive.d.ts
312
313
  declare const vueltipDirective: {
313
- updated: (el: HTMLElement, binding: Vue.DirectiveBinding<Binding, Modifier, any>) => void;
314
- created: (el: HTMLElement, binding: Vue.DirectiveBinding<Binding, Modifier, any>) => void;
314
+ updated: (el: HTMLElement, binding: DirectiveBinding<Binding, Modifier, Placement>) => void;
315
+ created: (el: HTMLElement, binding: DirectiveBinding<Binding, Modifier, Placement>) => void;
315
316
  beforeUnmount: (el: HTMLElement) => void;
316
317
  };
317
318
  //#endregion
package/dist/index.mjs CHANGED
@@ -1396,7 +1396,8 @@ let options = {
1396
1396
  showDelay: 0,
1397
1397
  hideDelay: 200,
1398
1398
  handleDialogModals: false,
1399
- defaultTruncateDetection: "both"
1399
+ defaultTruncateDetection: "both",
1400
+ defaultPlacement: "top"
1400
1401
  };
1401
1402
  const setOptions = (opts) => {
1402
1403
  options = {
@@ -1404,6 +1405,7 @@ const setOptions = (opts) => {
1404
1405
  ...opts
1405
1406
  };
1406
1407
  };
1408
+ const getOption = (key) => options[key];
1407
1409
 
1408
1410
  //#endregion
1409
1411
  //#region src/state.ts
@@ -1411,10 +1413,10 @@ const tooltipPlacement = ref("top");
1411
1413
  const debouncedTooltipPlacement = ref("top");
1412
1414
  const hoveredElement = ref();
1413
1415
  const debouncedHoveredElement = ref();
1414
- const contentMap = /* @__PURE__ */ new Map();
1415
- const getContent = (key) => contentMap.get(key);
1416
- const setContent = (key, value) => contentMap.set(key, value);
1417
- const deleteContent = (key) => contentMap.delete(key);
1416
+ const contentMap = ref(/* @__PURE__ */ new Map());
1417
+ const getContent = (key) => contentMap.value.get(key);
1418
+ const setContent = (key, value) => contentMap.value.set(key, value);
1419
+ const deleteContent = (key) => contentMap.value.delete(key);
1418
1420
  const generateKey = () => crypto.randomUUID();
1419
1421
  const tooltipKey = ref();
1420
1422
  const tooltipContent = ref();
@@ -1422,11 +1424,12 @@ let timerId;
1422
1424
  watch([
1423
1425
  tooltipKey,
1424
1426
  hoveredElement,
1425
- tooltipPlacement
1427
+ tooltipPlacement,
1428
+ () => getContent(tooltipKey.value ?? "")
1426
1429
  ], ([key, el, placement]) => {
1427
1430
  if (!key) return;
1428
1431
  if (timerId) clearTimeout(timerId);
1429
- const timeout = el ? options.showDelay : options.hideDelay;
1432
+ const timeout = el ? getOption("showDelay") : getOption("hideDelay");
1430
1433
  timerId = setTimeout(() => {
1431
1434
  tooltipContent.value = getContent(key);
1432
1435
  debouncedHoveredElement.value = el;
@@ -1479,7 +1482,7 @@ const useVueltip = ({ tooltipElement, arrowElement, offset: _offset, padding, ar
1479
1482
  };
1480
1483
  });
1481
1484
  const show = computed(() => !!debouncedHoveredElement.value);
1482
- if (options.handleDialogModals) watch(show, (value) => {
1485
+ if (getOption("handleDialogModals")) watch(show, (value) => {
1483
1486
  if (!value || !tooltipElement.value || !debouncedHoveredElement.value || !initialParent) return;
1484
1487
  const dialogEl = debouncedHoveredElement.value.closest("dialog");
1485
1488
  if (!dialogEl) {
@@ -1510,7 +1513,7 @@ function isTruncated(el) {
1510
1513
  }
1511
1514
  }
1512
1515
  function getTruncationDirection(el) {
1513
- return el.getAttribute(options.truncateAttribute) ?? options.defaultTruncateDetection;
1516
+ return el.getAttribute(getOption("truncateAttribute")) ?? getOption("defaultTruncateDetection");
1514
1517
  }
1515
1518
  function elementContainsText(el, text) {
1516
1519
  if (isInputElement(el) || isTextAreaElement(el)) return getInputValue(el).includes(text);
@@ -1529,7 +1532,7 @@ function getInputValue(el) {
1529
1532
  return el.value;
1530
1533
  }
1531
1534
  const ensureKey = (el, fn) => {
1532
- const key = el.getAttribute(options.keyAttribute);
1535
+ const key = el.getAttribute(getOption("keyAttribute"));
1533
1536
  if (!key) return;
1534
1537
  return fn(key);
1535
1538
  };
@@ -1546,7 +1549,7 @@ const onMouseover = ensureEventTarget((target) => ensureKey(target, (key) => {
1546
1549
  if (!content) return;
1547
1550
  const { text } = content;
1548
1551
  if (elementContainsText(target, text) && !isTruncated(target)) return;
1549
- const placement = target.getAttribute(options.placementAttribute);
1552
+ const placement = target.getAttribute(getOption("placementAttribute"));
1550
1553
  tooltipKey.value = key;
1551
1554
  hoveredElement.value = target;
1552
1555
  tooltipPlacement.value = placement;
@@ -1559,25 +1562,34 @@ const onMouseout = ensureEventTarget((target) => {
1559
1562
  //#endregion
1560
1563
  //#region src/directive.ts
1561
1564
  const toContent = (value) => typeof value === "string" ? { text: value } : typeof value.content === "string" ? { text: value.content } : value.content;
1562
- const extractPlacement = (value) => typeof value === "string" ? "top" : value.placement;
1565
+ const extractPlacement = (binding) => {
1566
+ const { value, arg } = binding;
1567
+ if (typeof value !== "string" && "placement" in value) return value.placement;
1568
+ if (!arg) return getOption("defaultPlacement");
1569
+ return arg;
1570
+ };
1563
1571
  const truncationDirection = (modifiers) => {
1564
1572
  if (modifiers.none) return "none";
1565
1573
  if (modifiers.both) return "both";
1566
1574
  if (modifiers.x && modifiers.y) return "both";
1567
1575
  if (modifiers.x) return "x";
1568
1576
  if (modifiers.y) return "y";
1569
- return options.defaultTruncateDetection;
1577
+ return getOption("defaultTruncateDetection");
1570
1578
  };
1571
1579
  const vueltipDirective = {
1572
1580
  updated: (el, binding) => {
1573
- ensureKey(el, (key) => setContent(key, toContent(binding.value)));
1581
+ ensureKey(el, (key) => {
1582
+ el.setAttribute(getOption("placementAttribute"), extractPlacement(binding));
1583
+ el.setAttribute(getOption("truncateAttribute"), truncationDirection(binding.modifiers ?? {}));
1584
+ setContent(key, toContent(binding.value));
1585
+ });
1574
1586
  },
1575
1587
  created: (el, binding) => {
1576
1588
  const key = generateKey();
1577
1589
  setContent(key, toContent(binding.value));
1578
- el.setAttribute(options.keyAttribute, key);
1579
- el.setAttribute(options.placementAttribute, extractPlacement(binding.value));
1580
- el.setAttribute(options.truncateAttribute, truncationDirection(binding.modifiers ?? {}));
1590
+ el.setAttribute(getOption("keyAttribute"), key);
1591
+ el.setAttribute(getOption("placementAttribute"), extractPlacement(binding));
1592
+ el.setAttribute(getOption("truncateAttribute"), truncationDirection(binding.modifiers ?? {}));
1581
1593
  el.addEventListener("mouseenter", onMouseover);
1582
1594
  el.addEventListener("focus", onMouseover);
1583
1595
  el.addEventListener("mouseleave", onMouseout);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vingy/vueltip",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Headless tooltip which only shows when necessary.",
5
5
  "keywords": [
6
6
  "composition-api",