v2.15.0

Tooltip

Небольшое окно с подробностями, которое появляется при наведении мыши на элемент.

Использование

Поместите в слот по умолчанию Tooltip Button или любой другой компонент.

<template>
  <B24Tooltip text="Открыть на GitHub">
    <B24Button label="Open" />
  </B24Tooltip>
</template>
Оберните приложение в компонент App: он использует компонент TooltipProvider из Reka UI.
Пропс tooltip компонента App показывает, как настроить Tooltip глобально.

Текст

Пропс text задаёт содержимое Tooltip.

<template>
  <B24Tooltip text="Открыть на GitHub">
    <B24Button label="Open" />
  </B24Tooltip>
</template>

Клавиши

Пропс kbds отрисовывает в Tooltip компоненты Kbd.

<template>
  <B24Tooltip
    text="Открыть на GitHub"
    :kbds="{
      '0': 'meta',
      '1': 'G'
    }"
  >
    <B24Button label="Open" />
  </B24Tooltip>
</template>
Можно использовать специальные клавиши, например meta: на macOS она отображается как ⌘, на остальных платформах как Ctrl.

Задержка

Пропс delay-duration меняет задержку перед появлением Tooltip. Например, значение 0 показывает его сразу.

<template>
  <B24Tooltip :delay-duration="0" text="Открыть на GitHub">
    <B24Button label="Open" />
  </B24Tooltip>
</template>
Глобально это настраивается опцией tooltip.delayDuration в компоненте App.

Содержимое

Пропс content управляет отрисовкой содержимого Tooltip, например его align или side.

Глобально это настраивается опцией tooltip.content в компоненте App.
<template>
  <B24Tooltip
    :content="{
      align: 'center',
      side: 'bottom',
      sideOffset: 8
    }"
    text="Открыть на GitHub"
  >
    <B24Button label="Open" />
  </B24Tooltip>
</template>

Стрелка

Пропс arrow показывает стрелку у Tooltip.

<template>
  <B24Tooltip text="Открыть на GitHub">
    <B24Button label="Open" />
  </B24Tooltip>
</template>

Отключение

Пропс disabled отключает Tooltip.

<template>
  <B24Tooltip disabled text="Открыть на GitHub">
    <B24Button label="Open" />
  </B24Tooltip>
</template>

Примеры

Управление открытием

Состоянием открытия управляют пропс default-open или директива v-model:open.

<script setup lang="ts">
const open = ref(false)

defineShortcuts({
  o: () => open.value = !open.value
})
</script>

<template>
  <B24Tooltip v-model:open="open" text="Открыть на GitHub">
    <B24Button label="Открыть" />
  </B24Tooltip>
</template>
<script setup lang="ts">
import { ref } from 'vue'

const open = ref(false)

defineShortcuts({
  o: () => open.value = !open.value
})
</script>

<template>
  <B24Tooltip v-model:open="open" text="Открыть на GitHub">
    <B24Button label="Открыть" />
  </B24Tooltip>
</template>
В этом примере с помощью defineShortcuts Tooltip переключается по нажатию O.

С привязкой к курсору

Чтобы Tooltip следовал за курсором при наведении на элемент, используйте пропс reference:

Наведите на меня
<script setup lang="ts">
const open = ref(false)
const anchor = ref({ x: 0, y: 0 })

const reference = computed(() => ({
  getBoundingClientRect: () =>
    ({
      width: 0,
      height: 0,
      left: anchor.value.x,
      right: anchor.value.x,
      top: anchor.value.y,
      bottom: anchor.value.y,
      ...anchor.value
    } as DOMRect)
}))
</script>

<template>
  <B24Tooltip
    :open="open"
    :reference="reference"
    :content="{ side: 'top', sideOffset: 16, updatePositionStrategy: 'always' }"
  >
    <div
      class="flex items-center justify-center rounded-md border border-dashed border-(--ui-color-accent-main-primary-alt) text-sm aspect-video w-72"
      @pointerenter="open = true"
      @pointerleave="open = false"
      @pointermove="(ev: PointerEvent) => {
        anchor.x = ev.clientX
        anchor.y = ev.clientY
      }"
    >
      Наведите на меня
    </div>

    <template #content>
      {{ anchor.x.toFixed(0) }} - {{ anchor.y.toFixed(0) }}
    </template>
  </B24Tooltip>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue'

const open = ref(false)
const anchor = ref({ x: 0, y: 0 })

const reference = computed(() => ({
  getBoundingClientRect: () =>
    ({
      width: 0,
      height: 0,
      left: anchor.value.x,
      right: anchor.value.x,
      top: anchor.value.y,
      bottom: anchor.value.y,
      ...anchor.value
    } as DOMRect)
}))
</script>

<template>
  <B24Tooltip
    :open="open"
    :reference="reference"
    :content="{ side: 'top', sideOffset: 16, updatePositionStrategy: 'always' }"
  >
    <div
      class="flex items-center justify-center rounded-md border border-dashed border-(--ui-color-accent-main-primary-alt) text-sm aspect-video w-72"
      @pointerenter="open = true"
      @pointerleave="open = false"
      @pointermove="(ev: PointerEvent) => {
        anchor.x = ev.clientX
        anchor.y = ev.clientY
      }"
    >
      Наведите на меня
    </div>

    <template #content>
      {{ anchor.x.toFixed(0) }} - {{ anchor.y.toFixed(0) }}
    </template>
  </B24Tooltip>
</template>

Иконка справки

Tooltip можно сочетать с иконкой.

<script setup lang="ts">
import HelpIcon from '@bitrix24/b24icons-vue/main/HelpIcon'
</script>

<template>
  <B24Tooltip
    :delay-duration="100"
    :content="{ side: 'right' }"
    text="Когда исполнитель завершит задачу, она придёт к вам на проверку. Вы можете принять работу (и задача будет закрыта) или вернуть задачу на доработку."
  >
    <HelpIcon class="size-5 cursor-help" />
  </B24Tooltip>
</template>

API

Пропсы

Prop Default Type
text string

The text content of the tooltip.

kbds (string | undefined)[] | KbdProps[]

The keyboard keys to display in the tooltip.

content{ side: 'bottom', sideOffset: 8, collisionPadding: 8 } TooltipContentProps & Partial<EmitsToProps<TooltipContentImplEmits>>

The content of the tooltip.

Inherits from the tooltip.content of the {@link AppProps } component when not provided.

arrowtrueboolean | TooltipArrowProps

Display an arrow alongside the tooltip. { rounded: true }

portaltrue string | false | true | HTMLElement

Render the tooltip in a portal.

reference Element | VirtualElement

The reference (or anchor) element that is being referred to for positioning.

If not provided will use the current component as anchor.

defaultOpenboolean

The open state of the tooltip when it is initially rendered. Use when you do not need to control its open state.

openboolean

The controlled open state of the tooltip.

delayDuration700 number

Override the duration given to the Provider to customise the open delay for a specific tooltip.

disableHoverableContentboolean

Prevents Tooltip.Content from remaining open when hovering. Disabling this has accessibility consequences. Inherits from Tooltip.Provider.

disableClosingTriggerfalseboolean

When true, clicking on trigger will not close the content.

disabledfalseboolean

When true, disable tooltip

ignoreNonKeyboardFocusfalseboolean

Prevent the tooltip from opening if the focus did not come from the keyboard by matching against the :focus-visible selector. This is useful if you want to avoid opening it when switching browser tabs or closing a dialog.

b24ui { content?: SlotClass; arrow?: SlotClass; text?: SlotClass; kbds?: SlotClass; kbdsSize?: SlotClass; kbdsAccent?: SlotClass; }

Слоты

Slot Type
default{ open: boolean; }
content{ b24ui: object; }

События

Event Type
update:open[value: boolean]

Тема

https://github.com/bx-shef/b24-ui/tree/main/src/theme/tooltip.ts
export default {
  slots: {
    content: 'light flex flex-row items-center justify-between gap-[5px] will-change-[opacity] min-h-[37px] shadow-xl/20 select-none motion-safe:data-[state=delayed-open]:animate-[scale-in_100ms_var(--ease-out)] motion-safe:data-[state=closed]:animate-[scale-out_100ms_var(--ease-out)] p-[13px] text-(length:--ui-font-size-lg)/[normal] font-[family-name:var(--ui-font-family-system)] bg-(--ui-color-bg-content-inapp) text-(--ui-color-base-8) rounded-[calc(var(--ui-border-radius-3xl)-2px)] ring ring-(--ui-color-bg-content-inapp) origin-(--reka-tooltip-content-transform-origin) pointer-events-auto',
    arrow: 'fill-(--ui-color-bg-content-inapp) stroke-(--ui-color-bg-content-inapp)',
    text: 'text-pretty max-w-[344px] min-w-[100px]',
    kbds: "hidden lg:inline-flex items-center shrink-0 gap-0.5 not-first-of-type:before:content-[''] not-first-of-type:before:me-0.5",
    kbdsSize: 'sm',
    kbdsAccent: 'default'
  }
}